Skip to content

Commit 677a671

Browse files
authored
[AutoDiff] Change cross-file derivative registration to a frontend flag. (#29339)
Change `-enable-experimental-cross-file-derivative-registration` from an `-Xllvm` flag to a `-Xfrontend` flag. This makes the flag consistent with: - `-enable-experimental-differentiable-programming` - `-enable-experimental-forward-mode-differentiation`
1 parent 66a66c9 commit 677a671

File tree

7 files changed

+21
-18
lines changed

7 files changed

+21
-18
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ function(add_swift_target_library name)
19141914
# SWIFT_ENABLE_TENSORFLOW
19151915
# NOTE(TF-1021): Enable cross-file derivative registration for stdlib.
19161916
list(APPEND swiftlib_swift_compile_flags_all
1917-
-Xllvm -enable-experimental-cross-file-derivative-registration)
1917+
-Xfrontend -enable-experimental-cross-file-derivative-registration)
19181918
# SWIFT_ENABLE_TENSORFLOW END
19191919

19201920
# Collect architecture agnostic SDK linker flags

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ namespace swift {
308308
// SWIFT_ENABLE_TENSORFLOW END
309309

310310
// SWIFT_ENABLE_TENSORFLOW
311+
/// Whether to enable cross-file derivative registration.
312+
bool EnableExperimentalCrossFileDerivativeRegistration = false;
313+
311314
/// Whether to enable forward mode differentiation.
312315
bool EnableExperimentalForwardModeDifferentiation = false;
313316
// SWIFT_ENABLE_TENSORFLOW END

include/swift/Option/Options.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,14 @@ def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
470470

471471
// Experimental feature options
472472
def enable_experimental_differentiable_programming : Flag<["-"], "enable-experimental-differentiable-programming">,
473-
Flags<[FrontendOption]>,
473+
Flags<[FrontendOption, ModuleInterfaceOption]>,
474474
HelpText<"Enable experimental differentiable programming features">;
475+
475476
// SWIFT_ENABLE_TENSORFLOW
477+
def enable_experimental_cross_file_derivative_registration : Flag<["-"], "enable-experimental-cross-file-derivative-registration">,
478+
Flags<[FrontendOption, ModuleInterfaceOption]>,
479+
HelpText<"Enable experimental cross-file derivative registration">;
480+
476481
// NOTE: This flag will be removed when JVP/differential generation is robust.
477482
def enable_experimental_forward_mode_differentiation : Flag<["-"], "enable-experimental-forward-mode-differentiation">,
478483
Flags<[FrontendOption]>,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,6 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
240240
return;
241241
ArgStringList RenderedArgs;
242242
for (auto A : Args) {
243-
// SWIFT_ENABLE_TENSORFLOW: Copy
244-
// `-Xllvm -enable-experimental-cross-file-derivative-registration` into the
245-
// .swiftinterface file.
246-
if (A->getOption().matches(options::OPT_Xllvm) &&
247-
StringRef(A->getValue()) ==
248-
"-enable-experimental-cross-file-derivative-registration") {
249-
A->render(Args, RenderedArgs);
250-
continue;
251-
}
252243
if (A->getOption().hasFlag(options::ModuleInterfaceOption))
253244
A->render(Args, RenderedArgs);
254245
}
@@ -382,9 +373,13 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
382373
if (Args.hasArg(OPT_enable_experimental_differentiable_programming))
383374
Opts.EnableExperimentalDifferentiableProgramming = true;
384375

385-
// TODO: Ignore if enable-experimental-differentiable-programming is false
376+
// TODO: Ignore differentiation-related flags if
377+
// `enable-experimental-differentiable-programming` is false.
378+
Opts.EnableExperimentalCrossFileDerivativeRegistration |=
379+
Args.hasArg(OPT_enable_experimental_cross_file_derivative_registration);
386380
Opts.EnableExperimentalForwardModeDifferentiation |=
387381
Args.hasArg(OPT_enable_experimental_forward_mode_differentiation);
382+
388383
if (Args.hasArg(OPT_enable_experimental_quasiquotes))
389384
Opts.EnableExperimentalQuasiquotes = true;
390385

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3797,7 +3797,7 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
37973797

37983798
// Reject different-file derivative registration.
37993799
// TODO(TF-1021): Lift same-file derivative registration restriction.
3800-
if (!EnableExperimentalCrossFileDerivativeRegistration &&
3800+
if (!ctx.LangOpts.EnableExperimentalCrossFileDerivativeRegistration &&
38013801
originalAFD->getParentSourceFile() != derivative->getParentSourceFile()) {
38023802
diags.diagnose(attr->getLocation(),
38033803
diag::derivative_attr_not_in_same_file_as_original);

test/AutoDiff/downstream/cross_module_derivative_attr_e2e.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -working-directory %t -I%t -parse-as-library -emit-module -module-name module1 -emit-module-path %t/module1.swiftmodule -emit-library -static %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 -Xfrontend -validate-tbd-against-ir=none
3-
// RUN: %target-build-swift -I%t -L%t %S/Inputs/cross_module_derivative_attr_e2e/main/main.swift -o %t/a.out -lm -lmodule1 -Xllvm -enable-experimental-cross-file-derivative-registration -Xfrontend -validate-tbd-against-ir=none
2+
// RUN: %target-build-swift -working-directory %t -I%t -parse-as-library -emit-module -module-name module1 -emit-module-path %t/module1.swiftmodule -emit-library -static %S/Inputs/cross_module_derivative_attr_e2e/module1/module1.swift %S/Inputs/cross_module_derivative_attr_e2e/module1/module1_other_file.swift -Xfrontend -enable-experimental-cross-file-derivative-registration -Xfrontend -validate-tbd-against-ir=none
3+
// RUN: %target-build-swift -I%t -L%t %S/Inputs/cross_module_derivative_attr_e2e/main/main.swift -o %t/a.out -lm -lmodule1 -Xfrontend -enable-experimental-cross-file-derivative-registration -Xfrontend -validate-tbd-against-ir=none
44
// RUN: %target-run %t/a.out
55
// REQUIRES: executable_test
66

test/AutoDiff/downstream/derivative_registration_foreign/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %clang -c %S/Inputs/Foreign.c -fmodules -o %t/CForeign.o
3-
// RUN: %target-swift-emit-silgen -Xllvm -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s | %FileCheck %s --check-prefix=CHECK-SILGEN --check-prefix=CHECK
4-
// RUN: %target-swift-emit-sil -Xllvm -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s | %FileCheck %s --check-prefix=CHECK-SIL --check-prefix=CHECK
5-
// RUN: %target-build-swift -Xllvm -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s %t/CForeign.o
3+
// RUN: %target-swift-emit-silgen -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s | %FileCheck %s --check-prefix=CHECK-SILGEN --check-prefix=CHECK
4+
// RUN: %target-swift-emit-sil -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s | %FileCheck %s --check-prefix=CHECK-SIL --check-prefix=CHECK
5+
// RUN: %target-build-swift -Xfrontend -enable-experimental-cross-file-derivative-registration -I %S/Inputs -I %t %s %t/CForeign.o
66

77
import CForeign
88

0 commit comments

Comments
 (0)