Skip to content

Commit 0e0665b

Browse files
committed
[concurrency] Add support for HopToMainActorIfNeededThunk.
It is behind the experimental flag GenerateForceToMainActorThunks.
1 parent f0f5ad5 commit 0e0665b

25 files changed

+575
-25
lines changed

include/swift/AST/SILThunkKind.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ struct SILThunkKind {
3434
/// purposes of the underlying thunking machinery.
3535
Identity = 1,
3636

37-
MaxValue = Identity,
37+
/// A thunk that checks if a value is on the main actor and if it isn't
38+
/// jumps to the main actor. It expects that the passed in function does not
39+
/// have any arguments, results, is synchronous, and does not throw.
40+
HopToMainActorIfNeeded = 2,
41+
42+
MaxValue = HopToMainActorIfNeeded,
3843
};
3944

4045
InnerTy innerTy;
@@ -68,6 +73,8 @@ struct SILThunkKind {
6873
return Demangle::MangledSILThunkKind::Invalid;
6974
case Identity:
7075
return Demangle::MangledSILThunkKind::Identity;
76+
case HopToMainActorIfNeeded:
77+
return Demangle::MangledSILThunkKind::HopToMainActorIfNeeded;
7178
}
7279
}
7380
};

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ EXPERIMENTAL_FEATURE(CoroutineAccessorsUnwindOnCallerError, false)
433433
/// modify/read coroutines use the callee-allocated ABI
434434
EXPERIMENTAL_FEATURE(CoroutineAccessorsAllocateInCallee, false)
435435

436+
// When a parameter has unspecified isolation, infer it as main actor isolated.
437+
EXPERIMENTAL_FEATURE(GenerateForceToMainActorThunks, false)
438+
436439
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
437440
#undef EXPERIMENTAL_FEATURE
438441
#undef UPCOMING_FEATURE

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ enum class MangledDifferentiabilityKind : char {
139139
enum class MangledSILThunkKind : char {
140140
Invalid = 0,
141141
Identity = 'I',
142+
HopToMainActorIfNeeded = 'H',
142143
};
143144

144145
/// The pass that caused the specialization to occur. We use this to make sure

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ NODE(ReflectionMetadataSuperclassDescriptor)
292292
NODE(GenericTypeParamDecl)
293293
NODE(CurryThunk)
294294
NODE(SILThunkIdentity)
295+
NODE(SILThunkHopToMainActorIfNeeded)
295296
NODE(DispatchThunk)
296297
NODE(MethodDescriptor)
297298
NODE(ProtocolRequirementsBaseDescriptor)

include/swift/SIL/SILBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,13 @@ class SILBuilder {
12511251
return createThunk(Loc, Op, ThunkInst::Kind::Identity, substitutionMap);
12521252
}
12531253

1254+
ThunkInst *
1255+
createHopToMainActorIfNeededThunk(SILLocation Loc, SILValue Op,
1256+
SubstitutionMap substitutionMap = {}) {
1257+
return createThunk(Loc, Op, ThunkInst::Kind::HopToMainActorIfNeeded,
1258+
substitutionMap);
1259+
}
1260+
12541261
ConvertEscapeToNoEscapeInst *
12551262
createConvertEscapeToNoEscape(SILLocation Loc, SILValue Op, SILType Ty,
12561263
bool lifetimeGuaranteed) {

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ UNINTERESTING_FEATURE(SameElementRequirements)
199199
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
200200
UNINTERESTING_FEATURE(GlobalActorInferenceCutoff)
201201
UNINTERESTING_FEATURE(KeyPathWithStaticMembers)
202+
UNINTERESTING_FEATURE(GenerateForceToMainActorThunks)
202203

203204
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
204205
auto isFunctionTypeWithSending = [](Type type) {

lib/Demangling/Demangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,9 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
27962796
switch (char c = nextChar()) {
27972797
case 'I':
27982798
return createWithChild(Node::Kind::SILThunkIdentity, popNode(isEntity));
2799+
case 'H':
2800+
return createWithChild(Node::Kind::SILThunkHopToMainActorIfNeeded,
2801+
popNode(isEntity));
27992802
default:
28002803
return nullptr;
28012804
}

lib/Demangling/NodePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ class NodePrinter {
368368
case Node::Kind::CoroutineContinuationPrototype:
369369
case Node::Kind::CurryThunk:
370370
case Node::Kind::SILThunkIdentity:
371+
case Node::Kind::SILThunkHopToMainActorIfNeeded:
371372
case Node::Kind::DispatchThunk:
372373
case Node::Kind::Deallocator:
373374
case Node::Kind::IsolatedDeallocator:
@@ -1430,6 +1431,10 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
14301431
Printer << "identity thunk of ";
14311432
print(Node->getChild(0), depth + 1);
14321433
return nullptr;
1434+
case Node::Kind::SILThunkHopToMainActorIfNeeded:
1435+
Printer << "hop to main actor thunk of ";
1436+
print(Node->getChild(0), depth + 1);
1437+
return nullptr;
14331438
case Node::Kind::DispatchThunk:
14341439
Printer << "dispatch thunk of ";
14351440
print(Node->getChild(0), depth + 1);

lib/Demangling/OldRemangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,12 @@ ManglingError Remangler::mangleSILThunkIdentity(Node *node, unsigned depth) {
25302530
return ManglingError::Success;
25312531
}
25322532

2533+
ManglingError Remangler::mangleSILThunkHopToMainActorIfNeeded(Node *node,
2534+
unsigned depth) {
2535+
Buffer << "<sil-hop-to-main-actor-if-needed-thunk>";
2536+
return ManglingError::Success;
2537+
}
2538+
25332539
ManglingError Remangler::mangleDispatchThunk(Node *node, unsigned depth) {
25342540
Buffer << "<dispatch-thunk>";
25352541
return ManglingError::Success;

lib/Demangling/Remangler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,14 @@ ManglingError Remangler::mangleSILThunkIdentity(Node *node, unsigned depth) {
33823382
return ManglingError::Success;
33833383
}
33843384

3385+
ManglingError Remangler::mangleSILThunkHopToMainActorIfNeeded(Node *node,
3386+
unsigned depth) {
3387+
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1)); // type
3388+
Buffer << "TT"
3389+
<< "H";
3390+
return ManglingError::Success;
3391+
}
3392+
33853393
ManglingError Remangler::mangleDispatchThunk(Node *node, unsigned depth) {
33863394
RETURN_IF_ERROR(mangleSingleChildNode(node, depth + 1));
33873395
Buffer << "Tj";

0 commit comments

Comments
 (0)