Skip to content

Commit e2a5a66

Browse files
committed
[DebugInfo] Emit linkage name into DWARF for types for Swift
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.
1 parent cc13d4f commit e2a5a66

File tree

4 files changed

+139
-9
lines changed

4 files changed

+139
-9
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,22 @@ void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
645645

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

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

659+
if (auto *CT = dyn_cast<DICompositeType>(Ty))
660+
if (Ty->getName() != CT->getIdentifier())
661+
DD->addAccelType(*this, CUNode->getNameTableKind(), CT->getIdentifier(),
662+
TyDIE, Flags);
663+
658664
addGlobalType(Ty, TyDIE, Context);
659665
}
660666

@@ -1042,6 +1048,10 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
10421048
if (!Name.empty())
10431049
addString(Buffer, dwarf::DW_AT_name, Name);
10441050

1051+
// For Swift, mangled names are put into DW_AT_linkage_name.
1052+
if (CTy->getRuntimeLang() == dwarf::DW_LANG_Swift && CTy->getRawIdentifier())
1053+
addString(Buffer, dwarf::DW_AT_linkage_name, CTy->getIdentifier());
1054+
10451055
addAnnotation(Buffer, CTy->getAnnotations());
10461056

10471057
if (Tag == dwarf::DW_TAG_enumeration_type ||

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,8 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
18371837
Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
18381838
} else if (Tag == dwarf::DW_TAG_imported_declaration && AttrInfo.Name) {
18391839
Unit.addNamespaceAccelerator(Die, AttrInfo.Name);
1840-
} else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
1841-
getDIENames(InputDIE, AttrInfo, DebugStrPool) && AttrInfo.Name &&
1842-
AttrInfo.Name.getString()[0]) {
1843-
uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1840+
} else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration) {
1841+
bool Success = getDIENames(InputDIE, AttrInfo, DebugStrPool);
18441842
uint64_t RuntimeLang =
18451843
dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_runtime_class))
18461844
.value_or(0);
@@ -1849,8 +1847,19 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
18491847
RuntimeLang == dwarf::DW_LANG_ObjC_plus_plus) &&
18501848
dwarf::toUnsigned(InputDIE.find(dwarf::DW_AT_APPLE_objc_complete_type))
18511849
.value_or(0);
1852-
Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1853-
Hash);
1850+
if (Success && AttrInfo.Name && !AttrInfo.Name.getString().empty()) {
1851+
uint32_t Hash = hashFullyQualifiedName(InputDIE, Unit, File);
1852+
Unit.addTypeAccelerator(Die, AttrInfo.Name, ObjCClassIsImplementation,
1853+
Hash);
1854+
}
1855+
1856+
if (Success && AttrInfo.MangledName &&
1857+
!AttrInfo.MangledName.getString().empty() &&
1858+
AttrInfo.MangledName != AttrInfo.Name) {
1859+
auto Hash = djbHash(AttrInfo.MangledName.getString().data());
1860+
Unit.addTypeAccelerator(Die, AttrInfo.MangledName,
1861+
ObjCClassIsImplementation, Hash);
1862+
}
18541863
}
18551864

18561865
// 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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, the two human readable names, one mangled name).
19+
; SAME-NAME: Name count: 5
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+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
35+
36+
@q = common global i8* null, align 8, !dbg !102
37+
@r = common global i8* null, align 8, !dbg !105
38+
39+
!llvm.dbg.cu = !{!2}
40+
!llvm.module.flags = !{!6, !7}
41+
42+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, emissionKind: FullDebug, globals: !5)
43+
!3 = !DIFile(filename: "/tmp/p.c", directory: "/")
44+
!4 = !{}
45+
!5 = !{!102, !105}
46+
!6 = !{i32 2, !"Dwarf Version", i32 4}
47+
!7 = !{i32 2, !"Debug Info Version", i32 3}
48+
49+
; marking the types as Swift is necessary because we only emit the linkage names for Swift types.
50+
!11 = !DICompositeType(tag: DW_TAG_structure_type, name: "SameName", file: !3, size: 64, runtimeLang: DW_LANG_Swift, identifier: "SameName")
51+
!12 = !DICompositeType(tag: DW_TAG_structure_type, name: "DifferentName", file: !3, size: 64, runtimeLang: DW_LANG_Swift, identifier: "UniqueDifferentName")
52+
53+
!102 = !DIGlobalVariableExpression(var: !103, expr: !DIExpression())
54+
!103 = distinct !DIGlobalVariable(name: "q", scope: !2, file: !3, line: 1, type: !11, isLocal: false, isDefinition: true)
55+
!104 = distinct !DIGlobalVariable(name: "r", scope: !2, file: !3, line: 1, type: !12, isLocal: false, isDefinition: true)
56+
!105 = !DIGlobalVariableExpression(var: !104, expr: !DIExpression())

0 commit comments

Comments
 (0)