Skip to content

Commit 711f6a8

Browse files
authored
[llvm][DebugInfo] Encode DW_AT_object_pointer on method declarations with DW_FORM_implicit_const (#124790)
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. The value of `DW_FORM_implicit_const` will be the *index* of the object parameter in the list of formal parameters of the subprogram (i.e., if the first `DW_TAG_formal_parameter` is the object pointer, the `DW_FORM_implicit_const` would be `0`). The DWARFv5 spec only mentions the use of the `reference` attribute class to for `DW_AT_object_pointer`. So using a `DW_FORM_impilicit_const` would be an extension to (and not something mandated/specified by) the standard. Though it'd make sense to extend the wording in the spec to allow for this optimization. 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 impact 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 dfb14b6 commit 711f6a8

File tree

5 files changed

+132
-9
lines changed

5 files changed

+132
-9
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,10 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
895895
}
896896
}
897897

898-
void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
898+
std::optional<unsigned>
899+
DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
900+
// Args[0] is the return type.
901+
std::optional<unsigned> ObjectPointerIndex;
899902
for (unsigned i = 1, N = Args.size(); i < N; ++i) {
900903
const DIType *Ty = Args[i];
901904
if (!Ty) {
@@ -906,8 +909,16 @@ void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
906909
addType(Arg, Ty);
907910
if (Ty->isArtificial())
908911
addFlag(Arg, dwarf::DW_AT_artificial);
912+
913+
if (Ty->isObjectPointer()) {
914+
assert(!ObjectPointerIndex &&
915+
"Can't have more than one object pointer");
916+
ObjectPointerIndex = i;
917+
}
909918
}
910919
}
920+
921+
return ObjectPointerIndex;
911922
}
912923

913924
void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
@@ -1458,7 +1469,20 @@ void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
14581469

14591470
// Add arguments. Do not add arguments for subprogram definition. They will
14601471
// be handled while processing variables.
1461-
constructSubprogramArguments(SPDie, Args);
1472+
//
1473+
// Encode the object pointer as an index instead of a DIE reference in order
1474+
// to minimize the affect on the .debug_info size.
1475+
if (std::optional<unsigned> ObjectPointerIndex =
1476+
constructSubprogramArguments(SPDie, Args)) {
1477+
if (getDwarfDebug().tuneForLLDB() &&
1478+
getDwarfDebug().getDwarfVersion() >= 5) {
1479+
// 0th index in Args is the return type, hence adjust by 1. In DWARF
1480+
// we want the first parameter to be at index 0.
1481+
assert(*ObjectPointerIndex > 0);
1482+
addSInt(SPDie, dwarf::DW_AT_object_pointer,
1483+
dwarf::DW_FORM_implicit_const, *ObjectPointerIndex - 1);
1484+
}
1485+
}
14621486
}
14631487

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

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ class DwarfUnit : public DIEUnit {
273273
void constructContainingTypeDIEs();
274274

275275
/// Construct function argument DIEs.
276-
void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
276+
///
277+
/// \returns The index of the object parameter in \c Args if one exists.
278+
/// Returns std::nullopt otherwise.
279+
std::optional<unsigned> constructSubprogramArguments(DIE &Buffer,
280+
DITypeRefArray Args);
277281

278282
/// Create a DIE with the given Tag, add the DIE to its parent, and
279283
/// call insertDIE if MD is not null.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
;
14+
; CHECK: DW_TAG_subprogram
15+
; CHECK: DW_AT_object_pointer [DW_FORM_ref4] (cu + 0x{{[0-9a-f]*}} => {[[PARAM:0x[0-9a-f]*]]})
16+
; CHECK: DW_AT_specification [DW_FORM_ref4] (cu + {{.*}} => {[[DECL]]}
17+
; CHECK: DW_TAG_formal_parameter
18+
; CHECK: DW_TAG_formal_parameter
19+
; CHECK-NOT: "this"
20+
; CHECK: [[PARAM]]: DW_TAG_formal_parameter
21+
; CHECK: DW_AT_name
22+
; CHECK-SAME: = "this")
23+
; CHECK: DW_TAG_formal_parameter
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, i32 noundef %z) !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+
%z.addr = alloca i32, align 4
33+
store ptr %this, ptr %this.addr, align 8
34+
#dbg_declare(ptr %this.addr, !26, !DIExpression(), !28)
35+
store i32 %x, ptr %x.addr, align 4
36+
#dbg_declare(ptr %x.addr, !29, !DIExpression(), !30)
37+
store i32 %y, ptr %y.addr, align 4
38+
#dbg_declare(ptr %y.addr, !31, !DIExpression(), !32)
39+
store i32 %z, ptr %y.addr, align 4
40+
#dbg_declare(ptr %z.addr, !36, !DIExpression(), !37)
41+
%this1 = load ptr, ptr %this.addr, align 8
42+
%0 = load i32, ptr %x.addr, align 4, !dbg !33
43+
%1 = load i32, ptr %y.addr, align 4, !dbg !33
44+
%2 = load i32, ptr %z.addr, align 4, !dbg !33
45+
ret ptr %this1, !dbg !34
46+
}
47+
48+
!llvm.dbg.cu = !{!2}
49+
!llvm.module.flags = !{!12, !13}
50+
51+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
52+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 3, type: !5, isLocal: false, isDefinition: true)
53+
!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: "/")
54+
!3 = !DIFile(filename: "object_ptr.cpp", directory: "/tmp")
55+
!4 = !{!0}
56+
!5 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "A", file: !3, line: 1, size: 8, flags: DIFlagTypePassByValue | DIFlagNonTrivial, elements: !6, identifier: "_ZTS1A")
57+
!6 = !{!7}
58+
!7 = !DISubprogram(name: "A", scope: !5, file: !3, line: 2, type: !8, scopeLine: 2, flags: DIFlagPublic | DIFlagPrototyped, spFlags: 0)
59+
!8 = !DISubroutineType(types: !9)
60+
!9 = !{null, !11, !11, !10, !35}
61+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
62+
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
63+
!12 = !{i32 7, !"Dwarf Version", i32 5}
64+
!13 = !{i32 2, !"Debug Info Version", i32 3}
65+
!18 = !{!"clang version 20.0.0git"}
66+
!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)
67+
!25 = !{}
68+
!26 = !DILocalVariable(name: "this", arg: 3, scope: !24, type: !27, flags: DIFlagArtificial | DIFlagObjectPointer)
69+
!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64)
70+
!28 = !DILocation(line: 0, scope: !24)
71+
!29 = !DILocalVariable(name: "x", arg: 2, scope: !24, file: !3, line: 2, type: !11)
72+
!30 = !DILocation(line: 2, column: 19, scope: !24)
73+
!31 = !DILocalVariable(name: "y", arg: 1, scope: !24, file: !3, line: 2, type: !11)
74+
!32 = !DILocation(line: 2, column: 26, scope: !24)
75+
!33 = !DILocation(line: 2, column: 29, scope: !24)
76+
!34 = !DILocation(line: 2, column: 30, scope: !24)
77+
!35 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed)
78+
!36 = !DILocalVariable(name: "z", arg: 4, scope: !24, file: !3, line: 2, type: !35)
79+
!37 = !DILocation(line: 2, column: 35, scope: !24)

llvm/test/DebugInfo/X86/DW_AT_object_pointer.ll

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +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
8-
; CHECK: DW_AT_object_pointer [DW_FORM_ref4] (cu + 0x{{[0-9a-f]*}} => {[[PARAM:0x[0-9a-f]*]]})
14+
; CHECK: [[DECL:0x[0-9a-f]+]]: DW_TAG_subprogram
15+
; CHECK: DW_AT_name {{.*}} "A"
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
20+
;
21+
; CHECK: DW_TAG_subprogram
22+
; 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]]}
924
; CHECK: [[PARAM]]: DW_TAG_formal_parameter
1025
; CHECK-NOT: DW_TAG
11-
; CHECK: DW_AT_name [DW_FORM_strp] ( .debug_str[0x{{[0-9a-f]*}}] = "this")
26+
; CHECK: DW_AT_name
27+
; CHECK-SAME = "this")
1228

1329
%class.A = type { i32 }
1430

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

Lines changed: 2 additions & 2 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,7 +55,7 @@
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": 384,
58+
; CHECK-NEXT: "#bytes in __debug_abbrev": 375,
5959
; CHECK-NEXT: "#bytes in __debug_info": 459,
6060
; CHECK-NEXT: "#bytes in __debug_str": 231,
6161
; CHECK-NEXT: "#bytes in __apple_names": 348,

0 commit comments

Comments
 (0)