Skip to content

GenericSpecializer: drop metatype arguments in specialized functions #66662

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 3 commits into from
Jun 16, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ private func optimize(function: Function, _ context: FunctionPassContext) {
switch instruction {
case let apply as FullApplySite:
inlineAndDevirtualize(apply: apply, context, simplifyCtxt)
case let mt as MetatypeInst:
if mt.isTriviallyDeadIgnoringDebugUses {
simplifyCtxt.erase(instructionIncludingDebugUses: mt)
}
default:
break
}
}

_ = context.specializeApplies(in: function, isMandatory: true)

removeUnusedMetatypeInstructions(in: function, context)

// If this is a just specialized function, try to optimize copy_addr, etc.
if context.optimizeMemoryAccesses(in: function) {
_ = context.eliminateDeadAllocations(in: function)
Expand Down Expand Up @@ -103,6 +101,15 @@ private func inlineAndDevirtualize(apply: FullApplySite, _ context: FunctionPass
}
}

private func removeUnusedMetatypeInstructions(in function: Function, _ context: FunctionPassContext) {
for inst in function.instructions {
if let mt = inst as? MetatypeInst,
mt.isTriviallyDeadIgnoringDebugUses {
context.erase(instructionIncludingDebugUses: mt)
}
}
}

private func shouldInline(apply: FullApplySite, callee: Function) -> Bool {
if callee.isTransparent {
return true
Expand Down
3 changes: 2 additions & 1 deletion include/swift/SIL/GenericSpecializationMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class GenericSpecializationMangler : public SpecializationMangler {
: SpecializationMangler(SpecializationPass::GenericSpecializer,
Serialized, F) {}

std::string mangleNotReabstracted(SubstitutionMap subs);
std::string mangleNotReabstracted(SubstitutionMap subs,
bool metatyeParamsRemoved);

/// Mangle a generic specialization with re-abstracted parameters.
///
Expand Down
16 changes: 9 additions & 7 deletions include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ class ReabstractionInfo {
LoadableAndTrivial
};

unsigned param2ArgIndex(unsigned ParamIdx) const {
return ParamIdx + NumFormalIndirectResults;
}

// Create a new substituted type with the updated signature.
CanSILFunctionType createSubstitutedType(SILFunction *OrigF,
SubstitutionMap SubstMap,
Expand Down Expand Up @@ -199,8 +195,8 @@ class ReabstractionInfo {
ApplySite Apply, SILFunction *Callee,
SubstitutionMap ParamSubs,
IsSerialized_t Serialized,
bool ConvertIndirectToDirect = true,
bool dropMetatypeArgs = false,
bool ConvertIndirectToDirect,
bool dropMetatypeArgs,
OptRemark::Emitter *ORE = nullptr);

/// Constructs the ReabstractionInfo for generic function \p Callee with
Expand All @@ -214,7 +210,11 @@ class ReabstractionInfo {
IsSerialized_t isSerialized() const {
return Serialized;
}


unsigned param2ArgIndex(unsigned ParamIdx) const {
return ParamIdx + NumFormalIndirectResults;
}

/// Returns true if the specialized function needs an alternative mangling.
/// See hasConvertedResilientParams.
bool needAlternativeMangling() const {
Expand Down Expand Up @@ -314,6 +314,8 @@ class ReabstractionInfo {
CanSILFunctionType createSpecializedType(CanSILFunctionType SubstFTy,
SILModule &M) const;

CanSILFunctionType createThunkType(PartialApplyInst *forPAI) const;

SILFunction *getNonSpecializedFunction() const { return Callee; }

/// Map type into a context of the specialized function.
Expand Down
9 changes: 7 additions & 2 deletions lib/SIL/Utils/GenericSpecializationMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ manglePrespecialized(GenericSignature sig, SubstitutionMap subs) {
}

std::string GenericSpecializationMangler::
mangleNotReabstracted(SubstitutionMap subs) {
mangleNotReabstracted(SubstitutionMap subs,
bool metatyeParamsRemoved) {
beginMangling();
appendSubstitutions(getGenericSignature(), subs);
appendSpecializationOperator("TG");
if (metatyeParamsRemoved) {
appendSpecializationOperator("TGm");
} else {
appendSpecializationOperator("TG");
}
return finalize();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/IPO/CapturePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ static SILFunction *getSpecializedWithDeadParams(
FuncBuilder.getModule().getSwiftModule(),
FuncBuilder.getModule().isWholeModule(), ApplySite(), Specialized,
PAI->getSubstitutionMap(), Specialized->isSerialized(),
/* ConvertIndirectToDirect */ false);
/* ConvertIndirectToDirect */ false, /*dropMetatypeArgs=*/ false);
GenericFuncSpecializer FuncSpecializer(FuncBuilder,
Specialized,
ReInfo.getClonerParamSubstitutionMap(),
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/IPO/UsePrespecialized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ bool UsePrespecialized::replaceByPrespecialized(SILFunction &F) {
continue;

ReabstractionInfo ReInfo(M.getSwiftModule(), M.isWholeModule(), AI,
ReferencedF, Subs, IsNotSerialized);
ReferencedF, Subs, IsNotSerialized,
/*ConvertIndirectToDirect=*/ true,
/*dropMetatypeArgs=*/ false);

if (!ReInfo.canBeSpecialized())
continue;
Expand Down
2 changes: 2 additions & 0 deletions lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ bool PerformanceDiagnostics::checkClosureValue(SILValue closure,
closure = tfi->getOperand();
} else if (auto *cp = dyn_cast<CopyValueInst>(closure)) {
closure = cp->getOperand();
} else if (auto *cv = dyn_cast<ConvertFunctionInst>(closure)) {
closure = cv->getOperand();
} else if (acceptFunctionArgs && isa<SILFunctionArgument>(closure)) {
// We can assume that a function closure argument is already checked at
// the call site.
Expand Down
Loading