Skip to content

Commit 51ea990

Browse files
MarijnS95claude
andcommitted
Keep debug values for every inlined instance of a variable
The declares PatchDebugInfo() creates for a subprogram inlined at several call sites share one variable and only differ in their inlinedAt. Two passes deduplicating debug values ignored that, and dropped all but one inlined instance's value: - LdStHasDebugValue() skips inserting a dbg.value for a store or load when the instruction right before it is already a dbg.value of the same variable, which is exactly where the previous declare put its. - DxilDeleteRedundantDebugValues erases a dbg.value when another one for the same variable and expression follows it. Include the inlinedAt in both comparisons, and add a second call site to the test, which fails without either change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 49b2b00 commit 51ea990

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

‎lib/HLSL/DxilDeleteRedundantDebugValues.cpp‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ bool DxilDeleteRedundantDebugValues::runOnModule(Module &M) {
5656
bool Changed = false;
5757

5858
std::unordered_set<DILocalScope *> SeenScopes;
59-
typedef std::pair<DILocalVariable *, DIExpression *> VarPair;
60-
SmallDenseMap<VarPair, DbgValueInst *> SeenVar;
59+
// A variable is identified by its inlinedAt as well: each inlined instance
60+
// of a scope has its own values for the variables in it.
61+
typedef std::pair<std::pair<DILocalVariable *, DIExpression *>, DILocation *>
62+
VarKey;
63+
SmallDenseMap<VarKey, DbgValueInst *> SeenVar;
6164

6265
for (Function &F : M) {
6366
SeenScopes.clear();
@@ -102,21 +105,21 @@ bool DxilDeleteRedundantDebugValues::runOnModule(Module &M) {
102105
continue;
103106
DILocalVariable *Var = DI->getVariable();
104107
DIExpression *Expr = DI->getExpression();
105-
VarPair Pair = VarPair(Var, Expr);
108+
VarKey Key = VarKey({Var, Expr}, DI->getDebugLoc().getInlinedAt());
106109

107110
if (!SeenScopes.count(Var->getScope())) {
108111
Changed = true;
109112
DI->eraseFromParent();
110113
continue;
111114
}
112115

113-
auto findIt = SeenVar.find(Pair);
116+
auto findIt = SeenVar.find(Key);
114117
if (findIt != SeenVar.end()) {
115118
findIt->second->eraseFromParent();
116119
findIt->second = DI;
117120
Changed = true;
118121
} else {
119-
SeenVar[Pair] = DI;
122+
SeenVar[Key] = DI;
120123
}
121124
}
122125
}

‎lib/Transforms/Utils/Local.cpp‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -972,18 +972,21 @@ unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
972972
/// Dbg Intrinsic utilities
973973
///
974974

975-
/// See if there is a dbg.value intrinsic for DIVar before I.
976-
static bool LdStHasDebugValue(const DILocalVariable *DIVar, Instruction *I) {
975+
/// See if there is a dbg.value intrinsic for DDI's variable before I.
976+
static bool LdStHasDebugValue(const DbgDeclareInst *DDI, Instruction *I) {
977977
// Since we can't guarantee that the original dbg.declare instrinsic
978978
// is removed by LowerDbgDeclare(), we need to make sure that we are
979979
// not inserting the same dbg.value intrinsic over and over.
980980
llvm::BasicBlock::InstListType::iterator PrevI(I);
981981
if (PrevI != I->getParent()->getInstList().begin()) {
982982
--PrevI;
983983
if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI))
984-
if (DVI->getValue() == I->getOperand(0) &&
985-
DVI->getOffset() == 0 &&
986-
DVI->getVariable() == DIVar)
984+
if (DVI->getValue() == I->getOperand(0) && DVI->getOffset() == 0 &&
985+
DVI->getVariable() == DDI->getVariable() &&
986+
// HLSL Change - Declares of one variable in different inlined
987+
// instances only differ in their inlinedAt.
988+
DVI->getDebugLoc().getInlinedAt() ==
989+
DDI->getDebugLoc().getInlinedAt())
987990
return true;
988991
}
989992
return false;
@@ -997,7 +1000,7 @@ bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
9971000
auto *DIExpr = DDI->getExpression();
9981001
assert(DIVar && "Missing variable");
9991002

1000-
if (LdStHasDebugValue(DIVar, SI))
1003+
if (LdStHasDebugValue(DDI, SI))
10011004
return true;
10021005

10031006
// If an argument is zero extended then use argument directly. The ZExt
@@ -1024,7 +1027,7 @@ bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
10241027
auto *DIExpr = DDI->getExpression();
10251028
assert(DIVar && "Missing variable");
10261029

1027-
if (LdStHasDebugValue(DIVar, LI))
1030+
if (LdStHasDebugValue(DDI, LI))
10281031
return true;
10291032

10301033
Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, DIVar, DIExpr,

‎tools/clang/test/HLSLFileCheck/dxil/debug/local_global_inlined_at.hlsl‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
// Check that the debug info created for a localized global variable in an
44
// inlined scope carries the inlinedAt chain back to the entry point, instead of
5-
// pointing at the inlined subprogram from inside main.
5+
// pointing at the inlined subprogram from inside main, and that every inlined
6+
// instance keeps its own value.
67

78
// CHECK: @main
89

910
// The sampler handle is annotated at the location of one of the declares of
1011
// the localized kSampler, all of which are inlined into main.
1112
// CHECK: call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle %{{.+}}, %dx.types.ResourceProperties { i32 14, i32 0 }), !dbg ![[annot_loc:[0-9]+]]
1213

13-
// CHECK: call void @llvm.dbg.value(metadata i32 %{{.+}}, i64 0, metadata ![[helper_var:[0-9]+]], metadata !{{[0-9]+}}), !dbg ![[helper_loc:[0-9]+]] ; var:"global.g_index" !DIExpression() func:"helper"
14+
// Both inlined instances of helper share one variable, but each gets a value.
15+
// CHECK: call void @llvm.dbg.value(metadata i32 %{{.+}}, i64 0, metadata ![[helper_var:[0-9]+]], metadata !{{[0-9]+}}), !dbg ![[helper_loc0:[0-9]+]] ; var:"global.g_index" !DIExpression() func:"helper"
16+
// CHECK: call void @llvm.dbg.value(metadata i32 %{{.+}}, i64 0, metadata ![[helper_var]], metadata !{{[0-9]+}}), !dbg ![[helper_loc1:[0-9]+]] ; var:"global.g_index" !DIExpression() func:"helper"
1417

1518
// CHECK-DAG: ![[main_scope:[0-9]+]] = !DISubprogram(name: "main"
1619
// CHECK-DAG: ![[helper_scope:[0-9]+]] = !DISubprogram(name: "helper"
1720

1821
// CHECK-DAG: ![[helper_var]] = !DILocalVariable(tag: DW_TAG_arg_variable, name: "global.g_index", arg: 0, scope: ![[helper_scope]],
19-
// CHECK-DAG: ![[helper_loc]] = !DILocation(line: [[@LINE+6]], scope: ![[helper_scope]], inlinedAt: ![[call_loc:[0-9]+]])
20-
// CHECK-DAG: ![[annot_loc]] = !DILocation(line: [[@LINE+4]], scope: !{{[0-9]+}}, inlinedAt: !{{[0-9]+}})
21-
// CHECK-DAG: ![[call_loc]] = distinct !DILocation(line: {{[0-9]+}}, column: {{[0-9]+}}, scope: ![[main_scope]])
22+
// CHECK-DAG: ![[helper_loc0]] = !DILocation(line: [[@LINE+8]], scope: ![[helper_scope]], inlinedAt: ![[call_loc0:[0-9]+]])
23+
// CHECK-DAG: ![[helper_loc1]] = !DILocation(line: [[@LINE+7]], scope: ![[helper_scope]], inlinedAt: ![[call_loc1:[0-9]+]])
24+
// CHECK-DAG: ![[annot_loc]] = !DILocation(line: [[@LINE+5]], scope: !{{[0-9]+}}, inlinedAt: !{{[0-9]+}})
25+
// CHECK-DAG: ![[call_loc0]] = distinct !DILocation(line: [[@LINE+15]], column: {{[0-9]+}}, scope: ![[main_scope]])
26+
// CHECK-DAG: ![[call_loc1]] = distinct !DILocation(line: [[@LINE+14]], column: {{[0-9]+}}, scope: ![[main_scope]])
2227

2328
SamplerState getSampler() { return SamplerDescriptorHeap[6]; }
2429
static const SamplerState kSampler = getSampler();
@@ -32,5 +37,5 @@ float4 helper(float uv) {
3237
[numthreads(1, 1, 1)] void main(uint id : SV_GroupIndex) {
3338
g_index = id;
3439
RWBuffer<float4> output = ResourceDescriptorHeap[2];
35-
output[0] = helper(0.5);
40+
output[0] = helper(0.5) + helper(0.25);
3641
}

0 commit comments

Comments
 (0)