Skip to content

[AutoDiff] Type-check @differentiable attributes during validation. #27613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3989,6 +3989,11 @@ void TypeChecker::validateDecl(ValueDecl *D) {
assert(VD->hasInterfaceType());
}

// SWIFT_ENABLE_TENSORFLOW
// TODO(TF-789): Find proper way to type-check `@differentiable` attributes.
checkDeclDifferentiableAttributes(VD);
// SWIFT_ENABLE_TENSORFLOW END

// We're not really done with processing the signature yet, but
// @objc checking requires the declaration to call itself validated
// so that it can be considered as a witness.
Expand Down Expand Up @@ -4118,8 +4123,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
// FIXME: Roll all of this interface type computation into a request.
FD->computeType();

// TODO(TF-789): Figure out the proper way to typecheck these.
// SWIFT_ENABLE_TENSORFLOW
// TODO(TF-789): Find proper way to type-check `@differentiable` attributes.
checkDeclDifferentiableAttributes(FD);
// SWIFT_ENABLE_TENSORFLOW END

// Member functions need some special validation logic.
if (FD->getDeclContext()->isTypeContext()) {
Expand Down Expand Up @@ -4164,6 +4171,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
typeCheckParameterList(CD->getParameters(), res,
TypeResolverContext::AbstractFunctionDecl);
CD->computeType();
// SWIFT_ENABLE_TENSORFLOW
// TODO(TF-789): Find proper way to type-check `@differentiable` attributes.
checkDeclDifferentiableAttributes(CD);
// SWIFT_ENABLE_TENSORFLOW END
break;
}

Expand Down Expand Up @@ -4196,6 +4207,10 @@ void TypeChecker::validateDecl(ValueDecl *D) {
SF->markDeclWithOpaqueResultTypeAsValidated(SD);
}
}
// SWIFT_ENABLE_TENSORFLOW
// TODO(TF-789): Find proper way to type-check `@differentiable` attributes.
checkDeclDifferentiableAttributes(SD);
// SWIFT_ENABLE_TENSORFLOW END

break;
}
Expand Down
26 changes: 26 additions & 0 deletions test/AutoDiff/Inputs/differentiable_attr_other_module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Verify that `@differentiable` declarations can be differentiated from other
// modules.

public struct Foo: Differentiable {
public var x: Float

@differentiable
public init(_ x: Float) {
self.x = x
}

@differentiable
public func method() -> Float {
x
}

@differentiable
public var computedProperty: Float {
x
}

@differentiable
public subscript() -> Float {
x
}
}
26 changes: 26 additions & 0 deletions test/AutoDiff/differentiable_attr_cross_module/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Verify that `@differentiable` declarations can be differentiated from other
// modules.

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %S/../Inputs/differentiable_attr_other_module.swift %s -o /dev/null -lm
// NOTE(TF-892): `-lm` is necessary to prevent linker errors related to `ElementaryFunctions` on Ubuntu.

@differentiable(wrt: x)
func testInitializer(_ x: Float) -> Float {
return Foo(x).x
}

@differentiable(wrt: foo)
func testMethod(_ foo: Foo) -> Float {
return foo.method()
}

@differentiable(wrt: foo)
func testComputedProperty(_ foo: Foo) -> Float {
return foo.computedProperty
}

@differentiable(wrt: foo)
func testSubscript(_ foo: Foo) -> Float {
return foo[]
}