Skip to content

Commit 6abbc0d

Browse files
committed
[DebugInfo] Use underlying type of global variables with opaque type
rdar://144881938
1 parent ecc9093 commit 6abbc0d

File tree

8 files changed

+68
-21
lines changed

8 files changed

+68
-21
lines changed

include/swift/AST/Types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4985,6 +4985,8 @@ enum class SILCoroutineKind : uint8_t {
49854985

49864986
class SILFunctionConventions;
49874987

4988+
Type substOpaqueTypesWithUnderlyingTypes(Type type,
4989+
TypeExpansionContext context);
49884990

49894991
CanType substOpaqueTypesWithUnderlyingTypes(CanType type,
49904992
TypeExpansionContext context);

lib/AST/TypeSubstitution.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ operator()(SubstitutableType *maybeOpaqueType) const {
10711071
return substTy;
10721072
}
10731073

1074-
CanType swift::substOpaqueTypesWithUnderlyingTypes(CanType ty,
1075-
TypeExpansionContext context) {
1074+
Type swift::substOpaqueTypesWithUnderlyingTypes(Type ty,
1075+
TypeExpansionContext context) {
10761076
if (!context.shouldLookThroughOpaqueTypeArchetypes() ||
10771077
!ty->hasOpaqueArchetype())
10781078
return ty;
@@ -1082,7 +1082,14 @@ CanType swift::substOpaqueTypesWithUnderlyingTypes(CanType ty,
10821082
context.isWholeModuleContext());
10831083
SubstOptions flags = (SubstFlags::SubstituteOpaqueArchetypes |
10841084
SubstFlags::PreservePackExpansionLevel);
1085-
return ty.subst(replacer, replacer, flags)->getCanonicalType();
1085+
return ty.subst(replacer, replacer, flags);
1086+
}
1087+
1088+
CanType
1089+
swift::substOpaqueTypesWithUnderlyingTypes(CanType ty,
1090+
TypeExpansionContext context) {
1091+
return substOpaqueTypesWithUnderlyingTypes(static_cast<Type>(ty), context)
1092+
->getCanonicalType();
10861093
}
10871094

10881095
static ProtocolConformanceRef substOpaqueTypesWithUnderlyingTypesRec(

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ DebugTypeInfo DebugTypeInfo::getForwardDecl(swift::Type Ty) {
105105
return DbgTy;
106106
}
107107

108-
DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
109-
IRGenModule &IGM) {
108+
static TypeBase *getTypeForGlobal(SILGlobalVariable *GV, IRGenModule &IGM) {
110109
// Prefer the original, potentially sugared version of the type if
111110
// the type hasn't been mucked with by an optimization pass.
112111
auto LowTy = GV->getLoweredType().getASTType();
@@ -116,6 +115,15 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
116115
if (DeclType->isEqual(LowTy))
117116
Type = DeclType.getPointer();
118117
}
118+
// If this global variable contains an opaque type, replace it with its
119+
// underlying type.
120+
Type = IGM.substOpaqueTypesWithUnderlyingTypes(Type).getPointer();
121+
return Type;
122+
}
123+
124+
DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
125+
IRGenModule &IGM) {
126+
auto *Type = getTypeForGlobal(GV, IGM);
119127
auto &TI = IGM.getTypeInfoForUnlowered(Type);
120128
DebugTypeInfo DbgTy = getFromTypeInfo(Type, TI, IGM);
121129
assert(!DbgTy.isContextArchetype() &&
@@ -124,17 +132,9 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
124132
}
125133

126134
DebugTypeInfo DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
127-
Size SizeInBytes,
128-
Alignment Align) {
129-
// Prefer the original, potentially sugared version of the type if
130-
// the type hasn't been mucked with by an optimization pass.
131-
auto LowTy = GV->getLoweredType().getASTType();
132-
auto *Type = LowTy.getPointer();
133-
if (auto *Decl = GV->getDecl()) {
134-
auto DeclType = Decl->getTypeInContext();
135-
if (DeclType->isEqual(LowTy))
136-
Type = DeclType.getPointer();
137-
}
135+
Alignment Align,
136+
IRGenModule &IGM) {
137+
auto *Type = getTypeForGlobal(GV, IGM);
138138
DebugTypeInfo DbgTy(Type, Align, ::hasDefaultAlignment(Type),
139139
/* IsMetadataType = */ false, /* IsFixedBuffer = */ true);
140140
assert(!DbgTy.isContextArchetype() &&

lib/IRGen/DebugTypeInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class DebugTypeInfo {
7676
/// Global variables.
7777
static DebugTypeInfo getGlobal(SILGlobalVariable *GV, IRGenModule &IGM);
7878
static DebugTypeInfo getGlobalFixedBuffer(SILGlobalVariable *GV,
79-
Size SizeInBytes, Alignment align);
79+
Alignment align, IRGenModule &IGM);
8080
/// ObjC classes.
8181
static DebugTypeInfo getObjCClass(ClassDecl *theClass, Size size,
8282
Alignment align);

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,9 +2778,9 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
27782778
}
27792779

27802780
DebugTypeInfo DbgTy =
2781-
inFixedBuffer ? DebugTypeInfo::getGlobalFixedBuffer(var, fixedSize,
2782-
fixedAlignment)
2783-
: DebugTypeInfo::getGlobal(var, *this);
2781+
inFixedBuffer
2782+
? DebugTypeInfo::getGlobalFixedBuffer(var, fixedAlignment, *this)
2783+
: DebugTypeInfo::getGlobal(var, *this);
27842784

27852785
gvar = createVariable(*this, link, globalTy, fixedAlignment, DbgTy, loc, name);
27862786
}

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ class IRGenModule {
11181118
clang::CodeGen::CodeGenModule &getClangCGM() const;
11191119

11201120
CanType getRuntimeReifiedType(CanType type);
1121+
Type substOpaqueTypesWithUnderlyingTypes(Type type);
11211122
CanType substOpaqueTypesWithUnderlyingTypes(CanType type);
11221123
SILType substOpaqueTypesWithUnderlyingTypes(SILType type, CanGenericSignature genericSig);
11231124
std::pair<CanType, ProtocolConformanceRef>

lib/IRGen/MetadataRequest.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ CanType IRGenModule::getRuntimeReifiedType(CanType type) {
526526
}));
527527
}
528528

529-
CanType IRGenModule::substOpaqueTypesWithUnderlyingTypes(CanType type) {
529+
Type IRGenModule::substOpaqueTypesWithUnderlyingTypes(Type type) {
530530
// Substitute away opaque types whose underlying types we're allowed to
531531
// assume are constant.
532532
if (type->hasOpaqueArchetype()) {
@@ -537,6 +537,11 @@ CanType IRGenModule::substOpaqueTypesWithUnderlyingTypes(CanType type) {
537537
return type;
538538
}
539539

540+
CanType IRGenModule::substOpaqueTypesWithUnderlyingTypes(CanType type) {
541+
return substOpaqueTypesWithUnderlyingTypes(static_cast<Type>(type))
542+
->getCanonicalType();
543+
}
544+
540545
SILType IRGenModule::substOpaqueTypesWithUnderlyingTypes(
541546
SILType type, CanGenericSignature genericSig) {
542547
// Substitute away opaque types whose underlying types we're allowed to
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g -o - | %FileCheck %s
2+
3+
public protocol TheProtocol {}
4+
5+
public class TheClass: TheProtocol {
6+
}
7+
8+
struct TheStruct<T> {
9+
let t: T
10+
}
11+
12+
func f() -> some TheProtocol {
13+
let p: some TheProtocol = TheClass()
14+
return p
15+
}
16+
17+
let v = f()
18+
// CHECK: !DIGlobalVariable(name: "v", {{.*}}type: ![[FIXED_BUFFER:[0-9]+]]
19+
// CHECK: ![[FIXED_BUFFER]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$swift.fixedbuffer", {{.*}}, elements: ![[TYPE_1:[0-9]+]]
20+
// CHECK: ![[TYPE_1]] = !{![[TYPE_2:[0-9]+]]}
21+
// CHECK: ![[TYPE_2]] = !DIDerivedType(tag: DW_TAG_member, name: "contents"{{.*}}baseType: ![[TYPE_3:[0-9]+]]
22+
// CHECK: ![[TYPE_3]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[TYPE:[0-9]+]])
23+
// CHECK: ![[TYPE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "TheClass"
24+
25+
26+
let v2 = TheStruct(t: f())
27+
// CHECK: !DIGlobalVariable(name: "v2", {{.*}}type: ![[CONST_TYPE_GEN:[0-9]+]]
28+
// CHECK: ![[CONST_TYPE_GEN]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[TYPE_GEN_1:[0-9]+]])
29+
// CHECK: ![[TYPE_GEN_1]] = !DICompositeType(tag: DW_TAG_structure_type{{.*}}elements: ![[TYPE_GEN_2:[0-9]+]]
30+
// CHECK: ![[TYPE_GEN_2]] = !{![[TYPE_GEN_3:[0-9]+]]}
31+
// CHECK: ![[TYPE_GEN_3]] = !DIDerivedType(tag: DW_TAG_member{{.*}}baseType: ![[TYPE_GEN:[0-9]+]]
32+
// CHECK: ![[TYPE_GEN]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$s17global_opaque_var9TheStructVyAA0D5ClassCGD"

0 commit comments

Comments
 (0)