Skip to content

Commit 2862531

Browse files
authored
Merge pull request #39941 from etcwilde/ewilde/concurrency/handle-missing-main-executor-decl
Workaround to cope with older SDK missing `getMainExecutor`
2 parents 2d179ce + e10bb9f commit 2862531

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
882882
FuncDecl *builtinDecl = cast<FuncDecl>(getBuiltinValueDecl(
883883
getASTContext(),
884884
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CreateAsyncTask))));
885-
886885
auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(),
887886
{TupleType::getEmpty(ctx)},
888887
ArrayRef<ProtocolConformanceRef>{});
@@ -907,6 +906,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
907906

908907
// Get swiftJobRun
909908
FuncDecl *swiftJobRunFuncDecl = SGM.getSwiftJobRun();
909+
assert(swiftJobRunFuncDecl && "Failed to find swift_job_run function decl");
910910
SILFunction *swiftJobRunSILFunc =
911911
SGM.getFunction(SILDeclRef(swiftJobRunFuncDecl, SILDeclRef::Kind::Func),
912912
NotForDefinition);
@@ -924,6 +924,28 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
924924

925925
// Get main executor
926926
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
927+
if (!getMainExecutorFuncDecl) {
928+
// If it doesn't exist due to an SDK-compiler mismatch, we can conjure one
929+
// up instead of crashing:
930+
// @available(SwiftStdlib 5.5, *)
931+
// @_silgen_name("swift_task_getMainExecutor")
932+
// internal func _getMainExecutor() -> Builtin.Executor
933+
934+
ParameterList *emptyParams = ParameterList::createEmpty(getASTContext());
935+
getMainExecutorFuncDecl = FuncDecl::createImplicit(
936+
getASTContext(), StaticSpellingKind::None,
937+
DeclName(
938+
getASTContext(),
939+
DeclBaseName(getASTContext().getIdentifier("_getMainExecutor")),
940+
/*Arguments*/ emptyParams),
941+
{}, /*async*/ false, /*throws*/ false, {}, emptyParams,
942+
getASTContext().TheExecutorType,
943+
entryPoint.getDecl()->getModuleContext());
944+
getMainExecutorFuncDecl->getAttrs().add(
945+
new (getASTContext())
946+
SILGenNameAttr("swift_task_getMainExecutor", /*implicit*/ true));
947+
}
948+
927949
SILFunction *getMainExeutorSILFunc = SGM.getFunction(
928950
SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func),
929951
NotForDefinition);

0 commit comments

Comments
 (0)