Skip to content

Commit ea81ed4

Browse files
committed
[AutoDiff] SILGen derivative function thunks must not be transparent.
SILGen generates thunks for derivative functions registered via `@differentiable` and `@differentiating` attributes. The thunks have a canonical, consistent naming (currently based on the original function name and parameter indices) that TBDGen can also generate via `SILDeclRef::mangle`. These SILGen derivative function thunks must not be transparent; otherwise, the functions will be inlined during MandatoryInlining and will not be exposed publicly. This bug was discovered as a DeadFunctionElimination crash: `function_ref` instructions referencing transparent derivative thunks had their referenced functions set to null by MandatoryInling and became invalid.
1 parent 92b0f22 commit ea81ed4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/SILGen/SILGenThunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ SILGenModule::getOrCreateAutoDiffThunk(SILDeclRef derivativeFnDeclRef,
8787
originalLinkage, /*isDerivativeFnExported*/ true);
8888
auto name = derivativeFnDeclRef.mangle();
8989
auto *thunk = builder.getOrCreateFunction(
90-
derivativeFnDecl, name, linkage, derivativeFnTy, IsBare, IsTransparent,
90+
derivativeFnDecl, name, linkage, derivativeFnTy, IsBare, IsNotTransparent,
9191
derivativeFnDeclRef.isSerialized(), IsNotDynamic, ProfileCounter(),
9292
IsThunk);
9393
if (!thunk->empty())

test/AutoDiff/silgen_thunking/main.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
import StdlibUnittest
1111
import DifferentiationUnittest
1212

13+
// Verify that SILGen derivative thunks are never `[transparent]`.
14+
@differentiable(vjp: vjpNoReabstraction)
15+
func noReabstraction<T: Differentiable>(_ x: T) -> T {
16+
return x
17+
}
18+
func vjpNoReabstraction<T: Differentiable>(_ x: T) -> (T, (T.TangentVector) -> T.TangentVector) {
19+
return (x, { $0 })
20+
}
21+
// Find the non-`[transparent]` SILGen thunk.
22+
// CHECK-LABEL: sil hidden [thunk] [ossa] @AD__$s4main15noReabstractionyxxs15_DifferentiableRzlF__vjp_src_0_wrt_0 : $@convention(thin) <τ_0_0 where τ_0_0 : _Differentiable> (@in_guaranteed τ_0_0) -> (@out τ_0_0, @owned @callee_guaranteed (@in_guaranteed τ_0_0.TangentVector) -> @out τ_0_0.TangentVector)
23+
1324
var DerivativeSILGenThunkTests = TestSuite("DerivativeSILGenThunks")
1425

1526
// TF-619: Test cross-module import of `@differentiable` methods with

0 commit comments

Comments
 (0)