Skip to content

Commit c076da9

Browse files
authored
[AutoDiff] Handle begin_borrow in reapplyFunctionConversion. (#29920)
Fix crash due to unhandled `begin_borrow` instruction in `reapplyFunctionConversion`. Resolves TF-1159.
1 parent 61c6862 commit c076da9

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,21 @@ static SILValue reapplyFunctionConversion(
356356
// Handle a few instruction cases.
357357
// copy_value
358358
if (auto *cvi = dyn_cast<CopyValueInst>(oldConvertedFunc)) {
359-
auto innerNewFunc = reapplyFunctionConversion(
360-
context, newFunc, oldFunc, cvi->getOperand(), builder, loc,
361-
newBuffersToDealloc, parameterIndices, newFuncGenSig);
362359
// Note: no `copy_value` is needed for the re-converted function because the
363360
// caller of `reapplyFunctionConversion` should consume the re-converted
364361
// function.
365-
return innerNewFunc;
362+
return reapplyFunctionConversion(
363+
context, newFunc, oldFunc, cvi->getOperand(), builder, loc,
364+
newBuffersToDealloc, parameterIndices, newFuncGenSig);
365+
}
366+
// begin_borrow
367+
if (auto *bbi = dyn_cast<BeginBorrowInst>(oldConvertedFunc)) {
368+
// Note: no `begin_borrow` is needed for the re-converted function because
369+
// the caller of `reapplyFunctionConversion` should consume the re-converted
370+
// function.
371+
return reapplyFunctionConversion(
372+
context, newFunc, oldFunc, bbi->getOperand(), builder, loc,
373+
newBuffersToDealloc, parameterIndices, newFuncGenSig);
366374
}
367375
// thin_to_thick_function
368376
if (auto *tttfi = dyn_cast<ThinToThickFunctionInst>(oldConvertedFunc)) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-swift-frontend -emit-sil %s -verify
2+
// REQUIRES: asserts
3+
4+
// TF-1159: `begin_borrow` instruction unhandled in the
5+
// `reapplyFunctionConversion` helper function.
6+
7+
func id<T>(_ x: T) -> T { x }
8+
9+
@differentiable
10+
func TF_1159(_ x: Float) -> Float {
11+
// Note: code below generates `partial_apply` and `begin_borrow`.
12+
let fn: (Float) -> Float = id
13+
return fn(x)
14+
}
15+
16+
// Unhandled function conversion instruction
17+
// UNREACHABLE executed at swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:433!
18+
// Stack dump:
19+
// ...
20+
// 1. Swift version 5.2-dev (Swift 415d33b3f1)
21+
// 2. While running pass #27 SILModuleTransform "Differentiation".
22+
// 3. While canonicalizing `differentiable_function` SIL node %12 = differentiable_function [parameters 0] %10 : $@callee_guaranteed (Float) -> Float // users: %17, %13
23+
// 4. While ...in SIL function "@AD__$s4main3fooyS2fF__vjp_src_0_wrt_0".
24+
// for 'foo(_:)' (at tf-1159.swift:4:1)
25+
// 0 swift 0x0000000107b15105 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
26+
// 1 swift 0x0000000107b14078 llvm::sys::RunSignalHandlers() + 248
27+
// 2 swift 0x0000000107b15706 SignalHandler(int) + 278
28+
// 3 libsystem_platform.dylib 0x00007fff68ed2b5d _sigtramp + 29
29+
// 4 libsystem_platform.dylib 0x0000000000000053 _sigtramp + 2534593811
30+
// 5 libsystem_c.dylib 0x00007fff68d8c6a6 abort + 127
31+
// 6 swift 0x0000000108dcc09e llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 462
32+
// 7 swift 0x0000000104047ec2 reapplyFunctionConversion(swift::autodiff::ADContext&, swift::SILValue, swift::SILValue, swift::SILValue, swift::SILBuilder&, swift::SILLocation, llvm::SmallVectorImpl<swift::AllocStackInst*>&, swift::IndexSubset*, swift::GenericSignature) + 1506
33+
// 8 swift 0x000000010402d5c0 (anonymous namespace)::DifferentiationTransformer::promoteToDifferentiableFunction(swift::DifferentiableFunctionInst*, swift::SILBuilder&, swift::SILLocation, swift::autodiff::DifferentiationInvoker) + 8880
34+
// 9 swift 0x00000001040292ea (anonymous namespace)::DifferentiationTransformer::processDifferentiableFunctionInst(swift::DifferentiableFunctionInst*) + 426
35+
// 10 swift 0x0000000104026b06 (anonymous namespace)::Differentiation::run() + 1174

0 commit comments

Comments
 (0)