Skip to content

Commit d0a9e78

Browse files
committed
[Mangling] Support function specializations that remove async
1 parent e6a8ec9 commit d0a9e78

File tree

10 files changed

+55
-4
lines changed

10 files changed

+55
-4
lines changed

docs/ABI/Mangling.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ Function Specializations
11501150
specialization ::= type '_' type* 'Ts' SPEC-INFO // Generic re-abstracted prespecialization
11511151
specialization ::= type '_' type* 'TG' SPEC-INFO // Generic not re-abstracted specialization
11521152
specialization ::= type '_' type* 'Ti' SPEC-INFO // Inlined function with generic substitutions.
1153+
specialization ::= type '_' type* 'Ta' SPEC-INFO // Non-async specialization
11531154

11541155
The types are the replacement types of the substitution list.
11551156

@@ -1172,19 +1173,23 @@ Some kinds need arguments, which precede ``Tf``.
11721173
spec-arg ::= identifier
11731174
spec-arg ::= type
11741175

1175-
SPEC-INFO ::= MT-REMOVED? FRAGILE? PASSID
1176+
SPEC-INFO ::= MT-REMOVED? FRAGILE? ASYNC-REMOVED? PASSID
11761177

11771178
PASSID ::= '0' // AllocBoxToStack,
11781179
PASSID ::= '1' // ClosureSpecializer,
11791180
PASSID ::= '2' // CapturePromotion,
11801181
PASSID ::= '3' // CapturePropagation,
11811182
PASSID ::= '4' // FunctionSignatureOpts,
11821183
PASSID ::= '5' // GenericSpecializer,
1184+
PASSID ::= '6' // MoveDiagnosticInOutToOut,
1185+
PASSID ::= '7' // AsyncDemotion,
11831186

11841187
MT-REMOVED ::= 'm' // non-generic metatype arguments are removed in the specialized function
11851188

11861189
FRAGILE ::= 'q'
11871190

1191+
ASYNC-REMOVED ::= 'a' // async effect removed
1192+
11881193
ARG-SPEC-KIND ::= 'n' // Unmodified argument
11891194
ARG-SPEC-KIND ::= 'c' // Consumes n 'type' arguments which are closed over types in argument order
11901195
// and one 'identifier' argument which is the closure symbol name

include/swift/Demangling/Demangle.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,24 @@ enum class MangledDifferentiabilityKind : char {
139139
/// that two passes that generate similar changes do not yield the same
140140
/// mangling. This currently cannot happen, so this is just a safety measure
141141
/// that creates separate name spaces.
142+
///
143+
/// The number of entries is limited! See `Demangler::demangleSpecAttributes`.
144+
/// If you exceed the max, you'll need to upgrade the mangling.
142145
enum class SpecializationPass : uint8_t {
143-
AllocBoxToStack,
146+
AllocBoxToStack = 0,
144147
ClosureSpecializer,
145148
CapturePromotion,
146149
CapturePropagation,
147150
FunctionSignatureOpts,
148151
GenericSpecializer,
149152
MoveDiagnosticInOutToOut,
153+
AsyncDemotion,
154+
LAST = AsyncDemotion
150155
};
151156

157+
constexpr uint8_t MAX_SPECIALIZATION_PASS = 10;
158+
static_assert((uint8_t)SpecializationPass::LAST < MAX_SPECIALIZATION_PASS);
159+
152160
static inline char encodeSpecializationPass(SpecializationPass Pass) {
153161
return char(uint8_t(Pass)) + '0';
154162
}

include/swift/Demangling/DemangleNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,5 +368,8 @@ NODE(HasSymbolQuery)
368368
NODE(OpaqueReturnTypeIndex)
369369
NODE(OpaqueReturnTypeParent)
370370

371+
// Added in Swift 5.9 + 1
372+
NODE(AsyncRemoved)
373+
371374
#undef CONTEXT_NODE
372375
#undef NODE

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_SIL_UTILS_GENERICSPECIALIZATIONMANGLER_H
1515

1616
#include "swift/AST/ASTMangler.h"
17+
#include "swift/AST/Effects.h"
1718
#include "swift/Basic/NullablePtr.h"
1819
#include "swift/Demangling/Demangler.h"
1920
#include "swift/SIL/SILFunction.h"
@@ -45,6 +46,9 @@ class SpecializationMangler : public Mangle::ASTMangler {
4546
llvm::SmallVector<char, 32> ArgOpStorage;
4647
llvm::raw_svector_ostream ArgOpBuffer;
4748

49+
// Effects that are removed from the original function in this specialization.
50+
PossibleEffects RemovedEffects;
51+
4852
protected:
4953
SpecializationMangler(SpecializationPass P, IsSerialized_t Serialized,
5054
SILFunction *F)

include/swift/SILOptimizer/Utils/SpecializationMangler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PartialSpecializationMangler : public SpecializationMangler {
4242
std::string mangle();
4343
};
4444

45-
// The mangler for functions where arguments are specialized.
45+
// The mangler for functions where arguments or effects are specialized.
4646
class FunctionSignatureSpecializationMangler : public SpecializationMangler {
4747

4848
using ReturnValueModifierIntBase = uint16_t;
@@ -95,6 +95,7 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
9595
FunctionSignatureSpecializationMangler(SpecializationPass Pass,
9696
IsSerialized_t Serialized,
9797
SILFunction *F);
98+
// For arguments / return values
9899
void setArgumentConstantProp(unsigned OrigArgIdx, SILInstruction *constInst);
99100
void appendStringAsIdentifier(StringRef str);
100101

@@ -111,6 +112,9 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
111112
void setArgumentInOutToOut(unsigned OrigArgIdx);
112113
void setReturnValueOwnedToUnowned();
113114

115+
// For effects
116+
void setRemovedEffect(EffectKind effect);
117+
114118
std::string mangle();
115119

116120
private:

lib/Demangling/Demangler.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3286,10 +3286,13 @@ NodePointer Demangler::addFuncSpecParamNumber(NodePointer Param,
32863286
NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
32873287
bool metatypeParamsRemoved = nextIf('m');
32883288
bool isSerialized = nextIf('q');
3289+
bool asyncRemoved = nextIf('a');
32893290

32903291
int PassID = (int)nextChar() - '0';
3291-
if (PassID < 0 || PassID > 9)
3292+
if (PassID < 0 || PassID >= MAX_SPECIALIZATION_PASS) {
3293+
assert(false && "unexpected pass id");
32923294
return nullptr;
3295+
}
32933296

32943297
NodePointer SpecNd = createNode(SpecKind);
32953298

@@ -3300,6 +3303,10 @@ NodePointer Demangler::demangleSpecAttributes(Node::Kind SpecKind) {
33003303
SpecNd->addChild(createNode(Node::Kind::IsSerialized),
33013304
*this);
33023305

3306+
if (asyncRemoved)
3307+
SpecNd->addChild(createNode(Node::Kind::AsyncRemoved),
3308+
*this);
3309+
33033310
SpecNd->addChild(createNode(Node::Kind::SpecializationPassID, PassID),
33043311
*this);
33053312
return SpecNd;

lib/Demangling/NodePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ class NodePrinter {
353353
case Node::Kind::AssociatedTypeDescriptor:
354354
case Node::Kind::AssociatedTypeMetadataAccessor:
355355
case Node::Kind::AssociatedTypeWitnessTableAccessor:
356+
case Node::Kind::AsyncRemoved:
356357
case Node::Kind::AutoClosureType:
357358
case Node::Kind::BaseConformanceDescriptor:
358359
case Node::Kind::BaseWitnessTableAccessor:
@@ -1307,6 +1308,10 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
13071308
Printer << "static ";
13081309
print(Node->getChild(0), depth + 1);
13091310
return nullptr;
1311+
case Node::Kind::AsyncRemoved:
1312+
Printer << "async demotion of ";
1313+
print(Node->getChild(0), depth + 1);
1314+
return nullptr;
13101315
case Node::Kind::CurryThunk:
13111316
Printer << "curry thunk of ";
13121317
print(Node->getChild(0), depth + 1);

lib/Demangling/OldRemangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
446446
return ManglingError::Success;
447447
}
448448

449+
ManglingError Remangler::mangleAsyncRemoved(Node *node, unsigned depth) {
450+
Buffer << "a";
451+
return ManglingError::Success;
452+
}
453+
449454
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
450455
Buffer << "m";
451456
return ManglingError::Success;

lib/Demangling/Remangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,11 @@ ManglingError Remangler::mangleIsSerialized(Node *node, unsigned depth) {
28862886
return ManglingError::Success;
28872887
}
28882888

2889+
ManglingError Remangler::mangleAsyncRemoved(Node *node, unsigned depth) {
2890+
Buffer << 'a';
2891+
return ManglingError::Success;
2892+
}
2893+
28892894
ManglingError Remangler::mangleMetatypeParamsRemoved(Node *node, unsigned depth) {
28902895
Buffer << 'm';
28912896
return ManglingError::Success;

lib/SIL/Utils/GenericSpecializationMangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ using namespace Mangle;
2121

2222
void SpecializationMangler::beginMangling() {
2323
ASTMangler::beginManglingWithoutPrefix();
24+
2425
if (Serialized)
2526
ArgOpBuffer << 'q';
27+
28+
if (RemovedEffects.contains(EffectKind::Async))
29+
ArgOpBuffer << 'a';
30+
2631
ArgOpBuffer << char(uint8_t(Pass) + '0');
2732
}
2833

0 commit comments

Comments
 (0)