Skip to content

Commit 41a3d95

Browse files
authored
Merge pull request #16400 from DougGregor/sil-apply-substitution-map
[SIL] Teach *ApplyInst to traffic in SubstitutionMap.
2 parents a95a4b9 + c009650 commit 41a3d95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+424
-579
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ class SubstitutionMap {
133133
lookupConformance(CanType type, ProtocolDecl *proto) const;
134134

135135
/// Whether the substitution map is empty.
136-
bool empty() const { return getGenericSignature() == nullptr; }
136+
bool empty() const;
137+
138+
/// Whether the substitution has any substitutable parameters, i.e.,
139+
/// it is non-empty and at least one of the type parameters can be
140+
/// substituted (i.e., is not mapped to a concrete type).
141+
bool hasAnySubstitutableParams() const;
137142

138143
/// Whether the substitution map is non-empty.
139144
explicit operator bool() const { return !empty(); }

include/swift/SIL/SILBuilder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class SILBuilder {
247247

248248
static SILType getPartialApplyResultType(SILType Ty, unsigned ArgCount,
249249
SILModule &M,
250-
SubstitutionList subs,
250+
SubstitutionMap subs,
251251
ParameterConvention calleeConvention);
252252

253253
//===--------------------------------------------------------------------===//
@@ -357,7 +357,7 @@ class SILBuilder {
357357
}
358358

359359
ApplyInst *createApply(
360-
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
360+
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
361361
ArrayRef<SILValue> Args, bool isNonThrowing,
362362
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
363363
return insert(ApplyInst::create(getSILDebugLocation(Loc), Fn, Subs, Args,
@@ -370,12 +370,12 @@ class SILBuilder {
370370
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
371371
SILFunctionConventions conventions(Fn->getType().castTo<SILFunctionType>(),
372372
getModule());
373-
return createApply(Loc, Fn, SubstitutionList(), Args, isNonThrowing,
373+
return createApply(Loc, Fn, SubstitutionMap(), Args, isNonThrowing,
374374
SpecializationInfo);
375375
}
376376

377377
TryApplyInst *createTryApply(
378-
SILLocation Loc, SILValue fn, SubstitutionList subs,
378+
SILLocation Loc, SILValue fn, SubstitutionMap subs,
379379
ArrayRef<SILValue> args, SILBasicBlock *normalBB, SILBasicBlock *errorBB,
380380
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
381381
return insertTerminator(TryApplyInst::create(getSILDebugLocation(Loc),
@@ -386,7 +386,7 @@ class SILBuilder {
386386
}
387387

388388
PartialApplyInst *createPartialApply(
389-
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
389+
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
390390
ArrayRef<SILValue> Args, ParameterConvention CalleeConvention,
391391
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
392392
return insert(PartialApplyInst::create(getSILDebugLocation(Loc), Fn,
@@ -396,7 +396,7 @@ class SILBuilder {
396396
}
397397

398398
BeginApplyInst *createBeginApply(
399-
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
399+
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
400400
ArrayRef<SILValue> Args, bool isNonThrowing,
401401
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
402402
return insert(BeginApplyInst::create(getSILDebugLocation(Loc), Fn, Subs,

include/swift/SIL/SILCloner.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
107107
const SILDebugScope *getOpScope(const SILDebugScope *DS) {
108108
return asImpl().remapScope(DS);
109109
}
110-
SmallVector<Substitution, 4> getOpSubstitutions(SubstitutionList Subs) {
111-
SmallVector<Substitution, 4> NewSubs;
112-
for (auto Sub : Subs) {
113-
NewSubs.push_back(getOpSubstitution(Sub));
114-
}
115-
return NewSubs;
116-
}
117110

118111
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {
119112
return Subs;
@@ -205,14 +198,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
205198
return ty->getASTContext().AllocateCopy(newConformances);
206199
}
207200

208-
Substitution getOpSubstitution(Substitution sub) {
209-
CanType newReplacement =
210-
getOpASTType(sub.getReplacement()->getCanonicalType());
211-
auto conformances = getOpConformances(sub.getReplacement(),
212-
sub.getConformances());
213-
return Substitution(newReplacement, conformances);
214-
}
215-
216201
SILValue getOpValue(SILValue Value) {
217202
return asImpl().remapValue(Value);
218203
}
@@ -592,7 +577,7 @@ SILCloner<ImplClass>::visitApplyInst(ApplyInst *Inst) {
592577
doPostProcess(Inst,
593578
getBuilder().createApply(getOpLocation(Inst->getLoc()),
594579
getOpValue(Inst->getCallee()),
595-
getOpSubstitutions(Inst->getSubstitutions()),
580+
getOpSubstitutionMap(Inst->getSubstitutionMap()),
596581
Args,
597582
Inst->isNonThrowing(),
598583
GenericSpecializationInformation::create(
@@ -607,7 +592,8 @@ SILCloner<ImplClass>::visitTryApplyInst(TryApplyInst *Inst) {
607592
doPostProcess(Inst,
608593
getBuilder().createTryApply(getOpLocation(Inst->getLoc()),
609594
getOpValue(Inst->getCallee()),
610-
getOpSubstitutions(Inst->getSubstitutions()),
595+
getOpSubstitutionMap(
596+
Inst->getSubstitutionMap()),
611597
Args,
612598
getOpBasicBlock(Inst->getNormalBB()),
613599
getOpBasicBlock(Inst->getErrorBB()),
@@ -624,7 +610,7 @@ SILCloner<ImplClass>::visitPartialApplyInst(PartialApplyInst *Inst) {
624610
getBuilder().createPartialApply(
625611
getOpLocation(Inst->getLoc()),
626612
getOpValue(Inst->getCallee()),
627-
getOpSubstitutions(Inst->getSubstitutions()), Args,
613+
getOpSubstitutionMap(Inst->getSubstitutionMap()), Args,
628614
Inst->getType().getAs<SILFunctionType>()
629615
->getCalleeConvention(),
630616
GenericSpecializationInformation::create(Inst,
@@ -639,7 +625,8 @@ SILCloner<ImplClass>::visitBeginApplyInst(BeginApplyInst *Inst) {
639625
doPostProcess(Inst,
640626
getBuilder().createBeginApply(getOpLocation(Inst->getLoc()),
641627
getOpValue(Inst->getCallee()),
642-
getOpSubstitutions(Inst->getSubstitutions()),
628+
getOpSubstitutionMap(
629+
Inst->getSubstitutionMap()),
643630
Args,
644631
Inst->isNonThrowing(),
645632
GenericSpecializationInformation::create(

include/swift/SIL/SILInstruction.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,15 +1688,16 @@ class ApplyInstBase<Impl, Base, false> : public Base {
16881688
protected:
16891689
template <class... As>
16901690
ApplyInstBase(SILInstructionKind kind, SILDebugLocation DebugLoc, SILValue callee,
1691-
SILType substCalleeType, SubstitutionList subs,
1691+
SILType substCalleeType, SubstitutionMap subs,
16921692
ArrayRef<SILValue> args,
16931693
ArrayRef<SILValue> typeDependentOperands,
16941694
const GenericSpecializationInformation *specializationInfo,
16951695
As... baseArgs)
16961696
: Base(kind, DebugLoc, baseArgs...), SubstCalleeType(substCalleeType),
16971697
SpecializationInfo(specializationInfo),
16981698
NonThrowing(false), NumCallArguments(args.size()),
1699-
NumTypeDependentOperands(typeDependentOperands.size()) {
1699+
NumTypeDependentOperands(typeDependentOperands.size()),
1700+
Substitutions(subs) {
17001701

17011702
// Initialize the operands.
17021703
auto allOperands = getAllOperands();
@@ -1708,13 +1709,6 @@ class ApplyInstBase<Impl, Base, false> : public Base {
17081709
new (&allOperands[NumStaticOperands + args.size() + i])
17091710
Operand(this, typeDependentOperands[i]);
17101711
}
1711-
1712-
// Initialize the substitutions.
1713-
if (!subs.empty()) {
1714-
if (auto genericSig = getOrigCalleeType()->getGenericSignature()) {
1715-
Substitutions = genericSig->getSubstitutionMap(subs);
1716-
}
1717-
}
17181712
}
17191713

17201714
~ApplyInstBase() {
@@ -1794,7 +1788,9 @@ class ApplyInstBase<Impl, Base, false> : public Base {
17941788
}
17951789

17961790
/// True if this application has generic substitutions.
1797-
bool hasSubstitutions() const { return !Substitutions.empty(); }
1791+
bool hasSubstitutions() const {
1792+
return Substitutions.hasAnySubstitutableParams();
1793+
}
17981794

17991795
/// The substitutions used to bind the generic arguments of this function.
18001796
SubstitutionList getSubstitutions() const { return Substitutions.toList(); }
@@ -1996,15 +1992,15 @@ class ApplyInst final
19961992

19971993
ApplyInst(SILDebugLocation DebugLoc, SILValue Callee,
19981994
SILType SubstCalleeType, SILType ReturnType,
1999-
SubstitutionList Substitutions,
1995+
SubstitutionMap Substitutions,
20001996
ArrayRef<SILValue> Args,
20011997
ArrayRef<SILValue> TypeDependentOperands,
20021998
bool isNonThrowing,
20031999
const GenericSpecializationInformation *SpecializationInfo);
20042000

20052001
static ApplyInst *
20062002
create(SILDebugLocation DebugLoc, SILValue Callee,
2007-
SubstitutionList Substitutions, ArrayRef<SILValue> Args,
2003+
SubstitutionMap Substitutions, ArrayRef<SILValue> Args,
20082004
bool isNonThrowing, Optional<SILModuleConventions> ModuleConventions,
20092005
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
20102006
const GenericSpecializationInformation *SpecializationInfo);
@@ -2028,15 +2024,15 @@ class PartialApplyInst final
20282024

20292025
PartialApplyInst(SILDebugLocation DebugLoc, SILValue Callee,
20302026
SILType SubstCalleeType,
2031-
SubstitutionList Substitutions,
2027+
SubstitutionMap Substitutions,
20322028
ArrayRef<SILValue> Args,
20332029
ArrayRef<SILValue> TypeDependentOperands,
20342030
SILType ClosureType,
20352031
const GenericSpecializationInformation *SpecializationInfo);
20362032

20372033
static PartialApplyInst *
20382034
create(SILDebugLocation DebugLoc, SILValue Callee, ArrayRef<SILValue> Args,
2039-
SubstitutionList Substitutions, ParameterConvention CalleeConvention,
2035+
SubstitutionMap Substitutions, ParameterConvention CalleeConvention,
20402036
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
20412037
const GenericSpecializationInformation *SpecializationInfo);
20422038

@@ -2097,15 +2093,15 @@ class BeginApplyInst final
20972093
SILType substCalleeType,
20982094
ArrayRef<SILType> allResultTypes,
20992095
ArrayRef<ValueOwnershipKind> allResultOwnerships,
2100-
SubstitutionList substitutions,
2096+
SubstitutionMap substitutions,
21012097
ArrayRef<SILValue> args,
21022098
ArrayRef<SILValue> typeDependentOperands,
21032099
bool isNonThrowing,
21042100
const GenericSpecializationInformation *specializationInfo);
21052101

21062102
static BeginApplyInst *
21072103
create(SILDebugLocation debugLoc, SILValue Callee,
2108-
SubstitutionList substitutions, ArrayRef<SILValue> args,
2104+
SubstitutionMap substitutions, ArrayRef<SILValue> args,
21092105
bool isNonThrowing, Optional<SILModuleConventions> moduleConventions,
21102106
SILFunction &F, SILOpenedArchetypesState &openedArchetypes,
21112107
const GenericSpecializationInformation *specializationInfo);
@@ -2775,7 +2771,7 @@ class BuiltinInst final
27752771
/// True if this builtin application has substitutions, which represent type
27762772
/// parameters to the builtin.
27772773
bool hasSubstitutions() const {
2778-
return !Substitutions.empty();
2774+
return Substitutions.hasAnySubstitutableParams();
27792775
}
27802776

27812777
/// Return the type parameters to the builtin.
@@ -7556,15 +7552,15 @@ class TryApplyInst final
75567552
friend SILBuilder;
75577553

75587554
TryApplyInst(SILDebugLocation DebugLoc, SILValue callee,
7559-
SILType substCalleeType, SubstitutionList substitutions,
7555+
SILType substCalleeType, SubstitutionMap substitutions,
75607556
ArrayRef<SILValue> args,
75617557
ArrayRef<SILValue> TypeDependentOperands,
75627558
SILBasicBlock *normalBB, SILBasicBlock *errorBB,
75637559
const GenericSpecializationInformation *SpecializationInfo);
75647560

75657561
static TryApplyInst *
75667562
create(SILDebugLocation DebugLoc, SILValue callee,
7567-
SubstitutionList substitutions, ArrayRef<SILValue> args,
7563+
SubstitutionMap substitutions, ArrayRef<SILValue> args,
75687564
SILBasicBlock *normalBB, SILBasicBlock *errorBB, SILFunction &F,
75697565
SILOpenedArchetypesState &OpenedArchetypes,
75707566
const GenericSpecializationInformation *SpecializationInfo);

include/swift/SIL/TypeSubstCloner.h

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,13 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
4040
llvm_unreachable("Clients need to explicitly call a base class impl!");
4141
}
4242

43-
void computeSubsMap() {
44-
if (auto genericSig = Original.getLoweredFunctionType()
45-
->getGenericSignature()) {
46-
SubsMap = genericSig->getSubstitutionMap(ApplySubs);
47-
}
48-
}
49-
5043
// A helper class for cloning different kinds of apply instructions.
5144
// Supports cloning of self-recursive functions.
5245
class ApplySiteCloningHelper {
5346
SILValue Callee;
54-
SubstitutionList Subs;
47+
SubstitutionMap Subs;
5548
SmallVector<SILValue, 8> Args;
56-
SmallVector<Substitution, 8> NewSubsList;
57-
SmallVector<Substitution, 8> RecursiveSubsList;
49+
SubstitutionMap RecursiveSubs;
5850

5951
public:
6052
ApplySiteCloningHelper(ApplySite AI, TypeSubstCloner &Cloner)
@@ -66,13 +58,12 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
6658
Builder.setCurrentDebugScope(Cloner.super::getOpScope(AI.getDebugScope()));
6759

6860
// Remap substitutions.
69-
NewSubsList = Cloner.getOpSubstitutions(AI.getSubstitutions());
70-
Subs = NewSubsList;
61+
Subs = Cloner.getOpSubstitutionMap(AI.getSubstitutionMap());
7162

7263
if (!Cloner.Inlining) {
7364
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(AI.getCallee());
7465
if (FRI && FRI->getReferencedFunction() == AI.getFunction() &&
75-
Subs == Cloner.ApplySubs) {
66+
Subs == Cloner.SubsMap) {
7667
// Handle recursions by replacing the apply to the callee with an
7768
// apply to the newly specialized function, but only if substitutions
7869
// are the same.
@@ -83,23 +74,23 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
8374
// Compute substitutions for the specialized function. These
8475
// substitutions may be different from the original ones, e.g.
8576
// there can be less substitutions.
86-
GenSig->getSubstitutions(AI.getFunction()
87-
->getLoweredFunctionType()
88-
->getGenericSignature()
89-
->getSubstitutionMap(Subs),
90-
RecursiveSubsList);
77+
RecursiveSubs = AI.getFunction()
78+
->getLoweredFunctionType()
79+
->getGenericSignature()
80+
->getSubstitutionMap(Subs.toList());
81+
9182
// Use the new set of substitutions to compute the new
9283
// substituted callee type.
9384
RecursiveSubstCalleeSILType = LoweredFnTy->substGenericArgs(
94-
AI.getModule(), RecursiveSubsList);
85+
AI.getModule(), RecursiveSubs);
9586
}
9687

9788
// The specialized recursive function may have different calling
9889
// convention for parameters. E.g. some of former indirect parameters
9990
// may become direct. Some of indirect return values may become
10091
// direct. Do not replace the callee in that case.
10192
if (SubstCalleeSILType.getASTType() == RecursiveSubstCalleeSILType) {
102-
Subs = RecursiveSubsList;
93+
Subs = RecursiveSubs;
10394
Callee = Builder.createFunctionRef(
10495
Cloner.getOpLocation(AI.getLoc()), &Builder.getFunction());
10596
SubstCalleeSILType =
@@ -121,7 +112,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
121112
return Callee;
122113
}
123114

124-
SubstitutionList getSubstitutions() const {
115+
SubstitutionMap getSubstitutions() const {
125116
return Subs;
126117
}
127118
};
@@ -143,27 +134,25 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
143134

144135
TypeSubstCloner(SILFunction &To,
145136
SILFunction &From,
146-
SubstitutionList ApplySubs,
137+
SubstitutionMap ApplySubs,
147138
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
148139
bool Inlining = false)
149140
: SILClonerWithScopes<ImplClass>(To, OpenedArchetypesTracker, Inlining),
150141
SwiftMod(From.getModule().getSwiftModule()),
142+
SubsMap(ApplySubs),
151143
Original(From),
152-
ApplySubs(ApplySubs),
153144
Inlining(Inlining) {
154-
computeSubsMap();
155145
}
156146

157147
TypeSubstCloner(SILFunction &To,
158148
SILFunction &From,
159-
SubstitutionList ApplySubs,
149+
SubstitutionMap ApplySubs,
160150
bool Inlining = false)
161151
: SILClonerWithScopes<ImplClass>(To, Inlining),
162152
SwiftMod(From.getModule().getSwiftModule()),
153+
SubsMap(ApplySubs),
163154
Original(From),
164-
ApplySubs(ApplySubs),
165155
Inlining(Inlining) {
166-
computeSubsMap();
167156
}
168157

169158

@@ -297,8 +286,6 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
297286
llvm::DenseMap<SILType, SILType> TypeCache;
298287
/// The original function to specialize.
299288
SILFunction &Original;
300-
/// The substitutions used at the call site.
301-
SubstitutionList ApplySubs;
302289
/// True, if used for inlining.
303290
bool Inlining;
304291
};

include/swift/SILOptimizer/Analysis/ArraySemantic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class ArraySemanticsCall {
155155
bool replaceByAppendingValues(SILModule &M, SILFunction *AppendFn,
156156
SILFunction *ReserveFn,
157157
const llvm::SmallVectorImpl<SILValue> &Vals,
158-
ArrayRef<Substitution> Subs);
158+
SubstitutionMap Subs);
159159

160160
/// Hoist the call to the insert point.
161161
void hoist(SILInstruction *InsertBefore, DominanceInfo *DT) {

0 commit comments

Comments
 (0)