Skip to content

Commit 54ba428

Browse files
committed
[AutoDiff] Emit SIL diff witnesses into original function's IRGenModule. (#28237)
The differentiation transform does not yet canonicalize differentiability witnesses so that they always have JVP/VJP functions. TF-894 tracks this. Until then, emit SIL differentiability witness into the IRGenModule for the original function. The significance of this is unclear; `IRGenerator::getGenModule` seems relevant only for multi-threaded compilation. Resolves TF-960: crash when `SILDifferentiabilityWitness::getVJP` is null.
1 parent 3b934c5 commit 54ba428

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,13 @@ void IRGenerator::emitGlobalTopLevel() {
10721072
if (dw.isDeclaration())
10731073
continue;
10741074

1075-
// Emit into same IRGenModule as the VJP.
1076-
CurrentIGMPtr IGM = getGenModule(dw.getVJP());
1075+
// Emit into same IRGenModule as the original function.
1076+
// NOTE(TF-894): Investigate whether `getGenModule(dw.getVJP())` is
1077+
// significant/desirable; `getGenModule` seems relevant for multi-threaded
1078+
// compilation. When the differentiation transform canonicalizes all
1079+
// differentiability witnesses to have JVP/VJP functions, we can assert
1080+
// that JVP/VJP functions exist and use `getGenModule(dw.getVJP())`.
1081+
CurrentIGMPtr IGM = getGenModule(dw.getOriginalFunction());
10771082

10781083
IGM->emitSILDifferentiabilityWitness(&dw);
10791084
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
struct TF_960: Differentiable {
2+
@differentiable
3+
func callAsFunction(_ input: Float) -> Float {
4+
return input
5+
}
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -c %S/Inputs/tf960-irgen-diff-witness-no-derivatives-other-module.swift %s -O -module-name main -num-threads 36
2+
// REQUIRES: asserts
3+
4+
// TF-960: IRGen crash for uncanonicalized differentiability witnesses.
5+
// This issue will become obsolete after TF-894, when the differentiation
6+
// transform canonicalizes differentiability witnesses to have derivative
7+
// functions and assertions are added to IRGen.
8+
9+
// Stack dump:
10+
// ...
11+
// 1. Swift version 5.1.1-dev (Swift af915c09de)
12+
// 0 swiftc 0x0000000109acda65 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
13+
// 1 swiftc 0x0000000109acca68 llvm::sys::RunSignalHandlers() + 248
14+
// 2 swiftc 0x0000000109ace058 SignalHandler(int) + 264
15+
// 3 libsystem_platform.dylib 0x00007fff728e4b5d _sigtramp + 29
16+
// 4 libsystem_platform.dylib 0x00007ff639560230 _sigtramp + 3334977264
17+
// 5 swiftc 0x0000000105b316bb swift::irgen::IRGenerator::emitGlobalTopLevel() + 1307
18+
// 6 swiftc 0x0000000105bf2572 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**) + 1682

0 commit comments

Comments
 (0)