Skip to content

[AutoDiff] Add SR-12526 negative test. #30826

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 2 commits into from
Apr 6, 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
8 changes: 0 additions & 8 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4423,14 +4423,6 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
return true;
}

// Reject different-file derivative registration.
// TODO(TF-1021): Lift same-file derivative registration restriction.
if (originalAFD->getParentSourceFile() != derivative->getParentSourceFile()) {
diags.diagnose(attr->getLocation(),
diag::derivative_attr_not_in_same_file_as_original);
return true;
}

// Reject duplicate `@derivative` attributes.
auto &derivativeAttrs = Ctx.DerivativeAttrs[std::make_tuple(
originalAFD, resolvedDiffParamIndices, kind)];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public struct Struct {
public func method(_ x: Float) -> Float { x }

public static func +(_ lhs: Self, rhs: Self) -> Self {
lhs
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import _Differentiation
import a

extension Struct: Differentiable {
public struct TangentVector: Differentiable & AdditiveArithmetic {}
public mutating func move(along _: TangentVector) {}

@usableFromInline
@derivative(of: method, wrt: x)
func vjpMethod(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
(x, { $0 })
}

@usableFromInline
@derivative(of: +)
static func vjpAdd(_ lhs: Self, rhs: Self) -> (
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)
) {
(lhs + rhs, { v in (v, v) })
}
}
14 changes: 14 additions & 0 deletions test/AutoDiff/Sema/DerivativeRegistrationCrossModule/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/a.swift -emit-module-path %t/a.swiftmodule
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/b.swift -emit-module-path %t/b.swiftmodule -I %t
// RUN: not --crash %target-swift-frontend-typecheck -verify -I %t %s

// SR-12526: Fix cross-module deserialization crash involving `@derivative` attribute.

import a
import b

func foo(_ s: Struct) {
_ = Struct()
_ = s.method(1)
}
4 changes: 1 addition & 3 deletions test/AutoDiff/Sema/derivative_attr_type_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,9 @@ extension InoutParameters {
}
}

// Test cross-file derivative registration. Currently unsupported.
// TODO(TF-1021): Lift this restriction.
// Test cross-file derivative registration.

extension FloatingPoint where Self: Differentiable {
// expected-error @+1 {{derivative not in the same file as the original function}}
@derivative(of: rounded)
func vjpRounded() -> (
value: Self,
Expand Down