Skip to content

Commit 12033e5

Browse files
[ISelDAG] Salvage debug info at isel by referring to frame indices. (#109126)
We can refer to frame index locations when salvaging debug info for certain nodes, which prevents the compiler from optimizing out the location.
1 parent 66c8dce commit 12033e5

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11237,6 +11237,12 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1123711237
if (!N.getHasDebugValue())
1123811238
return;
1123911239

11240+
auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11241+
if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11242+
return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11243+
return SDDbgOperand::fromNode(Node, ResNo);
11244+
};
11245+
1124011246
SmallVector<SDDbgValue *, 2> ClonedDVs;
1124111247
for (auto *DV : GetDbgValues(&N)) {
1124211248
if (DV->isInvalidated())
@@ -11272,7 +11278,7 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1127211278
if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
1127311279
NewLocOps[i].getSDNode() != &N)
1127411280
continue;
11275-
NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11281+
NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
1127611282
if (RHSConstant) {
1127711283
SmallVector<uint64_t, 3> ExprOps;
1127811284
DIExpression::appendOffset(ExprOps, Offset);
@@ -11327,7 +11333,7 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1132711333
NewLocOps[i].getSDNode() != &N)
1132811334
continue;
1132911335

11330-
NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11336+
NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
1133111337
DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
1133211338
Changed = true;
1133311339
}
@@ -11350,7 +11356,11 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1135011356
}
1135111357

1135211358
for (SDDbgValue *Dbg : ClonedDVs) {
11353-
assert(!Dbg->getSDNodes().empty() &&
11359+
assert((!Dbg->getSDNodes().empty() ||
11360+
llvm::any_of(Dbg->getLocationOps(),
11361+
[&](const SDDbgOperand &Op) {
11362+
return Op.getKind() == SDDbgOperand::FRAMEIX;
11363+
})) &&
1135411364
"Salvaged DbgValue should depend on a new SDNode");
1135511365
AddDbgValue(Dbg, false);
1135611366
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; RUN: llc -march=sparc -O1 %s -o - -stop-after=finalize-isel | FileCheck %s
2+
3+
; Debug info salvaging in isel means we should see a location for this variable.
4+
5+
; CHECK-LABEL: name: a
6+
; CHECK: DBG_VALUE %stack.0.b, $noreg, ![[#]], !DIExpression(DW_OP_plus_uconst, 3, DW_OP_stack_value)
7+
8+
define dso_local zeroext i16 @a() local_unnamed_addr #0 !dbg !7 {
9+
entry:
10+
%b = alloca [6 x i8], align 1
11+
%arrayidx = getelementptr inbounds [6 x i8], ptr %b, i32 0, i32 undef, !dbg !27
12+
store i8 4, ptr %arrayidx, align 1, !dbg !28
13+
%arrayidx1 = getelementptr inbounds i8, ptr %b, i32 3, !dbg !32
14+
#dbg_value(ptr %arrayidx1, !22, !DIExpression(), !25)
15+
%0 = load i8, ptr %arrayidx1, align 1, !dbg !33
16+
%tobool.not = icmp eq i8 %0, 0, !dbg !35
17+
br i1 %tobool.not, label %if.end, label %for.cond, !dbg !36
18+
19+
for.cond: ; preds = %entry, %for.cond
20+
br label %for.cond, !dbg !37, !llvm.loop !40
21+
22+
if.end: ; preds = %entry
23+
ret i16 undef, !dbg !44
24+
}
25+
26+
!llvm.dbg.cu = !{!0}
27+
!llvm.module.flags = !{!2, !3, !4, !5}
28+
!llvm.ident = !{!6}
29+
30+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git.prerel", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
31+
!1 = !DIFile(filename: "file.c", directory: "/path", checksumkind: CSK_MD5, checksum: "aa7b5139660a2329a6409414c44cc1f6")
32+
!2 = !{i32 7, !"Dwarf Version", i32 5}
33+
!3 = !{i32 2, !"Debug Info Version", i32 3}
34+
!4 = !{i32 1, !"wchar_size", i32 4}
35+
!5 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
36+
!6 = !{!"clang version 20.0.0git.prerel"}
37+
!7 = distinct !DISubprogram(name: "a", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !13)
38+
!8 = !DISubroutineType(types: !9)
39+
!9 = !{!10}
40+
!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint16_t", file: !11, line: 277, baseType: !12)
41+
!11 = !DIFile(filename: "stdint.h", directory: "", checksumkind: CSK_MD5, checksum: "d9e8f73f3756bbd642f1729623e09484")
42+
!12 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
43+
!13 = !{!14, !20, !22}
44+
!14 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 3, type: !15)
45+
!15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !16, size: 48, elements: !18)
46+
!16 = !DIDerivedType(tag: DW_TAG_typedef, name: "int8_t", file: !11, line: 298, baseType: !17)
47+
!17 = !DIBasicType(name: "signed char", size: 8, encoding: DW_ATE_signed_char)
48+
!18 = !{!19}
49+
!19 = !DISubrange(count: 6)
50+
!20 = !DILocalVariable(name: "c", scope: !7, file: !1, line: 4, type: !21)
51+
!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
52+
!22 = !DILocalVariable(name: "d", scope: !7, file: !1, line: 6, type: !23)
53+
!23 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 32)
54+
!25 = !DILocation(line: 0, scope: !7)
55+
!27 = !DILocation(line: 5, column: 3, scope: !7)
56+
!28 = !DILocation(line: 5, column: 8, scope: !7)
57+
!32 = !DILocation(line: 6, column: 16, scope: !7)
58+
!33 = !DILocation(line: 7, column: 33, scope: !34)
59+
!34 = distinct !DILexicalBlock(scope: !7, file: !1, line: 7, column: 7)
60+
!35 = !DILocation(line: 7, column: 7, scope: !34)
61+
!36 = !DILocation(line: 7, column: 7, scope: !7)
62+
!37 = !DILocation(line: 8, column: 5, scope: !38)
63+
!38 = distinct !DILexicalBlock(scope: !39, file: !1, line: 8, column: 5)
64+
!39 = distinct !DILexicalBlock(scope: !34, file: !1, line: 8, column: 5)
65+
!40 = distinct !{!40, !41, !42, !43}
66+
!41 = !DILocation(line: 8, column: 5, scope: !39)
67+
!42 = !DILocation(line: 9, column: 7, scope: !39)
68+
!43 = !{!"llvm.loop.unroll.disable"}
69+
!44 = !DILocation(line: 10, column: 1, scope: !7)

llvm/test/CodeGen/X86/pr57673.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ define void @foo() {
3737
; NORMAL-NEXT: {{ $}}
3838
; NORMAL-NEXT: [[MOVUPSrm:%[0-9]+]]:vr128 = MOVUPSrm %stack.1.i, 1, $noreg, 40, $noreg :: (load (s128) from %ir.i4, align 8)
3939
; NORMAL-NEXT: MOVUPSmr $noreg, 1, $noreg, 0, $noreg, killed [[MOVUPSrm]] :: (store (s128) into `ptr null`, align 8)
40-
; NORMAL-NEXT: DBG_VALUE $noreg, $noreg, !3, !DIExpression(), debug-location !8
40+
; NORMAL-NEXT: DBG_VALUE_LIST !3, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_plus_uconst, 40, DW_OP_stack_value), %stack.1.i, %stack.1.i, debug-location !8
4141
; NORMAL-NEXT: [[MOVUPSrm1:%[0-9]+]]:vr128 = MOVUPSrm %stack.1.i, 1, $noreg, 40, $noreg :: (load (s128) from %ir.i6, align 8)
4242
; NORMAL-NEXT: MOVUPSmr $noreg, 1, $noreg, 0, $noreg, killed [[MOVUPSrm1]] :: (store (s128) into `ptr null`, align 8)
4343
; NORMAL-NEXT: {{ $}}
@@ -76,7 +76,7 @@ define void @foo() {
7676
; INSTRREF-NEXT: {{ $}}
7777
; INSTRREF-NEXT: [[MOVUPSrm:%[0-9]+]]:vr128 = MOVUPSrm %stack.1.i, 1, $noreg, 40, $noreg :: (load (s128) from %ir.i4, align 8)
7878
; INSTRREF-NEXT: MOVUPSmr $noreg, 1, $noreg, 0, $noreg, killed [[MOVUPSrm]] :: (store (s128) into `ptr null`, align 8)
79-
; INSTRREF-NEXT: DBG_VALUE $noreg, $noreg, !3, !DIExpression(), debug-location !8
79+
; INSTRREF-NEXT: DBG_VALUE_LIST !3, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_plus_uconst, 40, DW_OP_stack_value), %stack.1.i, %stack.1.i, debug-location !8
8080
; INSTRREF-NEXT: [[MOVUPSrm1:%[0-9]+]]:vr128 = MOVUPSrm %stack.1.i, 1, $noreg, 40, $noreg :: (load (s128) from %ir.i6, align 8)
8181
; INSTRREF-NEXT: MOVUPSmr $noreg, 1, $noreg, 0, $noreg, killed [[MOVUPSrm1]] :: (store (s128) into `ptr null`, align 8)
8282
; INSTRREF-NEXT: {{ $}}

0 commit comments

Comments
 (0)