Skip to content

[AutoDiff] Fix stdlib @derivative(of: subscript) crash. #28935

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
Jan 3, 2020
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
13 changes: 4 additions & 9 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3195,20 +3195,15 @@ static AbstractFunctionDecl *findAbstractFunctionDecl(

// Perform lookup.
LookupResult results;
// If `baseType` is not null but `lookupContext` is a type context, set
// `baseType` to the `self` type of `lookupContext` to perform member lookup.
if (!baseType && lookupContext->isTypeContext())
baseType = lookupContext->getSelfTypeInContext();
if (baseType) {
results = TypeChecker::lookupMember(lookupContext, baseType, funcName);
} else {
results = TypeChecker::lookupUnqualified(lookupContext, funcName,
funcNameLoc, lookupOptions);

// If looking up an operator within a type context, look specifically within
// the type context.
// This tries to resolve unqualified operators, like `+`.
if (funcName.isOperator() && lookupContext->isTypeContext()) {
if (auto tmp = TypeChecker::lookupMember(
lookupContext, lookupContext->getSelfTypeInContext(), funcName))
results = tmp;
}
}

// Initialize error flags.
Expand Down
8 changes: 5 additions & 3 deletions stdlib/public/core/SIMDVectorTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct SIMD${n}<Scalar>: SIMD where Scalar: SIMDScalar {

/// Accesses the scalar at the specified position.
// SWIFT_ENABLE_TENSORFLOW
@differentiable(vjp: _vjpSubscript
@differentiable(
where Scalar : EuclideanDifferentiable & BinaryFloatingPoint,
Scalar.TangentVector : BinaryFloatingPoint)
public subscript(index: Int) -> Scalar {
Expand Down Expand Up @@ -206,9 +206,11 @@ extension SIMD${n} : EuclideanDifferentiable
extension SIMD${n}
where Scalar : EuclideanDifferentiable & BinaryFloatingPoint,
Scalar.TangentVector : BinaryFloatingPoint {
@usableFromInline
// NOTE(TF-1094): serialized `@derivative` for `.swiftinterface` compilation.
@inlinable
@derivative(of: subscript(_:))
internal func _vjpSubscript(index: Int)
-> (Scalar, (Scalar.TangentVector) -> TangentVector) {
-> (value: Scalar, pullback: (Scalar.TangentVector) -> TangentVector) {
return (self[index], { v in
var zeros = Self.zero
zeros[index] = Scalar(v)
Expand Down
36 changes: 36 additions & 0 deletions test/AutoDiff/differentiability_witness_swiftinterface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -swift-version 5 -enable-library-evolution -module-name Module > %t/Module.swiftinterface
// RUN: %target-swift-frontend -emit-silgen %t/Module.swiftinterface | %FileCheck %s --check-prefix=CHECK-SILGEN
// RUN: not %target-swift-frontend -compile-module-from-interface %t/Module.swiftinterface -o %t/Module.swiftmodule 2>&1 | %FileCheck %s --check-prefix=CHECK-COMPILE

// TF-1094: Derivative registration fails for `.swiftinterface` compilation when
// original `@differentiable` function is serialized but `@derivative` function
// is unserialized.

@inlinable // serialized
@differentiable
@_silgen_name("foo")
public func foo(_ x: Float) -> Float {
fatalError()
}

@usableFromInline // not serialized
@derivative(of: foo)
@_silgen_name("vjp_foo")
func vjpFoo(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
return (x, { $0 })
}

// Missing differentiability witness VJP entry because `func vjpFoo` is bodiless
// in the `.swiftinterface` file and not lowered to a SIL function.

// CHECK-SILGEN-LABEL: // differentiability witness for foo
// CHECK-SILGEN-NEXT: sil_differentiability_witness [serialized] [parameters 0] [results 0] @foo : $@convention(thin) (Float) -> Float {
// CHECK-SILGEN-NEXT: }

// CHECK-SILGEN-LABEL: sil [serialized] [ossa] @foo
// CHECK-SILGEN-NOT: sil {{.*}} @vjp_foo

// CHECK-COMPILE: Module.swiftinterface:5:2: error: function is not differentiable
// CHECK-COMPILE: Module.swiftinterface:7:24: note: when differentiating this function definition
// CHECK-COMPILE: Module.swiftinterface:9:1: note: missing return for differentiation