Skip to content

Commit 6263036

Browse files
committed
SIL: Sink local archetype substitution into remapConformance()
1 parent a356b10 commit 6263036

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
7979
SILBuilder Builder;
8080
DominanceInfo *DomTree = nullptr;
8181
SubstitutionMapWithLocalArchetypes Functor;
82-
TypeSubstitutionMap &LocalArchetypeSubs;
8382

8483
// The old-to-new value map.
8584
llvm::DenseMap<SILValue, SILValue> ValueMap;
@@ -106,10 +105,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
106105
using SILInstructionVisitor<ImplClass>::asImpl;
107106

108107
explicit SILCloner(SILFunction &F, DominanceInfo *DT = nullptr)
109-
: Builder(F), DomTree(DT), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
108+
: Builder(F), DomTree(DT) {}
110109

111110
explicit SILCloner(SILGlobalVariable *GlobVar)
112-
: Builder(GlobVar), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
111+
: Builder(GlobVar) {}
113112

114113
void clearClonerState() {
115114
ValueMap.clear();
@@ -198,7 +197,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
198197
/// Register a re-mapping for local archetypes such as opened existentials.
199198
void registerLocalArchetypeRemapping(ArchetypeType *From,
200199
ArchetypeType *To) {
201-
auto result = LocalArchetypeSubs.insert(
200+
auto result = Functor.LocalArchetypeSubs.insert(
202201
std::make_pair(CanArchetypeType(From), CanType(To)));
203202
assert(result.second);
204203
(void)result;
@@ -336,17 +335,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
336335

337336
ProtocolConformanceRef getOpConformance(Type ty,
338337
ProtocolConformanceRef conformance) {
339-
// If we have local archetypes to substitute, do so now.
340-
if (ty->hasLocalArchetype() && !LocalArchetypeSubs.empty()) {
341-
conformance =
342-
conformance.subst(ty,
343-
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
344-
MakeAbstractConformanceForGenericType());
345-
ty = ty.subst(
346-
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
347-
MakeAbstractConformanceForGenericType());
348-
}
349-
350338
return asImpl().remapConformance(ty, conformance);
351339
}
352340

@@ -448,8 +436,13 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
448436
}
449437

450438
ProtocolConformanceRef remapConformance(Type Ty, ProtocolConformanceRef C) {
439+
// If we have local archetypes to substitute, do so now.
440+
if (Ty->hasLocalArchetype())
441+
C = C.subst(Ty, Functor, Functor);
442+
451443
return C;
452444
}
445+
453446
/// Get the value that takes the place of the given `Value` within the cloned
454447
/// region. The given value must already have been mapped by this cloner.
455448
SILValue getMappedValue(SILValue Value);

include/swift/SIL/TypeSubstCloner.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
148148
using SILClonerWithScopes<ImplClass>::getOpBasicBlock;
149149
using SILClonerWithScopes<ImplClass>::recordClonedInstruction;
150150
using SILClonerWithScopes<ImplClass>::recordFoldedValue;
151-
using SILClonerWithScopes<ImplClass>::LocalArchetypeSubs;
152151
using SILClonerWithScopes<ImplClass>::Functor;
153152

154153
TypeSubstCloner(SILFunction &To,
@@ -201,15 +200,16 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
201200

202201
ProtocolConformanceRef remapConformance(Type ty,
203202
ProtocolConformanceRef conf) {
204-
auto conformance = conf.subst(ty, SubsMap);
205-
auto substTy = ty.subst(SubsMap)->getCanonicalType();
203+
auto substTy = ty.subst(Functor, Functor)->getCanonicalType();
204+
auto substConf = conf.subst(ty, Functor, Functor);
205+
206206
auto context = getBuilder().getTypeExpansionContext();
207-
if (substTy->hasOpaqueArchetype() &&
208-
context.shouldLookThroughOpaqueTypeArchetypes()) {
209-
conformance =
210-
substOpaqueTypesWithUnderlyingTypes(conformance, substTy, context);
211-
}
212-
return conformance;
207+
208+
if (!substTy->hasOpaqueArchetype() ||
209+
!context.shouldLookThroughOpaqueTypeArchetypes())
210+
return substConf;
211+
212+
return substOpaqueTypesWithUnderlyingTypes(substConf, substTy, context);
213213
}
214214

215215
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {

lib/SILOptimizer/UtilityPasses/SerializeSILPass.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ class MapOpaqueArchetypes : public SILCloner<MapOpaqueArchetypes> {
7676

7777
ProtocolConformanceRef remapConformance(Type ty,
7878
ProtocolConformanceRef conf) {
79+
// If we have local archetypes to substitute, do so now.
80+
if (ty->hasLocalArchetype()) {
81+
conf = conf.subst(ty, Functor, Functor);
82+
ty = ty.subst(Functor, Functor);
83+
}
84+
7985
auto context = getBuilder().getTypeExpansionContext();
80-
auto conformance = conf;
8186
if (ty->hasOpaqueArchetype() &&
8287
context.shouldLookThroughOpaqueTypeArchetypes()) {
83-
conformance =
84-
substOpaqueTypesWithUnderlyingTypes(conformance, ty, context);
88+
conf =
89+
substOpaqueTypesWithUnderlyingTypes(conf, ty, context);
8590
}
86-
return conformance;
91+
return conf;
8792
}
8893

8994
void replace();

0 commit comments

Comments
 (0)