Skip to content

Commit 54af8e4

Browse files
committed
Debug Info: Encode Archetype names in DWARF
Currently LLDB calls into ide::getDeclFromMangledSymbolName() to get to this information and we would like to get rid of this call. rdar://problem/47798056
1 parent af43ae7 commit 54af8e4

File tree

8 files changed

+85
-50
lines changed

8 files changed

+85
-50
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: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
107107
llvm::DIModule *MainModule = nullptr; /// The current module.
108108
llvm::DIScope *EntryPointFn =
109109
nullptr; /// Scope of SWIFT_ENTRY_POINT_FUNCTION.
110-
TypeAliasDecl *MetadataTypeDecl; /// The type decl for swift.type.
110+
/// The artificial type decls for named archetypes.
111+
llvm::StringMap<TypeAliasDecl *> MetadataTypeDeclCache;
111112
llvm::DIType *InternalType; /// Catch-all type for opaque internal types.
112113

113114
SILLocation::DebugLoc LastDebugLoc; /// The last location that was emitted.
@@ -170,7 +171,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
170171
bool IsLocalToUnit, bool InFixedBuffer,
171172
Optional<SILLocation> Loc);
172173
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
173-
unsigned Depth, unsigned Index, StringRef AssocType);
174+
unsigned Depth, unsigned Index, StringRef ArchetypeName,
175+
StringRef AssocType);
174176

175177
/// Return the DIBuilder.
176178
llvm::DIBuilder &getBuilder() { return DBuilder; }
@@ -704,15 +706,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
704706
return getOrCreateModule(M, TheCU, Name, Path);
705707
}
706708

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;
709+
TypeAliasDecl *getMetadataType(StringRef ArchetypeName) {
710+
TypeAliasDecl *&Entry = MetadataTypeDeclCache[ArchetypeName];
711+
if (Entry)
712+
return Entry;
713+
714+
SourceLoc NoLoc;
715+
Entry = new (IGM.Context) TypeAliasDecl(
716+
NoLoc, NoLoc, IGM.Context.getIdentifier(ArchetypeName), NoLoc,
717+
/*genericparams*/ nullptr, IGM.Context.TheBuiltinModule);
718+
Entry->setUnderlyingType(IGM.Context.TheRawPointerType);
719+
return Entry;
716720
}
717721

718722
/// Return the DIFile that is the ancestor of Scope.
@@ -765,8 +769,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
765769
#endif
766770

767771
StringRef getMangledName(DebugTypeInfo DbgTy) {
768-
if (MetadataTypeDecl && DbgTy.getDecl() == MetadataTypeDecl)
769-
return BumpAllocatedString(DbgTy.getDecl()->getName().str());
772+
if (DbgTy.IsMetadataType)
773+
return MetadataTypeDeclCache.find(DbgTy.getDecl()->getName().str())
774+
->getKey();
770775

771776
Type Ty = DbgTy.getType();
772777
if (!Ty->hasTypeParameter())
@@ -925,9 +930,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
925930
// one of the raw type as long as it is large enough to hold
926931
// all enum values. Use the raw type for the debug type, but
927932
// the storage size from the enum.
928-
ElemDbgTy =
929-
DebugTypeInfo(ED->getRawType(),
930-
DbgTy.StorageType, DbgTy.size, DbgTy.align, true);
933+
ElemDbgTy = DebugTypeInfo(ED->getRawType(), DbgTy.StorageType,
934+
DbgTy.size, DbgTy.align, true, false);
931935
else if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
932936
// A discriminated union. This should really be described as a
933937
// DW_TAG_variant_type. For now only describing the data.
@@ -938,8 +942,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
938942
// Discriminated union case without argument. Fallback to Int
939943
// as the element type; there is no storage here.
940944
Type IntTy = IGM.Context.getIntDecl()->getDeclaredType();
941-
ElemDbgTy = DebugTypeInfo(
942-
IntTy, DbgTy.StorageType, Size(0), Alignment(1), true);
945+
ElemDbgTy = DebugTypeInfo(IntTy, DbgTy.StorageType, Size(0),
946+
Alignment(1), true, false);
943947
}
944948
unsigned Offset = 0;
945949
auto MTy = createMemberType(ElemDbgTy, ElemDecl->getName().str(), Offset,
@@ -979,8 +983,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
979983
}
980984

981985
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy) {
982-
DebugTypeInfo BlandDbgTy(
983-
Ty, DbgTy.StorageType, DbgTy.size, DbgTy.align, DbgTy.DefaultAlignment);
986+
DebugTypeInfo BlandDbgTy(Ty, DbgTy.StorageType, DbgTy.size, DbgTy.align,
987+
DbgTy.DefaultAlignment, DbgTy.IsMetadataType);
984988
return getOrCreateType(BlandDbgTy);
985989
}
986990

@@ -1454,9 +1458,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
14541458
auto File = getOrCreateFile(L.Filename);
14551459
// For TypeAlias types, the DeclContext for the aliased type is
14561460
// in the decl of the alias type.
1457-
DebugTypeInfo AliasedDbgTy(
1458-
AliasedTy,
1459-
DbgTy.StorageType, DbgTy.size, DbgTy.align, DbgTy.DefaultAlignment);
1461+
DebugTypeInfo AliasedDbgTy(AliasedTy, DbgTy.StorageType, DbgTy.size,
1462+
DbgTy.align, DbgTy.DefaultAlignment,
1463+
false);
14601464
return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
14611465
File, L.Line, Scope);
14621466
}
@@ -1637,7 +1641,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
16371641
llvm::Module &M,
16381642
StringRef MainOutputFilenameForDebugInfo)
16391643
: Opts(Opts), CI(CI), SM(IGM.Context.SourceMgr), M(M), DBuilder(M),
1640-
IGM(IGM), DebugPrefixMap(Opts.DebugPrefixMap), MetadataTypeDecl(nullptr),
1644+
IGM(IGM), DebugPrefixMap(Opts.DebugPrefixMap),
16411645
InternalType(nullptr), LastDebugLoc({}), LastScope(nullptr) {
16421646
assert(Opts.DebugInfoLevel > IRGenDebugInfoLevel::None &&
16431647
"no debug info should be generated");
@@ -2244,7 +2248,9 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
22442248

22452249
void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
22462250
llvm::Value *Metadata, unsigned Depth,
2247-
unsigned Index, StringRef AssocType) {
2251+
unsigned Index,
2252+
StringRef ArchetypeName,
2253+
StringRef AssocType) {
22482254
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
22492255
return;
22502256

@@ -2254,11 +2260,11 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
22542260
return;
22552261

22562262
llvm::SmallString<8> Buf;
2257-
static const char *Tau = u8"\u03C4_";
2263+
static const char *Tau = u8"\u03C4";
22582264
llvm::raw_svector_ostream OS(Buf);
2259-
OS << '$' << Tau << Depth << '_' << Index << AssocType;
2260-
auto DbgTy = DebugTypeInfo::getMetadata(
2261-
getMetadataType()->getDeclaredInterfaceType().getPointer(),
2265+
OS << '$' << Tau << '_' << Depth << '_' << Index << AssocType;
2266+
auto DbgTy = DebugTypeInfo::getArchetype(
2267+
getMetadataType(ArchetypeName)->getDeclaredInterfaceType().getPointer(),
22622268
Metadata->getType(), Size(CI.getTargetInfo().getPointerWidth(0)),
22632269
Alignment(CI.getTargetInfo().getPointerAlign(0)));
22642270
emitVariableDeclaration(IGF.Builder, Metadata, DbgTy, IGF.getDebugScope(),
@@ -2381,9 +2387,10 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(
23812387

23822388
void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
23832389
unsigned Depth, unsigned Index,
2390+
StringRef ArchetypeName,
23842391
StringRef AssocType) {
23852392
static_cast<IRGenDebugInfoImpl *>(this)->emitTypeMetadata(
2386-
IGF, Metadata, Depth, Index, AssocType);
2393+
IGF, Metadata, Depth, Index, ArchetypeName, AssocType);
23872394
}
23882395

23892396
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)