Skip to content

Commit 6d23aaa

Browse files
committed
[DebugInfo][RemoveDIs] Implement redundant elimination for DPValues (#72284)
This pass steps through a block forwards and backwards, identifying those variable assignment records that are redundant, and erases them, saving us a decent wedge of compile-time. This patch re-implements it to use the replacement for DbgValueInsts, DPValues, in an almost identical way. Alas the test I've added the try-remove-dis flag to is the only one I've been able to find that manually runs this pass.
1 parent ef9bcac commit 6d23aaa

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,39 @@ bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
382382
/// - Check fully overlapping fragments and not only identical fragments.
383383
/// - Support dbg.declare. dbg.label, and possibly other meta instructions being
384384
/// part of the sequence of consecutive instructions.
385+
static bool DPValuesRemoveRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
386+
SmallVector<DPValue *, 8> ToBeRemoved;
387+
SmallDenseSet<DebugVariable> VariableSet;
388+
for (auto &I : reverse(*BB)) {
389+
for (DPValue &DPV : reverse(I.getDbgValueRange())) {
390+
DebugVariable Key(DPV.getVariable(), DPV.getExpression(),
391+
DPV.getDebugLoc()->getInlinedAt());
392+
auto R = VariableSet.insert(Key);
393+
// If the same variable fragment is described more than once it is enough
394+
// to keep the last one (i.e. the first found since we for reverse
395+
// iteration).
396+
// FIXME: add assignment tracking support (see parallel implementation
397+
// below).
398+
if (!R.second)
399+
ToBeRemoved.push_back(&DPV);
400+
continue;
401+
}
402+
// Sequence with consecutive dbg.value instrs ended. Clear the map to
403+
// restart identifying redundant instructions if case we find another
404+
// dbg.value sequence.
405+
VariableSet.clear();
406+
}
407+
408+
for (auto &DPV : ToBeRemoved)
409+
DPV->eraseFromParent();
410+
411+
return !ToBeRemoved.empty();
412+
}
413+
385414
static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
415+
if (BB->IsNewDbgInfoFormat)
416+
return DPValuesRemoveRedundantDbgInstrsUsingBackwardScan(BB);
417+
386418
SmallVector<DbgValueInst *, 8> ToBeRemoved;
387419
SmallDenseSet<DebugVariable> VariableSet;
388420
for (auto &I : reverse(*BB)) {
@@ -440,7 +472,38 @@ static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
440472
///
441473
/// Possible improvements:
442474
/// - Keep track of non-overlapping fragments.
475+
static bool DPValuesRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
476+
SmallVector<DPValue *, 8> ToBeRemoved;
477+
DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>
478+
VariableMap;
479+
for (auto &I : *BB) {
480+
for (DPValue &DPV : I.getDbgValueRange()) {
481+
DebugVariable Key(DPV.getVariable(), std::nullopt,
482+
DPV.getDebugLoc()->getInlinedAt());
483+
auto VMI = VariableMap.find(Key);
484+
// Update the map if we found a new value/expression describing the
485+
// variable, or if the variable wasn't mapped already.
486+
SmallVector<Value *, 4> Values(DPV.location_ops());
487+
if (VMI == VariableMap.end() || VMI->second.first != Values ||
488+
VMI->second.second != DPV.getExpression()) {
489+
VariableMap[Key] = {Values, DPV.getExpression()};
490+
continue;
491+
}
492+
// Found an identical mapping. Remember the instruction for later removal.
493+
ToBeRemoved.push_back(&DPV);
494+
}
495+
}
496+
497+
for (auto *DPV : ToBeRemoved)
498+
DPV->eraseFromParent();
499+
500+
return !ToBeRemoved.empty();
501+
}
502+
443503
static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
504+
if (BB->IsNewDbgInfoFormat)
505+
return DPValuesRemoveRedundantDbgInstrsUsingForwardScan(BB);
506+
444507
SmallVector<DbgValueInst *, 8> ToBeRemoved;
445508
DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>
446509
VariableMap;

llvm/test/Transforms/DCE/dbg-value-removal.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=redundant-dbg-inst-elim | FileCheck %s
3+
; RUN: opt < %s -S -passes=redundant-dbg-inst-elim --try-experimental-debuginfo-iterators | FileCheck %s
34

45
; All dbg.value with location "!dbg !19" are redundant in the input.
56
; FIXME: We do not handle non-overlapping/overlapping fragments perfectly yet.

0 commit comments

Comments
 (0)