Skip to content

[AutoDiff] flag for cross-file derivative registration #28891

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
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
10 changes: 9 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@
#include "swift/Sema/IDETypeChecking.h"
#include "clang/Basic/CharInfo.h"
#include "llvm/Support/Debug.h"
// SWIFT_ENABLE_TENSORFLOW
#include "llvm/Support/Options.h"

using namespace swift;

// SWIFT_ENABLE_TENSORFLOW
static llvm::cl::opt<bool> EnableExperimentalCrossFileDerivativeRegistration(
"enable-experimental-cross-file-derivative-registration",
llvm::cl::init(false));

namespace {
/// This emits a diagnostic with a fixit to remove the attribute.
template<typename ...ArgTypes>
Expand Down Expand Up @@ -3646,7 +3653,8 @@ void AttributeChecker::visitDerivativeAttr(DerivativeAttr *attr) {

// Reject different-file derivative registration.
// TODO(TF-1021): Lift this restriction.
if (originalAFD->getParentSourceFile() != derivative->getParentSourceFile()) {
if (!EnableExperimentalCrossFileDerivativeRegistration &&
originalAFD->getParentSourceFile() != derivative->getParentSourceFile()) {
diagnoseAndRemoveAttr(attr,
diag::derivative_attr_not_in_same_file_as_original);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import StdlibUnittest

import module1

var Tests = TestSuite("CrossModuleDerivativeAttr")

Tests.test("CrossFile") {
let grad = gradient(at: 0, in: fCrossFile)
expectEqual(10, grad)
}

runAllTests()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public func fCrossFile(_ x: Float) -> Float { x }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@derivative(of: fCrossFile)
public func vjpCrossFile(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
(x, { 10 * $0 })
}
5 changes: 5 additions & 0 deletions test/AutoDiff/cross_module_derivative_attr_e2e.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -I%t -c -parse-as-library -emit-module -module-name module1 -emit-module-path %t/module1.swiftmodule -o %t/module1.o %S/Inputs/cross_module_derivative_attr_e2e/module1/module1.swift %S/Inputs/cross_module_derivative_attr_e2e/module1/module1_other_file.swift -Xllvm -enable-experimental-cross-file-derivative-registration -validate-tbd-against-ir=none
// RUN: %target-build-swift -I%t %S/Inputs/cross_module_derivative_attr_e2e/main/main.swift %t/module1.o -o %t/a.out -lm -Xllvm -enable-experimental-cross-file-derivative-registration -Xfrontend -validate-tbd-against-ir=none
// RUN: %target-run %t/a.out
// REQUIRES: executable_test