Skip to content

Commit

Permalink
SIL: Sink local archetype substitution into remapType()
Browse files Browse the repository at this point in the history
  • Loading branch information
slavapestov committed May 15, 2024
1 parent 0a0b17a commit a356b10
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
25 changes: 9 additions & 16 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
}

SILType getOpType(SILType Ty) {
// Substitute local archetypes, if we have any.
if (Ty.hasLocalArchetype() && !LocalArchetypeSubs.empty()) {
Ty = Ty.subst(
Builder.getModule(),
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
MakeAbstractConformanceForGenericType(),
CanGenericSignature());
}

return asImpl().remapType(Ty);
}

Expand Down Expand Up @@ -439,17 +430,19 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
SILLocation remapLocation(SILLocation Loc) { return Loc; }
const SILDebugScope *remapScope(const SILDebugScope *DS) { return DS; }
SILType remapType(SILType Ty) {
return Ty;
// Substitute local archetypes, if we have any.
if (Ty.hasLocalArchetype()) {
Ty = Ty.subst(Builder.getModule(), Functor, Functor,
CanGenericSignature());
}

return Ty;
}

CanType remapASTType(CanType ty) {
// Substitute local archetypes, if we have any.
if (ty->hasLocalArchetype() && !LocalArchetypeSubs.empty()) {
ty = ty.subst(
QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
MakeAbstractConformanceForGenericType()
)->getCanonicalType();
}
if (ty->hasLocalArchetype())
ty = ty.subst(Functor, Functor)->getCanonicalType();

return ty;
}
Expand Down
14 changes: 9 additions & 5 deletions include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,16 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
SILType remapType(SILType Ty) {
SILType &Sty = TypeCache[Ty];
if (!Sty) {
Sty = Ty.subst(Original.getModule(), SubsMap);
if (!Sty.getASTType()->hasOpaqueArchetype() ||
!getBuilder()
.getTypeExpansionContext()
.shouldLookThroughOpaqueTypeArchetypes())
Sty = Ty;

Sty = Sty.subst(getBuilder().getModule(), Functor, Functor);

auto context = getBuilder().getTypeExpansionContext();

if (!Sty.hasOpaqueArchetype() ||
!context.shouldLookThroughOpaqueTypeArchetypes())
return Sty;

// Remap types containing opaque result types in the current context.
Sty = getBuilder().getTypeLowering(Sty).getLoweredType().getCategoryType(
Sty.getCategory());
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/LoadableByAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2983,6 +2983,7 @@ namespace {
}

SILType remapType(SILType ty) {
ty = ty.subst(getBuilder().getModule(), Functor, Functor);
if (auto fnType = ty.getAs<SILFunctionType>()) {
GenericEnvironment *genEnv = getSubstGenericEnvironment(fnType);
return SILType::getPrimitiveObjectType(
Expand Down
5 changes: 5 additions & 0 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class InstructionVisitor : public SILCloner<InstructionVisitor> {
}

SILType remapType(SILType Ty) {
if (Ty.hasLocalArchetype()) {
Ty = Ty.subst(getBuilder().getModule(), Functor, Functor,
CanGenericSignature());
}

CMS.makeTypeUsableFromInline(Ty.getASTType());
return Ty;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/SILOptimizer/UtilityPasses/SerializeSILPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ class MapOpaqueArchetypes : public SILCloner<MapOpaqueArchetypes> {
}

SILType remapType(SILType Ty) {
if (!Ty.getASTType()->hasOpaqueArchetype() ||
// Substitute local archetypes, if we have any.
if (Ty.hasLocalArchetype()) {
Ty = Ty.subst(getBuilder().getModule(), Functor, Functor,
CanGenericSignature());
}

if (!Ty.hasOpaqueArchetype() ||
!getBuilder()
.getTypeExpansionContext()
.shouldLookThroughOpaqueTypeArchetypes())
Expand Down

0 comments on commit a356b10

Please sign in to comment.