Skip to content

Commit df70628

Browse files
committed
DebugInfo: Use distinct inlinedAt MDLocations to avoid separate inlined calls being coalesced
When two calls from the same MDLocation are inlined they currently get treated as one inlined function call (creating difficulty debugging, duplicate variables, etc). Clang worked around this by including column information on inline calls which doesn't address LTO inlining or calls to the same function from the same line and column (such as through a macro). It also didn't address ctor and member function calls. By making the inlinedAt locations distinct, every call site has an explicitly distinct location that cannot be coalesced with any other call. This can produce linearly (2x in the worst case where every call is inlined and the call instruction has a non-call instruction at the same location) more debug locations. Any increase beyond that are in cases where the Clang workaround was insufficient and the new scheme is creating necessary distinct nodes that were being erroneously coalesced previously. After this change to LLVM the incomplete workarounds in Clang. That should reduce the number of debug locations (in a build without column info, the default on Darwin, not the default on Linux) by not creating pseudo-distinct locations for every call to an inline function. (oh, and I made the inlined-at chain rebuilding iterative instead of recursive because I was having trouble wrapping my head around it the way it was - open to discussion on the right design for that function (including going back to a recursive solution)) llvm-svn: 226736
1 parent 07c6bfd commit df70628

File tree

7 files changed

+181
-22
lines changed

7 files changed

+181
-22
lines changed

llvm/lib/Transforms/Utils/InlineFunction.cpp

+49-13
Original file line numberDiff line numberDiff line change
@@ -823,20 +823,42 @@ static bool hasLifetimeMarkers(AllocaInst *AI) {
823823
return false;
824824
}
825825

826-
/// updateInlinedAtInfo - Helper function used by fixupLineNumbers to
827-
/// recursively update InlinedAtEntry of a DebugLoc.
828-
static DebugLoc updateInlinedAtInfo(const DebugLoc &DL,
829-
const DebugLoc &InlinedAtDL,
830-
LLVMContext &Ctx) {
831-
if (MDNode *IA = DL.getInlinedAt(Ctx)) {
832-
DebugLoc NewInlinedAtDL
833-
= updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx);
834-
return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
835-
NewInlinedAtDL.getAsMDNode(Ctx));
826+
/// Rebuild the entire inlined-at chain for this instruction so that the top of
827+
/// the chain now is inlined-at the new call site.
828+
static DebugLoc
829+
updateInlinedAtInfo(DebugLoc DL, MDLocation *InlinedAtNode,
830+
LLVMContext &Ctx,
831+
DenseMap<const MDLocation *, MDLocation *> &IANodes) {
832+
SmallVector<MDLocation*, 3> InlinedAtLocations;
833+
MDLocation *Last = InlinedAtNode;
834+
DebugLoc CurInlinedAt = DL;
835+
836+
// Gather all the inlined-at nodes
837+
while (MDLocation *IA =
838+
cast_or_null<MDLocation>(CurInlinedAt.getInlinedAt(Ctx))) {
839+
// Skip any we've already built nodes for
840+
if (MDLocation *Found = IANodes[IA]) {
841+
Last = Found;
842+
break;
843+
}
844+
845+
InlinedAtLocations.push_back(IA);
846+
CurInlinedAt = DebugLoc::getFromDILocation(IA);
836847
}
837848

838-
return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
839-
InlinedAtDL.getAsMDNode(Ctx));
849+
// Starting from the top, rebuild the nodes to point to the new inlined-at
850+
// location (then rebuilding the rest of the chain behind it) and update the
851+
// map of already-constructed inlined-at nodes.
852+
for (auto I = InlinedAtLocations.rbegin(), E = InlinedAtLocations.rend();
853+
I != E; ++I) {
854+
const MDLocation *MD = *I;
855+
Last = IANodes[MD] = MDLocation::getDistinct(
856+
Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
857+
}
858+
859+
// And finally create the normal location for this instruction, referring to
860+
// the new inlined-at chain.
861+
return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx), Last);
840862
}
841863

842864
/// fixupLineNumbers - Update inlined instructions' line numbers to
@@ -847,6 +869,20 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
847869
if (TheCallDL.isUnknown())
848870
return;
849871

872+
auto &Ctx = Fn->getContext();
873+
auto *InlinedAtNode = cast<MDLocation>(TheCallDL.getAsMDNode(Ctx));
874+
875+
// Create a unique call site, not to be confused with any other call from the
876+
// same location.
877+
InlinedAtNode = MDLocation::getDistinct(
878+
Ctx, InlinedAtNode->getLine(), InlinedAtNode->getColumn(),
879+
InlinedAtNode->getScope(), InlinedAtNode->getInlinedAt());
880+
881+
// Cache the inlined-at nodes as they're built so they are reused, without
882+
// this every instruction's inlined-at chain would become distinct from each
883+
// other.
884+
DenseMap<const MDLocation *, MDLocation *> IANodes;
885+
850886
for (; FI != Fn->end(); ++FI) {
851887
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
852888
BI != BE; ++BI) {
@@ -864,7 +900,7 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
864900

865901
BI->setDebugLoc(TheCallDL);
866902
} else {
867-
BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext()));
903+
BI->setDebugLoc(updateInlinedAtInfo(DL, InlinedAtNode, BI->getContext(), IANodes));
868904
if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
869905
LLVMContext &Ctx = BI->getContext();
870906
MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx);

llvm/test/DebugInfo/inline-debug-info-multiret.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
; CHECK: br label %invoke.cont, !dbg ![[MD]]
1111
; The branch instruction has the source location of line 9 and its inlined location
1212
; has the source location of line 14.
13-
; CHECK: ![[INL:[0-9]+]] = !MDLocation(line: 14, scope: {{.*}})
13+
; CHECK: ![[INL:[0-9]+]] = distinct !MDLocation(line: 14, scope: {{.*}})
1414
; CHECK: ![[MD]] = !MDLocation(line: 9, scope: {{.*}}, inlinedAt: ![[INL]])
1515

1616
; ModuleID = 'test.cpp'

llvm/test/DebugInfo/inline-debug-info.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
; CHECK: _Z4testi.exit:
2929
; Make sure the branch instruction created during inlining has a debug location,
3030
; so the range of the inlined function is correct.
31-
; CHECK: br label %invoke.cont, !dbg ![[MD:[0-9]+]]
31+
; CHECK: br label %invoke.cont, !dbg [[MD:![0-9]+]]
3232
; The branch instruction has the source location of line 9 and its inlined location
3333
; has the source location of line 14.
34-
; CHECK: ![[INL:[0-9]+]] = !MDLocation(line: 14, scope: {{.*}})
35-
; CHECK: ![[MD]] = !MDLocation(line: 9, scope: {{.*}}, inlinedAt: ![[INL]])
34+
; CHECK: [[INL:![0-9]*]] = distinct !MDLocation(line: 14, scope: {{.*}})
35+
; CHECK: [[MD]] = !MDLocation(line: 9, scope: {{.*}}, inlinedAt: [[INL]])
3636

3737
; ModuleID = 'test.cpp'
3838
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"

llvm/test/DebugInfo/inline-no-debug-info.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
; Debug location of the code in caller() and of the inlined code that did not
2323
; have any debug location before.
24-
; CHECK-DAG: [[A]] = !MDLocation(line: 4, scope: !{{[01-9]+}})
24+
; CHECK-DAG: [[A]] = !MDLocation(line: 4, scope: !{{[0-9]+}})
2525

2626
; Debug location of the inlined code.
27-
; CHECK-DAG: [[B]] = !MDLocation(line: 2, scope: !{{[01-9]+}}, inlinedAt: [[A]])
27+
; CHECK-DAG: [[B]] = !MDLocation(line: 2, scope: !{{[0-9]+}}, inlinedAt: [[A_INL:![0-9]*]])
28+
; CHECK-DAG: [[A_INL]] = distinct !MDLocation(line: 4, scope: !{{[0-9]+}})
2829

2930

3031
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
; RUN: opt < %s -always-inline -S | FileCheck %s
2+
3+
; Original input generated from clang -emit-llvm -S -c -mllvm -disable-llvm-optzns
4+
;
5+
; #define CALLS1 f2(); f2();
6+
; #define CALLS2 f4(); f4();
7+
; void f1();
8+
; inline __attribute__((always_inline)) void f2() {
9+
; f1();
10+
; }
11+
; inline __attribute__((always_inline)) void f3() {
12+
; CALLS1
13+
; }
14+
; inline __attribute__((always_inline)) void f4() {
15+
; f3();
16+
; }
17+
; void f() {
18+
; CALLS2
19+
; }
20+
21+
; There should be unique locations for all 4 of these instructions, correctly
22+
; describing the inlining that has occurred, even in the face of duplicate call
23+
; site locations.
24+
25+
; The nomenclature used for the tags here is <function name>[cs<number>] where
26+
; 'cs' is an abbreviation for 'call site' and the number indicates which call
27+
; site from within the named function this is. (so, given the above inlining, we
28+
; should have 4 calls to 'f1', two from the first call to f4 and two from the
29+
; second call to f4)
30+
31+
; CHECK: call void @_Z2f1v(), !dbg [[fcs1_f4_f3cs1_f2:![0-9]+]]
32+
; CHECK: call void @_Z2f1v(), !dbg [[fcs1_f4_f3cs2_f2:![0-9]+]]
33+
; CHECK: call void @_Z2f1v(), !dbg [[fcs2_f4_f3cs1_f2:![0-9]+]]
34+
; CHECK: call void @_Z2f1v(), !dbg [[fcs2_f4_f3cs2_f2:![0-9]+]]
35+
36+
; CHECK-DAG: [[F:![0-9]+]] = {{.*}} ; [ DW_TAG_subprogram ] {{.*}} [f]
37+
; CHECK-DAG: [[F2:![0-9]+]] = {{.*}} ; [ DW_TAG_subprogram ] {{.*}} [f2]
38+
; CHECK-DAG: [[F3:![0-9]+]] = {{.*}} ; [ DW_TAG_subprogram ] {{.*}} [f3]
39+
; CHECK-DAG: [[F4:![0-9]+]] = {{.*}} ; [ DW_TAG_subprogram ] {{.*}} [f4]
40+
41+
; CHECK: [[fcs1_f4_f3cs1_f2]] = {{.*}}, scope: [[F2]], inlinedAt: [[fcs1_f4_f3cs1:![0-9]+]])
42+
; CHECK: [[fcs1_f4_f3cs1]] = {{.*}}, scope: [[F3]], inlinedAt: [[fcs1_f4:![0-9]+]])
43+
; CHECK: [[fcs1_f4]] = {{.*}}, scope: [[F4]], inlinedAt: [[fcs1:![0-9]+]])
44+
; CHECK: [[fcs1]] = {{.*}}, scope: [[F]])
45+
; CHECK: [[fcs1_f4_f3cs2_f2]] = {{.*}}, scope: [[F2]], inlinedAt: [[fcs1_f4_f3cs2:![0-9]+]])
46+
; CHECK: [[fcs1_f4_f3cs2]] = {{.*}}, scope: [[F3]], inlinedAt: [[fcs1_f4]])
47+
48+
; CHECK: [[fcs2_f4_f3cs1_f2]] = {{.*}}, scope: [[F2]], inlinedAt: [[fcs2_f4_f3cs1:![0-9]+]])
49+
; CHECK: [[fcs2_f4_f3cs1]] = {{.*}}, scope: [[F3]], inlinedAt: [[fcs2_f4:![0-9]+]])
50+
; CHECK: [[fcs2_f4]] = {{.*}}, scope: [[F4]], inlinedAt: [[fcs2:![0-9]+]])
51+
; CHECK: [[fcs2]] = {{.*}}, scope: [[F]])
52+
; CHECK: [[fcs2_f4_f3cs2_f2]] = {{.*}}, scope: [[F2]], inlinedAt: [[fcs2_f4_f3cs2:![0-9]+]])
53+
; CHECK: [[fcs2_f4_f3cs2]] = {{.*}}, scope: [[F3]], inlinedAt: [[fcs2_f4]])
54+
55+
$_Z2f4v = comdat any
56+
57+
$_Z2f3v = comdat any
58+
59+
$_Z2f2v = comdat any
60+
61+
; Function Attrs: uwtable
62+
define void @_Z1fv() #0 {
63+
entry:
64+
call void @_Z2f4v(), !dbg !13
65+
call void @_Z2f4v(), !dbg !13
66+
ret void, !dbg !14
67+
}
68+
69+
; Function Attrs: alwaysinline inlinehint uwtable
70+
define linkonce_odr void @_Z2f4v() #1 comdat {
71+
entry:
72+
call void @_Z2f3v(), !dbg !15
73+
ret void, !dbg !16
74+
}
75+
76+
; Function Attrs: alwaysinline inlinehint uwtable
77+
define linkonce_odr void @_Z2f3v() #1 comdat {
78+
entry:
79+
call void @_Z2f2v(), !dbg !17
80+
call void @_Z2f2v(), !dbg !17
81+
ret void, !dbg !18
82+
}
83+
84+
; Function Attrs: alwaysinline inlinehint uwtable
85+
define linkonce_odr void @_Z2f2v() #1 comdat {
86+
entry:
87+
call void @_Z2f1v(), !dbg !19
88+
ret void, !dbg !20
89+
}
90+
91+
declare void @_Z2f1v() #2
92+
93+
attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
94+
attributes #1 = { alwaysinline inlinehint uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
95+
attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
96+
97+
!llvm.dbg.cu = !{!0}
98+
!llvm.module.flags = !{!10, !11}
99+
!llvm.ident = !{!12}
100+
101+
!0 = !{!"0x11\004\00clang version 3.7.0 (trunk 226474) (llvm/trunk 226478)\000\00\000\00\002", !1, !2, !2, !3, !2, !2} ; [ DW_TAG_compile_unit ] [/tmp/dbginfo/debug-info-duplicate-calls.cpp] [DW_LANG_C_plus_plus]
102+
!1 = !{!"debug-info-duplicate-calls.cpp", !"/tmp/dbginfo"}
103+
!2 = !{}
104+
!3 = !{!4, !7, !8, !9}
105+
!4 = !{!"0x2e\00f\00f\00\0013\000\001\000\000\00256\000\0013", !1, !5, !6, null, void ()* @_Z1fv, null, null, !2} ; [ DW_TAG_subprogram ] [line 13] [def] [f]
106+
!5 = !{!"0x29", !1} ; [ DW_TAG_file_type ] [/tmp/dbginfo/debug-info-duplicate-calls.cpp]
107+
!6 = !{!"0x15\00\000\000\000\000\000\000", null, null, null, !2, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
108+
!7 = !{!"0x2e\00f4\00f4\00\0010\000\001\000\000\00256\000\0010", !1, !5, !6, null, void ()* @_Z2f4v, null, null, !2} ; [ DW_TAG_subprogram ] [line 10] [def] [f4]
109+
!8 = !{!"0x2e\00f3\00f3\00\007\000\001\000\000\00256\000\007", !1, !5, !6, null, void ()* @_Z2f3v, null, null, !2} ; [ DW_TAG_subprogram ] [line 7] [def] [f3]
110+
!9 = !{!"0x2e\00f2\00f2\00\004\000\001\000\000\00256\000\004", !1, !5, !6, null, void ()* @_Z2f2v, null, null, !2} ; [ DW_TAG_subprogram ] [line 4] [def] [f2]
111+
!10 = !{i32 2, !"Dwarf Version", i32 4}
112+
!11 = !{i32 2, !"Debug Info Version", i32 2}
113+
!12 = !{!"clang version 3.7.0 (trunk 226474) (llvm/trunk 226478)"}
114+
!13 = !MDLocation(line: 14, column: 3, scope: !4)
115+
!14 = !MDLocation(line: 15, column: 1, scope: !4)
116+
!15 = !MDLocation(line: 11, column: 3, scope: !7)
117+
!16 = !MDLocation(line: 12, column: 1, scope: !7)
118+
!17 = !MDLocation(line: 8, column: 3, scope: !8)
119+
!18 = !MDLocation(line: 9, column: 1, scope: !8)
120+
!19 = !MDLocation(line: 5, column: 3, scope: !9)
121+
!20 = !MDLocation(line: 6, column: 1, scope: !9)

llvm/test/Transforms/Inline/debug-invoke.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
; CHECK-NEXT: to label {{.*}} unwind label {{.*}}, !dbg [[INL_LOC:!.*]]
77
; CHECK: [[EMPTY:.*]] = !{}
88
; CHECK: [[INL_LOC]] = !MDLocation(line: 1, scope: [[EMPTY]], inlinedAt: [[INL_AT:.*]])
9-
; CHECK: [[INL_AT]] = !MDLocation(line: 2, scope: [[EMPTY]])
9+
; CHECK: [[INL_AT]] = distinct !MDLocation(line: 2, scope: [[EMPTY]])
1010

1111
declare void @test()
1212
declare i32 @__gxx_personality_v0(...)

llvm/test/Transforms/Inline/inline_dbg_declare.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@ attributes #1 = { nounwind readnone }
9292
!22 = !MDLocation(line: 8, column: 14, scope: !9)
9393
!23 = !MDLocation(line: 9, column: 1, scope: !9)
9494

95-
; CHECK: [[m23]] = !{!"0x101\00x\0016777217\000", !4, !5, !8, !22} ; [ DW_TAG_arg_variable ] [x] [line 1]
96-
; CHECK: [[m24]] = !MDLocation(line: 1, column: 17, scope: !4, inlinedAt: !22)
95+
; CHECK: [[m23]] = !{!"0x101\00x\0016777217\000", !4, !5, !8, [[CALL_SITE:![0-9]*]]} ; [ DW_TAG_arg_variable ] [x] [line 1]
96+
; CHECK: [[CALL_SITE]] = distinct !MDLocation(line: 8, column: 14, scope: !9)
97+
; CHECK: [[m24]] = !MDLocation(line: 1, column: 17, scope: !4, inlinedAt: [[CALL_SITE]])

0 commit comments

Comments
 (0)