Skip to content

Commit de31b13

Browse files
authored
Merge pull request #66662 from eeckstein/fix-specializer
GenericSpecializer: drop metatype arguments in specialized functions
2 parents 5dc59a5 + 7839b54 commit de31b13

17 files changed

+352
-88
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,15 @@ private func optimize(function: Function, _ context: FunctionPassContext) {
5858
switch instruction {
5959
case let apply as FullApplySite:
6060
inlineAndDevirtualize(apply: apply, context, simplifyCtxt)
61-
case let mt as MetatypeInst:
62-
if mt.isTriviallyDeadIgnoringDebugUses {
63-
simplifyCtxt.erase(instructionIncludingDebugUses: mt)
64-
}
6561
default:
6662
break
6763
}
6864
}
6965

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

68+
removeUnusedMetatypeInstructions(in: function, context)
69+
7270
// If this is a just specialized function, try to optimize copy_addr, etc.
7371
if context.optimizeMemoryAccesses(in: function) {
7472
_ = context.eliminateDeadAllocations(in: function)
@@ -103,6 +101,15 @@ private func inlineAndDevirtualize(apply: FullApplySite, _ context: FunctionPass
103101
}
104102
}
105103

104+
private func removeUnusedMetatypeInstructions(in function: Function, _ context: FunctionPassContext) {
105+
for inst in function.instructions {
106+
if let mt = inst as? MetatypeInst,
107+
mt.isTriviallyDeadIgnoringDebugUses {
108+
context.erase(instructionIncludingDebugUses: mt)
109+
}
110+
}
111+
}
112+
106113
private func shouldInline(apply: FullApplySite, callee: Function) -> Bool {
107114
if callee.isTransparent {
108115
return true

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class GenericSpecializationMangler : public SpecializationMangler {
8888
: SpecializationMangler(SpecializationPass::GenericSpecializer,
8989
Serialized, F) {}
9090

91-
std::string mangleNotReabstracted(SubstitutionMap subs);
91+
std::string mangleNotReabstracted(SubstitutionMap subs,
92+
bool metatyeParamsRemoved);
9293

9394
/// Mangle a generic specialization with re-abstracted parameters.
9495
///

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ class ReabstractionInfo {
157157
LoadableAndTrivial
158158
};
159159

160-
unsigned param2ArgIndex(unsigned ParamIdx) const {
161-
return ParamIdx + NumFormalIndirectResults;
162-
}
163-
164160
// Create a new substituted type with the updated signature.
165161
CanSILFunctionType createSubstitutedType(SILFunction *OrigF,
166162
SubstitutionMap SubstMap,
@@ -199,8 +195,8 @@ class ReabstractionInfo {
199195
ApplySite Apply, SILFunction *Callee,
200196
SubstitutionMap ParamSubs,
201197
IsSerialized_t Serialized,
202-
bool ConvertIndirectToDirect = true,
203-
bool dropMetatypeArgs = false,
198+
bool ConvertIndirectToDirect,
199+
bool dropMetatypeArgs,
204200
OptRemark::Emitter *ORE = nullptr);
205201

206202
/// Constructs the ReabstractionInfo for generic function \p Callee with
@@ -214,7 +210,11 @@ class ReabstractionInfo {
214210
IsSerialized_t isSerialized() const {
215211
return Serialized;
216212
}
217-
213+
214+
unsigned param2ArgIndex(unsigned ParamIdx) const {
215+
return ParamIdx + NumFormalIndirectResults;
216+
}
217+
218218
/// Returns true if the specialized function needs an alternative mangling.
219219
/// See hasConvertedResilientParams.
220220
bool needAlternativeMangling() const {
@@ -314,6 +314,8 @@ class ReabstractionInfo {
314314
CanSILFunctionType createSpecializedType(CanSILFunctionType SubstFTy,
315315
SILModule &M) const;
316316

317+
CanSILFunctionType createThunkType(PartialApplyInst *forPAI) const;
318+
317319
SILFunction *getNonSpecializedFunction() const { return Callee; }
318320

319321
/// Map type into a context of the specialized function.

lib/SIL/Utils/GenericSpecializationMangler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,15 @@ manglePrespecialized(GenericSignature sig, SubstitutionMap subs) {
100100
}
101101

102102
std::string GenericSpecializationMangler::
103-
mangleNotReabstracted(SubstitutionMap subs) {
103+
mangleNotReabstracted(SubstitutionMap subs,
104+
bool metatyeParamsRemoved) {
104105
beginMangling();
105106
appendSubstitutions(getGenericSignature(), subs);
106-
appendSpecializationOperator("TG");
107+
if (metatyeParamsRemoved) {
108+
appendSpecializationOperator("TGm");
109+
} else {
110+
appendSpecializationOperator("TG");
111+
}
107112
return finalize();
108113
}
109114

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ static SILFunction *getSpecializedWithDeadParams(
492492
FuncBuilder.getModule().getSwiftModule(),
493493
FuncBuilder.getModule().isWholeModule(), ApplySite(), Specialized,
494494
PAI->getSubstitutionMap(), Specialized->isSerialized(),
495-
/* ConvertIndirectToDirect */ false);
495+
/* ConvertIndirectToDirect */ false, /*dropMetatypeArgs=*/ false);
496496
GenericFuncSpecializer FuncSpecializer(FuncBuilder,
497497
Specialized,
498498
ReInfo.getClonerParamSubstitutionMap(),

lib/SILOptimizer/IPO/UsePrespecialized.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ bool UsePrespecialized::replaceByPrespecialized(SILFunction &F) {
9191
continue;
9292

9393
ReabstractionInfo ReInfo(M.getSwiftModule(), M.isWholeModule(), AI,
94-
ReferencedF, Subs, IsNotSerialized);
94+
ReferencedF, Subs, IsNotSerialized,
95+
/*ConvertIndirectToDirect=*/ true,
96+
/*dropMetatypeArgs=*/ false);
9597

9698
if (!ReInfo.canBeSpecialized())
9799
continue;

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ bool PerformanceDiagnostics::checkClosureValue(SILValue closure,
217217
closure = tfi->getOperand();
218218
} else if (auto *cp = dyn_cast<CopyValueInst>(closure)) {
219219
closure = cp->getOperand();
220+
} else if (auto *cv = dyn_cast<ConvertFunctionInst>(closure)) {
221+
closure = cv->getOperand();
220222
} else if (acceptFunctionArgs && isa<SILFunctionArgument>(closure)) {
221223
// We can assume that a function closure argument is already checked at
222224
// the call site.

0 commit comments

Comments
 (0)