Skip to content

Commit 7839b54

Browse files
committed
GenericSpecializer: drop metatype arguments in specialized functions
And replace them with explicit `metatype` instruction in the entry block. This allows such metatype instructions to be deleted if they are dead. This was already done for performance-annotated functions. But now do this for all functions. It is essential that performance-annotated functions are specialized in the same way as other functions. Because otherwise it can happen that the same specialization has different performance characteristics in different modules. And it's up to the linker to select one of those ODR functions when linking. Also, dropping metatype arguments is good for performance and code size in general. This change also contains a few bug fixes for dropping metatype arguments. rdar://110509780
1 parent fe87b99 commit 7839b54

15 files changed

+339
-84
lines changed

include/swift/SIL/GenericSpecializationMangler.h

+2-1
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

+9-7
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

+7-2
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

+1-1
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

+3-1
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;

0 commit comments

Comments
 (0)