Skip to content

Commit 116baea

Browse files
committed
[AutoDiff] Fix DI verification failure with trampoline blocks in VJP.
- Set the builder's debug scope to the remapped debug scope in the overriden visiters. - In trampoline blocks where we build predecessor enums, make the trampoline builder inherit the original terminator's remapped debug scope. Resolves rdar://74087329.
1 parent ce587f0 commit 116baea

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/SILOptimizer/Differentiation/VJPCloner.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class VJPCloner::Implementation final
218218
}
219219

220220
void visitReturnInst(ReturnInst *ri) {
221+
Builder.setCurrentDebugScope(getOpScope(ri->getDebugScope()));
221222
auto loc = ri->getOperand().getLoc();
222223
// Build pullback struct value for original block.
223224
auto *origExit = ri->getParent();
@@ -291,6 +292,7 @@ class VJPCloner::Implementation final
291292
}
292293

293294
void visitBranchInst(BranchInst *bi) {
295+
Builder.setCurrentDebugScope(getOpScope(bi->getDebugScope()));
294296
// Build pullback struct value for original block.
295297
// Build predecessor enum value for destination block.
296298
auto *origBB = bi->getParent();
@@ -310,6 +312,7 @@ class VJPCloner::Implementation final
310312
}
311313

312314
void visitCondBranchInst(CondBranchInst *cbi) {
315+
Builder.setCurrentDebugScope(getOpScope(cbi->getDebugScope()));
313316
// Build pullback struct value for original block.
314317
auto *pbStructVal = buildPullbackValueStructValue(cbi);
315318
// Create a new `cond_br` instruction.
@@ -320,6 +323,7 @@ class VJPCloner::Implementation final
320323
}
321324

322325
void visitSwitchEnumTermInst(SwitchEnumTermInst inst) {
326+
Builder.setCurrentDebugScope(getOpScope(inst->getDebugScope()));
323327
// Build pullback struct value for original block.
324328
auto *pbStructVal = buildPullbackValueStructValue(*inst);
325329

@@ -360,6 +364,7 @@ class VJPCloner::Implementation final
360364
}
361365

362366
void visitCheckedCastBranchInst(CheckedCastBranchInst *ccbi) {
367+
Builder.setCurrentDebugScope(getOpScope(ccbi->getDebugScope()));
363368
// Build pullback struct value for original block.
364369
auto *pbStructVal = buildPullbackValueStructValue(ccbi);
365370
// Create a new `checked_cast_branch` instruction.
@@ -373,6 +378,7 @@ class VJPCloner::Implementation final
373378
}
374379

375380
void visitCheckedCastValueBranchInst(CheckedCastValueBranchInst *ccvbi) {
381+
Builder.setCurrentDebugScope(getOpScope(ccvbi->getDebugScope()));
376382
// Build pullback struct value for original block.
377383
auto *pbStructVal = buildPullbackValueStructValue(ccvbi);
378384
// Create a new `checked_cast_value_branch` instruction.
@@ -386,6 +392,7 @@ class VJPCloner::Implementation final
386392
}
387393

388394
void visitCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *ccabi) {
395+
Builder.setCurrentDebugScope(getOpScope(ccabi->getDebugScope()));
389396
// Build pullback struct value for original block.
390397
auto *pbStructVal = buildPullbackValueStructValue(ccabi);
391398
// Create a new `checked_cast_addr_branch` instruction.
@@ -437,6 +444,7 @@ class VJPCloner::Implementation final
437444
return;
438445
}
439446

447+
Builder.setCurrentDebugScope(getOpScope(ai->getDebugScope()));
440448
auto loc = ai->getLoc();
441449
auto &builder = getBuilder();
442450
auto origCallee = getOpValue(ai->getCallee());
@@ -698,6 +706,7 @@ class VJPCloner::Implementation final
698706
}
699707

700708
void visitTryApplyInst(TryApplyInst *tai) {
709+
Builder.setCurrentDebugScope(getOpScope(tai->getDebugScope()));
701710
// Build pullback struct value for original block.
702711
auto *pbStructVal = buildPullbackValueStructValue(tai);
703712
// Create a new `try_apply` instruction.
@@ -1008,6 +1017,7 @@ SILBasicBlock *VJPCloner::Implementation::createTrampolineBasicBlock(
10081017
// In the trampoline block, build predecessor enum value for VJP successor
10091018
// block and branch to it.
10101019
SILBuilder trampolineBuilder(trampolineBB);
1020+
trampolineBuilder.setCurrentDebugScope(getOpScope(termInst->getDebugScope()));
10111021
auto *origBB = termInst->getParent();
10121022
auto *succEnumVal =
10131023
buildPredecessorEnumValue(trampolineBuilder, origBB, succBB, pbStructVal);
@@ -1032,7 +1042,6 @@ VJPCloner::Implementation::buildPullbackValueStructValue(TermInst *termInst) {
10321042
auto *predEnumArg = vjpBB->getArguments().back();
10331043
bbPullbackValues.insert(bbPullbackValues.begin(), predEnumArg);
10341044
}
1035-
getBuilder().setCurrentDebugScope(getOpScope(termInst->getDebugScope()));
10361045
return getBuilder().createStruct(loc, structLoweredTy, bbPullbackValues);
10371046
}
10381047

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-build-swift %s
2+
// RUN: %target-swift-frontend -c -g -Xllvm -verify-di-holes=true %s
3+
4+
// rdar://74087329 (DI verification failure with trampoline blocks in VJP)
5+
6+
import _Differentiation
7+
8+
func foo(x: Float?) -> Float {
9+
_ = withoutDerivative(at: x ?? 0)
10+
return 0
11+
}
12+
gradient(at: 0, in: foo)

0 commit comments

Comments
 (0)