Skip to content

Commit 49b2b00

Browse files
MarijnS95claude
andcommitted
Fix missing inlinedAt on localized global debug info
Since microsoft#7799, PatchDebugInfo() creates a DILocalVariable and dbg.declare for every subprogram on the inlined-at chain of a localized global's users. The declare's location is built from just the global's line and that subprogram as scope, without the inlinedAt the subprogram was inlined at. For any subprogram other than the function itself, that location points at a different subprogram than the function holding the declare, which the LLVM verifier rejects: !dbg attachment points at wrong subprogram for function Later passes propagate the location from those declares onto the dbg.value and dx.op.annotateHandle instructions that replace them, so DXIL with -Zi carries these broken locations for every static global read in an inlined function. Drivers that verify the module, e.g. when linking ray tracing libraries into a state object, print the error for each one. Collect each subprogram together with the location it is inlined at, and give every declare that inlinedAt. A subprogram inlined at several call sites keeps a single variable with one declare per inlined instance, rather than a duplicate variable per call site. Fixes microsoft#8592 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 17d9afb commit 49b2b00

2 files changed

Lines changed: 58 additions & 11 deletions

File tree

‎lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6527,11 +6527,14 @@ struct GVDebugInfoPatchCache {
65276527
return Result;
65286528
}
65296529

6530-
// Collect DISubprograms from a DILocation's inlined-at chain.
6531-
void CollectSubprograms(DILocation *Loc, SetVector<DISubprogram *> &Set) {
6530+
// Collect DISubprograms from a DILocation's inlined-at chain, together with
6531+
// the location each one is inlined at (null for the function itself).
6532+
void
6533+
CollectSubprograms(DILocation *Loc,
6534+
SetVector<std::pair<DISubprogram *, DILocation *>> &Set) {
65326535
while (Loc) {
65336536
if (DISubprogram *SP = GetSubprogramForLoc(Loc))
6534-
Set.insert(SP);
6537+
Set.insert({SP, Loc->getInlinedAt()});
65356538
Loc = Loc->getInlinedAt();
65366539
}
65376540
}
@@ -6776,22 +6779,30 @@ static void PatchDebugInfo(GVDebugInfoPatchCache &Cache,
67766779

67776780
// Only collect subprograms relevant to this GV to avoid creating
67786781
// O(subprograms × globals) debug instructions.
6779-
SetVector<DISubprogram *> Subprograms;
6782+
SetVector<std::pair<DISubprogram *, DILocation *>> Subprograms;
67806783

67816784
if (DISubprogram *SP = Cache.GetFuncSubprogram(F, DbgFinder))
6782-
Subprograms.insert(SP);
6785+
Subprograms.insert({SP, nullptr});
67836786

67846787
for (User *U : AI->users()) {
67856788
if (Instruction *I = dyn_cast<Instruction>(U))
67866789
Cache.CollectSubprograms(I->getDebugLoc(), Subprograms);
67876790
}
67886791

6789-
for (DISubprogram *Subprogram : Subprograms) {
6790-
DIScope *Scope = Subprogram;
6791-
DebugLoc Loc = DebugLoc::get(DGV->getLine(), 0, Scope);
6792-
6793-
DILocalVariable *ConvertedLocalVar = DIB.createLocalVariable(
6794-
Tag, Scope, Name, DGV->getFile(), DGV->getLine(), Ty);
6792+
// A subprogram inlined at several call sites shares one variable, but each
6793+
// inlined instance needs its own declare: its location must carry the
6794+
// inlinedAt chain, or it points at a subprogram other than F.
6795+
DenseMap<DISubprogram *, DILocalVariable *> LocalVars;
6796+
for (auto &SubprogramAndInlinedAt : Subprograms) {
6797+
DIScope *Scope = SubprogramAndInlinedAt.first;
6798+
DebugLoc Loc =
6799+
DebugLoc::get(DGV->getLine(), 0, Scope, SubprogramAndInlinedAt.second);
6800+
6801+
DILocalVariable *&ConvertedLocalVar =
6802+
LocalVars[SubprogramAndInlinedAt.first];
6803+
if (!ConvertedLocalVar)
6804+
ConvertedLocalVar = DIB.createLocalVariable(
6805+
Tag, Scope, Name, DGV->getFile(), DGV->getLine(), Ty);
67956806

67966807
DIExpression *Expr = nullptr;
67976808
if (IsFragment)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %dxc -E main -T cs_6_6 -Zi %s | FileCheck %s
2+
3+
// Check that the debug info created for a localized global variable in an
4+
// inlined scope carries the inlinedAt chain back to the entry point, instead of
5+
// pointing at the inlined subprogram from inside main.
6+
7+
// CHECK: @main
8+
9+
// The sampler handle is annotated at the location of one of the declares of
10+
// the localized kSampler, all of which are inlined into main.
11+
// 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]+]]
12+
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+
15+
// CHECK-DAG: ![[main_scope:[0-9]+]] = !DISubprogram(name: "main"
16+
// CHECK-DAG: ![[helper_scope:[0-9]+]] = !DISubprogram(name: "helper"
17+
18+
// 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+
23+
SamplerState getSampler() { return SamplerDescriptorHeap[6]; }
24+
static const SamplerState kSampler = getSampler();
25+
static uint g_index;
26+
27+
float4 helper(float uv) {
28+
Texture2D<float4> tex = ResourceDescriptorHeap[g_index];
29+
return tex.SampleLevel(kSampler, uv, 0);
30+
}
31+
32+
[numthreads(1, 1, 1)] void main(uint id : SV_GroupIndex) {
33+
g_index = id;
34+
RWBuffer<float4> output = ResourceDescriptorHeap[2];
35+
output[0] = helper(0.5);
36+
}

0 commit comments

Comments
 (0)