mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
SILOptimizer: Clean up the generic specializer a bit
This commit is contained in:
@@ -530,13 +530,14 @@ bool ReabstractionInfo::canBeSpecialized(ApplySite Apply, SILFunction *Callee,
|
||||
|
||||
ReabstractionInfo::ReabstractionInfo(ApplySite Apply, SILFunction *Callee,
|
||||
SubstitutionMap ParamSubs,
|
||||
IsSerialized_t Serialized,
|
||||
bool ConvertIndirectToDirect,
|
||||
OptRemark::Emitter *ORE) {
|
||||
OptRemark::Emitter *ORE)
|
||||
: ConvertIndirectToDirect(ConvertIndirectToDirect),
|
||||
Serialized(Serialized) {
|
||||
if (!prepareAndCheck(Apply, Callee, ParamSubs, ORE))
|
||||
return;
|
||||
|
||||
this->ConvertIndirectToDirect = ConvertIndirectToDirect;
|
||||
|
||||
SILFunction *Caller = nullptr;
|
||||
if (Apply)
|
||||
Caller = Apply.getFunction();
|
||||
@@ -557,7 +558,7 @@ ReabstractionInfo::ReabstractionInfo(ApplySite Apply, SILFunction *Callee,
|
||||
<< SpecializedType << "\n\n");
|
||||
}
|
||||
|
||||
// Some sanity checks.
|
||||
// Some correctness checks.
|
||||
auto SpecializedFnTy = getSpecializedType();
|
||||
auto SpecializedSubstFnTy = SpecializedFnTy;
|
||||
|
||||
@@ -777,10 +778,15 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
|
||||
if (isFormalResultConverted(IndirectResultIdx++)) {
|
||||
// Convert the indirect result to a direct result.
|
||||
SILType SILResTy = SILType::getPrimitiveObjectType(RI.getType());
|
||||
// FIXME: Expansion
|
||||
auto &TL = M.Types.getTypeLowering(SILResTy,
|
||||
ResilienceExpansion::Minimal);
|
||||
|
||||
// Indirect results are passed as owned, so we also need to pass the
|
||||
// direct result as owned (except it's a trivial type).
|
||||
auto C = (SILResTy.isTrivial(M) ? ResultConvention::Unowned :
|
||||
ResultConvention::Owned);
|
||||
auto C = (TL.isTrivial()
|
||||
? ResultConvention::Unowned
|
||||
: ResultConvention::Owned);
|
||||
SpecializedResults.push_back(SILResultInfo(RI.getType(), C));
|
||||
continue;
|
||||
}
|
||||
@@ -798,11 +804,15 @@ createSpecializedType(CanSILFunctionType SubstFTy, SILModule &M) const {
|
||||
|
||||
// Convert the indirect parameter to a direct parameter.
|
||||
SILType SILParamTy = SILType::getPrimitiveObjectType(PI.getType());
|
||||
// FIXME: Expansion
|
||||
auto &TL = M.Types.getTypeLowering(SILParamTy,
|
||||
ResilienceExpansion::Minimal);
|
||||
|
||||
// Indirect parameters are passed as owned/guaranteed, so we also
|
||||
// need to pass the direct/guaranteed parameter as
|
||||
// owned/guaranteed (except it's a trivial type).
|
||||
auto C = ParameterConvention::Direct_Unowned;
|
||||
if (!SILParamTy.isTrivial(M)) {
|
||||
if (!TL.isTrivial()) {
|
||||
if (PI.isGuaranteed()) {
|
||||
C = ParameterConvention::Direct_Guaranteed;
|
||||
} else {
|
||||
@@ -1774,6 +1784,8 @@ checkSpecializationRequirements(ArrayRef<Requirement> Requirements) {
|
||||
/// This constructor is used when processing @_specialize.
|
||||
ReabstractionInfo::ReabstractionInfo(SILFunction *Callee,
|
||||
ArrayRef<Requirement> Requirements) {
|
||||
Serialized = Callee->isSerialized();
|
||||
|
||||
if (shouldNotSpecialize(Callee, nullptr))
|
||||
return;
|
||||
|
||||
@@ -1802,12 +1814,11 @@ ReabstractionInfo::ReabstractionInfo(SILFunction *Callee,
|
||||
|
||||
GenericFuncSpecializer::GenericFuncSpecializer(
|
||||
SILOptFunctionBuilder &FuncBuilder, SILFunction *GenericFunc,
|
||||
SubstitutionMap ParamSubs, IsSerialized_t Serialized,
|
||||
SubstitutionMap ParamSubs,
|
||||
const ReabstractionInfo &ReInfo)
|
||||
: FuncBuilder(FuncBuilder), M(GenericFunc->getModule()),
|
||||
GenericFunc(GenericFunc),
|
||||
ParamSubs(ParamSubs),
|
||||
Serialized(Serialized),
|
||||
ReInfo(ReInfo) {
|
||||
|
||||
assert(GenericFunc->isDefinition() && "Expected definition to specialize!");
|
||||
@@ -1815,11 +1826,11 @@ GenericFuncSpecializer::GenericFuncSpecializer(
|
||||
|
||||
if (ReInfo.isPartialSpecialization()) {
|
||||
Mangle::PartialSpecializationMangler Mangler(
|
||||
GenericFunc, FnTy, Serialized, /*isReAbstracted*/ true);
|
||||
GenericFunc, FnTy, ReInfo.isSerialized(), /*isReAbstracted*/ true);
|
||||
ClonedName = Mangler.mangle();
|
||||
} else {
|
||||
Mangle::GenericSpecializationMangler Mangler(
|
||||
GenericFunc, ParamSubs, Serialized, /*isReAbstracted*/ true);
|
||||
GenericFunc, ParamSubs, ReInfo.isSerialized(), /*isReAbstracted*/ true);
|
||||
ClonedName = Mangler.mangle();
|
||||
}
|
||||
LLVM_DEBUG(llvm::dbgs() << " Specialized function " << ClonedName << '\n');
|
||||
@@ -1869,7 +1880,7 @@ SILFunction *GenericFuncSpecializer::tryCreateSpecialization() {
|
||||
|
||||
// Create a new function.
|
||||
SILFunction *SpecializedF = GenericCloner::cloneFunction(
|
||||
FuncBuilder, GenericFunc, Serialized, ReInfo,
|
||||
FuncBuilder, GenericFunc, ReInfo,
|
||||
// Use these substitutions inside the new specialized function being
|
||||
// created.
|
||||
ReInfo.getClonerParamSubstitutionMap(),
|
||||
@@ -2065,7 +2076,6 @@ class ReabstractionThunkGenerator {
|
||||
const ReabstractionInfo &ReInfo;
|
||||
PartialApplyInst *OrigPAI;
|
||||
|
||||
IsSerialized_t Serialized = IsNotSerialized;
|
||||
std::string ThunkName;
|
||||
RegularLocation Loc;
|
||||
SmallVector<SILValue, 4> Arguments;
|
||||
@@ -2078,23 +2088,18 @@ public:
|
||||
: FunctionBuilder(FunctionBuilder), OrigF(OrigPAI->getCalleeFunction()), M(OrigF->getModule()),
|
||||
SpecializedFunc(SpecializedFunc), ReInfo(ReInfo), OrigPAI(OrigPAI),
|
||||
Loc(RegularLocation::getAutoGeneratedLocation()) {
|
||||
if (OrigF->isSerialized() && OrigPAI->getFunction()->isSerialized())
|
||||
Serialized = IsSerializable;
|
||||
if (!ReInfo.isPartialSpecialization()) {
|
||||
Mangle::GenericSpecializationMangler Mangler(
|
||||
OrigF, ReInfo.getCalleeParamSubstitutionMap(), ReInfo.isSerialized(),
|
||||
/*isReAbstracted*/ false);
|
||||
|
||||
{
|
||||
if (!ReInfo.isPartialSpecialization()) {
|
||||
Mangle::GenericSpecializationMangler Mangler(
|
||||
OrigF, ReInfo.getCalleeParamSubstitutionMap(), Serialized,
|
||||
/*isReAbstracted*/ false);
|
||||
ThunkName = Mangler.mangle();
|
||||
} else {
|
||||
Mangle::PartialSpecializationMangler Mangler(
|
||||
OrigF, ReInfo.getSpecializedType(), ReInfo.isSerialized(),
|
||||
/*isReAbstracted*/ false);
|
||||
|
||||
ThunkName = Mangler.mangle();
|
||||
} else {
|
||||
Mangle::PartialSpecializationMangler Mangler(
|
||||
OrigF, ReInfo.getSpecializedType(), Serialized,
|
||||
/*isReAbstracted*/ false);
|
||||
|
||||
ThunkName = Mangler.mangle();
|
||||
}
|
||||
ThunkName = Mangler.mangle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2109,7 +2114,7 @@ protected:
|
||||
SILFunction *ReabstractionThunkGenerator::createThunk() {
|
||||
SILFunction *Thunk = FunctionBuilder.getOrCreateSharedFunction(
|
||||
Loc, ThunkName, ReInfo.getSubstitutedType(), IsBare, IsTransparent,
|
||||
Serialized, ProfileCounter(), IsThunk, IsNotDynamic);
|
||||
ReInfo.isSerialized(), ProfileCounter(), IsThunk, IsNotDynamic);
|
||||
// Re-use an existing thunk.
|
||||
if (!Thunk->empty())
|
||||
return Thunk;
|
||||
@@ -2320,7 +2325,8 @@ void swift::trySpecializeApplyOfGeneric(
|
||||
Serialized = IsNotSerialized;
|
||||
|
||||
ReabstractionInfo ReInfo(Apply, RefF, Apply.getSubstitutionMap(),
|
||||
/*ConvertIndirectToDirect=*/true, &ORE);
|
||||
Serialized, /*ConvertIndirectToDirect=*/true,
|
||||
&ORE);
|
||||
if (!ReInfo.canBeSpecialized())
|
||||
return;
|
||||
|
||||
@@ -2365,7 +2371,7 @@ void swift::trySpecializeApplyOfGeneric(
|
||||
|
||||
GenericFuncSpecializer FuncSpecializer(FuncBuilder,
|
||||
RefF, Apply.getSubstitutionMap(),
|
||||
Serialized, ReInfo);
|
||||
ReInfo);
|
||||
SILFunction *SpecializedF = FuncSpecializer.lookupSpecialization();
|
||||
if (SpecializedF) {
|
||||
// Even if the pre-specialization exists already, try to preserve it
|
||||
@@ -2408,7 +2414,8 @@ void swift::trySpecializeApplyOfGeneric(
|
||||
auto *PAI = cast<PartialApplyInst>(Apply.getInstruction());
|
||||
SILBuilderWithScope Builder(PAI);
|
||||
SILFunction *Thunk =
|
||||
ReabstractionThunkGenerator(FuncBuilder, ReInfo, PAI, SpecializedF).createThunk();
|
||||
ReabstractionThunkGenerator(FuncBuilder, ReInfo, PAI, SpecializedF)
|
||||
.createThunk();
|
||||
NewFunctions.push_back(Thunk);
|
||||
auto *FRI = Builder.createFunctionRef(PAI->getLoc(), Thunk);
|
||||
SmallVector<SILValue, 4> Arguments;
|
||||
|
||||
Reference in New Issue
Block a user