Skip to content

Commit 6a34ba5

Browse files
authored
Merge pull request #6415 from eeckstein/mangling-changes
Mangling: a few small changes and fixes
2 parents 59950a6 + a8eec59 commit 6a34ba5

File tree

10 files changed

+94
-26
lines changed

10 files changed

+94
-26
lines changed

docs/ABI.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ The type is the function type of the specialized function.
13431343

13441344
::
13451345

1346-
specialization ::= spec-arg* 'Tf' SPEC-INFO ARG-SPEC-KIND* '_' ARG-SPEC-KIND // Function signature specialization kind
1346+
specialization ::= spec-arg* 'Tf' SPEC-INFO UNIQUE-ID? ARG-SPEC-KIND* '_' ARG-SPEC-KIND // Function signature specialization kind
13471347

13481348
The ``<ARG-SPEC-KIND>`` describes how arguments are specialized.
13491349
Some kinds need arguments, which precede ``Tf``.
@@ -1364,6 +1364,8 @@ Some kinds need arguments, which precede ``Tf``.
13641364

13651365
FRAGILE ::= 'q'
13661366

1367+
UNIQUE-ID ::= NATURAL // Used to make unique function names
1368+
13671369
ARG-SPEC-KIND ::= 'n' // Unmodified argument
13681370
ARG-SPEC-KIND ::= 'c' // Consumes n 'type' arguments which are closed over types in argument order
13691371
// and one 'identifier' argument which is the closure symbol name

include/swift/Basic/Demangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ class Demangler {
223223
NodePointer addFuncSpecParamNumber(NodePointer Param,
224224
FunctionSigSpecializationParamKind Kind);
225225

226-
NodePointer demangleSpecAttributes(Node::Kind SpecKind);
226+
NodePointer demangleSpecAttributes(Node::Kind SpecKind,
227+
bool demangleUniqueID = false);
227228

228229
NodePointer demangleWitness();
229230
NodePointer demangleSpecialType();

include/swift/SILOptimizer/Utils/SpecializationMangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
150150
void setArgumentBoxToStack(unsigned ArgNo);
151151
void setReturnValueOwnedToUnowned();
152152

153-
std::string mangle();
153+
std::string mangle(int UniqueID = 0);
154154

155155
private:
156156
void mangleConstantProp(LiteralInst *LI);

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void ASTMangler::appendType(Type type) {
454454
TypeAliasDecl *decl = NameAliasTy->getDecl();
455455
if (decl->getModuleContext() == decl->getASTContext().TheBuiltinModule) {
456456
// It's not possible to mangle the context of the builtin module.
457-
return appendType(decl->getDeclaredInterfaceType());
457+
return appendType(NameAliasTy->getSinglyDesugaredType());
458458
}
459459

460460
// For the DWARF output we want to mangle the type alias + context,

lib/Basic/Demangle.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,12 +3610,22 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
36103610
NodePointer child0 = pointer->getChild(0);
36113611
NodePointer child1 = pointer->getChild(1);
36123612
NodePointer child2 = pointer->getChild(2);
3613-
print(child0);
3614-
if (Options.DisplayProtocolConformances) {
3613+
if (pointer->getNumChildren() == 4) {
3614+
// TODO: check if this is correct
3615+
Printer << "property behavior storage of ";
3616+
print(child2);
3617+
Printer << " in ";
3618+
print(child0);
36153619
Printer << " : ";
36163620
print(child1);
3617-
Printer << " in ";
3618-
print(child2);
3621+
} else {
3622+
print(child0);
3623+
if (Options.DisplayProtocolConformances) {
3624+
Printer << " : ";
3625+
print(child1);
3626+
Printer << " in ";
3627+
print(child2);
3628+
}
36193629
}
36203630
return;
36213631
}

lib/Basic/Demangler.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,13 @@ NodePointer Demangler::demangleMetatype() {
858858
case 'A':
859859
return createWithChild(Node::Kind::ReflectionMetadataAssocTypeDescriptor,
860860
popProtocolConformance());
861-
case 'C':
861+
case 'C': {
862+
NodePointer Ty = popNode(Node::Kind::Type);
863+
if (!Ty || !isNominal(Ty->getChild(0)->getKind()))
864+
return nullptr;
862865
return createWithChild(Node::Kind::ReflectionMetadataSuperclassDescriptor,
863-
popNode(isNominal));
866+
Ty->getChild(0));
867+
}
864868
default:
865869
return nullptr;
866870
}
@@ -1016,12 +1020,20 @@ NodePointer Demangler::popProtocolConformance() {
10161020
NodePointer Module = popModule();
10171021
NodePointer Proto = popProtocol();
10181022
NodePointer Type = popNode(Node::Kind::Type);
1023+
NodePointer Ident;
1024+
if (!Type) {
1025+
// Property behavior conformance
1026+
Ident = popNode(Node::Kind::Identifier);
1027+
Type = popNode(Node::Kind::Type);
1028+
}
10191029
if (GenSig) {
10201030
Type = createType(createWithChildren(Node::Kind::DependentGenericType,
10211031
GenSig, Type));
10221032
}
1023-
return createWithChildren(Node::Kind::ProtocolConformance,
1024-
Type, Proto, Module);
1033+
NodePointer Conf = createWithChildren(Node::Kind::ProtocolConformance,
1034+
Type, Proto, Module);
1035+
addChild(Conf, Ident);
1036+
return Conf;
10251037
}
10261038

10271039
NodePointer Demangler::demangleThunkOrSpecialization() {
@@ -1089,7 +1101,7 @@ NodePointer Demangler::demangleGenericSpecialization(Node::Kind SpecKind) {
10891101

10901102
NodePointer Demangler::demangleFunctionSpecialization() {
10911103
NodePointer Spec = demangleSpecAttributes(
1092-
Node::Kind::FunctionSignatureSpecialization);
1104+
Node::Kind::FunctionSignatureSpecialization, /*demangleUniqueID*/ true);
10931105
unsigned ParamIdx = 0;
10941106
while (Spec && !nextIf('_')) {
10951107
Spec = addChild(Spec, demangleFuncSpecParam(ParamIdx));
@@ -1226,15 +1238,27 @@ NodePointer Demangler::addFuncSpecParamNumber(NodePointer Param,
12261238
Node::Kind::FunctionSignatureSpecializationParamPayload, Str));
12271239
}
12281240

1229-
NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
1230-
NodePointer SpecNd = NodeFactory::create(SpecKind);
1231-
if (nextIf('q'))
1232-
SpecNd->addChild(NodeFactory::create(Node::Kind::SpecializationIsFragile));
1241+
NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind,
1242+
bool demangleUniqueID) {
1243+
bool isFragile = nextIf('q');
12331244

12341245
int PassID = (int)nextChar() - '0';
12351246
if (PassID < 0 || PassID > 9)
12361247
return nullptr;
12371248

1249+
int Idx = -1;
1250+
if (demangleUniqueID)
1251+
Idx = demangleNatural();
1252+
1253+
NodePointer SpecNd;
1254+
if (Idx >= 0) {
1255+
SpecNd = NodeFactory::create(SpecKind, Idx);
1256+
} else {
1257+
SpecNd = NodeFactory::create(SpecKind);
1258+
}
1259+
if (isFragile)
1260+
SpecNd->addChild(NodeFactory::create(Node::Kind::SpecializationIsFragile));
1261+
12381262
SpecNd->addChild(NodeFactory::create(Node::Kind::SpecializationPassID,
12391263
PassID));
12401264
return SpecNd;

lib/Basic/Mangler.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ static bool areTreesEqual(Demangle::NodePointer Old, Demangle::NodePointer New)
104104
return true;
105105
}
106106

107-
static bool containsDependentAssociatedTypeRef(Demangle::NodePointer Nd) {
108-
if (Nd->getKind() == Demangle::Node::Kind::DependentAssociatedTypeRef)
107+
static bool treeContains(Demangle::NodePointer Nd, Demangle::Node::Kind Kind) {
108+
if (Nd->getKind() == Kind)
109109
return true;
110110

111111
for (auto Child : *Nd) {
112-
if (containsDependentAssociatedTypeRef(Child))
112+
if (treeContains(Child, Kind))
113113
return true;
114114
}
115115
return false;
@@ -164,7 +164,13 @@ std::string NewMangling::selectMangling(const std::string &Old,
164164
NodePointer OldNode = demangleSymbolAsNode(Old);
165165
NodePointer NewNode = demangleSymbolAsNode(New);
166166

167-
if (OldNode) {
167+
if (StringRef(New).startswith(MANGLING_PREFIX_STR) &&
168+
(!NewNode || treeContains(NewNode, Demangle::Node::Kind::Suffix))) {
169+
llvm::errs() << "Can't demangle " << New << '\n';
170+
assert(false);
171+
}
172+
173+
if (OldNode && !treeContains(OldNode, Demangle::Node::Kind::Suffix)) {
168174
if (!areTreesEqual(OldNode, NewNode)) {
169175
llvm::errs() << "Mangling differs at #" << numCmp << ":\n"
170176
"old: " << Old << "\n"
@@ -180,7 +186,8 @@ std::string NewMangling::selectMangling(const std::string &Old,
180186
std::string Remangled = mangleNodeNew(NewNode);
181187
if (New != Remangled) {
182188
bool isEqual = false;
183-
if (containsDependentAssociatedTypeRef(NewNode) ||
189+
if (treeContains(NewNode,
190+
Demangle::Node::Kind::DependentAssociatedTypeRef) ||
184191
// Does the mangling contain an identifier which is the name of
185192
// an old-mangled function?
186193
New.find("_T") != std::string::npos) {
@@ -262,6 +269,11 @@ void NewMangling::printManglingStats() {
262269
}
263270

264271
void Mangler::beginMangling() {
272+
Storage.clear();
273+
Substitutions.clear();
274+
StringSubstitutions.clear();
275+
lastSubstIdx = -2;
276+
Words.clear();
265277
Buffer << MANGLING_PREFIX_STR;
266278
}
267279

lib/Basic/Remangler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@ void Remangler::mangleFunctionSignatureSpecialization(Node *node) {
860860
}
861861
}
862862
mangle(Child.get());
863+
864+
if (Child->getKind() == Node::Kind::SpecializationPassID &&
865+
node->hasIndex()) {
866+
Buffer << node->getIndex();
867+
}
863868
}
864869
if (!returnValMangled)
865870
Buffer << "_n";
@@ -1349,6 +1354,8 @@ void Remangler::mangleProtocolConformance(Node *node) {
13491354
Ty = Ty->getChild(1).get();
13501355
}
13511356
mangle(Ty);
1357+
if (node->getNumChildren() == 4)
1358+
mangleChildNode(node, 3);
13521359
manglePureProtocol(node->getChild(1).get());
13531360
mangleChildNode(node, 2);
13541361
if (GenSig)

lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,16 @@ std::string FunctionSignatureTransform::createOptimizedSILFunctionName() {
364364
}
365365

366366
OldFM.mangle();
367-
std::string Old = OldFM.getMangler().finalize();
368-
std::string New = NewFM.mangle();
367+
SILModule &M = F->getModule();
368+
std::string Old = getUniqueName(OldFM.getMangler().finalize(), M);
369+
370+
int UniqueID = 0;
371+
std::string New;
372+
do {
373+
New = NewFM.mangle(UniqueID);
374+
++UniqueID;
375+
} while (M.lookUpFunction(New));
376+
369377
return NewMangling::selectMangling(Old, New);
370378
}
371379

@@ -472,7 +480,7 @@ CanSILFunctionType FunctionSignatureTransform::createOptimizedSILFunctionType()
472480
void FunctionSignatureTransform::createFunctionSignatureOptimizedFunction() {
473481
// Create the optimized function !
474482
SILModule &M = F->getModule();
475-
std::string Name = getUniqueName(createOptimizedSILFunctionName(), M);
483+
std::string Name = createOptimizedSILFunctionName();
476484
SILLinkage linkage = F->getLinkage();
477485
if (isAvailableExternally(linkage))
478486
linkage = SILLinkage::Shared;

lib/SILOptimizer/Utils/SpecializationMangler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,13 @@ mangleReturnValue(ReturnValueModifierIntBase RetMod) {
309309
}
310310
}
311311

312-
std::string FunctionSignatureSpecializationMangler::mangle() {
312+
std::string FunctionSignatureSpecializationMangler::mangle(int UniqueID) {
313+
ArgOpStorage.clear();
313314
beginMangling();
314315

316+
if (UniqueID)
317+
ArgOpBuffer << UniqueID;
318+
315319
for (unsigned i : indices(Args)) {
316320
ArgumentModifierIntBase ArgMod;
317321
NullablePtr<SILInstruction> Inst;

0 commit comments

Comments
 (0)