Skip to content
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
9 changes: 9 additions & 0 deletions lib/SIL/SILFunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ void SILFunctionBuilder::addFunctionAttributes(SILFunction *F,
for (auto *A : Attrs.getAttributes<DifferentiableAttr>())
(void)A->getParameterIndices();

// Propagate `@noDerivative` as `[_semantics "autodiff.nonvarying"]`.
//
// `@noDerivative` implies non-varying semantics for differentiable activity
// analysis. SIL values produced from references to `@noDerivative`
// declarations will not be marked as varying; these values do not need a
// derivative.
if (Attrs.hasAttribute<NoDerivativeAttr>())
F->addSemanticsAttr("autodiff.nonvarying");

// Propagate @_dynamicReplacement(for:).
if (constant.isNull())
return;
Expand Down
13 changes: 1 addition & 12 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
IGNORED_ATTR(ReferenceOwnership)
IGNORED_ATTR(OriginallyDefinedIn)
// SWIFT_ENABLE_TENSORFLOW
IGNORED_ATTR(NoDerivative)
// TODO(TF-715): Allow @quoted on more decls.
IGNORED_ATTR(Quoted)
// SWIFT_ENABLE_TENSORFLOW END
Expand Down Expand Up @@ -260,7 +261,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
// TODO(TF-999): Remove deprecated `@differentiating` attribute.
void visitDifferentiatingAttr(DerivativeAttr *attr);
void visitCompilerEvaluableAttr(CompilerEvaluableAttr *attr);
void visitNoDerivativeAttr(NoDerivativeAttr *attr);
// SWIFT_ENABLE_TENSORFLOW END
};
} // end anonymous namespace
Expand Down Expand Up @@ -4865,14 +4865,3 @@ void AttributeChecker::visitCompilerEvaluableAttr(CompilerEvaluableAttr *attr) {
// checked, and it's not type checked yet, so we check these rules later in
// TypeChecker::checkFunctionBodyCompilerEvaluable().
}

// SWIFT_ENABLE_TENSORFLOW
void AttributeChecker::visitNoDerivativeAttr(NoDerivativeAttr *attr) {
auto &ctx = D->getASTContext();
// `@noDerivative` implies non-varying semantics for differentiable activity
// analysis. SIL values produced from references to `@noDerivative`
// declarations will not be marked as varying; these values do not need a
// derivative.
D->getAttrs().add(
new (ctx) SemanticsAttr("autodiff.nonvarying", /*implicit*/ true));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@noDerivative
@_silgen_name("float_to_int_noderivative")
func floatToIntNoDerivative(_ x: Float) -> Int {
Int(x)
}
6 changes: 3 additions & 3 deletions test/AutoDiff/downstream/derived_differentiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct TestNoDerivative : EuclideanDifferentiable {

// CHECK-AST-LABEL: internal struct TestNoDerivative : EuclideanDifferentiable {
// CHECK-AST: var w: Float
// CHECK-AST: @noDerivative @_semantics("autodiff.nonvarying") internal var technicallyDifferentiable: Float
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: Float
// CHECK-AST: internal init(w: Float, technicallyDifferentiable: Float)
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, ElementaryFunctions
// CHECK-AST: internal typealias TangentVector = TestNoDerivative.TangentVector
Expand All @@ -55,7 +55,7 @@ struct TestPointwiseMultiplicative : Differentiable {

// CHECK-AST-LABEL: internal struct TestPointwiseMultiplicative : Differentiable {
// CHECK-AST: var w: PointwiseMultiplicativeDummy
// CHECK-AST: @noDerivative @_semantics("autodiff.nonvarying") internal var technicallyDifferentiable: PointwiseMultiplicativeDummy
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: PointwiseMultiplicativeDummy
// CHECK-AST: internal init(w: PointwiseMultiplicativeDummy, technicallyDifferentiable: PointwiseMultiplicativeDummy)
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative
// CHECK-AST: internal typealias TangentVector = TestPointwiseMultiplicative.TangentVector
Expand All @@ -68,7 +68,7 @@ struct TestKeyPathIterable : Differentiable, KeyPathIterable {

// CHECK-AST-LABEL: internal struct TestKeyPathIterable : Differentiable, KeyPathIterable {
// CHECK-AST: var w: Float
// CHECK-AST: @noDerivative @_semantics("autodiff.nonvarying") internal var technicallyDifferentiable: Float
// CHECK-AST: @noDerivative internal var technicallyDifferentiable: Float
// CHECK-AST: internal init(w: Float, technicallyDifferentiable: Float)
// CHECK-AST: internal struct TangentVector : Differentiable, AdditiveArithmetic, ElementaryFunctions, VectorProtocol, KeyPathIterable
// CHECK-AST: internal typealias TangentVector = TestKeyPathIterable.TangentVector
Expand Down
10 changes: 5 additions & 5 deletions test/AutoDiff/downstream/noderivative_attr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ struct NotDifferentiable {
}

// CHECK-LABEL: struct NotDifferentiable {
// CHECK: @noDerivative @_hasStorage @_semantics("autodiff.nonvarying") var stored: Float { get set }
// CHECK: @noDerivative @_semantics("autodiff.nonvarying") var computedProperty: Float { get set _modify }
// CHECK: @noDerivative @_semantics("autodiff.nonvarying") func instanceMethod(_ x: Float) -> Float
// CHECK: @noDerivative @_semantics("autodiff.nonvarying") static func staticMethod(_ x: Float) -> Float
// CHECK: @noDerivative @_semantics("autodiff.nonvarying") subscript(x: Float) -> Float { get set _modify }
// CHECK: @noDerivative @_hasStorage var stored: Float { get set }
// CHECK: @noDerivative var computedProperty: Float { get set _modify }
// CHECK: @noDerivative func instanceMethod(_ x: Float) -> Float
// CHECK: @noDerivative static func staticMethod(_ x: Float) -> Float
// CHECK: @noDerivative subscript(x: Float) -> Float { get set _modify }
// CHECK: }

// CHECK-LABEL: // NotDifferentiable.computedProperty.getter
Expand Down
8 changes: 8 additions & 0 deletions test/AutoDiff/downstream/noderivative_attr_cross_file.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend -emit-sil %S/Inputs/noderivative_attr_other_file.swift %s | %FileCheck %s

@differentiable
func bar(_ x: Float) -> Float {
return Float(floatToIntNoDerivative(x))
}

// CHECK: sil hidden [_semantics "autodiff.nonvarying"] @float_to_int_noderivative : $@convention(thin) (Float) -> Int