Skip to content

[SIL] Teach *ApplyInst to traffic in SubstitutionMap. #16400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ class SubstitutionMap {
lookupConformance(CanType type, ProtocolDecl *proto) const;

/// Whether the substitution map is empty.
bool empty() const { return getGenericSignature() == nullptr; }
bool empty() const;

/// Whether the substitution has any substitutable parameters, i.e.,
/// it is non-empty and at least one of the type parameters can be
/// substituted (i.e., is not mapped to a concrete type).
bool hasAnySubstitutableParams() const;

/// Whether the substitution map is non-empty.
explicit operator bool() const { return !empty(); }
Expand Down
12 changes: 6 additions & 6 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class SILBuilder {

static SILType getPartialApplyResultType(SILType Ty, unsigned ArgCount,
SILModule &M,
SubstitutionList subs,
SubstitutionMap subs,
ParameterConvention calleeConvention);

//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -357,7 +357,7 @@ class SILBuilder {
}

ApplyInst *createApply(
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
ArrayRef<SILValue> Args, bool isNonThrowing,
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
return insert(ApplyInst::create(getSILDebugLocation(Loc), Fn, Subs, Args,
Expand All @@ -370,12 +370,12 @@ class SILBuilder {
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
SILFunctionConventions conventions(Fn->getType().castTo<SILFunctionType>(),
getModule());
return createApply(Loc, Fn, SubstitutionList(), Args, isNonThrowing,
return createApply(Loc, Fn, SubstitutionMap(), Args, isNonThrowing,
SpecializationInfo);
}

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

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

BeginApplyInst *createBeginApply(
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
ArrayRef<SILValue> Args, bool isNonThrowing,
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
return insert(BeginApplyInst::create(getSILDebugLocation(Loc), Fn, Subs,
Expand Down
25 changes: 6 additions & 19 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
const SILDebugScope *getOpScope(const SILDebugScope *DS) {
return asImpl().remapScope(DS);
}
SmallVector<Substitution, 4> getOpSubstitutions(SubstitutionList Subs) {
SmallVector<Substitution, 4> NewSubs;
for (auto Sub : Subs) {
NewSubs.push_back(getOpSubstitution(Sub));
}
return NewSubs;
}

SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {
return Subs;
Expand Down Expand Up @@ -205,14 +198,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
return ty->getASTContext().AllocateCopy(newConformances);
}

Substitution getOpSubstitution(Substitution sub) {
CanType newReplacement =
getOpASTType(sub.getReplacement()->getCanonicalType());
auto conformances = getOpConformances(sub.getReplacement(),
sub.getConformances());
return Substitution(newReplacement, conformances);
}

SILValue getOpValue(SILValue Value) {
return asImpl().remapValue(Value);
}
Expand Down Expand Up @@ -592,7 +577,7 @@ SILCloner<ImplClass>::visitApplyInst(ApplyInst *Inst) {
doPostProcess(Inst,
getBuilder().createApply(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getCallee()),
getOpSubstitutions(Inst->getSubstitutions()),
getOpSubstitutionMap(Inst->getSubstitutionMap()),
Args,
Inst->isNonThrowing(),
GenericSpecializationInformation::create(
Expand All @@ -607,7 +592,8 @@ SILCloner<ImplClass>::visitTryApplyInst(TryApplyInst *Inst) {
doPostProcess(Inst,
getBuilder().createTryApply(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getCallee()),
getOpSubstitutions(Inst->getSubstitutions()),
getOpSubstitutionMap(
Inst->getSubstitutionMap()),
Args,
getOpBasicBlock(Inst->getNormalBB()),
getOpBasicBlock(Inst->getErrorBB()),
Expand All @@ -624,7 +610,7 @@ SILCloner<ImplClass>::visitPartialApplyInst(PartialApplyInst *Inst) {
getBuilder().createPartialApply(
getOpLocation(Inst->getLoc()),
getOpValue(Inst->getCallee()),
getOpSubstitutions(Inst->getSubstitutions()), Args,
getOpSubstitutionMap(Inst->getSubstitutionMap()), Args,
Inst->getType().getAs<SILFunctionType>()
->getCalleeConvention(),
GenericSpecializationInformation::create(Inst,
Expand All @@ -639,7 +625,8 @@ SILCloner<ImplClass>::visitBeginApplyInst(BeginApplyInst *Inst) {
doPostProcess(Inst,
getBuilder().createBeginApply(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getCallee()),
getOpSubstitutions(Inst->getSubstitutions()),
getOpSubstitutionMap(
Inst->getSubstitutionMap()),
Args,
Inst->isNonThrowing(),
GenericSpecializationInformation::create(
Expand Down
34 changes: 15 additions & 19 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1688,15 +1688,16 @@ class ApplyInstBase<Impl, Base, false> : public Base {
protected:
template <class... As>
ApplyInstBase(SILInstructionKind kind, SILDebugLocation DebugLoc, SILValue callee,
SILType substCalleeType, SubstitutionList subs,
SILType substCalleeType, SubstitutionMap subs,
ArrayRef<SILValue> args,
ArrayRef<SILValue> typeDependentOperands,
const GenericSpecializationInformation *specializationInfo,
As... baseArgs)
: Base(kind, DebugLoc, baseArgs...), SubstCalleeType(substCalleeType),
SpecializationInfo(specializationInfo),
NonThrowing(false), NumCallArguments(args.size()),
NumTypeDependentOperands(typeDependentOperands.size()) {
NumTypeDependentOperands(typeDependentOperands.size()),
Substitutions(subs) {

// Initialize the operands.
auto allOperands = getAllOperands();
Expand All @@ -1708,13 +1709,6 @@ class ApplyInstBase<Impl, Base, false> : public Base {
new (&allOperands[NumStaticOperands + args.size() + i])
Operand(this, typeDependentOperands[i]);
}

// Initialize the substitutions.
if (!subs.empty()) {
if (auto genericSig = getOrigCalleeType()->getGenericSignature()) {
Substitutions = genericSig->getSubstitutionMap(subs);
}
}
}

~ApplyInstBase() {
Expand Down Expand Up @@ -1794,7 +1788,9 @@ class ApplyInstBase<Impl, Base, false> : public Base {
}

/// True if this application has generic substitutions.
bool hasSubstitutions() const { return !Substitutions.empty(); }
bool hasSubstitutions() const {
return Substitutions.hasAnySubstitutableParams();
}

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

ApplyInst(SILDebugLocation DebugLoc, SILValue Callee,
SILType SubstCalleeType, SILType ReturnType,
SubstitutionList Substitutions,
SubstitutionMap Substitutions,
ArrayRef<SILValue> Args,
ArrayRef<SILValue> TypeDependentOperands,
bool isNonThrowing,
const GenericSpecializationInformation *SpecializationInfo);

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

PartialApplyInst(SILDebugLocation DebugLoc, SILValue Callee,
SILType SubstCalleeType,
SubstitutionList Substitutions,
SubstitutionMap Substitutions,
ArrayRef<SILValue> Args,
ArrayRef<SILValue> TypeDependentOperands,
SILType ClosureType,
const GenericSpecializationInformation *SpecializationInfo);

static PartialApplyInst *
create(SILDebugLocation DebugLoc, SILValue Callee, ArrayRef<SILValue> Args,
SubstitutionList Substitutions, ParameterConvention CalleeConvention,
SubstitutionMap Substitutions, ParameterConvention CalleeConvention,
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
const GenericSpecializationInformation *SpecializationInfo);

Expand Down Expand Up @@ -2097,15 +2093,15 @@ class BeginApplyInst final
SILType substCalleeType,
ArrayRef<SILType> allResultTypes,
ArrayRef<ValueOwnershipKind> allResultOwnerships,
SubstitutionList substitutions,
SubstitutionMap substitutions,
ArrayRef<SILValue> args,
ArrayRef<SILValue> typeDependentOperands,
bool isNonThrowing,
const GenericSpecializationInformation *specializationInfo);

static BeginApplyInst *
create(SILDebugLocation debugLoc, SILValue Callee,
SubstitutionList substitutions, ArrayRef<SILValue> args,
SubstitutionMap substitutions, ArrayRef<SILValue> args,
bool isNonThrowing, Optional<SILModuleConventions> moduleConventions,
SILFunction &F, SILOpenedArchetypesState &openedArchetypes,
const GenericSpecializationInformation *specializationInfo);
Expand Down Expand Up @@ -2775,7 +2771,7 @@ class BuiltinInst final
/// True if this builtin application has substitutions, which represent type
/// parameters to the builtin.
bool hasSubstitutions() const {
return !Substitutions.empty();
return Substitutions.hasAnySubstitutableParams();
}

/// Return the type parameters to the builtin.
Expand Down Expand Up @@ -7556,15 +7552,15 @@ class TryApplyInst final
friend SILBuilder;

TryApplyInst(SILDebugLocation DebugLoc, SILValue callee,
SILType substCalleeType, SubstitutionList substitutions,
SILType substCalleeType, SubstitutionMap substitutions,
ArrayRef<SILValue> args,
ArrayRef<SILValue> TypeDependentOperands,
SILBasicBlock *normalBB, SILBasicBlock *errorBB,
const GenericSpecializationInformation *SpecializationInfo);

static TryApplyInst *
create(SILDebugLocation DebugLoc, SILValue callee,
SubstitutionList substitutions, ArrayRef<SILValue> args,
SubstitutionMap substitutions, ArrayRef<SILValue> args,
SILBasicBlock *normalBB, SILBasicBlock *errorBB, SILFunction &F,
SILOpenedArchetypesState &OpenedArchetypes,
const GenericSpecializationInformation *SpecializationInfo);
Expand Down
45 changes: 16 additions & 29 deletions include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,13 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
llvm_unreachable("Clients need to explicitly call a base class impl!");
}

void computeSubsMap() {
if (auto genericSig = Original.getLoweredFunctionType()
->getGenericSignature()) {
SubsMap = genericSig->getSubstitutionMap(ApplySubs);
}
}

// A helper class for cloning different kinds of apply instructions.
// Supports cloning of self-recursive functions.
class ApplySiteCloningHelper {
SILValue Callee;
SubstitutionList Subs;
SubstitutionMap Subs;
SmallVector<SILValue, 8> Args;
SmallVector<Substitution, 8> NewSubsList;
SmallVector<Substitution, 8> RecursiveSubsList;
SubstitutionMap RecursiveSubs;

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

// Remap substitutions.
NewSubsList = Cloner.getOpSubstitutions(AI.getSubstitutions());
Subs = NewSubsList;
Subs = Cloner.getOpSubstitutionMap(AI.getSubstitutionMap());

if (!Cloner.Inlining) {
FunctionRefInst *FRI = dyn_cast<FunctionRefInst>(AI.getCallee());
if (FRI && FRI->getReferencedFunction() == AI.getFunction() &&
Subs == Cloner.ApplySubs) {
Subs == Cloner.SubsMap) {
// Handle recursions by replacing the apply to the callee with an
// apply to the newly specialized function, but only if substitutions
// are the same.
Expand All @@ -83,23 +74,23 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
// Compute substitutions for the specialized function. These
// substitutions may be different from the original ones, e.g.
// there can be less substitutions.
GenSig->getSubstitutions(AI.getFunction()
->getLoweredFunctionType()
->getGenericSignature()
->getSubstitutionMap(Subs),
RecursiveSubsList);
RecursiveSubs = AI.getFunction()
->getLoweredFunctionType()
->getGenericSignature()
->getSubstitutionMap(Subs.toList());

// Use the new set of substitutions to compute the new
// substituted callee type.
RecursiveSubstCalleeSILType = LoweredFnTy->substGenericArgs(
AI.getModule(), RecursiveSubsList);
AI.getModule(), RecursiveSubs);
}

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

SubstitutionList getSubstitutions() const {
SubstitutionMap getSubstitutions() const {
return Subs;
}
};
Expand All @@ -143,27 +134,25 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {

TypeSubstCloner(SILFunction &To,
SILFunction &From,
SubstitutionList ApplySubs,
SubstitutionMap ApplySubs,
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
bool Inlining = false)
: SILClonerWithScopes<ImplClass>(To, OpenedArchetypesTracker, Inlining),
SwiftMod(From.getModule().getSwiftModule()),
SubsMap(ApplySubs),
Original(From),
ApplySubs(ApplySubs),
Inlining(Inlining) {
computeSubsMap();
}

TypeSubstCloner(SILFunction &To,
SILFunction &From,
SubstitutionList ApplySubs,
SubstitutionMap ApplySubs,
bool Inlining = false)
: SILClonerWithScopes<ImplClass>(To, Inlining),
SwiftMod(From.getModule().getSwiftModule()),
SubsMap(ApplySubs),
Original(From),
ApplySubs(ApplySubs),
Inlining(Inlining) {
computeSubsMap();
}


Expand Down Expand Up @@ -297,8 +286,6 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
llvm::DenseMap<SILType, SILType> TypeCache;
/// The original function to specialize.
SILFunction &Original;
/// The substitutions used at the call site.
SubstitutionList ApplySubs;
/// True, if used for inlining.
bool Inlining;
};
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Analysis/ArraySemantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ArraySemanticsCall {
bool replaceByAppendingValues(SILModule &M, SILFunction *AppendFn,
SILFunction *ReserveFn,
const llvm::SmallVectorImpl<SILValue> &Vals,
ArrayRef<Substitution> Subs);
SubstitutionMap Subs);

/// Hoist the call to the insert point.
void hoist(SILInstruction *InsertBefore, DominanceInfo *DT) {
Expand Down
Loading