Skip to content

Commit a43137c

Browse files
authored
[LLVM][DWARF] Make some effort to avoid duplicates in .debug_ranges. (#106614)
Inlining and zero-cost abstractions tend to produce volumes of debug info with identical ranges. When built with full debugging information (the equivalent of -g2) librustc_driver.so has 2.1 million entries in .debug_ranges. But only 1.1 million of those entries are unique. While in principle all duplicates could be eliminated with a hashtable, checking to see if the new range is exactly identical to the previous range and skipping a new addition if it is is sufficient to eliminate 99.99% of the duplicates. This reduces the size of librustc_driver.so's .debug_ranges section by 35%, or the overall binary size a little more than 1%.
1 parent b30880e commit a43137c

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfFile.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,19 @@ void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
121121

122122
std::pair<uint32_t, RangeSpanList *>
123123
DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
124-
CURangeLists.push_back(
125-
RangeSpanList{Asm->createTempSymbol("debug_ranges"), &CU, std::move(R)});
124+
bool CanReuseLastRange = false;
125+
126+
if (!CURangeLists.empty()) {
127+
auto Last = CURangeLists.back();
128+
if (Last.CU == &CU && Last.Ranges == R) {
129+
CanReuseLastRange = true;
130+
}
131+
}
132+
133+
if (!CanReuseLastRange) {
134+
CURangeLists.push_back(RangeSpanList{Asm->createTempSymbol("debug_ranges"),
135+
&CU, std::move(R)});
136+
}
137+
126138
return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
127139
}

llvm/lib/CodeGen/AsmPrinter/DwarfFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class MDNode;
3737
struct RangeSpan {
3838
const MCSymbol *Begin;
3939
const MCSymbol *End;
40+
41+
bool operator==(const RangeSpan &Other) const {
42+
return Begin == Other.Begin && End == Other.End;
43+
}
4044
};
4145

4246
struct RangeSpanList {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; RUN: %llc_dwarf -O0 -filetype=obj < %s | llvm-dwarfdump -debug-info - | FileCheck %s
2+
;
3+
; Generated from the following C++ source with:
4+
; clang -S -emit-llvm -g -O2 test.c
5+
;
6+
; /* BEGIN SOURCE */
7+
; void f1();
8+
; inline void f2() {
9+
; f1();
10+
; f1();
11+
; }
12+
; inline void f3() {
13+
; f2();
14+
; }
15+
; void f4() {
16+
; f3();
17+
; f1();
18+
; }
19+
; /* END SOURCE */
20+
;
21+
; Minor complication: after generating the LLVM IR, it was manually edited so
22+
; that the 'f1()' call from f3 was reordered to appear between the two inlined
23+
; f1 calls from f2. This causes f2's inlined_subroutine to use DW_AT_ranges.
24+
25+
; Check that identical debug ranges in succession reuse the same entry in
26+
; .debug_ranges rather than emitting duplicate entries.
27+
28+
; CHECK: DW_TAG_inlined_subroutine
29+
; CHECK: DW_AT_ranges
30+
; CHECK-SAME: rangelist = 0x[[#%.8X,RANGE:]]
31+
; CHECK: DW_TAG_inlined_subroutine
32+
; CHECK: DW_AT_ranges
33+
; CHECK-SAME: rangelist = 0x[[#RANGE]]
34+
35+
; Function Attrs: nounwind uwtable
36+
define dso_local void @f4() local_unnamed_addr !dbg !9 {
37+
entry:
38+
tail call void (...) @f1(), !dbg !12
39+
tail call void (...) @f1(), !dbg !18
40+
tail call void (...) @f1(), !dbg !17
41+
ret void, !dbg !19
42+
}
43+
44+
declare !dbg !20 void @f1(...) local_unnamed_addr
45+
46+
!llvm.dbg.cu = !{!0}
47+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
48+
!llvm.ident = !{!8}
49+
50+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git (https://github.com/llvm/llvm-project.git 9edd998e10fabfff067b9e6e5b044f85a24d0dd5)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
51+
!1 = !DIFile(filename: "test.c", directory: "/home/khuey/dev/llvm-project", checksumkind: CSK_MD5, checksum: "4510feb241cf078af753e3dc13205127")
52+
!2 = !{i32 7, !"Dwarf Version", i32 5}
53+
!3 = !{i32 2, !"Debug Info Version", i32 3}
54+
!4 = !{i32 1, !"wchar_size", i32 4}
55+
!5 = !{i32 8, !"PIC Level", i32 2}
56+
!6 = !{i32 7, !"PIE Level", i32 2}
57+
!7 = !{i32 7, !"uwtable", i32 2}
58+
!8 = !{!"clang version 20.0.0git (https://github.com/llvm/llvm-project.git 9edd998e10fabfff067b9e6e5b044f85a24d0dd5)"}
59+
!9 = distinct !DISubprogram(name: "f4", scope: !1, file: !1, line: 9, type: !10, scopeLine: 9, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
60+
!10 = !DISubroutineType(types: !11)
61+
!11 = !{null}
62+
!12 = !DILocation(line: 3, column: 3, scope: !13, inlinedAt: !14)
63+
!13 = distinct !DISubprogram(name: "f2", scope: !1, file: !1, line: 2, type: !10, scopeLine: 2, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
64+
!14 = distinct !DILocation(line: 7, column: 3, scope: !15, inlinedAt: !16)
65+
!15 = distinct !DISubprogram(name: "f3", scope: !1, file: !1, line: 6, type: !10, scopeLine: 6, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
66+
!16 = distinct !DILocation(line: 10, column: 3, scope: !9)
67+
!17 = !DILocation(line: 4, column: 3, scope: !13, inlinedAt: !14)
68+
!18 = !DILocation(line: 11, column: 3, scope: !9)
69+
!19 = !DILocation(line: 12, column: 1, scope: !9)
70+
!20 = !DISubprogram(name: "f1", scope: !1, file: !1, line: 1, type: !10, spFlags: DISPFlagOptimized)

0 commit comments

Comments
 (0)