Skip to content

Commit f702be9

Browse files
committed
SIL: cleanup the GenericSpecializationMangler API
NFC
1 parent e365fc5 commit f702be9

File tree

5 files changed

+82
-58
lines changed

5 files changed

+82
-58
lines changed

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,36 @@ class SpecializationMangler : public Mangle::ASTMangler {
6969
// The mangler for specialized generic functions.
7070
class GenericSpecializationMangler : public SpecializationMangler {
7171

72-
SubstitutionMap SubMap;
73-
bool isReAbstracted;
74-
bool isInlined;
75-
bool isPrespecializaton;
72+
GenericSpecializationMangler(std::string origFuncName)
73+
: SpecializationMangler(SpecializationPass::GenericSpecializer,
74+
IsNotSerialized, origFuncName) {}
75+
76+
GenericSignature getGenericSignature() {
77+
assert(Function && "Need a SIL function to get a generic signature");
78+
return Function->getLoweredFunctionType()->getInvocationGenericSignature();
79+
}
80+
81+
void appendSubstitutions(GenericSignature sig, SubstitutionMap subs);
82+
83+
std::string manglePrespecialized(GenericSignature sig,
84+
SubstitutionMap subs);
7685

7786
public:
78-
GenericSpecializationMangler(SILFunction *F, SubstitutionMap SubMap,
79-
IsSerialized_t Serialized, bool isReAbstracted,
80-
bool isInlined = false,
81-
bool isPrespecializaton = false)
87+
GenericSpecializationMangler(SILFunction *F, IsSerialized_t Serialized)
8288
: SpecializationMangler(SpecializationPass::GenericSpecializer,
83-
Serialized, F),
84-
SubMap(SubMap), isReAbstracted(isReAbstracted), isInlined(isInlined),
85-
isPrespecializaton(isPrespecializaton) {}
89+
Serialized, F) {}
8690

87-
GenericSpecializationMangler(std::string origFuncName, SubstitutionMap SubMap)
88-
: SpecializationMangler(SpecializationPass::GenericSpecializer,
89-
IsNotSerialized, origFuncName),
90-
SubMap(SubMap), isReAbstracted(true), isInlined(false),
91-
isPrespecializaton(true) {}
91+
std::string mangleNotReabstracted(SubstitutionMap subs);
92+
93+
std::string mangleReabstracted(SubstitutionMap subs);
9294

93-
std::string mangle(GenericSignature Sig = GenericSignature());
95+
std::string mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs,
96+
bool forInlining);
9497

95-
// TODO: This utility should move from the libswiftSILOptimizer to
96-
// libswiftSIL.
98+
std::string manglePrespecialized(SubstitutionMap subs) {
99+
return manglePrespecialized(getGenericSignature(), subs);
100+
}
101+
97102
static std::string manglePrespecialization(std::string unspecializedName,
98103
GenericSignature genericSig,
99104
GenericSignature specializedSig);

include/swift/SIL/TypeSubstCloner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,10 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
428428
return ParentFunction;
429429

430430
// Clone the function with the substituted type for the debug info.
431-
Mangle::GenericSpecializationMangler Mangler(
432-
ParentFunction, SubsMap, IsNotSerialized, false, ForInlining);
433-
std::string MangledName = Mangler.mangle(RemappedSig);
431+
Mangle::GenericSpecializationMangler Mangler(ParentFunction,
432+
IsNotSerialized);
433+
std::string MangledName =
434+
Mangler.mangleForDebugInfo(RemappedSig, SubsMap, ForInlining);
434435

435436
if (ParentFunction->getName() == MangledName)
436437
return ParentFunction;

lib/SIL/Utils/GenericSpecializationMangler.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,50 @@ std::string SpecializationMangler::finalize() {
7474
// Generic Specialization
7575
//===----------------------------------------------------------------------===//
7676

77-
std::string GenericSpecializationMangler::mangle(GenericSignature Sig) {
78-
beginMangling();
79-
80-
if (!Sig) {
81-
assert(Function &&
82-
"Need a SIL function if no generic signature is provided");
83-
SILFunctionType *FTy = Function->getLoweredFunctionType();
84-
Sig = FTy->getInvocationGenericSignature();
85-
}
86-
77+
void GenericSpecializationMangler::
78+
appendSubstitutions(GenericSignature sig, SubstitutionMap subs) {
8779
bool First = true;
88-
Sig->forEachParam([&](GenericTypeParamType *ParamType, bool Canonical) {
80+
sig->forEachParam([&](GenericTypeParamType *ParamType, bool Canonical) {
8981
if (Canonical) {
90-
appendType(Type(ParamType).subst(SubMap)->getCanonicalType());
82+
appendType(Type(ParamType).subst(subs)->getCanonicalType());
9183
appendListSeparator(First);
9284
}
9385
});
9486
assert(!First && "no generic substitutions");
87+
}
9588

96-
if (isInlined)
97-
appendSpecializationOperator("Ti");
98-
else
99-
appendSpecializationOperator(
100-
isPrespecializaton ? "Ts" : (isReAbstracted ? "Tg" : "TG"));
89+
std::string GenericSpecializationMangler::
90+
manglePrespecialized(GenericSignature sig, SubstitutionMap subs) {
91+
beginMangling();
92+
appendSubstitutions(sig, subs);
93+
appendSpecializationOperator("Ts");
94+
return finalize();
95+
}
96+
97+
std::string GenericSpecializationMangler::
98+
mangleNotReabstracted(SubstitutionMap subs) {
99+
beginMangling();
100+
appendSubstitutions(getGenericSignature(), subs);
101+
appendSpecializationOperator("TG");
101102
return finalize();
102103
}
104+
105+
std::string GenericSpecializationMangler::
106+
mangleReabstracted(SubstitutionMap subs) {
107+
beginMangling();
108+
appendSubstitutions(getGenericSignature(), subs);
109+
appendSpecializationOperator("Tg");
110+
return finalize();
111+
}
112+
113+
std::string GenericSpecializationMangler::
114+
mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs, bool forInlining) {
115+
beginMangling();
116+
appendSubstitutions(sig, subs);
117+
appendSpecializationOperator(forInlining ? "Ti" : "TG");
118+
return finalize();
119+
}
120+
103121

104122
static SubstitutionMap
105123
getSubstitutionMapForPrespecialization(GenericSignature genericSig,
@@ -132,6 +150,6 @@ std::string GenericSpecializationMangler::manglePrespecialization(
132150
GenericSignature specializedSig) {
133151
auto subs =
134152
getSubstitutionMapForPrespecialization(genericSig, specializedSig);
135-
GenericSpecializationMangler mangler(unspecializedName, subs);
136-
return mangler.mangle(genericSig);
153+
GenericSpecializationMangler mangler(unspecializedName);
154+
return mangler.manglePrespecialized(genericSig, subs);
137155
}

lib/SILOptimizer/IPO/UsePrespecialized.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ bool UsePrespecialized::replaceByPrespecialized(SILFunction &F) {
105105
// Create a name of the specialization. All external pre-specializations
106106
// are serialized without bodies. Thus use IsNotSerialized here.
107107
Mangle::GenericSpecializationMangler NewGenericMangler(ReferencedF,
108-
Subs, IsNotSerialized,
109-
/*isReAbstracted*/ true);
110-
std::string ClonedName = NewGenericMangler.mangle();
108+
IsNotSerialized);
109+
std::string ClonedName = NewGenericMangler.mangleReabstracted(Subs);
111110

112111
SILFunction *NewF = nullptr;
113112
// If we already have this specialization, reuse it.

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,12 @@ GenericFuncSpecializer::GenericFuncSpecializer(
18181818
ClonedName = Mangler.mangle();
18191819
} else {
18201820
Mangle::GenericSpecializationMangler Mangler(
1821-
GenericFunc, ParamSubs, ReInfo.isSerialized(), /*isReAbstracted*/ true,
1822-
/*isInlined*/ false, ReInfo.isPrespecialized());
1823-
ClonedName = Mangler.mangle();
1821+
GenericFunc, ReInfo.isSerialized());
1822+
if (ReInfo.isPrespecialized()) {
1823+
ClonedName = Mangler.manglePrespecialized(ParamSubs);
1824+
} else {
1825+
ClonedName = Mangler.mangleReabstracted(ParamSubs);
1826+
}
18241827
}
18251828
LLVM_DEBUG(llvm::dbgs() << " Specialized function " << ClonedName << '\n');
18261829
}
@@ -2140,11 +2143,9 @@ class ReabstractionThunkGenerator {
21402143
SpecializedFunc(SpecializedFunc), ReInfo(ReInfo), OrigPAI(OrigPAI),
21412144
Loc(RegularLocation::getAutoGeneratedLocation()) {
21422145
if (!ReInfo.isPartialSpecialization()) {
2143-
Mangle::GenericSpecializationMangler Mangler(
2144-
OrigF, ReInfo.getCalleeParamSubstitutionMap(), ReInfo.isSerialized(),
2145-
/*isReAbstracted*/ false);
2146-
2147-
ThunkName = Mangler.mangle();
2146+
Mangle::GenericSpecializationMangler Mangler(OrigF, ReInfo.isSerialized());
2147+
ThunkName = Mangler.mangleNotReabstracted(
2148+
ReInfo.getCalleeParamSubstitutionMap());
21482149
} else {
21492150
Mangle::PartialSpecializationMangler Mangler(
21502151
OrigF, ReInfo.getSpecializedType(), ReInfo.isSerialized(),
@@ -2462,14 +2463,14 @@ usePrespecialized(SILOptFunctionBuilder &funcBuilder, ApplySite apply,
24622463
if (specializedReInfo.getSpecializedType() != reInfo.getSpecializedType())
24632464
continue;
24642465

2465-
Mangle::GenericSpecializationMangler mangler(
2466-
refF, reInfo.getCalleeParamSubstitutionMap(), reInfo.isSerialized(),
2467-
/*isReAbstracted*/ true, /*isInlined*/ false,
2468-
reInfo.isPrespecialized());
2466+
SubstitutionMap subs = reInfo.getCalleeParamSubstitutionMap();
2467+
Mangle::GenericSpecializationMangler mangler(refF, reInfo.isSerialized());
2468+
std::string name = reInfo.isPrespecialized() ?
2469+
mangler.manglePrespecialized(subs) :
2470+
mangler.mangleReabstracted(subs);
24692471

24702472
prespecializedReInfo = reInfo;
2471-
return lookupOrCreatePrespecialization(funcBuilder, refF, mangler.mangle(),
2472-
reInfo);
2473+
return lookupOrCreatePrespecialization(funcBuilder, refF, name, reInfo);
24732474
}
24742475
return nullptr;
24752476
}

0 commit comments

Comments
 (0)