Skip to content

Commit 5b80101

Browse files
Merge pull request swiftlang#22752 from adrian-prantl/47798056
[dontmerge] Debug Info: Encode Archetype names in DWARF
2 parents f648d97 + 3b7353a commit 5b80101

File tree

8 files changed

+104
-62
lines changed

8 files changed

+104
-62
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ using namespace swift;
2525
using namespace irgen;
2626

2727
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy, Size size,
28-
Alignment align, bool HasDefaultAlignment)
28+
Alignment align, bool HasDefaultAlignment,
29+
bool IsMetadata)
2930
: Type(Ty.getPointer()), StorageType(StorageTy), size(size), align(align),
30-
DefaultAlignment(HasDefaultAlignment) {
31+
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata) {
3132
assert(StorageType && "StorageType is a nullptr");
3233
assert(align.getValue() != 0);
3334
}
@@ -53,7 +54,8 @@ DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty,
5354
size = Size(0);
5455
}
5556
return DebugTypeInfo(Ty.getPointer(), Info.getStorageType(), size,
56-
Info.getBestKnownAlignment(), hasDefaultAlignment(Ty));
57+
Info.getBestKnownAlignment(), hasDefaultAlignment(Ty),
58+
false);
5759
}
5860

5961
DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
@@ -77,7 +79,15 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
7779
DebugTypeInfo DebugTypeInfo::getMetadata(swift::Type Ty, llvm::Type *StorageTy,
7880
Size size, Alignment align) {
7981
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
80-
align, true);
82+
align, true, false);
83+
assert(!DbgTy.isArchetype() && "type metadata cannot contain an archetype");
84+
return DbgTy;
85+
}
86+
87+
DebugTypeInfo DebugTypeInfo::getArchetype(swift::Type Ty, llvm::Type *StorageTy,
88+
Size size, Alignment align) {
89+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
90+
align, true, true);
8191
assert(!DbgTy.isArchetype() && "type metadata cannot contain an archetype");
8292
return DbgTy;
8393
}
@@ -94,8 +104,8 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
94104
if (DeclType->isEqual(LowTy))
95105
Type = DeclType.getPointer();
96106
}
97-
DebugTypeInfo DbgTy(Type, StorageTy, size, align,
98-
hasDefaultAlignment(Type));
107+
DebugTypeInfo DbgTy(Type, StorageTy, size, align, hasDefaultAlignment(Type),
108+
false);
99109
assert(StorageTy && "StorageType is a nullptr");
100110
assert(!DbgTy.isArchetype() &&
101111
"type of global variable cannot be an archetype");
@@ -107,11 +117,18 @@ DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
107117
llvm::Type *StorageType, Size size,
108118
Alignment align) {
109119
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(), StorageType,
110-
size, align, true);
120+
size, align, true, false);
111121
assert(!DbgTy.isArchetype() && "type of objc class cannot be an archetype");
112122
return DbgTy;
113123
}
114124

125+
DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
126+
llvm::Type *StorageType, Size size,
127+
Alignment align) {
128+
assert(StorageType && "StorageType is a nullptr");
129+
return {Ty, StorageType, size, align, true, false};
130+
}
131+
115132
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
116133
return (getType() == T.getType() &&
117134
size == T.size &&

lib/IRGen/DebugTypeInfo.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,23 @@ class DebugTypeInfo {
4646
Size size = Size(0);
4747
Alignment align = Alignment(0);
4848
bool DefaultAlignment = true;
49+
bool IsMetadataType = false;
4950

5051
DebugTypeInfo() {}
5152
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy, Size SizeInBytes,
52-
Alignment AlignInBytes, bool HasDefaultAlignment);
53+
Alignment AlignInBytes, bool HasDefaultAlignment,
54+
bool IsMetadataType);
55+
5356
/// Create type for a local variable.
5457
static DebugTypeInfo getLocalVariable(VarDecl *Decl,
5558
swift::Type Ty, const TypeInfo &Info);
56-
/// Create type for an artificial metadata variable.
59+
/// Create type for global type metadata.
5760
static DebugTypeInfo getMetadata(swift::Type Ty, llvm::Type *StorageTy,
5861
Size size, Alignment align);
62+
/// Create type for an artificial metadata variable.
63+
static DebugTypeInfo getArchetype(swift::Type Ty, llvm::Type *StorageTy,
64+
Size size, Alignment align);
65+
5966
/// Create a standalone type from a TypeInfo object.
6067
static DebugTypeInfo getFromTypeInfo(swift::Type Ty, const TypeInfo &Info);
6168
/// Global variables.
@@ -65,6 +72,9 @@ class DebugTypeInfo {
6572
static DebugTypeInfo getObjCClass(ClassDecl *theClass,
6673
llvm::Type *StorageType, Size size,
6774
Alignment align);
75+
/// Error type.
76+
static DebugTypeInfo getErrorResult(swift::Type Ty, llvm::Type *StorageType,
77+
Size size, Alignment align);
6878

6979
TypeBase *getType() const { return Type; }
7080

@@ -94,7 +104,7 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
94104
static swift::irgen::DebugTypeInfo getTombstoneKey() {
95105
return swift::irgen::DebugTypeInfo(
96106
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
97-
swift::irgen::Size(0), swift::irgen::Alignment(0), false);
107+
swift::irgen::Size(0), swift::irgen::Alignment(0), false, false);
98108
}
99109
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {
100110
return DenseMapInfo<swift::CanType>::getHashValue(Val.getType());

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,27 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
100100
llvm::DenseSet<ModuleDecl *> ImportedModules;
101101

102102
llvm::BumpPtrAllocator DebugInfoNames;
103-
StringRef CWDName; /// The current working directory.
104-
SmallString<0> ConfigMacros; /// User-provided -D macro definitions.
105-
llvm::DICompileUnit *TheCU = nullptr; /// The current compilation unit.
106-
llvm::DIFile *MainFile = nullptr; /// The main file.
107-
llvm::DIModule *MainModule = nullptr; /// The current module.
108-
llvm::DIScope *EntryPointFn =
109-
nullptr; /// Scope of SWIFT_ENTRY_POINT_FUNCTION.
110-
TypeAliasDecl *MetadataTypeDecl; /// The type decl for swift.type.
111-
llvm::DIType *InternalType; /// Catch-all type for opaque internal types.
112-
113-
SILLocation::DebugLoc LastDebugLoc; /// The last location that was emitted.
114-
const SILDebugScope *LastScope; /// The scope of that last location.
103+
/// The current working directory.
104+
StringRef CWDName;
105+
/// User-provided -D macro definitions.
106+
SmallString<0> ConfigMacros;
107+
/// The current compilation unit.
108+
llvm::DICompileUnit *TheCU = nullptr;
109+
/// The main file.
110+
llvm::DIFile *MainFile = nullptr;
111+
/// The current module.
112+
llvm::DIModule *MainModule = nullptr;
113+
/// Scope of SWIFT_ENTRY_POINT_FUNCTION.
114+
llvm::DIScope *EntryPointFn = nullptr;
115+
/// The artificial type decls for named archetypes.
116+
llvm::StringMap<TypeAliasDecl *> MetadataTypeDeclCache;
117+
/// Catch-all type for opaque internal types.
118+
llvm::DIType *InternalType = nullptr;
119+
120+
/// The last location that was emitted.
121+
SILLocation::DebugLoc LastDebugLoc;
122+
/// The scope of that last location.
123+
const SILDebugScope *LastScope = nullptr;
115124

116125
/// Used by pushLoc.
117126
SmallVector<std::pair<SILLocation::DebugLoc, const SILDebugScope *>, 8>
@@ -170,7 +179,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
170179
bool IsLocalToUnit, bool InFixedBuffer,
171180
Optional<SILLocation> Loc);
172181
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
173-
unsigned Depth, unsigned Index, StringRef AssocType);
182+
unsigned Depth, unsigned Index, StringRef ArchetypeName,
183+
StringRef AssocType);
174184

175185
/// Return the DIBuilder.
176186
llvm::DIBuilder &getBuilder() { return DBuilder; }
@@ -704,15 +714,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
704714
return getOrCreateModule(M, TheCU, Name, Path);
705715
}
706716

707-
TypeAliasDecl *getMetadataType() {
708-
if (!MetadataTypeDecl) {
709-
MetadataTypeDecl = new (IGM.Context) TypeAliasDecl(
710-
SourceLoc(), SourceLoc(), IGM.Context.getIdentifier("$swift.type"),
711-
SourceLoc(),
712-
/*genericparams*/ nullptr, IGM.Context.TheBuiltinModule);
713-
MetadataTypeDecl->setUnderlyingType(IGM.Context.TheRawPointerType);
714-
}
715-
return MetadataTypeDecl;
717+
TypeAliasDecl *getMetadataType(StringRef ArchetypeName) {
718+
TypeAliasDecl *&Entry = MetadataTypeDeclCache[ArchetypeName];
719+
if (Entry)
720+
return Entry;
721+
722+
SourceLoc NoLoc;
723+
Entry = new (IGM.Context) TypeAliasDecl(
724+
NoLoc, NoLoc, IGM.Context.getIdentifier(ArchetypeName), NoLoc,
725+
/*genericparams*/ nullptr, IGM.Context.TheBuiltinModule);
726+
Entry->setUnderlyingType(IGM.Context.TheRawPointerType);
727+
return Entry;
716728
}
717729

718730
/// Return the DIFile that is the ancestor of Scope.
@@ -765,8 +777,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
765777
#endif
766778

767779
StringRef getMangledName(DebugTypeInfo DbgTy) {
768-
if (MetadataTypeDecl && DbgTy.getDecl() == MetadataTypeDecl)
769-
return BumpAllocatedString(DbgTy.getDecl()->getName().str());
780+
if (DbgTy.IsMetadataType)
781+
return MetadataTypeDeclCache.find(DbgTy.getDecl()->getName().str())
782+
->getKey();
770783

771784
Type Ty = DbgTy.getType();
772785
if (!Ty->hasTypeParameter())
@@ -925,9 +938,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
925938
// one of the raw type as long as it is large enough to hold
926939
// all enum values. Use the raw type for the debug type, but
927940
// the storage size from the enum.
928-
ElemDbgTy =
929-
DebugTypeInfo(ED->getRawType(),
930-
DbgTy.StorageType, DbgTy.size, DbgTy.align, true);
941+
ElemDbgTy = DebugTypeInfo(ED->getRawType(), DbgTy.StorageType,
942+
DbgTy.size, DbgTy.align, true, false);
931943
else if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
932944
// A discriminated union. This should really be described as a
933945
// DW_TAG_variant_type. For now only describing the data.
@@ -938,8 +950,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
938950
// Discriminated union case without argument. Fallback to Int
939951
// as the element type; there is no storage here.
940952
Type IntTy = IGM.Context.getIntDecl()->getDeclaredType();
941-
ElemDbgTy = DebugTypeInfo(
942-
IntTy, DbgTy.StorageType, Size(0), Alignment(1), true);
953+
ElemDbgTy = DebugTypeInfo(IntTy, DbgTy.StorageType, Size(0),
954+
Alignment(1), true, false);
943955
}
944956
unsigned Offset = 0;
945957
auto MTy = createMemberType(ElemDbgTy, ElemDecl->getName().str(), Offset,
@@ -979,8 +991,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
979991
}
980992

981993
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy) {
982-
DebugTypeInfo BlandDbgTy(
983-
Ty, DbgTy.StorageType, DbgTy.size, DbgTy.align, DbgTy.DefaultAlignment);
994+
DebugTypeInfo BlandDbgTy(Ty, DbgTy.StorageType, DbgTy.size, DbgTy.align,
995+
DbgTy.DefaultAlignment, DbgTy.IsMetadataType);
984996
return getOrCreateType(BlandDbgTy);
985997
}
986998

@@ -1454,9 +1466,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
14541466
auto File = getOrCreateFile(L.Filename);
14551467
// For TypeAlias types, the DeclContext for the aliased type is
14561468
// in the decl of the alias type.
1457-
DebugTypeInfo AliasedDbgTy(
1458-
AliasedTy,
1459-
DbgTy.StorageType, DbgTy.size, DbgTy.align, DbgTy.DefaultAlignment);
1469+
DebugTypeInfo AliasedDbgTy(AliasedTy, DbgTy.StorageType, DbgTy.size,
1470+
DbgTy.align, DbgTy.DefaultAlignment,
1471+
false);
14601472
return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
14611473
File, L.Line, Scope);
14621474
}
@@ -1637,8 +1649,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
16371649
llvm::Module &M,
16381650
StringRef MainOutputFilenameForDebugInfo)
16391651
: Opts(Opts), CI(CI), SM(IGM.Context.SourceMgr), M(M), DBuilder(M),
1640-
IGM(IGM), DebugPrefixMap(Opts.DebugPrefixMap), MetadataTypeDecl(nullptr),
1641-
InternalType(nullptr), LastDebugLoc({}), LastScope(nullptr) {
1652+
IGM(IGM), DebugPrefixMap(Opts.DebugPrefixMap) {
16421653
assert(Opts.DebugInfoLevel > IRGenDebugInfoLevel::None &&
16431654
"no debug info should be generated");
16441655
llvm::SmallString<256> SourcePath;
@@ -2244,7 +2255,9 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
22442255

22452256
void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
22462257
llvm::Value *Metadata, unsigned Depth,
2247-
unsigned Index, StringRef AssocType) {
2258+
unsigned Index,
2259+
StringRef ArchetypeName,
2260+
StringRef AssocType) {
22482261
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
22492262
return;
22502263

@@ -2254,11 +2267,11 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
22542267
return;
22552268

22562269
llvm::SmallString<8> Buf;
2257-
static const char *Tau = u8"\u03C4_";
2270+
static const char *Tau = u8"\u03C4";
22582271
llvm::raw_svector_ostream OS(Buf);
2259-
OS << '$' << Tau << Depth << '_' << Index << AssocType;
2260-
auto DbgTy = DebugTypeInfo::getMetadata(
2261-
getMetadataType()->getDeclaredInterfaceType().getPointer(),
2272+
OS << '$' << Tau << '_' << Depth << '_' << Index << AssocType;
2273+
auto DbgTy = DebugTypeInfo::getArchetype(
2274+
getMetadataType(ArchetypeName)->getDeclaredInterfaceType().getPointer(),
22622275
Metadata->getType(), Size(CI.getTargetInfo().getPointerWidth(0)),
22632276
Alignment(CI.getTargetInfo().getPointerAlign(0)));
22642277
emitVariableDeclaration(IGF.Builder, Metadata, DbgTy, IGF.getDebugScope(),
@@ -2381,9 +2394,10 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(
23812394

23822395
void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
23832396
unsigned Depth, unsigned Index,
2397+
StringRef ArchetypeName,
23842398
StringRef AssocType) {
23852399
static_cast<IRGenDebugInfoImpl *>(this)->emitTypeMetadata(
2386-
IGF, Metadata, Depth, Index, AssocType);
2400+
IGF, Metadata, Depth, Index, ArchetypeName, AssocType);
23872401
}
23882402

23892403
llvm::DIBuilder &IRGenDebugInfo::getBuilder() {

lib/IRGen/IRGenDebugInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class IRGenDebugInfo {
149149

150150
/// Emit debug metadata for type metadata (for generic types). So meta.
151151
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
152-
unsigned Depth, unsigned Index, StringRef AssocType);
152+
unsigned Depth, unsigned Index, StringRef ArchetypeName,
153+
StringRef AssocType);
153154

154155
/// Return the DIBuilder.
155156
llvm::DIBuilder &getBuilder();

lib/IRGen/IRGenSIL.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,12 +3645,12 @@ void IRGenSILFunction::emitErrorResultVar(SILResultInfo ErrorInfo,
36453645
Var->Name, Var->ArgNo, false);
36463646
if (!IGM.DebugInfo)
36473647
return;
3648-
DebugTypeInfo DTI(ErrorInfo.getType(),
3649-
ErrorResultSlot->getType(), IGM.getPointerSize(),
3650-
IGM.getPointerAlignment(), true);
3651-
IGM.DebugInfo->emitVariableDeclaration(Builder, Storage, DTI, getDebugScope(),
3652-
nullptr, Var->Name, Var->ArgNo,
3653-
IndirectValue, ArtificialValue);
3648+
auto DbgTy = DebugTypeInfo::getErrorResult(
3649+
ErrorInfo.getType(), ErrorResultSlot->getType(), IGM.getPointerSize(),
3650+
IGM.getPointerAlignment());
3651+
IGM.DebugInfo->emitVariableDeclaration(
3652+
Builder, Storage, DbgTy, getDebugScope(), nullptr, Var->Name, Var->ArgNo,
3653+
IndirectValue, ArtificialValue);
36543654
}
36553655

36563656
void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {

lib/IRGen/LocalTypeData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static void maybeEmitDebugInfoForLocalTypeData(IRGenFunction &IGF,
366366
}
367367
auto *typeParam = cast<GenericTypeParamType>(oocTy);
368368
IGF.IGM.DebugInfo->emitTypeMetadata(IGF, data, typeParam->getDepth(),
369-
typeParam->getIndex(), AssocType);
369+
typeParam->getIndex(), name, AssocType);
370370
}
371371

372372
void

test/DebugInfo/protocol-extension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public extension P {
1717

1818
// CHECK: ![[SELFMETA]] = !DILocalVariable(name: "$\CF\84_0_0",
1919
// CHECK-SAME: type: ![[SELFTY:[0-9]+]], flags: DIFlagArtificial)
20-
// CHECK: ![[SELFTY]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$swift.type"
20+
// CHECK: ![[SELFTY]] = !DIDerivedType(tag: DW_TAG_typedef, name: "Self"

test/DebugInfo/typearg.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AClass : AProtocol {
1212
// CHECK: ![[TYPEARG]] = !DILocalVariable(name: "$\CF\84_0_0"
1313
// CHECK-SAME: type: ![[SWIFTMETATYPE:[^,)]+]]
1414
// CHECK-SAME: flags: DIFlagArtificial
15-
// CHECK: ![[SWIFTMETATYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$swift.type",
15+
// CHECK: ![[SWIFTMETATYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "T",
1616
// CHECK-SAME: baseType: ![[VOIDPTR:[0-9]+]]
1717
// CHECK: ![[VOIDPTR]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "$sBpD", baseType: null
1818
func aFunction<T : AProtocol>(_ x: T) {

0 commit comments

Comments
 (0)