Skip to content

Commit 858fe6c

Browse files
authored
Remove debug instructions on dead temporaries in CopyForwarding (#32941)
1 parent a2c5f05 commit 858fe6c

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ class CopyForwarding {
612612

613613
protected:
614614
bool propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy);
615-
CopyAddrInst *findCopyIntoDeadTemp(CopyAddrInst *destCopy);
616-
bool forwardDeadTempCopy(CopyAddrInst *srcCopy, CopyAddrInst *destCopy);
615+
CopyAddrInst *findCopyIntoDeadTemp(
616+
CopyAddrInst *destCopy,
617+
SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete);
618+
bool forwardDeadTempCopy(CopyAddrInst *destCopy);
617619
bool forwardPropagateCopy();
618620
bool backwardPropagateCopy();
619621
bool hoistDestroy(SILInstruction *DestroyPoint, SILLocation DestroyLoc);
@@ -681,12 +683,10 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
681683
// Handle copy-of-copy without analyzing uses.
682684
// Assumes that CurrentCopy->getSrc() is dead after CurrentCopy.
683685
assert(CurrentCopy->isTakeOfSrc() || hoistingDestroy);
684-
if (auto *srcCopy = findCopyIntoDeadTemp(CurrentCopy)) {
685-
if (forwardDeadTempCopy(srcCopy, CurrentCopy)) {
686-
HasChanged = true;
687-
++NumDeadTemp;
688-
return true;
689-
}
686+
if (forwardDeadTempCopy(CurrentCopy)) {
687+
HasChanged = true;
688+
++NumDeadTemp;
689+
return true;
690690
}
691691

692692
if (forwardPropagateCopy()) {
@@ -737,7 +737,9 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
737737
/// Unlike the forward and backward propagation that finds all use points, this
738738
/// handles copies of address projections. By conservatively checking all
739739
/// intervening instructions, it avoids the need to analyze projection paths.
740-
CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
740+
CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(
741+
CopyAddrInst *destCopy,
742+
SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete) {
741743
auto tmpVal = destCopy->getSrc();
742744
assert(tmpVal == CurrentDef);
743745
assert(isIdentifiedSourceValue(tmpVal));
@@ -750,8 +752,20 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
750752
if (srcCopy->getDest() == tmpVal)
751753
return srcCopy;
752754
}
755+
// 'SrcUserInsts' consists of all users of the 'temp'
753756
if (SrcUserInsts.count(UserInst))
754757
return nullptr;
758+
759+
// Collect all debug_value_addr instructions between temp to dest copy and
760+
// src to temp copy. On success, these debug_value_addr instructions should
761+
// be deleted.
762+
if (auto *debugUser = dyn_cast<DebugValueAddrInst>(UserInst)) {
763+
// 'SrcDebugValueInsts' consists of all the debug users of 'temp'
764+
if (SrcDebugValueInsts.count(debugUser))
765+
debugInstsToDelete.push_back(debugUser);
766+
continue;
767+
}
768+
755769
if (UserInst->mayWriteToMemory())
756770
return nullptr;
757771
}
@@ -780,12 +794,16 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
780794
/// - %temp is uninitialized following `srcCopy` and subsequent instruction
781795
/// attempts to destroy this uninitialized value.
782796
bool CopyForwarding::
783-
forwardDeadTempCopy(CopyAddrInst *srcCopy, CopyAddrInst *destCopy) {
797+
forwardDeadTempCopy(CopyAddrInst *destCopy) {
798+
SmallVector<DebugValueAddrInst*, 2> debugInstsToDelete;
799+
auto *srcCopy = findCopyIntoDeadTemp(CurrentCopy, debugInstsToDelete);
800+
if (!srcCopy)
801+
return false;
802+
784803
LLVM_DEBUG(llvm::dbgs() << " Temp Copy:" << *srcCopy
785804
<< " to " << *destCopy);
786805

787806
assert(srcCopy->getDest() == destCopy->getSrc());
788-
789807
// This pattern can be trivially folded without affecting %temp destroys:
790808
// copy_addr [...] %src, [init] %temp
791809
// copy_addr [take] %temp, [...] %dest
@@ -798,6 +816,11 @@ forwardDeadTempCopy(CopyAddrInst *srcCopy, CopyAddrInst *destCopy) {
798816
.createDestroyAddr(srcCopy->getLoc(), srcCopy->getDest());
799817
}
800818

819+
// Delete all dead debug_value_addr instructions
820+
for (auto *deadDebugUser : debugInstsToDelete) {
821+
deadDebugUser->eraseFromParent();
822+
}
823+
801824
// Either `destCopy` is a take, or the caller is hoisting a destroy:
802825
// copy_addr %temp, %dest
803826
// ...

test/SILOptimizer/copyforward.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ public struct S<T> {
613613
// CHECK-LABEL: deadtemp
614614
// CHECK: %[[G:.*]] = struct_element_addr %0 : $*S<T>, #S.g
615615
// CHECK-NOT: copy_addr
616+
// CHECK-NOT: debug_value_addr
616617
// CHECK: %[[F:.*]] = struct_element_addr %0 : $*S<T>, #S.f
617618
// CHECK: copy_addr %[[G]] to %[[F]] : $*T
618619
// CHECK: return
@@ -621,6 +622,7 @@ bb0(%0 : $*S<T>):
621622
%1 = struct_element_addr %0 : $*S<T>, #S.g
622623
%2 = alloc_stack $T
623624
copy_addr %1 to [initialization] %2 : $*T
625+
debug_value_addr %2 : $*T
624626
%4 = struct_element_addr %0 : $*S<T>, #S.f
625627
copy_addr [take] %2 to %4 : $*T
626628
dealloc_stack %2 : $*T

0 commit comments

Comments
 (0)