Skip to content

Commit 2cf550a

Browse files
authored
[DebugInfo] Force early line-zero calls to have meaningful locations (#156850)
In functions that have been seriously deformed during optimisation, there can be call instructions with line-zero immediately after frame setup (see C reproducer in the test added). Our previous algorithms for prologue_end ignored these, meaning someone entering a function at prologue_end would break-in after a function call had completed. Prefer instead to place prologue_end and the function scope-line on the line zero call: this isn't false (it's the first meaningful instruction of the function) and is approximately true. Given a less than ideal function, this is an OK solution.
1 parent 74cebce commit 2cf550a

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,13 +2211,19 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
22112211
PrevInstLoc = DL;
22122212
}
22132213

2214+
// Returns the position where we should place prologue_end, potentially nullptr,
2215+
// which means "no good place to put prologue_end". Returns true in the second
2216+
// return value if there are no setup instructions in this function at all,
2217+
// meaning we should not emit a start-of-function linetable entry, because it
2218+
// would be zero-lengthed.
22142219
static std::pair<const MachineInstr *, bool>
22152220
findPrologueEndLoc(const MachineFunction *MF) {
22162221
// First known non-DBG_VALUE and non-frame setup location marks
22172222
// the beginning of the function body.
22182223
const auto &TII = *MF->getSubtarget().getInstrInfo();
22192224
const MachineInstr *NonTrivialInst = nullptr;
22202225
const Function &F = MF->getFunction();
2226+
DISubprogram *SP = const_cast<DISubprogram *>(F.getSubprogram());
22212227

22222228
// Some instructions may be inserted into prologue after this function. Must
22232229
// keep prologue for these cases.
@@ -2305,6 +2311,26 @@ findPrologueEndLoc(const MachineFunction *MF) {
23052311
return *FoundInst;
23062312
}
23072313

2314+
// In very rare scenarios function calls can have line zero, and we
2315+
// shouldn't step over such a call while trying to reach prologue_end. In
2316+
// these extraordinary conditions, force the call to have the scope line
2317+
// and put prologue_end there. This isn't ideal, but signals that the call
2318+
// is where execution in the function starts, and is less catastrophic than
2319+
// stepping over the call.
2320+
if (CurInst->isCall()) {
2321+
if (const DILocation *Loc = CurInst->getDebugLoc().get();
2322+
Loc && Loc->getLine() == 0) {
2323+
// Create and assign the scope-line position.
2324+
unsigned ScopeLine = SP->getScopeLine();
2325+
DILocation *ScopeLineDILoc =
2326+
DILocation::get(SP->getContext(), ScopeLine, 0, SP);
2327+
const_cast<MachineInstr *>(&*CurInst)->setDebugLoc(ScopeLineDILoc);
2328+
2329+
// Consider this position to be where prologue_end is placed.
2330+
return std::make_pair(&*CurInst, false);
2331+
}
2332+
}
2333+
23082334
// Try to continue searching, but use a backup-location if substantive
23092335
// computation is happening.
23102336
auto NextInst = std::next(CurInst);

llvm/test/DebugInfo/MIR/X86/debug-loc-0.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# CHECK: Ltmp0:
66
# CHECK: .loc 1 0 0
77
# CHECK-NOT: .loc 1 0 0
8-
# CHECK: .loc 1 37 1 prologue_end
8+
# CHECK: .loc 1 37 1
99

1010
--- |
1111
; ModuleID = '<stdin>'
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# RUN: llc %s -start-after=livedebugvalues -o - | FileCheck %s
2+
#
3+
## Original code, compiled clang -O2 -g -c
4+
##
5+
## void ext();
6+
## int main(int argc, char **argv) {
7+
## if (argc == 1)
8+
## ext();
9+
## else
10+
## ext();
11+
## return 0;
12+
## }
13+
##
14+
## In the code sequence above, the call to ext is given line zero during
15+
## optimisation, because the code is duplicated down all function paths thus
16+
## gets merged. We get something like this as the output:
17+
##
18+
## 0: 50 push %rax
19+
## 1: 31 c0 xor %eax,%eax
20+
## 3: e8 00 00 00 00 call 8 <main+0x8>
21+
## 4: R_X86_64_PLT32 ext-0x4
22+
## 8: 31 c0 xor %eax,%eax
23+
## a: 59 pop %rcx
24+
## b: c3 ret
25+
##
26+
## And we could choose to set prologue_end on address 8, the clearing of the
27+
## return register, because it's the first "real" instruction that isn't line
28+
## zero. But this then causes debuggers to skip over the call instruction when
29+
## entering the function, which is catastrophic.
30+
##
31+
## Instead: force the call itself to have a source location (the function scope
32+
## line number), and put a prologue_end there. While it's not the original
33+
## source of the call, it's better to have a prologue_end that means we'll stop
34+
## in the prologue than to step over the call. This gives consumers the
35+
## opportunity to recognise "this is a crazy function" and act accordingly.
36+
##
37+
## Check lines: ensure that we set prologue_end. The first entry is the
38+
## start-of-function scope line, the second entry is the prologue_end on the
39+
## call.
40+
#
41+
#
42+
# CHECK: main:
43+
# CHECK-NEXT: .Lfunc_begin0:
44+
# CHECK-NEXT: .file 0 "/tmp/test.c"
45+
# CHECK-NEXT: .loc 0 2 0
46+
# CHECK-NEXT: .cfi_startproc
47+
# CHECK-NEXT: # %bb.0:
48+
# CHECK-NEXT: pushq %rax
49+
# CHECK-NEXT: .cfi_def_cfa_offset 16
50+
# CHECK-NEXT: .Ltmp0:
51+
# CHECK-NEXT: .loc 0 0 0 is_stmt 0
52+
# CHECK-NEXT: xorl %eax, %eax
53+
# CHECK-NEXT: .loc 0 2 0 prologue_end is_stmt 1
54+
# CHECK-NEXT: callq ext@PLT
55+
56+
--- |
57+
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"
58+
target triple = "x86_64-unknown-linux-gnu"
59+
60+
; Function Attrs: nounwind uwtable
61+
define dso_local noundef i32 @main(i32 noundef %argc, ptr noundef readnone captures(none) %argv) local_unnamed_addr !dbg !10 {
62+
entry:
63+
tail call void (...) @ext(), !dbg !22
64+
ret i32 0, !dbg !24
65+
}
66+
67+
declare !dbg !25 void @ext(...) local_unnamed_addr
68+
69+
!llvm.dbg.cu = !{!0}
70+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
71+
!llvm.ident = !{!9}
72+
73+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
74+
!1 = !DIFile(filename: "/tmp/test.c", directory: "")
75+
!2 = !{i32 7, !"Dwarf Version", i32 5}
76+
!3 = !{i32 2, !"Debug Info Version", i32 3}
77+
!4 = !{i32 1, !"wchar_size", i32 4}
78+
!5 = !{i32 8, !"PIC Level", i32 2}
79+
!6 = !{i32 7, !"PIE Level", i32 2}
80+
!7 = !{i32 7, !"uwtable", i32 2}
81+
!8 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
82+
!9 = !{!"clang"}
83+
!10 = distinct !DISubprogram(name: "main", scope: !11, file: !11, line: 2, type: !12, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !18, keyInstructions: true)
84+
!11 = !DIFile(filename: "/tmp/test.c", directory: "")
85+
!12 = !DISubroutineType(types: !13)
86+
!13 = !{!14, !14, !15}
87+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
88+
!15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !16, size: 64)
89+
!16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64)
90+
!17 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
91+
!18 = !{!19, !20}
92+
!19 = !DILocalVariable(name: "argc", arg: 1, scope: !10, file: !11, line: 2, type: !14)
93+
!20 = !DILocalVariable(name: "argv", arg: 2, scope: !10, file: !11, line: 2, type: !15)
94+
!21 = !DILocation(line: 0, scope: !10)
95+
!22 = !DILocation(line: 0, scope: !23)
96+
!23 = distinct !DILexicalBlock(scope: !10, file: !11, line: 3, column: 7)
97+
!24 = !DILocation(line: 7, column: 4, scope: !10, atomGroup: 2, atomRank: 1)
98+
!25 = !DISubprogram(name: "ext", scope: !11, file: !11, line: 1, type: !26, spFlags: DISPFlagOptimized)
99+
!26 = !DISubroutineType(types: !27)
100+
!27 = !{null}
101+
...
102+
---
103+
name: main
104+
alignment: 16
105+
tracksRegLiveness: true
106+
noPhis: true
107+
isSSA: false
108+
noVRegs: true
109+
hasFakeUses: false
110+
debugInstrRef: true
111+
tracksDebugUserValues: true
112+
frameInfo:
113+
stackSize: 8
114+
offsetAdjustment: -8
115+
maxAlignment: 1
116+
adjustsStack: true
117+
hasCalls: true
118+
maxCallFrameSize: 0
119+
isCalleeSavedInfoValid: true
120+
machineFunctionInfo:
121+
amxProgModel: None
122+
body: |
123+
bb.0.entry:
124+
frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
125+
frame-setup CFI_INSTRUCTION def_cfa_offset 16
126+
dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !22
127+
CALL64pcrel32 target-flags(x86-plt) @ext, csr_64, implicit $rsp, implicit $ssp, implicit killed $al, implicit-def $rsp, implicit-def $ssp, debug-location !22
128+
$eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, debug-location !24
129+
$rcx = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !24
130+
frame-destroy CFI_INSTRUCTION def_cfa_offset 8, debug-location !24
131+
RET64 $eax, debug-location !24
132+
...

0 commit comments

Comments
 (0)