Skip to content

Commit eaffcc8

Browse files
authored
[DebugInfo][RemoveDIs] Make dropping variable locations explicit (#72399)
In present-day debug-info, when you delete all instructions, you delete all their debug-info with it because debug-info is stored in instructions. With debug-info stored in DPValue objects however, deleting instructions causes DPValue objects to clump together into a large blob of debug-info that hangs around in the block, as nothing has explicitly deleted it. To restore this behaviour, scatter calls to dropDbgValues around in places that used to delete chunks of dbg.values, for example during stripDebugInfo and in the code that deletes everything after an Unreachable instruction. DCE is another example. The tests with --try... added to them are new scenarios where we can now correctly replicate the "normal" debug-info behaviour. Alas, there's no explicit test for the opt -strip-debug option though (in dbg.value mode or DPValue mode).
1 parent 5271d33 commit eaffcc8

File tree

7 files changed

+35
-0
lines changed

7 files changed

+35
-0
lines changed

llvm/lib/IR/DebugInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ bool llvm::stripDebugInfo(Function &F) {
547547
// DIAssignID are debug info metadata primitives.
548548
I.setMetadata(LLVMContext::MD_DIAssignID, nullptr);
549549
}
550+
I.dropDbgValues();
550551
}
551552
}
552553
return Changed;

llvm/lib/Transforms/Scalar/ADCE.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,16 @@ ADCEChanged AggressiveDeadCodeElimination::removeDeadInstructions() {
544544
// value of the function, and may therefore be deleted safely.
545545
// NOTE: We reuse the Worklist vector here for memory efficiency.
546546
for (Instruction &I : llvm::reverse(instructions(F))) {
547+
// With "RemoveDIs" debug-info stored in DPValue objects, debug-info
548+
// attached to this instruction, and drop any for scopes that aren't alive,
549+
// like the rest of this loop does. Extending support to assignment tracking
550+
// is future work.
551+
for (DPValue &DPV : make_early_inc_range(I.getDbgValueRange())) {
552+
if (AliveScopes.count(DPV.getDebugLoc()->getScope()))
553+
continue;
554+
I.dropOneDbgValue(&DPV);
555+
}
556+
547557
// Check if the instruction is alive.
548558
if (isLive(&I))
549559
continue;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,6 +3122,11 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI,
31223122
}
31233123

31243124
// Hoist the instructions.
3125+
// In "RemoveDIs" non-instr debug-info mode, drop DPValues attached to these
3126+
// instructions, in the same way that dbg.value intrinsics are dropped at the
3127+
// end of this block.
3128+
for (auto &It : make_range(ThenBB->begin(), ThenBB->end()))
3129+
It.dropDbgValues();
31253130
BB->splice(BI->getIterator(), ThenBB, ThenBB->begin(),
31263131
std::prev(ThenBB->end()));
31273132

@@ -5194,6 +5199,15 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
51945199

51955200
bool Changed = false;
51965201

5202+
// Ensure that any debug-info records that used to occur after the Unreachable
5203+
// are moved to in front of it -- otherwise they'll "dangle" at the end of
5204+
// the block.
5205+
BB->flushTerminatorDbgValues();
5206+
5207+
// Debug-info records on the unreachable inst itself should be deleted, as
5208+
// below we delete everything past the final executable instruction.
5209+
UI->dropDbgValues();
5210+
51975211
// If there are any instructions immediately before the unreachable that can
51985212
// be removed, do so.
51995213
while (UI->getIterator() != BB->begin()) {
@@ -5210,6 +5224,10 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
52105224
// block will be the unwind edges of Invoke/CatchSwitch/CleanupReturn,
52115225
// and we can therefore guarantee this block will be erased.
52125226

5227+
// If we're deleting this, we're deleting any subsequent dbg.values, so
5228+
// delete DPValue records of variable information.
5229+
BBI->dropDbgValues();
5230+
52135231
// Delete this instruction (any uses are guaranteed to be dead)
52145232
BBI->replaceAllUsesWith(PoisonValue::get(BBI->getType()));
52155233
BBI->eraseFromParent();

llvm/test/Transforms/ADCE/adce-salvage-dbg-value.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
;; Check that adce salvages debug info properly.
22
; RUN: opt -passes=adce -S < %s | FileCheck %s
3+
; RUN: opt -passes=adce -S < %s --try-experimental-debuginfo-iterators| FileCheck %s
34

45
; ModuleID = 'test.ll'
56
source_filename = "test.ll"

llvm/test/Transforms/ADCE/debug-info-intrinsic.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes=adce -S < %s | FileCheck %s
2+
; RUN: opt -passes=adce -S < %s --try-experimental-debuginfo-iterators | FileCheck %s
23
; Test that debug info intrinsics in dead scopes get eliminated by -adce.
34

45
; Generated with 'clang -g -S -emit-llvm | opt -passes=mem2reg -inline' at r262899

llvm/test/Transforms/SimplifyCFG/return-merge.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck --check-prefixes=CHECK %s
33
; RUN: opt < %s -passes=debugify,simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck --check-prefixes=DBGINFO %s
44

5+
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S --try-experimental-debuginfo-iterators | FileCheck --check-prefixes=CHECK %s
6+
; RUN: opt < %s -passes=debugify,simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S --try-experimental-debuginfo-iterators | FileCheck --check-prefixes=DBGINFO %s
7+
58
define i32 @test1(i1 %C) {
69
; CHECK-LABEL: @test1(
710
; CHECK-NEXT: entry:

llvm/test/Transforms/SimplifyCFG/speculate-dbgvalue.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -S -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 | FileCheck %s
3+
; RUN: opt < %s -S -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 --try-experimental-debuginfo-iterators | FileCheck %s
34

45
; This test case was generated from speculate-dbgvalue.c:
56
;

0 commit comments

Comments
 (0)