Skip to content

Commit 19b65a9

Browse files
authored
[DebugInfo][RemoveDIs] Add a DPValue implementation for instcombine sinking (#77930)
In instcombine, when we sink an instruction into a successor block, we try to clone and salvage all the variable assignments that use that Value. This is a behaviour that's (IMO) flawed, but there are important use cases where we want to avoid regressions, thus we're implementing this for the non-instruction debug-info representation. This patch refactors the dbg.value sinking code into it's own function, and installs a parallel implementation for DPValues, the non-instruction debug-info container. This is mostly identical to the dbg.value implementation, except that we don't have an easy-to-access ordering between DPValues, and have to jump through extra hoops to establish one in the (rare) cases where that ordering is required. The test added represents a common use-case in LLVM where these behaviours are important: a loop has been completely optimised away, leaving several dbg.values in a row referring to an instruction that's going to sink. The dbg.values should sink in both dbg.value and RemoveDIs mode, and additionally only the last assignment should sink.
1 parent e8a5010 commit 19b65a9

File tree

5 files changed

+233
-7
lines changed

5 files changed

+233
-7
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
8989
public:
9090
void deleteInstr();
9191

92+
const Instruction *getInstruction() const;
9293
const BasicBlock *getParent() const;
9394
BasicBlock *getParent();
9495
void dump() const;

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ bool DPValue::isKillAddress() const {
337337
return !Addr || isa<UndefValue>(Addr);
338338
}
339339

340+
const Instruction *DPValue::getInstruction() const {
341+
return Marker->MarkedInstr;
342+
}
343+
340344
const BasicBlock *DPValue::getParent() const {
341345
return Marker->MarkedInstr->getParent();
342346
}

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
736736
Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
737737

738738
bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
739+
void tryToSinkInstructionDbgValues(
740+
Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
741+
BasicBlock *DestBlock, SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers);
742+
void tryToSinkInstructionDPValues(Instruction *I,
743+
BasicBlock::iterator InsertPos,
744+
BasicBlock *SrcBlock, BasicBlock *DestBlock,
745+
SmallVectorImpl<DPValue *> &DPUsers);
739746

740747
bool removeInstructionsBeforeUnreachable(Instruction &I);
741748
void addDeadEdge(BasicBlock *From, BasicBlock *To,

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 147 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4313,12 +4313,31 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
43134313
// mark the location undef: we know it was supposed to receive a new location
43144314
// here, but that computation has been sunk.
43154315
SmallVector<DbgVariableIntrinsic *, 2> DbgUsers;
4316-
findDbgUsers(DbgUsers, I);
4316+
SmallVector<DPValue *, 2> DPValues;
4317+
findDbgUsers(DbgUsers, I, &DPValues);
4318+
if (!DbgUsers.empty())
4319+
tryToSinkInstructionDbgValues(I, InsertPos, SrcBlock, DestBlock, DbgUsers);
4320+
if (!DPValues.empty())
4321+
tryToSinkInstructionDPValues(I, InsertPos, SrcBlock, DestBlock, DPValues);
4322+
4323+
// PS: there are numerous flaws with this behaviour, not least that right now
4324+
// assignments can be re-ordered past other assignments to the same variable
4325+
// if they use different Values. Creating more undef assignements can never be
4326+
// undone. And salvaging all users outside of this block can un-necessarily
4327+
// alter the lifetime of the live-value that the variable refers to.
4328+
// Some of these things can be resolved by tolerating debug use-before-defs in
4329+
// LLVM-IR, however it depends on the instruction-referencing CodeGen backend
4330+
// being used for more architectures.
43174331

4332+
return true;
4333+
}
4334+
4335+
void InstCombinerImpl::tryToSinkInstructionDbgValues(
4336+
Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
4337+
BasicBlock *DestBlock, SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers) {
43184338
// For all debug values in the destination block, the sunk instruction
43194339
// will still be available, so they do not need to be dropped.
43204340
SmallVector<DbgVariableIntrinsic *, 2> DbgUsersToSalvage;
4321-
SmallVector<DPValue *, 2> DPValuesToSalvage;
43224341
for (auto &DbgUser : DbgUsers)
43234342
if (DbgUser->getParent() != DestBlock)
43244343
DbgUsersToSalvage.push_back(DbgUser);
@@ -4362,19 +4381,140 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
43624381

43634382
// Perform salvaging without the clones, then sink the clones.
43644383
if (!DIIClones.empty()) {
4365-
// RemoveDIs: pass in empty vector of DPValues until we get to instrumenting
4366-
// this pass.
4367-
SmallVector<DPValue *, 1> DummyDPValues;
4368-
salvageDebugInfoForDbgValues(*I, DbgUsersToSalvage, DummyDPValues);
4384+
salvageDebugInfoForDbgValues(*I, DbgUsersToSalvage, {});
43694385
// The clones are in reverse order of original appearance, reverse again to
43704386
// maintain the original order.
43714387
for (auto &DIIClone : llvm::reverse(DIIClones)) {
43724388
DIIClone->insertBefore(&*InsertPos);
43734389
LLVM_DEBUG(dbgs() << "SINK: " << *DIIClone << '\n');
43744390
}
43754391
}
4392+
}
43764393

4377-
return true;
4394+
void InstCombinerImpl::tryToSinkInstructionDPValues(
4395+
Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
4396+
BasicBlock *DestBlock, SmallVectorImpl<DPValue *> &DPValues) {
4397+
// Implementation of tryToSinkInstructionDbgValues, but for the DPValue of
4398+
// variable assignments rather than dbg.values.
4399+
4400+
// Fetch all DPValues not already in the destination.
4401+
SmallVector<DPValue *, 2> DPValuesToSalvage;
4402+
for (auto &DPV : DPValues)
4403+
if (DPV->getParent() != DestBlock)
4404+
DPValuesToSalvage.push_back(DPV);
4405+
4406+
// Fetch a second collection, of DPValues in the source block that we're going
4407+
// to sink.
4408+
SmallVector<DPValue *> DPValuesToSink;
4409+
for (DPValue *DPV : DPValuesToSalvage)
4410+
if (DPV->getParent() == SrcBlock)
4411+
DPValuesToSink.push_back(DPV);
4412+
4413+
// Sort DPValues according to their position in the block. This is a partial
4414+
// order: DPValues attached to different instructions will be ordered by the
4415+
// instruction order, but DPValues attached to the same instruction won't
4416+
// have an order.
4417+
auto Order = [](DPValue *A, DPValue *B) -> bool {
4418+
return B->getInstruction()->comesBefore(A->getInstruction());
4419+
};
4420+
llvm::stable_sort(DPValuesToSink, Order);
4421+
4422+
// If there are two assignments to the same variable attached to the same
4423+
// instruction, the ordering between the two assignments is important. Scan
4424+
// for this (rare) case and establish which is the last assignment.
4425+
using InstVarPair = std::pair<const Instruction *, DebugVariable>;
4426+
SmallDenseMap<InstVarPair, DPValue *> FilterOutMap;
4427+
if (DPValuesToSink.size() > 1) {
4428+
SmallDenseMap<InstVarPair, unsigned> CountMap;
4429+
// Count how many assignments to each variable there is per instruction.
4430+
for (DPValue *DPV : DPValuesToSink) {
4431+
DebugVariable DbgUserVariable =
4432+
DebugVariable(DPV->getVariable(), DPV->getExpression(),
4433+
DPV->getDebugLoc()->getInlinedAt());
4434+
CountMap[std::make_pair(DPV->getInstruction(), DbgUserVariable)] += 1;
4435+
}
4436+
4437+
// If there are any instructions with two assignments, add them to the
4438+
// FilterOutMap to record that they need extra filtering.
4439+
SmallPtrSet<const Instruction *, 4> DupSet;
4440+
for (auto It : CountMap) {
4441+
if (It.second > 1) {
4442+
FilterOutMap[It.first] = nullptr;
4443+
DupSet.insert(It.first.first);
4444+
}
4445+
}
4446+
4447+
// For all instruction/variable pairs needing extra filtering, find the
4448+
// latest assignment.
4449+
for (const Instruction *Inst : DupSet) {
4450+
for (DPValue &DPV : llvm::reverse(Inst->getDbgValueRange())) {
4451+
DebugVariable DbgUserVariable =
4452+
DebugVariable(DPV.getVariable(), DPV.getExpression(),
4453+
DPV.getDebugLoc()->getInlinedAt());
4454+
auto FilterIt =
4455+
FilterOutMap.find(std::make_pair(Inst, DbgUserVariable));
4456+
if (FilterIt == FilterOutMap.end())
4457+
continue;
4458+
if (FilterIt->second != nullptr)
4459+
continue;
4460+
FilterIt->second = &DPV;
4461+
}
4462+
}
4463+
}
4464+
4465+
// Perform cloning of the DPValues that we plan on sinking, filter out any
4466+
// duplicate assignments identified above.
4467+
SmallVector<DPValue *, 2> DPVClones;
4468+
SmallSet<DebugVariable, 4> SunkVariables;
4469+
for (DPValue *DPV : DPValuesToSink) {
4470+
if (DPV->Type == DPValue::LocationType::Declare)
4471+
continue;
4472+
4473+
DebugVariable DbgUserVariable =
4474+
DebugVariable(DPV->getVariable(), DPV->getExpression(),
4475+
DPV->getDebugLoc()->getInlinedAt());
4476+
4477+
// For any variable where there were multiple assignments in the same place,
4478+
// ignore all but the last assignment.
4479+
if (!FilterOutMap.empty()) {
4480+
InstVarPair IVP = std::make_pair(DPV->getInstruction(), DbgUserVariable);
4481+
auto It = FilterOutMap.find(IVP);
4482+
4483+
// Filter out.
4484+
if (It != FilterOutMap.end() && It->second != DPV)
4485+
continue;
4486+
}
4487+
4488+
if (!SunkVariables.insert(DbgUserVariable).second)
4489+
continue;
4490+
4491+
if (DPV->isDbgAssign())
4492+
continue;
4493+
4494+
DPVClones.emplace_back(DPV->clone());
4495+
LLVM_DEBUG(dbgs() << "CLONE: " << *DPVClones.back() << '\n');
4496+
}
4497+
4498+
// Perform salvaging without the clones, then sink the clones.
4499+
if (DPVClones.empty())
4500+
return;
4501+
4502+
salvageDebugInfoForDbgValues(*I, {}, DPValuesToSalvage);
4503+
4504+
// The clones are in reverse order of original appearance. Assert that the
4505+
// head bit is set on the iterator as we _should_ have received it via
4506+
// getFirstInsertionPt. Inserting like this will reverse the clone order as
4507+
// we'll repeatedly insert at the head, such as:
4508+
// DPV-3 (third insertion goes here)
4509+
// DPV-2 (second insertion goes here)
4510+
// DPV-1 (first insertion goes here)
4511+
// Any-Prior-DPVs
4512+
// InsertPtInst
4513+
assert(InsertPos.getHeadBit());
4514+
for (DPValue *DPVClone : DPVClones) {
4515+
InsertPos->getParent()->insertDPValueBefore(DPVClone, InsertPos);
4516+
LLVM_DEBUG(dbgs() << "SINK: " << *DPVClone << '\n');
4517+
}
43784518
}
43794519

43804520
bool InstCombinerImpl::run() {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
; RUN: opt %s -o - -S --passes=instcombine | FileCheck %s
2+
; RUN: opt %s -o - -S --passes=instcombine --try-experimental-debuginfo-iterators | FileCheck %s
3+
;
4+
; CHECK-LABEL: for.body:
5+
; CHECK-NEXT: %sub.ptr.rhs.cast.i.i = ptrtoint ptr %call2.i.i to i64,
6+
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i64 %sub.ptr.rhs.cast.i.i, metadata !{{[0-9]*}}, metadata !DIExpression(DW_OP_LLVM_convert, 64, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 1, DW_OP_minus, DW_OP_stack_value)
7+
;
8+
;; The code below is representative of a common situation: where we've had a
9+
;; loop be completely optimised out, leaving dbg.values representing the
10+
;; assignments for it, including a rotated assignment to the loop counter.
11+
;; Below, it's the two dbg.values in the entry block, assigning first the
12+
;; value of %conv.i, then the value of %conv.i minus one.
13+
;;
14+
;; When instcombine sinks %conv.i, it's critical that if it sinks a dbg.value
15+
;; with it, it sinks the most recent assignment. Otherwise it will re-order the
16+
;; assignments below, and a fall counter assignment will continue on from the
17+
;; end of the optimised-out loop.
18+
;;
19+
;; The check lines test that when the trunc sinks (along with the ptrtoint),
20+
;; we get the dbg.value with a DW_OP_minus in it.
21+
22+
; ModuleID = 'tmp.ll'
23+
source_filename = "tmp.ll"
24+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
25+
target triple = "x86_64-unknown-linux-gnu"
26+
27+
declare void @llvm.dbg.value(metadata, metadata, metadata)
28+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
29+
30+
define void @_ZN4llvm16MCObjectStreamer15EmitInstructionERKNS_6MCInstE(i1 %tobool.not) local_unnamed_addr {
31+
entry:
32+
%call2.i.i = load volatile ptr, ptr null, align 8, !dbg !4
33+
%sub.ptr.rhs.cast.i.i = ptrtoint ptr %call2.i.i to i64, !dbg !4
34+
%conv.i = trunc i64 %sub.ptr.rhs.cast.i.i to i32, !dbg !4
35+
tail call void @llvm.dbg.value(metadata i32 %conv.i, metadata !16, metadata !DIExpression()), !dbg !18
36+
tail call void @llvm.dbg.value(metadata i32 %conv.i, metadata !16, metadata !DIExpression(DW_OP_constu, 1, DW_OP_minus, DW_OP_stack_value)), !dbg !18
37+
br i1 %tobool.not, label %common.ret, label %for.body
38+
39+
common.ret: ; preds = %for.body, %entry
40+
ret void
41+
42+
for.body: ; preds = %entry
43+
%call2 = call ptr @_ZNK4llvm6MCInst10getOperandEj(i32 %conv.i)
44+
br label %common.ret
45+
}
46+
47+
declare i32 @_ZNK4llvm6MCInst14getNumOperandsEv()
48+
49+
declare ptr @_ZNK4llvm6MCInst10getOperandEj(i32) local_unnamed_addr
50+
51+
declare i64 @_ZNK4llvm25SmallVectorTemplateCommonINS_9MCOperandEvE4sizeEv()
52+
53+
!llvm.dbg.cu = !{!0}
54+
!llvm.module.flags = !{!3}
55+
56+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2, splitDebugInlining: false, nameTableKind: None)
57+
!1 = !DIFile(filename: "foo.cpp", directory: ".")
58+
!2 = !{}
59+
!3 = !{i32 2, !"Debug Info Version", i32 3}
60+
!4 = !DILocation(line: 197, column: 26, scope: !5)
61+
!5 = distinct !DILexicalBlock(scope: !7, file: !6, line: 197, column: 3)
62+
!6 = !DIFile(filename: "foo.cpp", directory: ".")
63+
!7 = distinct !DISubprogram(name: "EmitInstruction", linkageName: "_ZN4llvm16MCObjectStreamer15EmitInstructionERKNS_6MCInstE", scope: !8, file: !6, line: 195, type: !13, scopeLine: 195, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: !15, retainedNodes: !2)
64+
!8 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "MCObjectStreamer", scope: !10, file: !9, line: 33, size: 2432, flags: DIFlagTypePassByReference | DIFlagNonTrivial, elements: !2, vtableHolder: !11)
65+
!9 = !DIFile(filename: "bar.h", directory: ".")
66+
!10 = !DINamespace(name: "llvm", scope: null)
67+
!11 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "MCStreamer", scope: !10, file: !12, line: 108, size: 2240, flags: DIFlagFwdDecl | DIFlagNonTrivial, identifier: "_ZTSN4llvm10MCStreamerE")
68+
!12 = !DIFile(filename: "baz.h", directory: ".")
69+
!13 = distinct !DISubroutineType(types: !14)
70+
!14 = !{null}
71+
!15 = !DISubprogram(name: "EmitInstruction", linkageName: "_ZN4llvm16MCObjectStreamer15EmitInstructionERKNS_6MCInstE", scope: !8, file: !9, line: 88, type: !13, scopeLine: 88, containingType: !8, virtualIndex: 86, flags: DIFlagPublic | DIFlagPrototyped, spFlags: DISPFlagVirtual | DISPFlagOptimized)
72+
!16 = !DILocalVariable(name: "i", scope: !5, file: !6, line: 197, type: !17)
73+
!17 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
74+
!18 = !DILocation(line: 0, scope: !5)

0 commit comments

Comments
 (0)