Skip to content

Commit 424bc40

Browse files
authored
Merge pull request #40046 from etcwilde/ewilde/5.5/handle-missing-getMainExecutor-decl
5.5: Workaround to cope with older SDKs
2 parents 5d24d97 + f0c5c4b commit 424bc40

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,6 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
928928
FuncDecl *builtinDecl = cast<FuncDecl>(getBuiltinValueDecl(
929929
getASTContext(),
930930
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CreateAsyncTask))));
931-
932931
auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(),
933932
{TupleType::getEmpty(ctx)},
934933
ArrayRef<ProtocolConformanceRef>{});
@@ -953,6 +952,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
953952

954953
// Get swiftJobRun
955954
FuncDecl *swiftJobRunFuncDecl = SGM.getSwiftJobRun();
955+
assert(swiftJobRunFuncDecl && "Failed to find swift_job_run function decl");
956956
SILFunction *swiftJobRunSILFunc =
957957
SGM.getFunction(SILDeclRef(swiftJobRunFuncDecl, SILDeclRef::Kind::Func),
958958
NotForDefinition);
@@ -970,6 +970,28 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
970970

971971
// Get main executor
972972
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
973+
if (!getMainExecutorFuncDecl) {
974+
// If it doesn't exist due to an SDK-compiler mismatch, we can conjure one
975+
// up instead of crashing:
976+
// @available(SwiftStdlib 5.5, *)
977+
// @_silgen_name("swift_task_getMainExecutor")
978+
// internal func _getMainExecutor() -> Builtin.Executor
979+
980+
ParameterList *emptyParams = ParameterList::createEmpty(getASTContext());
981+
getMainExecutorFuncDecl = FuncDecl::createImplicit(
982+
getASTContext(), StaticSpellingKind::None,
983+
DeclName(
984+
getASTContext(),
985+
DeclBaseName(getASTContext().getIdentifier("_getMainExecutor")),
986+
/*Arguments*/ emptyParams),
987+
{}, /*async*/ false, /*throws*/ false, {}, emptyParams,
988+
getASTContext().TheExecutorType,
989+
entryPoint.getDecl()->getModuleContext());
990+
getMainExecutorFuncDecl->getAttrs().add(
991+
new (getASTContext())
992+
SILGenNameAttr("swift_task_getMainExecutor", /*implicit*/ true));
993+
}
994+
973995
SILFunction *getMainExeutorSILFunc = SGM.getFunction(
974996
SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func),
975997
NotForDefinition);
@@ -983,6 +1005,25 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
9831005

9841006
// Start Main loop!
9851007
FuncDecl *drainQueueFuncDecl = SGM.getAsyncMainDrainQueue();
1008+
if (!drainQueueFuncDecl) {
1009+
// If it doesn't exist, we can conjure one up instead of crashing
1010+
// @available(SwiftStdlib 5.5, *)
1011+
// @_silgen_name("swift_task_asyncMainDrainQueue")
1012+
// internal func _asyncMainDrainQueue() -> Never
1013+
ParameterList *emptyParams = ParameterList::createEmpty(getASTContext());
1014+
drainQueueFuncDecl = FuncDecl::createImplicit(
1015+
getASTContext(), StaticSpellingKind::None,
1016+
DeclName(
1017+
getASTContext(),
1018+
DeclBaseName(getASTContext().getIdentifier("_asyncMainDrainQueue")),
1019+
/*Arguments*/emptyParams),
1020+
{}, /*async*/ false, /*throws*/ false, {}, emptyParams,
1021+
getASTContext().getNeverType(),
1022+
entryPoint.getDecl()->getModuleContext());
1023+
drainQueueFuncDecl->getAttrs().add(
1024+
new (getASTContext()) SILGenNameAttr("swift_task_asyncMainDrainQueue", /*implicit*/ true));
1025+
}
1026+
9861027
SILFunction *drainQueueSILFunc = SGM.getFunction(
9871028
SILDeclRef(drainQueueFuncDecl, SILDeclRef::Kind::Func), NotForDefinition);
9881029
SILValue drainQueueFunc =

0 commit comments

Comments
 (0)