Skip to content

Commit dc959a4

Browse files
authored
Merge pull request #71332 from augusto2112/emit-class-deb
Generate full debug info for special builtins and classes
2 parents b676cc6 + cc0c488 commit dc959a4

File tree

4 files changed

+101
-44
lines changed

4 files changed

+101
-44
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "IRGenDebugInfo.h"
18+
#include "GenEnum.h"
1819
#include "GenOpaque.h"
1920
#include "GenStruct.h"
2021
#include "GenType.h"
@@ -1078,8 +1079,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10781079
llvm::dwarf::DW_TAG_structure_type, UniqueID, Scope, File, Line,
10791080
llvm::dwarf::DW_LANG_Swift, SizeInBits, 0);
10801081
}
1081-
if (OffsetInBits > SizeInBits)
1082-
SizeInBits = OffsetInBits;
10831082

10841083
auto DITy = DBuilder.createStructType(
10851084
Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, DerivedFrom,
@@ -1525,7 +1524,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
15251524
? 0
15261525
: DbgTy.getAlignment().getValue() * SizeOfByte;
15271526
unsigned Encoding = 0;
1528-
uint32_t NumExtraInhabitants = 0;
1527+
uint32_t NumExtraInhabitants = DbgTy.getNumExtraInhabitants().value_or(0);
1528+
15291529
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
15301530

15311531
TypeBase *BaseTy = DbgTy.getType();
@@ -1549,17 +1549,13 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
15491549
Encoding = llvm::dwarf::DW_ATE_unsigned;
15501550
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
15511551
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
1552-
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
1553-
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
15541552
break;
15551553
}
15561554

15571555
case TypeKind::BuiltinIntegerLiteral: {
15581556
Encoding = llvm::dwarf::DW_ATE_unsigned; // ?
15591557
if (auto CompletedDbgTy = CompletedDebugTypeInfo::get(DbgTy))
15601558
SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
1561-
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
1562-
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
15631559
break;
15641560
}
15651561

@@ -1568,48 +1564,30 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
15681564
// Assuming that the bitwidth and FloatTy->getFPKind() are identical.
15691565
SizeInBits = FloatTy->getBitWidth();
15701566
Encoding = llvm::dwarf::DW_ATE_float;
1571-
if (auto DbgTyNumExtraInhabitants = DbgTy.getNumExtraInhabitants())
1572-
NumExtraInhabitants = *DbgTyNumExtraInhabitants;
15731567
break;
15741568
}
15751569

1576-
case TypeKind::BuiltinNativeObject: {
1577-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1578-
auto PTy = DBuilder.createPointerType(nullptr, PtrSize, 0,
1579-
/* DWARFAddressSpace */ llvm::None,
1580-
MangledName);
1581-
return DBuilder.createObjectPointerType(PTy);
1582-
}
1583-
1584-
case TypeKind::BuiltinBridgeObject: {
1585-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1586-
auto PTy = DBuilder.createPointerType(nullptr, PtrSize, 0,
1587-
/* DWARFAddressSpace */ llvm::None,
1588-
MangledName);
1589-
return DBuilder.createObjectPointerType(PTy);
1590-
}
1591-
1592-
case TypeKind::BuiltinRawPointer: {
1593-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1594-
return DBuilder.createPointerType(nullptr, PtrSize, 0,
1595-
/* DWARFAddressSpace */ llvm::None,
1596-
MangledName);
1597-
}
1570+
case TypeKind::BuiltinNativeObject:
1571+
case TypeKind::BuiltinBridgeObject:
1572+
case TypeKind::BuiltinRawPointer:
1573+
case TypeKind::BuiltinRawUnsafeContinuation:
1574+
case TypeKind::BuiltinJob: {
1575+
unsigned PtrSize =
1576+
CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1577+
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
1578+
Flags |= llvm::DINode::FlagArtificial;
1579+
llvm::DICompositeType *PTy = DBuilder.createStructType(
1580+
Scope, MangledName, File, 0, PtrSize, 0, Flags, nullptr, nullptr,
1581+
llvm::dwarf::DW_LANG_Swift, nullptr, {}, NumExtraInhabitants);
1582+
return PTy;
15981583

1599-
case TypeKind::BuiltinRawUnsafeContinuation: {
1600-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1601-
return DBuilder.createPointerType(nullptr, PtrSize, 0,
1602-
/* DWARFAddressSpace */ llvm::None,
1603-
MangledName);
1604-
}
1584+
}
1585+
llvm::DIDerivedType *PTy = DBuilder.createPointerType(
1586+
nullptr, PtrSize, 0,
1587+
/* DWARFAddressSpace */ llvm::None, MangledName);
16051588

1606-
case TypeKind::BuiltinJob: {
1607-
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
1608-
return DBuilder.createPointerType(nullptr, PtrSize, 0,
1609-
/* DWARFAddressSpace */ llvm::None,
1610-
MangledName);
1589+
return DBuilder.createObjectPointerType(PTy);
16111590
}
1612-
16131591
case TypeKind::BuiltinExecutor: {
16141592
return createDoublePointerSizedStruct(
16151593
Scope, "Builtin.Executor", nullptr, MainFile, 0,
@@ -1663,6 +1641,25 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
16631641
unsigned FwdDeclLine = 0;
16641642
assert(SizeInBits ==
16651643
CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
1644+
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
1645+
auto *DIType = createStructType(
1646+
DbgTy, Decl, ClassTy, Scope, File, L.Line, SizeInBits, AlignInBits,
1647+
Flags, nullptr, llvm::dwarf::DW_LANG_Swift, MangledName);
1648+
assert(DIType && "Unexpected null DIType!");
1649+
assert(DIType && "createStructType should never return null!");
1650+
auto SuperClassTy = ClassTy->getSuperclass();
1651+
if (SuperClassTy) {
1652+
auto SuperClassDbgTy = DebugTypeInfo::getFromTypeInfo(
1653+
SuperClassTy, IGM.getTypeInfoForUnlowered(SuperClassTy), IGM,
1654+
false);
1655+
1656+
llvm::DIType *SuperClassDITy = getOrCreateType(SuperClassDbgTy);
1657+
assert(SuperClassDITy && "getOrCreateType should never return null!");
1658+
DBuilder.retainType(DBuilder.createInheritance(
1659+
DIType, SuperClassDITy, 0, 0, llvm::DINode::FlagZero));
1660+
}
1661+
return DIType;
1662+
}
16661663
return createPointerSizedStruct(Scope, Decl->getNameStr(), L.File,
16671664
FwdDeclLine, Flags, MangledName);
16681665
}
@@ -2037,6 +2034,20 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
20372034
}
20382035
#endif
20392036

2037+
/// Emits the special builtin types into the debug info. These types are the
2038+
/// ones that are unconditionally emitted into the stdlib's metadata and are
2039+
/// needed to correctly calculate the layout of more complex types built on
2040+
/// top of them.
2041+
void createSpecialStlibBuiltinTypes() {
2042+
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes)
2043+
return;
2044+
for (auto BuiltinType: IGM.getOrCreateSpecialStlibBuiltinTypes()) {
2045+
auto DbgTy = DebugTypeInfo::getFromTypeInfo(
2046+
BuiltinType, IGM.getTypeInfoForUnlowered(BuiltinType), IGM, false);
2047+
DBuilder.retainType(getOrCreateType(DbgTy));
2048+
}
2049+
}
2050+
20402051
llvm::DIType *getOrCreateType(DebugTypeInfo DbgTy) {
20412052
// Is this an empty type?
20422053
if (DbgTy.isNull())
@@ -2083,6 +2094,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
20832094
TypeDecl = ND;
20842095
Context = ND->getParent();
20852096
ClangDecl = ND->getClangDecl();
2097+
} else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType())) {
2098+
Context = BNO->getASTContext().TheBuiltinModule;
20862099
}
20872100
if (ClangDecl) {
20882101
clang::ASTReader &Reader = *CI.getClangInstance().getASTReader();
@@ -2259,6 +2272,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
22592272
}
22602273
OS << '"';
22612274
}
2275+
createSpecialStlibBuiltinTypes();
22622276
}
22632277

22642278
void IRGenDebugInfoImpl::finalize() {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-swift-frontend %s -Onone -emit-ir -gdwarf-types -o - | %FileCheck %s
2+
3+
// File is empty as this test check for the builtin stdlib types that should
4+
// always be emitted.
5+
6+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$sBoD",
7+
// CHECK-SAME: size: 64, num_extra_inhabitants: {{2147483647|4096}}, flags: DIFlagArtificial,
8+
// CHECK-SAME: runtimeLang: DW_LANG_Swift)
9+
10+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$syXlD",
11+
// CHECK-SAME: size: 64,
12+
13+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$sBbD",
14+
// CHECK-SAME: size: 64, num_extra_inhabitants: {{2147483647|4096}}, flags: DIFlagArtificial,
15+
// CHECK-SAME: runtimeLang: DW_LANG_Swift)
16+
17+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$sBpD",
18+
// CHECK-SAME: size: 64, num_extra_inhabitants: 1, flags: DIFlagArtificial,
19+
// CHECK-SAME: runtimeLang: DW_LANG_Swift)
20+
21+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$syyXfD",
22+
// CHECK-SAME: size: 64,
23+
// CHECK-SAME: runtimeLang: DW_LANG_Swift
24+
25+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$sypXpD", size: 64,
26+
// CHECK-SAME: flags: DIFlagArtificial, runtimeLang: DW_LANG_Swift, identifier: "$sypXpD")
27+
28+

test/DebugInfo/EagerTypeMetadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class C<T>
1313
}
1414
// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "T",
1515
// CHECK-SAME: baseType: ![[PTRTY:[0-9]+]]
16-
// CHECK: ![[PTRTY]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "$sBpD", baseType: null, size: {{64|32}})
16+
// CHECK: ![[PTRTY]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "$sBpD", baseType: null, size: {{64|32}}, flags: DIFlagArtificial | DIFlagObjectPointer)
1717
// CHECK: ![[LOC]] = !DILocation(line: 0,
1818

test/DebugInfo/classes.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -gdwarf-types -o - | %FileCheck %s
2+
3+
class SomeClass {
4+
let first = 4
5+
let second = "Hello"
6+
}
7+
8+
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "SomeClass",
9+
// CHECK-SAME: size: 64, elements:
10+
// CHECK-SAME: runtimeLang: DW_LANG_Swift, identifier: "$s7classes9SomeClassCD")
11+
12+
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "first",
13+
// CHECK-SAME: size: 64)
14+
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "second",
15+
// CHECK-SAME: size: 128, offset: 64)

0 commit comments

Comments
 (0)