Skip to content

Commit c476124

Browse files
committed
[DebugInfo] Avoid crash from dropped fragments in LiveDebugValues
This patch avoids a crash caused by DW_OP_LLVM_fragments being dropped from DIExpressions by LiveDebugValues spill-restore code. The appearance of a previously unseen fragment configuration confuses LDV, as documented in PR42773, and reproduced by the test function this patch adds (Crashes on a x86_64 debug build). To avoid this, on spill restore, we now use fragment information from the spilt-location-expression. In addition, when spilling, we now don't spill any DBG_VALUE with a complex expression, as it can't be safely restored and will definitely lead to an incorrect variable location. The discussion of this is in D65368. Differential Revision: https://reviews.llvm.org/D66284 llvm-svn: 369026
1 parent 626ed22 commit c476124

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed

llvm/lib/CodeGen/LiveDebugValues.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,17 @@ void LiveDebugValues::insertTransferDebugPair(
691691
"No register supplied when handling a restore of a debug value");
692692
MachineFunction *MF = MI.getMF();
693693
DIBuilder DIB(*const_cast<Function &>(MF->getFunction()).getParent());
694+
695+
const DIExpression *NewExpr;
696+
if (auto Fragment = DebugInstr->getDebugExpression()->getFragmentInfo())
697+
NewExpr = *DIExpression::createFragmentExpression(DIB.createExpression(),
698+
Fragment->OffsetInBits, Fragment->SizeInBits);
699+
else
700+
NewExpr = DIB.createExpression();
701+
694702
NewDebugInstr =
695703
BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), false,
696-
NewReg, DebugInstr->getDebugVariable(), DIB.createExpression());
704+
NewReg, DebugInstr->getDebugVariable(), NewExpr);
697705
VarLoc VL(*NewDebugInstr, LS);
698706
ProcessVarLoc(VL, NewDebugInstr);
699707
LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register restore: ";
@@ -848,9 +856,14 @@ void LiveDebugValues::transferSpillOrRestoreInst(MachineInstr &MI,
848856
<< "\n");
849857
}
850858
// Check if the register or spill location is the location of a debug value.
859+
// FIXME: Don't create a spill transfer if there is a complex expression,
860+
// because we currently cannot recover the original expression on restore.
851861
for (unsigned ID : OpenRanges.getVarLocs()) {
862+
const MachineInstr *DebugInstr = &VarLocIDs[ID].MI;
863+
852864
if (TKind == TransferKind::TransferSpill &&
853-
VarLocIDs[ID].isDescribedByReg() == Reg) {
865+
VarLocIDs[ID].isDescribedByReg() == Reg &&
866+
!DebugInstr->getDebugExpression()->isComplex()) {
854867
LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '('
855868
<< VarLocIDs[ID].Var.getVar()->getName() << ")\n");
856869
} else if (TKind == TransferKind::TransferRestore &&

llvm/test/DebugInfo/MIR/X86/live-debug-values-restore.mir

+113-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
# return *(p + 1);
1515
# }
1616

17+
# Pick out DILocalVariable numbers for "p" and "q"
18+
# CHECK: ![[PVAR:[0-9]+]] = !DILocalVariable(name: "p",
19+
# CHECK: ![[QVAR:[0-9]+]] = !DILocalVariable(name: "q",
20+
1721
# Ascertain that the spill has been recognized and manifested in a DBG_VALUE.
1822
# CHECK: MOV64mr $rsp,{{.*-8.*}}killed{{.*}}$rdi :: (store 8 into %stack.0)
19-
# CHECK-NEXT: DBG_VALUE $rsp,{{.*}}![[MDIX:[0-9]+]],{{.*}}!DIExpression(DW_OP_constu, 8, DW_OP_minus)
23+
# CHECK-NEXT: DBG_VALUE $rsp,{{.*}}![[PVAR]],{{.*}}!DIExpression(DW_OP_constu, 8, DW_OP_minus)
2024

2125
# Check for the restore.
2226
# CHECK: $rdi = MOV64rm $rsp,{{.*-8.*}}:: (load 8 from %stack.0)
23-
# CHECK-NEXT: DBG_VALUE $rdi,{{.*}}![[MDIX]], !DIExpression()
27+
# CHECK-NEXT: DBG_VALUE $rdi,{{.*}}![[PVAR]], !DIExpression()
2428

2529
--- |
2630
define dso_local i32 @f(i32* readonly %p) local_unnamed_addr !dbg !7 {
@@ -39,6 +43,22 @@
3943
ret i32 %0, !dbg !28
4044
}
4145

46+
define dso_local i32 @g(i32* readonly %p) local_unnamed_addr !dbg !107 {
47+
entry:
48+
call void @llvm.dbg.value(metadata i32* %p, metadata !113, metadata !DIExpression()), !dbg !114
49+
%tobool = icmp eq i32* %p, null, !dbg !115
50+
br i1 %tobool, label %if.end, label %if.then, !dbg !117
51+
52+
if.then: ; preds = %entry
53+
tail call void asm sideeffect "", "~{rax},~{rbx},~{rcx},~{rdx},~{rsi},~{rdi},~{rbp},~{r8},~{r9},~{r10},~{r11},~{r12},~{r13},~{r14},~{r15},~{dirflag},~{fpsr},~{flags}"(), !dbg !118, !srcloc !120
54+
br label %if.end, !dbg !121
55+
56+
if.end: ; preds = %entry, %if.then
57+
%add.ptr = getelementptr inbounds i32, i32* %p, i64 1, !dbg !122
58+
%0 = load i32, i32* %add.ptr, align 4, !dbg !123, !tbaa !24
59+
ret i32 %0, !dbg !128
60+
}
61+
4262
declare void @llvm.dbg.value(metadata, metadata, metadata)
4363

4464
!llvm.dbg.cu = !{!0}
@@ -74,6 +94,22 @@
7494
!26 = !{!"omnipotent char", !27, i64 0}
7595
!27 = !{!"Simple C/C++ TBAA"}
7696
!28 = !DILocation(line: 9, column: 3, scope: !7)
97+
!101 = !DIBasicType(name: "looong int", size: 64, encoding: DW_ATE_signed)
98+
!107 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 105, type: !8, scopeLine: 105, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !112)
99+
!112 = !{!113}
100+
!113 = !DILocalVariable(name: "q", arg: 1, scope: !107, file: !1, line: 105, type: !101)
101+
!114 = !DILocation(line: 105, column: 12, scope: !107)
102+
!115 = !DILocation(line: 106, column: 7, scope: !116)
103+
!116 = distinct !DILexicalBlock(scope: !107, file: !1, line: 106, column: 7)
104+
!117 = !DILocation(line: 106, column: 7, scope: !107)
105+
!118 = !DILocation(line: 107, column: 5, scope: !119)
106+
!119 = distinct !DILexicalBlock(scope: !116, file: !1, line: 106, column: 10)
107+
!120 = !{i32 -2147471544}
108+
!121 = !DILocation(line: 108, column: 3, scope: !119)
109+
!122 = !DILocation(line: 109, column: 14, scope: !107)
110+
!123 = !DILocation(line: 109, column: 10, scope: !107)
111+
!128 = !DILocation(line: 109, column: 3, scope: !107)
112+
77113

78114
...
79115
---
@@ -187,3 +223,78 @@ body: |
187223
RETQ $eax, debug-location !28
188224
189225
...
226+
---
227+
# This second function has been appended as a regression test against a
228+
# crash, caused by expressions being created from spill restores that did
229+
# not preserve fragment information. Test that no empty expressions are
230+
# created at all, and the last block describes both variable fragments.
231+
232+
# CHECK-LABEL: name: g
233+
# CHECK-NOT: !DIExpression()
234+
# CHECK-LABEL: bb.2.if.end:
235+
# CHECK: DBG_VALUE $rdi, $noreg, ![[QVAR]], !DIExpression(DW_OP_LLVM_fragment, 0, 32)
236+
# CHECK-NEXT: DBG_VALUE $rbx, $noreg, ![[QVAR]], !DIExpression(DW_OP_LLVM_fragment, 32, 32)
237+
238+
name: g
239+
alignment: 4
240+
tracksRegLiveness: true
241+
liveins:
242+
- { reg: '$rdi', virtual-reg: '' }
243+
frameInfo:
244+
stackSize: 48
245+
offsetAdjustment: -48
246+
maxAlignment: 8
247+
cvBytesOfCalleeSavedRegisters: 48
248+
localFrameSize: 0
249+
fixedStack:
250+
- { id: 0, type: spill-slot, offset: -56, size: 8, alignment: 8, stack-id: default,
251+
callee-saved-register: '$rbx', callee-saved-restored: true, debug-info-variable: '',
252+
debug-info-expression: '', debug-info-location: '' }
253+
- { id: 1, type: spill-slot, offset: -48, size: 8, alignment: 16, stack-id: default,
254+
callee-saved-register: '$r12', callee-saved-restored: true, debug-info-variable: '',
255+
debug-info-expression: '', debug-info-location: '' }
256+
- { id: 2, type: spill-slot, offset: -40, size: 8, alignment: 8, stack-id: default,
257+
callee-saved-register: '$r13', callee-saved-restored: true, debug-info-variable: '',
258+
debug-info-expression: '', debug-info-location: '' }
259+
- { id: 3, type: spill-slot, offset: -32, size: 8, alignment: 16, stack-id: default,
260+
callee-saved-register: '$r14', callee-saved-restored: true, debug-info-variable: '',
261+
debug-info-expression: '', debug-info-location: '' }
262+
- { id: 4, type: spill-slot, offset: -24, size: 8, alignment: 8, stack-id: default,
263+
callee-saved-register: '$r15', callee-saved-restored: true, debug-info-variable: '',
264+
debug-info-expression: '', debug-info-location: '' }
265+
- { id: 5, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: default,
266+
callee-saved-register: '$rbp', callee-saved-restored: true, debug-info-variable: '',
267+
debug-info-expression: '', debug-info-location: '' }
268+
stack:
269+
- { id: 0, name: '', type: spill-slot, offset: -64, size: 8, alignment: 8,
270+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
271+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
272+
constants: []
273+
body: |
274+
bb.0.entry:
275+
successors: %bb.1(0x50000000)
276+
liveins: $rdi, $rbx, $r12, $r13, $r14, $r15, $rbp
277+
278+
DBG_VALUE $rdi, $noreg, !113, !DIExpression(DW_OP_LLVM_fragment, 0, 32), debug-location !114
279+
TEST64rr renamable $rdi, renamable $rdi, implicit-def $eflags, debug-location !115
280+
JMP_1 %bb.1, implicit $eflags, debug-location !117
281+
282+
bb.1.if.then:
283+
successors: %bb.2(0x80000000)
284+
liveins: $rdi, $rbp, $r15, $r14, $r13, $r12, $rbx
285+
286+
MOV64mr $rsp, 1, $noreg, -8, $noreg, killed renamable $rdi :: (store 8 into %stack.0)
287+
renamable $rdi = MOV64rm $rsp, 1, $noreg, -8, $noreg :: (load 8 from %stack.0)
288+
289+
bb.2.if.end:
290+
liveins: $rdi, $rbx, $r12, $r13, $r14, $r15, $rbp
291+
292+
DBG_VALUE $rbx, $noreg, !113, !DIExpression(DW_OP_LLVM_fragment, 32, 32), debug-location !114
293+
MOV64mr $rsp, 1, $noreg, -8, $noreg, killed renamable $rbx :: (store 8 into %stack.0)
294+
renamable $rsi = MOV64rm $rsp, 1, $noreg, -8, $noreg :: (load 8 from %stack.0)
295+
296+
renamable $eax = MOV32rm killed renamable $rsi, 1, $noreg, 4, $noreg, debug-location !123 :: (load 4 from %ir.add.ptr, !tbaa !24)
297+
$rdi = MOV64ri 0
298+
RETQ $eax, debug-location !128
299+
300+
...

0 commit comments

Comments
 (0)