Skip to content

Commit be1b1d6

Browse files
committed
[llvm][DebugInfo] Encode DW_AT_object_pointer on method declarations with DW_FORM_implicit_const
We started attaching `DW_AT_object_pointer`s on method declarations in #122742. However, that caused the `.debug_info` section size to increase significantly (by around ~10% on some projects). This was mainly due to the large number of new `DW_FORM_ref4` values. This patch tries to address that regression by changing the `DW_FORM_ref4` to a `DW_FORM_implicit_const` for declarations. That way we don't pay for the 4 byte references on every attribute occurrence. In a local build of clang this barely affected the `.debug_info` section size (but did increase `.debug_abbrev` by up to 10%, which doesn't affect the total debug-info size much however). We guarded this on LLDB tuning (since using `DW_FORM_implicit_const` for this purpose may surprise consumers) and DWARFv5 (since that's where `DW_FORM_implicit_const` was first standardized).
1 parent 5f5cdf4 commit be1b1d6

File tree

5 files changed

+115
-19
lines changed

5 files changed

+115
-19
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,10 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
849849
}
850850
}
851851

852-
DIE *DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
852+
std::optional<unsigned>
853+
DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
853854
// Args[0] is the return type.
854-
DIE *ObjectPointer = nullptr;
855+
std::optional<unsigned> ObjectPointerIndex;
855856
for (unsigned i = 1, N = Args.size(); i < N; ++i) {
856857
const DIType *Ty = Args[i];
857858
if (!Ty) {
@@ -863,13 +864,14 @@ DIE *DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
863864
if (Ty->isArtificial())
864865
addFlag(Arg, dwarf::DW_AT_artificial);
865866
if (Ty->isObjectPointer()) {
866-
assert(!ObjectPointer && "Can't have more than one object pointer");
867-
ObjectPointer = &Arg;
867+
assert(!ObjectPointerIndex &&
868+
"Can't have more than one object pointer");
869+
ObjectPointerIndex = i;
868870
}
869871
}
870872
}
871873

872-
return ObjectPointer;
874+
return ObjectPointerIndex;
873875
}
874876

875877
void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
@@ -1366,8 +1368,20 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
13661368

13671369
// Add arguments. Do not add arguments for subprogram definition. They will
13681370
// be handled while processing variables.
1369-
if (auto *ObjectPointer = constructSubprogramArguments(SPDie, Args))
1370-
addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, *ObjectPointer);
1371+
//
1372+
// Encode the object pointer as an index instead of a DIE reference in order
1373+
// to minimize the affect on the .debug_info size.
1374+
if (std::optional<unsigned> ObjectPointerIndex =
1375+
constructSubprogramArguments(SPDie, Args)) {
1376+
if (getDwarfDebug().tuneForLLDB() &&
1377+
getDwarfDebug().getDwarfVersion() >= 5) {
1378+
// 0th index in Args is the return type, hence adjust by 1. In DWARF
1379+
// we want the first parameter to be at index 0.
1380+
assert(*ObjectPointerIndex > 0);
1381+
addSInt(SPDie, dwarf::DW_AT_object_pointer,
1382+
dwarf::DW_FORM_implicit_const, *ObjectPointerIndex - 1);
1383+
}
1384+
}
13711385
}
13721386

13731387
addThrownTypes(SPDie, SP->getThrownTypes());

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,10 @@ class DwarfUnit : public DIEUnit {
269269

270270
/// Construct function argument DIEs.
271271
///
272-
/// \returns DIE of the object pointer if one exists. Nullptr otherwise.
273-
DIE *constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
272+
/// \returns The index of the object parameter in \c Args if one exists.
273+
/// Returns std::nullopt otherwise.
274+
std::optional<unsigned> constructSubprogramArguments(DIE &Buffer,
275+
DITypeRefArray Args);
274276

275277
/// Create a DIE with the given Tag, add the DIE to its parent, and
276278
/// call insertDIE if MD is not null.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
; Similar to DW_AT_object_pointer.ll but tests that we correctly
2+
; encode the object pointer index even if it's not the first argument
3+
; of the subprogram (which isn't something the major compilers do,
4+
; but is not mandated by DWARF).
5+
6+
; RUN: llc -mtriple=x86_64-apple-darwin -debugger-tune=lldb -dwarf-version=5 -filetype=obj < %s | \
7+
; RUN: llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefixes=CHECK
8+
9+
; CHECK: DW_TAG_class_type
10+
; CHECK: [[DECL:0x[0-9a-f]+]]: DW_TAG_subprogram
11+
; CHECK: DW_AT_name {{.*}} "A"
12+
; CHECK: DW_AT_object_pointer [DW_FORM_implicit_const] (2)
13+
; CHECK: DW_TAG_formal_parameter
14+
;
15+
; CHECK: DW_TAG_subprogram
16+
; CHECK: DW_AT_object_pointer [DW_FORM_ref4] (cu + 0x{{[0-9a-f]*}} => {[[PARAM:0x[0-9a-f]*]]})
17+
; CHECK: DW_AT_specification [DW_FORM_ref4] (cu + {{.*}} => {[[DECL]]}
18+
; CHECK: DW_TAG_formal_parameter
19+
; CHECK: DW_TAG_formal_parameter
20+
; CHECK: [[PARAM]]: DW_TAG_formal_parameter
21+
; CHECK-NOT: DW_TAG
22+
; CHECK: DW_AT_name
23+
; CHECK-SAME = "this")
24+
25+
%class.A = type { i8 }
26+
27+
define linkonce_odr noundef ptr @_ZN1AC1Eii(ptr noundef nonnull returned align 1 dereferenceable(1) %this, i32 noundef %x, i32 noundef %y) !dbg !24 {
28+
entry:
29+
%this.addr = alloca ptr, align 8
30+
%x.addr = alloca i32, align 4
31+
%y.addr = alloca i32, align 4
32+
store ptr %this, ptr %this.addr, align 8
33+
#dbg_declare(ptr %this.addr, !26, !DIExpression(), !28)
34+
store i32 %x, ptr %x.addr, align 4
35+
#dbg_declare(ptr %x.addr, !29, !DIExpression(), !30)
36+
store i32 %y, ptr %y.addr, align 4
37+
#dbg_declare(ptr %y.addr, !31, !DIExpression(), !32)
38+
%this1 = load ptr, ptr %this.addr, align 8
39+
%0 = load i32, ptr %x.addr, align 4, !dbg !33
40+
%1 = load i32, ptr %y.addr, align 4, !dbg !33
41+
ret ptr %this1, !dbg !34
42+
}
43+
44+
!llvm.dbg.cu = !{!2}
45+
!llvm.module.flags = !{!12, !13}
46+
47+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
48+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 3, type: !5, isLocal: false, isDefinition: true)
49+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 20.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
50+
!3 = !DIFile(filename: "object_ptr.cpp", directory: "/tmp")
51+
!4 = !{!0}
52+
!5 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "A", file: !3, line: 1, size: 8, flags: DIFlagTypePassByValue | DIFlagNonTrivial, elements: !6, identifier: "_ZTS1A")
53+
!6 = !{!7}
54+
!7 = !DISubprogram(name: "A", scope: !5, file: !3, line: 2, type: !8, scopeLine: 2, flags: DIFlagPublic | DIFlagPrototyped, spFlags: 0)
55+
!8 = !DISubroutineType(types: !9)
56+
!9 = !{null, !11, !11, !10}
57+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
58+
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
59+
!12 = !{i32 7, !"Dwarf Version", i32 5}
60+
!13 = !{i32 2, !"Debug Info Version", i32 3}
61+
!18 = !{!"clang version 20.0.0git"}
62+
!24 = distinct !DISubprogram(name: "A", linkageName: "_ZN1AC1Eii", scope: !5, file: !3, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, declaration: !7, retainedNodes: !25)
63+
!25 = !{}
64+
!26 = !DILocalVariable(name: "this", arg: 3, scope: !24, type: !27, flags: DIFlagArtificial | DIFlagObjectPointer)
65+
!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64)
66+
!28 = !DILocation(line: 0, scope: !24)
67+
!29 = !DILocalVariable(name: "x", arg: 2, scope: !24, file: !3, line: 2, type: !11)
68+
!30 = !DILocation(line: 2, column: 19, scope: !24)
69+
!31 = !DILocalVariable(name: "y", arg: 1, scope: !24, file: !3, line: 2, type: !11)
70+
!32 = !DILocation(line: 2, column: 26, scope: !24)
71+
!33 = !DILocation(line: 2, column: 29, scope: !24)
72+
!34 = !DILocation(line: 2, column: 30, scope: !24)

llvm/test/DebugInfo/X86/DW_AT_object_pointer.ll

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
; RUN: llc -mtriple=x86_64-apple-darwin %s -o %t -filetype=obj
2-
; RUN: llvm-dwarfdump -v -debug-info %t | FileCheck %s
1+
; RUN: llc -mtriple=x86_64-apple-darwin -debugger-tune=gdb -dwarf-version=5 -filetype=obj < %s | \
2+
; RUN: llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefixes=CHECK,CHECK-GDB
3+
4+
; RUN: llc -mtriple=x86_64-apple-darwin -debugger-tune=lldb -dwarf-version=4 -filetype=obj < %s | \
5+
; RUN: llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefixes=CHECK,CHECK-LLDB-DWARF4
6+
7+
; RUN: llc -mtriple=x86_64-apple-darwin -debugger-tune=lldb -dwarf-version=5 -filetype=obj < %s | \
8+
; RUN: llvm-dwarfdump -v -debug-info - | FileCheck %s --check-prefixes=CHECK,CHECK-LLDB-DWARF5
39

410
; CHECK: DW_TAG_formal_parameter [
511
; CHECK-NOT: ""
612
; CHECK: DW_TAG
713
; CHECK: DW_TAG_class_type
814
; CHECK: [[DECL:0x[0-9a-f]+]]: DW_TAG_subprogram
915
; CHECK: DW_AT_name {{.*}} "A"
10-
; CHECK: DW_AT_object_pointer [DW_FORM_ref4]
11-
; CHECK-SAME: (cu + 0x{{[0-9a-f]*}} => {[[DECL_PARAM:0x[0-9a-f]*]]})
12-
; CHECK: [[DECL_PARAM]]: DW_TAG_formal_parameter
16+
; CHECK-LLDB-DWARF5: DW_AT_object_pointer [DW_FORM_implicit_const] (0)
17+
; CHECK-GDB-NOT: DW_AT_object_pointer
18+
; CHECK-LLDB-DWARF4-NOT: DW_AT_object_pointer
19+
; CHECK: DW_TAG_formal_parameter
1320
;
1421
; CHECK: DW_TAG_subprogram
15-
; CHECK: DW_AT_specification [DW_FORM_ref4] (cu + {{.*}} => {[[DECL]]}
1622
; CHECK: DW_AT_object_pointer [DW_FORM_ref4] (cu + 0x{{[0-9a-f]*}} => {[[PARAM:0x[0-9a-f]*]]})
23+
; CHECK: DW_AT_specification [DW_FORM_ref4] (cu + {{.*}} => {[[DECL]]}
1724
; CHECK: [[PARAM]]: DW_TAG_formal_parameter
1825
; CHECK-NOT: DW_TAG
19-
; CHECK: DW_AT_name [DW_FORM_strp] ( .debug_str[0x{{[0-9a-f]*}}] = "this")
26+
; CHECK: DW_AT_name
27+
; CHECK-SAME = "this")
2028

2129
%class.A = type { i32 }
2230

llvm/test/tools/llvm-dwarfdump/X86/statistics.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -O0 %s -o - -filetype=obj \
1+
; RUN: llc -O0 %s -o - -filetype=obj -debugger-tune=gdb -accel-tables=Apple \
22
; RUN: | llvm-dwarfdump -statistics - | FileCheck %s
33
; CHECK: "version": 9,
44

@@ -55,8 +55,8 @@
5555
; CHECK: "#bytes within functions": [[FUNCSIZE:[0-9]+]]
5656
; CHECK: "#bytes within inlined functions": [[INLINESIZE:[0-9]+]]
5757
; CHECK: "#bytes in __debug_loc": 35,
58-
; CHECK-NEXT: "#bytes in __debug_abbrev": 386,
59-
; CHECK-NEXT: "#bytes in __debug_info": 463,
58+
; CHECK-NEXT: "#bytes in __debug_abbrev": 375,
59+
; CHECK-NEXT: "#bytes in __debug_info": 459,
6060
; CHECK-NEXT: "#bytes in __debug_str": 231,
6161
; CHECK-NEXT: "#bytes in __apple_names": 348,
6262
; CHECK-NEXT: "#bytes in __apple_objc": 36,

0 commit comments

Comments
 (0)