Skip to content

Commit 42fe2c1

Browse files
committed
[SIL] Remove SubstitutionList from the rest of the generic specializer.
1 parent 761b7c6 commit 42fe2c1

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,21 @@ class TypeComparator : public TypeMatcher<TypeComparator> {
232232

233233
} // anonymous namespace
234234

235-
/// Checks if a second substitution list is an expanded version of
236-
/// the first substitution list.
235+
/// Checks if a second substitution map is an expanded version of
236+
/// the first substitution map.
237237
/// This is the case if at least one of the substitution type in Subs2 is
238238
/// "bigger" than the corresponding substitution type in Subs1.
239239
/// Type T2 is "smaller" than type T1 if T2 is structurally contained in T1.
240-
static bool growingSubstitutions(SubstitutionList Subs1,
241-
SubstitutionList Subs2) {
242-
assert(Subs1.size() == Subs2.size());
240+
static bool growingSubstitutions(SubstitutionMap Subs1,
241+
SubstitutionMap Subs2) {
242+
auto Replacements1 = Subs1.getReplacementTypes();
243+
auto Replacements2 = Subs2.getReplacementTypes();
244+
assert(Replacements1.size() == Replacements2.size());
243245
TypeComparator TypeCmp;
244246
// Perform component-wise comparisions for substitutions.
245-
for (unsigned idx = 0, e = Subs1.size(); idx < e; ++idx) {
246-
auto Type1 = Subs1[idx].getReplacement()->getCanonicalType();
247-
auto Type2 = Subs2[idx].getReplacement()->getCanonicalType();
247+
for (unsigned idx : indices(Replacements1)) {
248+
auto Type1 = Replacements1[idx]->getCanonicalType();
249+
auto Type2 = Replacements2[idx]->getCanonicalType();
248250
// If types are the same, the substitution type does not grow.
249251
if (TypeCmp.isEqual(Type2, Type1))
250252
continue;
@@ -255,7 +257,7 @@ static bool growingSubstitutions(SubstitutionList Subs1,
255257
if (TypeCmp.isPartiallyContainedIn(Type1, Type2)) {
256258
DEBUG(llvm::dbgs() << "Type:\n"; Type1.dump();
257259
llvm::dbgs() << "is (partially) contained in type:\n"; Type2.dump();
258-
llvm::dbgs() << "SubstitutionList[" << idx
260+
llvm::dbgs() << "Replacements[" << idx
259261
<< "] has got bigger since last time.\n");
260262
return true;
261263
}
@@ -317,8 +319,8 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {
317319
DEBUG(llvm::dbgs() << "Found a call graph loop, checking substitutions\n");
318320
// Consider if components of the substitution list gets bigger compared to
319321
// the previously seen specialization of the same generic function.
320-
if (growingSubstitutions(CurSpecializationInfo->getSubstitutions().toList(),
321-
Apply.getSubstitutions())) {
322+
if (growingSubstitutions(CurSpecializationInfo->getSubstitutions(),
323+
Apply.getSubstitutionMap())) {
322324
DEBUG(llvm::dbgs() << "Found a generic specialization loop!\n");
323325
return true;
324326
}
@@ -346,11 +348,11 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {
346348
// =============================================================================
347349

348350
static bool shouldNotSpecializeCallee(SILFunction *Callee,
349-
SubstitutionList Subs = {}) {
351+
SubstitutionMap Subs = {}) {
350352
if (Callee->hasSemanticsAttr("optimize.sil.specialize.generic.never"))
351353
return true;
352354

353-
if (!Subs.empty() &&
355+
if (Subs.hasAnySubstitutableParams() &&
354356
Callee->hasSemanticsAttr("optimize.sil.specialize.generic.partial.never"))
355357
return true;
356358

@@ -467,7 +469,7 @@ bool ReabstractionInfo::prepareAndCheck(ApplySite Apply, SILFunction *Callee,
467469
return false;
468470

469471
// Bail if the callee should not be partially specialized.
470-
if (shouldNotSpecializeCallee(Callee, ParamSubs.toList()))
472+
if (shouldNotSpecializeCallee(Callee, ParamSubs))
471473
return false;
472474
}
473475

@@ -978,7 +980,6 @@ static void collectRequirements(ArchetypeType *Archetype, GenericSignature *Sig,
978980
/// really used?
979981
static bool
980982
shouldBePartiallySpecialized(Type Replacement,
981-
ArrayRef<ProtocolConformanceRef> Conformances,
982983
GenericSignature *Sig, GenericEnvironment *Env) {
983984
// If replacement is a concrete type, this substitution
984985
// should participate.
@@ -1024,8 +1025,8 @@ shouldBePartiallySpecialized(Type Replacement,
10241025

10251026
if (OptimizeGenericSubstitutions) {
10261027
// Is it an unconstrained generic parameter?
1027-
if (Conformances.empty()) {
1028-
if (Replacement->is<ArchetypeType>()) {
1028+
if (auto Archetype = Replacement->getAs<ArchetypeType>()) {
1029+
if (Archetype->getConformsTo().empty()) {
10291030
// TODO: If Replacement add a new layout constraint, then
10301031
// it may be still useful to perform the partial specialization.
10311032
return false;
@@ -1128,7 +1129,7 @@ class FunctionSignaturePartialSpecializer {
11281129
/// Take into account only those archetypes that occur in the
11291130
/// substitutions of generic parameters which will be partially
11301131
/// specialized. Ignore all others.
1131-
void collectUsedCallerArchetypes(SubstitutionList ParamSubs);
1132+
void collectUsedCallerArchetypes(SubstitutionMap ParamSubs);
11321133

11331134
/// Create a new generic parameter.
11341135
GenericTypeParamType *createGenericParam();
@@ -1139,15 +1140,14 @@ class FunctionSignaturePartialSpecializer {
11391140
GenericEnvironment *CallerGenericEnv,
11401141
GenericSignature *CalleeGenericSig,
11411142
GenericEnvironment *CalleeGenericEnv,
1142-
SubstitutionList ParamSubs)
1143+
SubstitutionMap ParamSubs)
11431144
: CallerGenericSig(CallerGenericSig), CallerGenericEnv(CallerGenericEnv),
11441145
CalleeGenericSig(CalleeGenericSig), CalleeGenericEnv(CalleeGenericEnv),
11451146
M(M), SM(M.getSwiftModule()), Ctx(M.getASTContext()),
11461147
Builder(Ctx) {
11471148
SpecializedGenericSig = nullptr;
11481149
SpecializedGenericEnv = nullptr;
1149-
CalleeInterfaceToCallerArchetypeMap =
1150-
CalleeGenericSig->getSubstitutionMap(ParamSubs);
1150+
CalleeInterfaceToCallerArchetypeMap = ParamSubs;
11511151
}
11521152

11531153
/// This constructor is used by when processing @_specialize.
@@ -1194,7 +1194,7 @@ class FunctionSignaturePartialSpecializer {
11941194
return SpecializedGenericEnv;
11951195
}
11961196

1197-
void createSpecializedGenericSignature(SubstitutionList ParamSubs);
1197+
void createSpecializedGenericSignature(SubstitutionMap ParamSubs);
11981198

11991199
void createSpecializedGenericSignatureWithNonGenericSubs();
12001200

@@ -1217,16 +1217,15 @@ FunctionSignaturePartialSpecializer::createGenericParam() {
12171217

12181218
/// Collect all used caller's archetypes from all the substitutions.
12191219
void FunctionSignaturePartialSpecializer::collectUsedCallerArchetypes(
1220-
SubstitutionList ParamSubs) {
1221-
for (auto Sub : ParamSubs) {
1222-
auto Replacement = Sub.getReplacement();
1220+
SubstitutionMap ParamSubs) {
1221+
for (auto Replacement : ParamSubs.getReplacementTypes()) {
12231222
if (!Replacement->hasArchetype())
12241223
continue;
12251224

12261225
// If the substitution will not be performed in the specialized
12271226
// function, there is no need to check for any archetypes inside
12281227
// the replacement.
1229-
if (!shouldBePartiallySpecialized(Replacement, Sub.getConformances(),
1228+
if (!shouldBePartiallySpecialized(Replacement,
12301229
CallerGenericSig, CallerGenericEnv))
12311230
continue;
12321231

@@ -1350,7 +1349,7 @@ void FunctionSignaturePartialSpecializer::
13501349
DEBUG(llvm::dbgs() << "Replacement found:\n"; Replacement->dump());
13511350

13521351
bool ShouldSpecializeGP = shouldBePartiallySpecialized(
1353-
Replacement, {}, CallerGenericSig, CallerGenericEnv);
1352+
Replacement, CallerGenericSig, CallerGenericEnv);
13541353

13551354
if (ShouldSpecializeGP) {
13561355
DEBUG(llvm::dbgs() << "Should be partially specialized.\n");
@@ -1559,7 +1558,7 @@ void FunctionSignaturePartialSpecializer::
15591558
}
15601559

15611560
void FunctionSignaturePartialSpecializer::createSpecializedGenericSignature(
1562-
SubstitutionList ParamSubs) {
1561+
SubstitutionMap ParamSubs) {
15631562
// Collect all used caller's archetypes from all the substitutions.
15641563
collectUsedCallerArchetypes(ParamSubs);
15651564

@@ -1644,11 +1643,11 @@ void ReabstractionInfo::performPartialSpecializationPreparation(
16441643
FunctionSignaturePartialSpecializer FSPS(M,
16451644
CallerGenericSig, CallerGenericEnv,
16461645
CalleeGenericSig, CalleeGenericEnv,
1647-
ParamSubs.toList());
1646+
ParamSubs);
16481647

16491648
// Create the partially specialized generic signature and generic environment.
16501649
if (SupportGenericSubstitutions)
1651-
FSPS.createSpecializedGenericSignature(ParamSubs.toList());
1650+
FSPS.createSpecializedGenericSignature(ParamSubs);
16521651
else
16531652
FSPS.createSpecializedGenericSignatureWithNonGenericSubs();
16541653

0 commit comments

Comments
 (0)