Skip to content

Commit e144d0f

Browse files
committed
[sil-combine] Do not form the SubstitutionList by hand. Use SubstitutionMap APIs for it.
1 parent 28d7f80 commit e144d0f

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define DEBUG_TYPE "sil-combine"
1414
#include "SILCombiner.h"
15+
#include "swift/AST/SubstitutionMap.h"
1516
#include "swift/SIL/DynamicCasts.h"
1617
#include "swift/SIL/PatternMatch.h"
1718
#include "swift/SIL/SILBuilder.h"
@@ -686,27 +687,39 @@ SILCombiner::createApplyWithConcreteType(FullApplySite AI,
686687
}
687688
Args.push_back(NewSelf);
688689

689-
// Form a new set of substitutions where Self is
690-
// replaced by a concrete type.
691-
SmallVector<Substitution, 8> Substitutions;
692-
for (auto Subst : AI.getSubstitutions()) {
693-
auto *A = Subst.getReplacement()->getAs<ArchetypeType>();
694-
if (A && A == OpenedArchetype) {
695-
auto Conformances = AI.getModule().getASTContext()
696-
.AllocateUninitialized<ProtocolConformanceRef>(1);
697-
Conformances[0] = Conformance;
698-
Substitution NewSubst(ConcreteType, Conformances);
699-
Substitutions.push_back(NewSubst);
700-
} else
701-
Substitutions.push_back(Subst);
702-
}
703-
690+
auto FnTy = AI.getCallee()->getType().castTo<SILFunctionType>();
704691
SILType SubstCalleeType = AI.getSubstCalleeSILType();
705-
706692
SILType NewSubstCalleeType;
707693

708-
auto FnTy = AI.getCallee()->getType().castTo<SILFunctionType>();
694+
// Form a new set of substitutions where Self is
695+
// replaced by a concrete type.
696+
SmallVector<Substitution, 8> Substitutions;
709697
if (FnTy->isPolymorphic()) {
698+
auto FnSubsMap =
699+
FnTy->getGenericSignature()->getSubstitutionMap(AI.getSubstitutions());
700+
auto FinalSubsMap = FnSubsMap.subst(
701+
[&](SubstitutableType *type) -> Type {
702+
if (type == OpenedArchetype)
703+
return ConcreteType;
704+
return type;
705+
},
706+
[&](CanType origTy, Type substTy,
707+
ProtocolType *proto) -> Optional<ProtocolConformanceRef> {
708+
if (substTy->getCanonicalType() == ConcreteType) {
709+
if (proto->getDecl() != Conformance.getRequirement()) {
710+
assert(
711+
Conformance.getRequirement()->inheritsFrom(proto->getDecl()));
712+
if (Conformance.isAbstract())
713+
return ProtocolConformanceRef(proto->getDecl());
714+
Conformance = ProtocolConformanceRef(
715+
Conformance.getConcrete()->getInheritedConformance(
716+
proto->getDecl()));
717+
}
718+
return Conformance;
719+
}
720+
return ProtocolConformanceRef(proto->getDecl());
721+
});
722+
FnTy->getGenericSignature()->getSubstitutions(FinalSubsMap, Substitutions);
710723
// Handle polymorphic functions by properly substituting
711724
// their parameter types.
712725
CanSILFunctionType SFT = FnTy->substGenericArgs(

0 commit comments

Comments
 (0)