Skip to content

Commit 990fc4b

Browse files
authored
Merge pull request swiftlang#38945 from meg-gupta/fixcopyfwd
Cleanup dead debug_value_addr in CopyForwarding
2 parents f90f06d + f4b585e commit 990fc4b

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ class CopyForwarding {
618618
bool forwardDeadTempCopy(CopyAddrInst *destCopy);
619619
bool forwardPropagateCopy();
620620
bool backwardPropagateCopy();
621-
bool hoistDestroy(SILInstruction *DestroyPoint, SILLocation DestroyLoc);
621+
bool hoistDestroy(SILInstruction *DestroyPoint, SILLocation DestroyLoc,
622+
SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete);
622623

623624
bool isSourceDeadAtCopy();
624625

@@ -1224,8 +1225,11 @@ bool CopyForwarding::backwardPropagateCopy() {
12241225
///
12251226
/// Return true if a destroy was inserted, forwarded from a copy, or the
12261227
/// block was marked dead-in.
1227-
bool CopyForwarding::hoistDestroy(SILInstruction *DestroyPoint,
1228-
SILLocation DestroyLoc) {
1228+
/// \p debugInstsToDelete will contain the debug_value_addr users of copy src
1229+
/// that need to be deleted if the hoist is successful
1230+
bool CopyForwarding::hoistDestroy(
1231+
SILInstruction *DestroyPoint, SILLocation DestroyLoc,
1232+
SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete) {
12291233
if (!EnableDestroyHoisting)
12301234
return false;
12311235

@@ -1266,6 +1270,14 @@ bool CopyForwarding::hoistDestroy(SILInstruction *DestroyPoint,
12661270
<< *Inst);
12671271
return tryToInsertHoistedDestroyAfter(Inst);
12681272
}
1273+
if (auto *debugAddr = dyn_cast<DebugValueAddrInst>(Inst)) {
1274+
// Collect debug_value_addr uses of copy src. If the hoist is
1275+
// successful, these instructions will have consumed operand and need to
1276+
// be erased.
1277+
if (SrcDebugValueInsts.contains(debugAddr)) {
1278+
debugInstsToDelete.push_back(debugAddr);
1279+
}
1280+
}
12691281
if (!ShouldHoist && isa<ApplyInst>(Inst))
12701282
ShouldHoist = true;
12711283
continue;
@@ -1316,10 +1328,11 @@ void CopyForwarding::forwardCopiesOf(SILValue Def, SILFunction *F) {
13161328
bool HoistedDestroyFound = false;
13171329
SILLocation HoistedDestroyLoc = F->getLocation();
13181330
const SILDebugScope *HoistedDebugScope = nullptr;
1331+
SmallVector<DebugValueAddrInst *, 2> debugInstsToDelete;
13191332

13201333
for (auto *Destroy : DestroyPoints) {
13211334
// If hoistDestroy returns false, it was not worth hoisting.
1322-
if (hoistDestroy(Destroy, Destroy->getLoc())) {
1335+
if (hoistDestroy(Destroy, Destroy->getLoc(), debugInstsToDelete)) {
13231336
// Propagate DestroyLoc for any destroy hoisted above a block.
13241337
if (DeadInBlocks.count(Destroy->getParent())) {
13251338
HoistedDestroyLoc = Destroy->getLoc();
@@ -1331,7 +1344,12 @@ void CopyForwarding::forwardCopiesOf(SILValue Def, SILFunction *F) {
13311344
// original Destroy.
13321345
Destroy->eraseFromParent();
13331346
assert(HasChanged || !DeadInBlocks.empty() && "HasChanged should be set");
1347+
// Since the hoist was successful, delete all dead debug_value_addr
1348+
for (auto *deadDebugUser : debugInstsToDelete) {
1349+
deadDebugUser->eraseFromParent();
1350+
}
13341351
}
1352+
debugInstsToDelete.clear();
13351353
}
13361354
// Any blocks containing a DestroyPoints where hoistDestroy did not find a use
13371355
// are now marked in DeadInBlocks.
@@ -1358,9 +1376,14 @@ void CopyForwarding::forwardCopiesOf(SILValue Def, SILFunction *F) {
13581376
if (DeadInSuccs.size() == Succs.size() &&
13591377
!SrcUserInsts.count(BB->getTerminator())) {
13601378
// All successors are dead, so continue hoisting.
1361-
bool WasHoisted = hoistDestroy(BB->getTerminator(), HoistedDestroyLoc);
1379+
bool WasHoisted = hoistDestroy(BB->getTerminator(), HoistedDestroyLoc,
1380+
debugInstsToDelete);
13621381
(void)WasHoisted;
13631382
assert(WasHoisted && "should always hoist above a terminator");
1383+
for (auto *deadDebugUser : debugInstsToDelete) {
1384+
deadDebugUser->eraseFromParent();
1385+
}
1386+
debugInstsToDelete.clear();
13641387
continue;
13651388
}
13661389
// Emit a destroy on each CFG edge leading to a dead-in block. This requires

test/SILOptimizer/copyforward_ossa.sil

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ bb0(%0 : $*T):
6262
%f1 = function_ref @f_in : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
6363
%c1 = apply %f1<T>(%l1) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
6464
dealloc_stack %l1 : $*T
65-
// forwardPropagateCopy should cleanup debug_value_addr. See rdar://66000188
66-
// debug_value_addr %0 : $*T
65+
debug_value_addr %0 : $*T
6766
destroy_addr %0 : $*T
6867
%r1 = tuple ()
6968
return %r1 : $()
@@ -136,8 +135,7 @@ bb0(%0 : $*T):
136135
debug_value_addr %l1 : $*T
137136
copy_addr %l1 to [initialization] %0 : $*T
138137
debug_value_addr %0 : $*T
139-
// backwardPropagateCopy should cleanup debug_value_addr. See rdar://66000188
140-
// debug_value_addr %l1 : $*T
138+
debug_value_addr %l1 : $*T
141139
destroy_addr %l1 : $*T
142140
dealloc_stack %l1 : $*T
143141
%t = tuple ()

0 commit comments

Comments
 (0)