Skip to content

Commit 4b7f2f6

Browse files
committed
[DebugInfo] Emit linkage name into DWARF for types for Swift (llvm#112802)
Store Swift mangled names in DW_AT_linkage_name. The Swift compiler emits only the type mangled name in debug information, and LLDB uses those mangled names as keys to look up size, alignment, fields, etc from either reflection metadata or Swift modules. Additionally, emit types linkage names for types into the accelerator table if they exist and they're different from the display name. (cherry picked from commit 8234f8a)
1 parent cc3ddbf commit 4b7f2f6

File tree

4 files changed

+153
-11
lines changed

4 files changed

+153
-11
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,16 +646,23 @@ void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
646646

647647
// add temporary record for this type to be added later
648648

649-
bool IsImplementation = false;
649+
unsigned Flags = 0;
650650
if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
651651
// A runtime language of 0 actually means C/C++ and that any
652652
// non-negative value is some version of Objective-C/C++.
653-
IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
653+
if (CT->getRuntimeLang() == 0 || CT->isObjcClassComplete())
654+
Flags = dwarf::DW_FLAG_type_implementation;
654655
}
655-
unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
656+
656657
DD->addAccelType(*this, CUNode->getNameTableKind(), Ty->getName(), TyDIE,
657658
Flags);
658659

660+
if (auto *CT = dyn_cast<DICompositeType>(Ty))
661+
if (Ty->getName() != CT->getIdentifier() &&
662+
CT->getRuntimeLang() == dwarf::DW_LANG_Swift)
663+
DD->addAccelType(*this, CUNode->getNameTableKind(), CT->getIdentifier(),
664+
TyDIE, Flags);
665+
659666
addGlobalType(Ty, TyDIE, Context);
660667
}
661668

@@ -1052,8 +1059,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
10521059
if (!Name.empty())
10531060
addString(Buffer, dwarf::DW_AT_name, Name);
10541061

1055-
// For Swift, mangled names are put into DW_AT_linkage_name; human-readable
1056-
// names are emitted put into DW_AT_name and the accelerator table.
1062+
// For Swift, mangled names are put into DW_AT_linkage_name.
10571063
if (CTy->getRuntimeLang() == dwarf::DW_LANG_Swift && CTy->getRawIdentifier())
10581064
addString(Buffer, dwarf::DW_AT_linkage_name, CTy->getIdentifier());
10591065

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,10 +1831,8 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
18311831
Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
18321832
} else if (Tag == dwarf::DW_TAG_imported_declaration && AttrInfo.Name) {
18331833
Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1834-
} else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1835-
getDIENames(InputDIE, AttrInfo, DebugStrPool) && AttrInfo.Name &&
1836-
AttrInfo.Name.getString()[0]) {
1837-
uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1834+
} else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration) {
1835+
bool Success = getDIENames(InputDIE, AttrInfo, DebugStrPool);
18381836
uint64_t RuntimeLang =
18391837
dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
18401838
.value_or(0);
@@ -1843,8 +1841,21 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
18431841
RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
18441842
dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
18451843
.value_or(0);
1846-
Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1847-
Hash);
1844+
if (Success && AttrInfo.Name && !AttrInfo.Name.getString().empty()) {
1845+
uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1846+
Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1847+
Hash);
1848+
}
1849+
1850+
// For Swift, mangled names are put into DW_AT_linkage_name.
1851+
if (Success && AttrInfo.MangledName &&
1852+
RuntimeLang == dwarf::DW_LANG_Swift &&
1853+
!AttrInfo.MangledName.getString().empty() &&
1854+
AttrInfo.MangledName != AttrInfo.Name) {
1855+
auto Hash = djbHash(AttrInfo.MangledName.getString().data());
1856+
Unit.addTypeAccelerator(Die, AttrInfo.MangledName,
1857+
ObjCClassIsImplementation, Hash);
1858+
}
18481859
}
18491860

18501861
// Determine whether there are any children that we want to keep.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; RUN: %llc_dwarf -debugger-tune=lldb -accel-tables=Dwarf -filetype=obj -o %t < %s
2+
; RUN: llvm-dwarfdump %t | FileCheck %s
3+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=SAME-NAME %s
4+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=DIFFERENT-NAME %s
5+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=UNIQUE-DIFFERENT-NAME %s
6+
; RUN: llvm-dwarfdump -debug-names -verify %t | FileCheck --check-prefix=VERIFY %s
7+
8+
9+
; CHECK: DW_TAG_structure_type
10+
; CHECK: DW_AT_name ("SameName")
11+
; CHECK: DW_AT_linkage_name ("SameName")
12+
13+
; CHECK: DW_TAG_structure_type
14+
; CHECK: DW_AT_name ("DifferentName")
15+
; CHECK: DW_AT_linkage_name ("UniqueDifferentName")
16+
17+
; The name count should be 5 (the two variables, the two human readable names, one mangled name).
18+
; SAME-NAME: Name count: 5
19+
20+
; The accelarator should only have one entry for the three following names.
21+
; SAME-NAME: "SameName"
22+
; SAME-NAME-NOT: "SameName"
23+
24+
; DIFFERENT-NAME: "DifferentName"
25+
; DIFFERENT-NAME-NOT: "DifferentName"
26+
27+
; UNIQUE-DIFFERENT-NAME: "UniqueDifferentName"
28+
; UNIQUE-DIFFERENT-NAME-NOT: "UniqueDifferentName"
29+
30+
; Verification should succeed.
31+
; VERIFY: No errors.
32+
33+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
34+
35+
@q = common global i8* null, align 8, !dbg !102
36+
@r = common global i8* null, align 8, !dbg !105
37+
38+
!llvm.dbg.cu = !{!2}
39+
!llvm.module.flags = !{!6, !7}
40+
41+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, emissionKind: FullDebug, globals: !5)
42+
!3 = !DIFile(filename: "/tmp/p.c", directory: "/")
43+
!4 = !{}
44+
!5 = !{!102, !105}
45+
!6 = !{i32 2, !"Dwarf Version", i32 4}
46+
!7 = !{i32 2, !"Debug Info Version", i32 3}
47+
48+
; marking the types as Swift is necessary because we only emit the linkage names for Swift types.
49+
!11 = !DICompositeType(tag: DW_TAG_structure_type, name: "SameName", file: !3, size: 64, runtimeLang: DW_LANG_Swift, identifier: "SameName")
50+
!12 = !DICompositeType(tag: DW_TAG_structure_type, name: "DifferentName", file: !3, size: 64, runtimeLang: DW_LANG_Swift, identifier: "UniqueDifferentName")
51+
52+
!102 = !DIGlobalVariableExpression(var: !103, expr: !DIExpression())
53+
!103 = distinct !DIGlobalVariable(name: "q", scope: !2, file: !3, line: 1, type: !11, isLocal: false, isDefinition: true)
54+
!104 = distinct !DIGlobalVariable(name: "r", scope: !2, file: !3, line: 1, type: !12, isLocal: false, isDefinition: true)
55+
!105 = !DIGlobalVariableExpression(var: !104, expr: !DIExpression())
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; RUN: %llc_dwarf -debugger-tune=lldb -accel-tables=Dwarf -filetype=obj -o %t < %s
2+
; RUN: dsymutil %t -o %t.dSYM
3+
; RUN: llvm-dwarfdump %t | FileCheck %s
4+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=SAME-NAME %s
5+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=DIFFERENT-NAME %s
6+
; RUN: llvm-dwarfdump -debug-names %t | FileCheck --check-prefix=UNIQUE-DIFFERENT-NAME %s
7+
; RUN: llvm-dwarfdump -debug-names -verify %t | FileCheck --check-prefix=VERIFY %s
8+
9+
10+
; CHECK: DW_TAG_structure_type
11+
; CHECK: DW_AT_name ("SameName")
12+
; CHECK: DW_AT_linkage_name ("SameName")
13+
14+
; CHECK: DW_TAG_structure_type
15+
; CHECK: DW_AT_name ("DifferentName")
16+
; CHECK: DW_AT_linkage_name ("UniqueDifferentName")
17+
18+
; The name count should be 5 (the two variables, "int", "SameName", "DifferentName", "UniqueDifferentName").
19+
; SAME-NAME: Name count: 6
20+
21+
; The accelarator should only have one entry for the three following names.
22+
; SAME-NAME: "SameName"
23+
; SAME-NAME-NOT: "SameName"
24+
25+
; DIFFERENT-NAME: "DifferentName"
26+
; DIFFERENT-NAME-NOT: "DifferentName"
27+
28+
; UNIQUE-DIFFERENT-NAME: "UniqueDifferentName"
29+
; UNIQUE-DIFFERENT-NAME-NOT: "UniqueDifferentName"
30+
31+
; Verification should succeed.
32+
; VERIFY: No errors.
33+
34+
; ModuleID = 't.c'
35+
source_filename = "t.c"
36+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
37+
target triple = "arm64-apple-macosx14.5.0"
38+
39+
%struct.SameName = type { i32 }
40+
%struct.DifferentName = type { i32 }
41+
42+
@q = global %struct.SameName zeroinitializer, align 4, !dbg !0
43+
@r = global %struct.DifferentName zeroinitializer, align 4, !dbg !5
44+
45+
!llvm.module.flags = !{!14, !15, !16, !17, !18, !19, !20}
46+
!llvm.dbg.cu = !{!2}
47+
!llvm.ident = !{!21}
48+
49+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
50+
!1 = distinct !DIGlobalVariable(name: "q", scope: !2, file: !3, line: 9, type: !11, isLocal: false, isDefinition: true)
51+
!2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 1", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: Apple)
52+
!3 = !DIFile(filename: "t.c", directory: "/")
53+
!4 = !{!0, !5}
54+
!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
55+
!6 = distinct !DIGlobalVariable(name: "r", scope: !2, file: !3, line: 10, type: !7, isLocal: false, isDefinition: true)
56+
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "DifferentName", file: !3, line: 5, size: 32, runtimeLang: DW_LANG_Swift, identifier: "UniqueDifferentName", elements: !8)
57+
!8 = !{!9}
58+
!9 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !7, file: !3, line: 6, baseType: !10, size: 32)
59+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
60+
!11 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "SameName", file: !3, line: 1, size: 32, runtimeLang: DW_LANG_Swift, identifier: "SameName", elements: !12)
61+
!12 = !{!13}
62+
!13 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !11, file: !3, line: 2, baseType: !10, size: 32)
63+
!14 = !{i32 2, !"SDK Version", [2 x i32] [i32 14, i32 5]}
64+
!15 = !{i32 7, !"Dwarf Version", i32 4}
65+
!16 = !{i32 2, !"Debug Info Version", i32 3}
66+
!17 = !{i32 1, !"wchar_size", i32 4}
67+
!18 = !{i32 8, !"PIC Level", i32 2}
68+
!19 = !{i32 7, !"uwtable", i32 1}
69+
!20 = !{i32 7, !"frame-pointer", i32 1}
70+
!21 = !{!"clang version 1"}

0 commit comments

Comments
 (0)