Skip to content

Commit b52ef8b

Browse files
Merge pull request #16937 from adrian-prantl/38306256
Use depth and index to lookup type metadata artificial variables
2 parents 5672c69 + bab3000 commit b52ef8b

27 files changed

+135
-122
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ void ASTMangler::appendType(Type type) {
889889
case TypeKind::Archetype: {
890890
auto *archetype = cast<ArchetypeType>(tybase);
891891

892-
assert(DWARFMangling && "Cannot mangle free-standing archetypes");
892+
assert(false && DWARFMangling && "Cannot mangle free-standing archetypes");
893893

894894
// Mangle the associated type of a parent archetype.
895895
if (auto parent = archetype->getParent()) {

lib/IDE/TypeReconstruction.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,28 @@ static void VisitNodeDestructor(
10051005
}
10061006
}
10071007

1008+
static void VisitNodeDependentMember(ASTContext *ast,
1009+
Demangle::NodePointer cur_node,
1010+
VisitNodeResult &result) {
1011+
if (cur_node->getNumChildren() == 2) {
1012+
auto dep = cur_node->getChild(0);
1013+
auto assoc = cur_node->getChild(1);
1014+
VisitNodeResult dependency;
1015+
if (dep->getKind() == Demangle::Node::Kind::Type &&
1016+
assoc->getKind() == Demangle::Node::Kind::DependentAssociatedTypeRef) {
1017+
VisitNode(ast, dep, dependency);
1018+
if (dependency._types.size() == 1 && assoc->hasText()) {
1019+
Identifier name = ast->getIdentifier(assoc->getText());
1020+
result._types.push_back(
1021+
DependentMemberType::get(dependency._types[0], name));
1022+
return;
1023+
}
1024+
}
1025+
}
1026+
result._error = "bad dependent member type";
1027+
}
1028+
1029+
10081030
static Demangle::NodePointer DropGenericSignature(
10091031
Demangle::NodePointer cur_node) {
10101032
if (cur_node->getKind() != Demangle::Node::Kind::DependentGenericType)
@@ -2285,6 +2307,10 @@ static void VisitNode(
22852307
VisitNodeConstructor(ast, node, result);
22862308
break;
22872309

2310+
case Demangle::Node::Kind::DependentMemberType:
2311+
VisitNodeDependentMember(ast, node, result);
2312+
break;
2313+
22882314
case Demangle::Node::Kind::Destructor:
22892315
VisitNodeDestructor(ast, node, result);
22902316
break;

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(DeclContext *DC,
6666
const TypeInfo &Info,
6767
bool Unwrap) {
6868

69-
auto DeclType = (Decl->hasType()
70-
? Decl->getType()
71-
: Decl->getDeclContext()->mapTypeIntoContext(
72-
Decl->getInterfaceType()));
69+
auto DeclType =
70+
Decl->hasInterfaceType() ? Decl->getInterfaceType() : Decl->getType();
7371
auto RealType = Ty;
7472
if (Unwrap) {
7573
DeclType = DeclType->getInOutObjectType();
@@ -118,7 +116,7 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
118116
hasDefaultAlignment(Type));
119117
assert(StorageTy && "StorageType is a nullptr");
120118
assert(!DbgTy.isArchetype() &&
121-
"type of a global var cannot contain an archetype");
119+
"type of global variable cannot be an archetype");
122120
assert(align.getValue() != 0);
123121
return DbgTy;
124122
}
@@ -129,8 +127,7 @@ DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
129127
DebugTypeInfo DbgTy(nullptr, nullptr,
130128
theClass->getInterfaceType().getPointer(), StorageType,
131129
size, align, true);
132-
assert(!DbgTy.isArchetype() &&
133-
"type of an objc class cannot contain an archetype");
130+
assert(!DbgTy.isArchetype() && "type of objc class cannot be an archetype");
134131
return DbgTy;
135132
}
136133

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
157157
bool IsLocalToUnit, bool InFixedBuffer,
158158
Optional<SILLocation> Loc);
159159
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
160-
StringRef Name);
160+
unsigned Depth, unsigned Index, StringRef AssocType);
161161

162162
/// Return the DIBuilder.
163163
llvm::DIBuilder &getBuilder() { return DBuilder; }
@@ -616,9 +616,13 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
616616
if (MetadataTypeDecl && DbgTy.getDecl() == MetadataTypeDecl)
617617
return BumpAllocatedString(DbgTy.getDecl()->getName().str());
618618

619+
Type Ty = DbgTy.getType();
620+
if (!Ty->hasTypeParameter())
621+
Ty = Ty->mapTypeOutOfContext();
622+
619623
Mangle::ASTMangler Mangler;
620624
std::string Name = Mangler.mangleTypeForDebugger(
621-
DbgTy.getType(), DbgTy.getDeclContext(), DbgTy.getGenericEnvironment());
625+
Ty, DbgTy.getDeclContext(), DbgTy.getGenericEnvironment());
622626
return BumpAllocatedString(Name);
623627
}
624628

@@ -1308,15 +1312,13 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13081312
return getOrCreateDesugaredType(CanTy, DbgTy);
13091313
}
13101314

1315+
case TypeKind::DependentMember:
13111316
case TypeKind::GenericTypeParam: {
1312-
auto *ParamTy = cast<GenericTypeParamType>(BaseTy);
1313-
// FIXME: Provide a more meaningful debug type.
1314-
return DBuilder.createUnspecifiedType(ParamTy->getName().str());
1315-
}
1316-
case TypeKind::DependentMember: {
1317-
auto *MemberTy = cast<DependentMemberType>(BaseTy);
13181317
// FIXME: Provide a more meaningful debug type.
1319-
return DBuilder.createUnspecifiedType(MemberTy->getName().str());
1318+
return DBuilder.createStructType(
1319+
Scope, MangledName, File, 0, SizeInBits, AlignInBits, Flags,
1320+
nullptr, nullptr,
1321+
llvm::dwarf::DW_LANG_Swift, nullptr, MangledName);
13201322
}
13211323

13221324
// The following types exist primarily for internal use by the type
@@ -2028,8 +2030,8 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
20282030
}
20292031

20302032
void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
2031-
llvm::Value *Metadata,
2032-
StringRef Name) {
2033+
llvm::Value *Metadata, unsigned Depth,
2034+
unsigned Index, StringRef AssocType) {
20332035
if (Opts.DebugInfoKind <= IRGenDebugInfoKind::LineTables)
20342036
return;
20352037

@@ -2038,12 +2040,16 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
20382040
if (!DS || DS->getInlinedFunction()->isTransparent())
20392041
return;
20402042

2041-
auto TName = BumpAllocatedString(("$swift.type." + Name).str());
2043+
llvm::SmallString<8> Buf;
2044+
static const char *Tau = u8"\u03C4_";
2045+
llvm::raw_svector_ostream OS(Buf);
2046+
OS << '$' << Tau << Depth << '_' << Index << AssocType;
20422047
auto DbgTy = DebugTypeInfo::getMetadata(
20432048
getMetadataType()->getDeclaredInterfaceType().getPointer(),
20442049
Metadata->getType(), Size(CI.getTargetInfo().getPointerWidth(0)),
20452050
Alignment(CI.getTargetInfo().getPointerAlign(0)));
2046-
emitVariableDeclaration(IGF.Builder, Metadata, DbgTy, DS, nullptr, TName, 0,
2051+
emitVariableDeclaration(IGF.Builder, Metadata, DbgTy, IGF.getDebugScope(),
2052+
nullptr, OS.str().str(), 0,
20472053
// swift.type is already a pointer type,
20482054
// having a shadow copy doesn't add another
20492055
// layer of indirection.
@@ -2155,9 +2161,10 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(
21552161
}
21562162

21572163
void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
2158-
StringRef Name) {
2159-
static_cast<IRGenDebugInfoImpl *>(this)->emitTypeMetadata(IGF, Metadata,
2160-
Name);
2164+
unsigned Depth, unsigned Index,
2165+
StringRef AssocType) {
2166+
static_cast<IRGenDebugInfoImpl *>(this)->emitTypeMetadata(
2167+
IGF, Metadata, Depth, Index, AssocType);
21612168
}
21622169

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

lib/IRGen/IRGenDebugInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class IRGenDebugInfo {
147147

148148
/// Emit debug metadata for type metadata (for generic types). So meta.
149149
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
150-
StringRef Name);
150+
unsigned Depth, unsigned Index, StringRef AssocType);
151151

152152
/// Return the DIBuilder.
153153
llvm::DIBuilder &getBuilder();

lib/IRGen/LocalTypeData.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,18 @@ static void maybeEmitDebugInfoForLocalTypeData(IRGenFunction &IGF,
353353
return;
354354

355355
// Emit debug info for the metadata.
356-
IGF.IGM.DebugInfo->emitTypeMetadata(IGF, data, name);
356+
llvm::SmallString<8> AssocType;
357+
auto *oocTy = type->mapTypeOutOfContext().getPointer();
358+
{
359+
llvm::raw_svector_ostream OS(AssocType);
360+
while (auto *dependentMemberType = dyn_cast<DependentMemberType>(oocTy)) {
361+
OS << '.' << dependentMemberType->getName();
362+
oocTy = dependentMemberType->getBase().getPointer();
363+
}
364+
}
365+
auto *typeParam = cast<GenericTypeParamType>(oocTy);
366+
IGF.IGM.DebugInfo->emitTypeMetadata(IGF, data, typeParam->getDepth(),
367+
typeParam->getIndex(), AssocType);
357368
}
358369

359370
void

test/DebugInfo/DumpTypeFromMangledName.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extension Collection where Element: Equatable {
1818
var results = [SubSequence]()
1919
return results
2020
}
21+
func foo(_ x: Iterator.Element) {
22+
print(x)
23+
}
2124
}
2225

2326
class Foo<T> {

test/DebugInfo/Inputs/type-reconstr-names.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ $S4blah4mainyyF8PatatinoL_VMa ---> Can't resolve type of $S4blah4mainyyF8Patatin
55
$Ss10CollectionP7Element ---> Can't resolve type of $Ss10CollectionP7Element
66
$Ss15ContiguousArrayV9formIndex5afterySiz_tFSS_Tg5 ---> (inout Int) -> ()
77
$S12TypeReconstr8PatatinoaySiGD ---> Patatino<Int>
8+
$S7ElementQzD ---> τ_0_0.Element
89
$S13EyeCandySwift21_previousUniqueNumber33_ADC08935D64EA4F796440E7335798735LLs6UInt64Vvp -> UInt64

test/DebugInfo/archetype.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ func ExistentialTuple<T: RandomAccessIndex>(_ x: T, y: T) -> T.Distance {
2222
return _overflowChecked((tmp.0, tmp.1))
2323
}
2424
// CHECK: ![[TT]] = !DICompositeType(tag: DW_TAG_structure_type,
25-
// CHECK-SAME: name: "$S9archetype16ExistentialTuple_1y8Distance
25+
// CHECK-SAME: name: "$S8DistanceQz_SbtD"
2626

test/DebugInfo/archetypes2.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
func markUsed<T>(_ t: T) {}
44

55
class C<A> {
6+
// CHECK: ![[A:.*]] = !DICompositeType(tag: DW_TAG_structure_type,{{.*}}identifier: "$SxD"
67
// CHECK: !DILocalVariable(name: "x", arg: 1,
7-
// CHECK-SAME: line: [[@LINE+9]],
8-
// CHECK-SAME: type: ![[A:[0-9]+]]
9-
// CHECK: ![[A]] = !DICompositeType(tag: DW_TAG_structure_type,
10-
// CHECK-SAME: identifier: "$S11archetypes21CCQq_D"
8+
// CHECK-SAME: line: [[@LINE+7]],
9+
// CHECK-SAME: type: ![[A]]
1110
// CHECK: !DILocalVariable(name: "y", arg: 2,
1211
// CHECK-SAME: line: [[@LINE+4]],
1312
// CHECK-SAME: type: ![[B:[0-9]+]]
1413
// CHECK: ![[B]] = !DICompositeType(tag: DW_TAG_structure_type,
15-
// CHECK-SAME: identifier: "$S11archetypes21CC3foo
14+
// CHECK-SAME: identifier: "$Sqd__D"
1615
func foo<B>(_ x: A, y :B) {
1716
markUsed("hello world")
1817
}

test/DebugInfo/atype.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/DebugInfo/dynamic_layout.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ class Class <T> {
77

88
init(_x : T) {x = _x}
99

10-
// Verify that the mangling of the decl context of the type U is correct.
11-
// CHECK: !DICompositeType({{.*}}name: "{{[^"]*}}$S14dynamic_layout5ClassC3fooyx_qd__tqd__lFQq_{{[^"]*}}"
10+
// Verify that the mangling of the type U is correct.
11+
// CHECK: define {{.*}}3foo
12+
// CHECK: %[[U1:.*]] = alloca %swift.type*
13+
// CHECK: call void @llvm.dbg.declare(metadata %swift.type** %[[U1]],
14+
// CHECK-SAME: metadata ![[U:[0-9]+]]
15+
// CHECK: %[[T2:.*]] = alloca %swift.type*
16+
// CHECK: call void @llvm.dbg.declare(metadata %swift.type** %[[T2]],
17+
// CHECK-SAME: metadata ![[T:[0-9]+]]
18+
// CHECK: ![[U]] = !DILocalVariable(name: "$\CF\84_1_0"
19+
// CHECK: ![[T]] = !DILocalVariable(name: "$\CF\84_0_0"
1220
func foo <U> (_ y : U) -> (T,U) {
1321
var tuple = (x,y)
1422
return tuple

test/DebugInfo/enum.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ public func foo(_ empty : Nothing) { }
6464
// CHECK-SAME: {{.*}}identifier: "$S4enum4RoseOyxG{{z?}}D")
6565
enum Rose<A> {
6666
case MkRose(() -> A, () -> [Rose<A>])
67-
// DWARF: !DICompositeType({{.*}}name: "Rose",{{.*}}identifier: "$S4enum4RoseOyAA3fooyACyxGAElFQq_GD")
67+
// DWARF: !DICompositeType({{.*}}name: "Rose",{{.*}}identifier: "$S4enum4RoseOyxGD")
6868
case IORose(() -> Rose<A>)
6969
}
7070

7171
func foo<T>(_ x : Rose<T>) -> Rose<T> { return x }
7272

73-
// CHECK: !DICompositeType({{.*}}name: "Tuple", {{.*}}elements: ![[ELTS:[0-9]+]], {{.*}}identifier: "$S4enum5TupleOyAA3baryACyxGAElFQq_GD")
73+
// CHECK: !DICompositeType({{.*}}name: "Tuple", {{.*}}elements: ![[ELTS:[0-9]+]], {{.*}}identifier: "$S4enum5TupleOyxGD")
7474
// DWARF: !DICompositeType({{.*}}name: "Tuple", {{.*}}elements: ![[ELTS:[0-9]+]],
7575
// DWARF-SAME: {{.*}}identifier: "$S4enum5TupleOyxG{{z?}}D")
7676
public enum Tuple<P> {
77-
// DWARF: !DICompositeType({{.*}}name: "Tuple",{{.*}}identifier: "$S4enum5TupleOyAA3baryACyxGAElFQq_GD")
77+
// DWARF: !DICompositeType({{.*}}name: "Tuple",{{.*}}identifier: "$S4enum5TupleOyxGD")
7878
case C(P, () -> Tuple)
7979
}
8080

8181
func bar<T>(_ x : Tuple<T>) -> Tuple<T> { return x }
8282

83-
// CHECK: !DILocalVariable(name: "self", arg: 1, {{.*}} line: [[@LINE+5]], type: ![[LIST:.*]], flags: DIFlagArtificial)
84-
// CHECK: ![[LIST]] = !DICompositeType({{.*}}identifier: "$S4enum4ListOyACQq_GD"
83+
// CHECK: ![[LIST:.*]] = !DICompositeType({{.*}}identifier: "$S4enum4ListOyxGD"
84+
// CHECK: !DILocalVariable(name: "self", arg: 1, {{.*}} line: [[@LINE+4]], type: ![[LIST]], flags: DIFlagArtificial)
8585
public enum List<T> {
8686
indirect case Tail(List, T)
8787
case End

test/DebugInfo/generic_arg.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ func foo<T>(_ x: T) -> () {
1010
// CHECK-SAME: metadata ![[X1:.*]], metadata !DIExpression())
1111
// CHECK: store %swift.type* %T, %swift.type** %[[T]],
1212
// CHECK: store %swift.opaque* %0, %swift.opaque** %[[X]],
13-
// CHECK: ![[T1]] = !DILocalVariable(name: "$swift.type.T",
13+
// CHECK: ![[T1]] = !DILocalVariable(name: "$\CF\84_0_0",
1414
// CHECK-SAME: flags: DIFlagArtificial)
1515
// CHECK: ![[X1]] = !DILocalVariable(name: "x", arg: 1,
1616
// CHECK-SAME: line: 3, type: ![[TY:.*]])
17-
// CHECK: ![[TY]] = !DICompositeType({{.*}}identifier: "$S11generic_arg3fooyyxlFQq_D")
17+
// CHECK: ![[TY]] = !DICompositeType({{.*}}identifier: "$SxD")
1818
_blackHole(x)
1919
}
2020

test/DebugInfo/generic_arg3.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public func f<Type>(_ value : Type)
88
// CHECK: call void @llvm.dbg.declare(metadata %swift.opaque** %[[ALLOCA:[^,]+]],
99
// CHECK-SAME: metadata ![[ARG:.*]], metadata !DIExpression())
1010
// CHECK: store %swift.opaque* %1, %swift.opaque** %[[ALLOCA]], align
11-
// No deref here: The argument is an Archetype and this implicitly indirect.
12-
// CHECK: ![[TY:.*]] = !DICompositeType({{.*}}identifier: "$S12generic_arg31fyyxlFQq_D"
11+
// No deref here.
12+
// CHECK: ![[TY:.*]] = !DICompositeType({{.*}}identifier: "$SxD"
1313
// CHECK: ![[ARG]] = !DILocalVariable(name: "arg", arg: 1,
1414
// CHECK-SAME: line: [[@LINE+1]], type: ![[TY]])
1515
apply(value) { arg in return arg }

test/DebugInfo/generic_arg4.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public struct Q<T> {
99
// CHECK-SAME: metadata ![[ARG:.*]], metadata !DIExpression())
1010
// CHECK: store %[[TY]]* %0, %[[TY]]** %[[ALLOCA]], align
1111
// No deref here: the array argument is passed by value.
12-
// CHECK: ![[ARG]] = !DILocalVariable(name: "arg", arg: 1,
13-
// CHECK-SAME: line: [[@LINE+2]], type: ![[TY:.*]])
14-
// CHECK: ![[TY]] = !DICompositeType({{.*}}identifier: "$SSay12generic_arg41QVyAA3fooyySayACyxGGlFQq_GGD")
12+
// CHECK: ![[DITY:.*]] = !DICompositeType({{.*}}identifier: "$SSay12generic_arg41QVyxGGD")
1513
public func foo<T>(_ arg: [Q<T>]) {
14+
// CHECK: ![[ARG]] = !DILocalVariable(name: "arg", arg: 1,
15+
// CHECK-SAME: line: [[@LINE-2]], type: ![[DITY:.*]])
1616
}

test/DebugInfo/generic_arg5.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public func foo<Type>(_ values : [S<Type>])
1414
// CHECK: store %[[TY]]* %1, %[[TY]]** %[[ALLOCA]], align
1515
// The argument is a by-ref struct and thus needs to be dereferenced.
1616
// CHECK: ![[ARG]] = !DILocalVariable(name: "arg", arg: 1,
17-
// CHECK-SAME: line: [[@LINE+3]],
17+
// CHECK-SAME: line: [[@LINE+4]],
1818
// CHECK-SAME: type: ![[TY:.*]])
19-
// CHECK: ![[TY]] = !DICompositeType({{.*}}identifier: "$S12generic_arg51SVyAA3fooyySayACyxGGlFQq_GD")
19+
// CHECK: ![[TY]] = !DICompositeType(
20+
// CHECK-SAME: identifier: "$S12generic_arg51SVyxGD")
2021
let _ = values.flatMap { arg in
2122
return .some(arg)
2223
}

0 commit comments

Comments
 (0)