Skip to content

Commit 189a38d

Browse files
authored
Merge pull request #23985 from slavapestov/dynamic-self-thunks
SILGen: Fix function conversions involving DynamicSelfType
2 parents 6f3a812 + 099bd8e commit 189a38d

19 files changed

+194
-118
lines changed

docs/ABI/Mangling.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ types where the metadata itself has unknown layout.)
191191
global ::= global specialization // function specialization
192192
global ::= global 'Tm' // merged function
193193
global ::= entity // some identifiable thing
194-
global ::= type type generic-signature? 'T' REABSTRACT-THUNK-TYPE // reabstraction thunk helper function
194+
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
195+
global ::= from-type to-type self-type generic-signature? 'Ty' // reabstraction thunk with dynamic 'Self' capture
196+
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
195197
global ::= entity generic-signature? type type* 'TK' // key path getter
196198
global ::= entity generic-signature? type type* 'Tk' // key path setter
197199
global ::= type generic-signature 'TH' // key path equality
@@ -204,11 +206,11 @@ types where the metadata itself has unknown layout.)
204206
global ::= type assoc-type-list protocol 'TN' // default associated conformance witness accessor
205207
global ::= type protocol 'Tb' // base conformance descriptor
206208

207-
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk helper function
208-
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk
209+
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk
210+
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk (obsolete)
209211

210-
The types in a reabstraction thunk helper function are always non-polymorphic
211-
``<impl-function-type>`` types.
212+
The `from-type` and `to-type` in a reabstraction thunk helper function
213+
are always non-polymorphic ``<impl-function-type>`` types.
212214

213215
::
214216

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class ASTMangler : public Mangler {
143143

144144
std::string mangleReabstractionThunkHelper(CanSILFunctionType ThunkType,
145145
Type FromType, Type ToType,
146+
Type SelfType,
146147
ModuleDecl *Module);
147148

148149
std::string mangleKeyPathGetterThunkHelper(const AbstractStorageDecl *property,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ NODE(ProtocolWitnessTableAccessor)
176176
NODE(ProtocolWitnessTablePattern)
177177
NODE(ReabstractionThunk)
178178
NODE(ReabstractionThunkHelper)
179+
NODE(ReabstractionThunkHelperWithSelf)
179180
CONTEXT_NODE(ReadAccessor)
180181
NODE(RelatedEntityDeclName)
181182
NODE(RetroactiveConformance)

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
344344
CanSILFunctionType ThunkType,
345345
Type FromType,
346346
Type ToType,
347+
Type SelfType,
347348
ModuleDecl *Module) {
348349
Mod = Module;
349350
GenericSignature *GenSig = ThunkType->getGenericSignature();
@@ -353,10 +354,17 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
353354
beginMangling();
354355
appendType(FromType);
355356
appendType(ToType);
357+
if (SelfType)
358+
appendType(SelfType);
359+
356360
if (GenSig)
357361
appendGenericSignature(GenSig);
358-
// TODO: mangle ThunkType->isPseudogeneric()
359-
appendOperator("TR");
362+
363+
if (SelfType)
364+
appendOperator("Ty");
365+
else
366+
appendOperator("TR");
367+
360368
return finalize();
361369
}
362370

lib/Demangling/Demangler.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,15 +2057,20 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
20572057
return createWithChild(Node::Kind::ProtocolSelfConformanceWitness,
20582058
popNode(isEntity));
20592059
case 'R':
2060-
case 'r': {
2061-
NodePointer Thunk = createNode(c == 'R' ?
2062-
Node::Kind::ReabstractionThunkHelper :
2063-
Node::Kind::ReabstractionThunk);
2060+
case 'r':
2061+
case 'y': {
2062+
Node::Kind kind;
2063+
if (c == 'R') kind = Node::Kind::ReabstractionThunkHelper;
2064+
else if (c == 'y') kind = Node::Kind::ReabstractionThunkHelperWithSelf;
2065+
else kind = Node::Kind::ReabstractionThunk;
2066+
NodePointer Thunk = createNode(kind);
20642067
if (NodePointer GenSig = popNode(Node::Kind::DependentGenericSignature))
20652068
addChild(Thunk, GenSig);
2066-
NodePointer Ty2 = popNode(Node::Kind::Type);
2067-
Thunk = addChild(Thunk, popNode(Node::Kind::Type));
2068-
return addChild(Thunk, Ty2);
2069+
if (kind == Node::Kind::ReabstractionThunkHelperWithSelf)
2070+
addChild(Thunk, popNode(Node::Kind::Type));
2071+
addChild(Thunk, popNode(Node::Kind::Type));
2072+
addChild(Thunk, popNode(Node::Kind::Type));
2073+
return Thunk;
20692074
}
20702075
case 'g':
20712076
return demangleGenericSpecialization(Node::Kind::GenericSpecialization);

lib/Demangling/NodePrinter.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ class NodePrinter {
435435
case Node::Kind::ProtocolWitnessTablePattern:
436436
case Node::Kind::ReabstractionThunk:
437437
case Node::Kind::ReabstractionThunkHelper:
438+
case Node::Kind::ReabstractionThunkHelperWithSelf:
438439
case Node::Kind::ReadAccessor:
439440
case Node::Kind::RelatedEntityDeclName:
440441
case Node::Kind::RetroactiveConformance:
@@ -1536,22 +1537,40 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
15361537
case Node::Kind::ReabstractionThunkHelper: {
15371538
if (Options.ShortenThunk) {
15381539
Printer << "thunk for ";
1539-
print(Node->getChild(Node->getNumChildren() - 2));
1540+
print(Node->getChild(Node->getNumChildren() - 1));
15401541
return nullptr;
15411542
}
15421543
Printer << "reabstraction thunk ";
15431544
if (Node->getKind() == Node::Kind::ReabstractionThunkHelper)
15441545
Printer << "helper ";
1545-
auto generics = getFirstChildOfKind(Node, Node::Kind::DependentGenericSignature);
1546-
assert(Node->getNumChildren() == 2 + unsigned(generics != nullptr));
1547-
if (generics) {
1546+
unsigned idx = 0;
1547+
if (Node->getNumChildren() == 3) {
1548+
auto generics = Node->getChild(0);
1549+
idx = 1;
1550+
print(generics);
1551+
Printer << " ";
1552+
}
1553+
Printer << "from ";
1554+
print(Node->getChild(idx + 1));
1555+
Printer << " to ";
1556+
print(Node->getChild(idx));
1557+
return nullptr;
1558+
}
1559+
case Node::Kind::ReabstractionThunkHelperWithSelf: {
1560+
Printer << "reabstraction thunk ";
1561+
unsigned idx = 0;
1562+
if (Node->getNumChildren() == 4) {
1563+
auto generics = Node->getChild(0);
1564+
idx = 1;
15481565
print(generics);
15491566
Printer << " ";
15501567
}
15511568
Printer << "from ";
1552-
print(Node->getChild(Node->getNumChildren() - 2));
1569+
print(Node->getChild(idx + 2));
15531570
Printer << " to ";
1554-
print(Node->getChild(Node->getNumChildren() - 1));
1571+
print(Node->getChild(idx + 1));
1572+
Printer << " self ";
1573+
print(Node->getChild(idx));
15551574
return nullptr;
15561575
}
15571576
case Node::Kind::MergedFunction:

lib/Demangling/OldRemangler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,15 +715,15 @@ void Remangler::mangleBaseWitnessTableAccessor(Node *node) {
715715
}
716716

717717
void Remangler::mangleReabstractionThunkHelper(Node *node) {
718-
Buffer << "TR";
719-
if (node->getNumChildren() == 3) Buffer << 'G';
720-
mangleChildNodes(node); // generic signature?, type, type
718+
Buffer << "<reabstraction-thunk-helper>";
719+
}
720+
721+
void Remangler::mangleReabstractionThunkHelperWithSelf(Node *node) {
722+
Buffer << "<reabstraction-thunk-helper-with-self>";
721723
}
722724

723725
void Remangler::mangleReabstractionThunk(Node *node) {
724-
Buffer << "Tr";
725-
if (node->getNumChildren() == 3) Buffer << 'G';
726-
mangleChildNodes(node); // generic signature?, type, type
726+
Buffer << "<reabstraction-thunk>";
727727
}
728728

729729
void Remangler::mangleProtocolSelfConformanceWitness(Node *node) {

lib/Demangling/Remangler.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,27 +1856,20 @@ void Remangler::mangleProtocolWitnessTableAccessor(Node *node) {
18561856
}
18571857

18581858
void Remangler::mangleReabstractionThunk(Node *node) {
1859-
if (node->getNumChildren() == 3) {
1860-
mangleChildNode(node, 1); // type 1
1861-
mangleChildNode(node, 2); // type 2
1862-
mangleChildNode(node, 0); // generic signature
1863-
} else {
1864-
mangleChildNodes(node);
1865-
}
1859+
mangleChildNodesReversed(node);
18661860
Buffer << "Tr";
18671861
}
18681862

18691863
void Remangler::mangleReabstractionThunkHelper(Node *node) {
1870-
if (node->getNumChildren() == 3) {
1871-
mangleChildNode(node, 1); // type 1
1872-
mangleChildNode(node, 2); // type 2
1873-
mangleChildNode(node, 0); // generic signature
1874-
} else {
1875-
mangleChildNodes(node);
1876-
}
1864+
mangleChildNodesReversed(node);
18771865
Buffer << "TR";
18781866
}
18791867

1868+
void Remangler::mangleReabstractionThunkHelperWithSelf(Node *node) {
1869+
mangleChildNodesReversed(node);
1870+
Buffer << "Ty";
1871+
}
1872+
18801873
void Remangler::mangleReadAccessor(Node *node) {
18811874
mangleAbstractStorage(node->getFirstChild(), "r");
18821875
}

lib/SIL/InstructionUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,10 @@ bool swift::isSanitizerInstrumentation(SILInstruction *Instruction) {
352352
}
353353

354354
SILValue swift::isPartialApplyOfReabstractionThunk(PartialApplyInst *PAI) {
355-
if (PAI->getNumArguments() != 1)
355+
// A partial_apply of a reabstraction thunk either has a single capture
356+
// (a function) or two captures (function and dynamic Self type).
357+
if (PAI->getNumArguments() != 1 &&
358+
PAI->getNumArguments() != 2)
356359
return SILValue();
357360

358361
auto *Fun = PAI->getReferencedFunction();

lib/SILGen/SILGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
173173
CanSILFunctionType thunkType,
174174
CanSILFunctionType fromType,
175175
CanSILFunctionType toType,
176-
IsSerialized_t Serialized);
176+
CanType dynamicSelfType);
177177

178178
/// Determine whether the given class has any instance variables that
179179
/// need to be destroyed.

lib/SILGen/SILGenBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
577577
auto thunk = SGM.getOrCreateReabstractionThunk(invokeTy,
578578
loweredFuncTy,
579579
loweredBlockTy,
580-
F.isSerialized());
580+
/*dynamicSelfType=*/CanType());
581581

582582
// Build it if necessary.
583583
if (thunk->empty()) {
@@ -937,7 +937,7 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
937937
auto thunk = SGM.getOrCreateReabstractionThunk(thunkTy,
938938
loweredBlockTy,
939939
loweredFuncTyWithoutNoEscape,
940-
F.isSerialized());
940+
/*dynamicSelfType=*/CanType());
941941

942942
// Build it if necessary.
943943
if (thunk->empty()) {

0 commit comments

Comments
 (0)