Skip to content

Commit 862ef62

Browse files
committed
[silgen] Make async_Main compatible with calling nonisolated(nonsending) functions.
The problem is that async_Main was setting an executor as its main executor instead of an actor. This patch fixes the issue by just grabbing the main actor instead. rdar://153082633
1 parent 286f975 commit 862ef62

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

lib/SILGen/SILGenTopLevel.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,31 @@ void SILGenModule::emitEntryPoint(SourceFile *SF, SILFunction *TopLevel) {
6565

6666
auto prologueLoc = RegularLocation::getModuleLocation();
6767
prologueLoc.markAsPrologue();
68-
if (SF->isAsyncContext()) {
69-
// emitAsyncMainThreadStart will create argc and argv.
70-
// Just set the main actor as the expected executor; we should
71-
// already be running on it.
72-
SILValue executor = TopLevelSGF.emitMainExecutor(prologueLoc);
73-
TopLevelSGF.ExpectedExecutor.set(TopLevelSGF.B.createOptionalSome(
74-
prologueLoc, executor, SILType::getOptionalType(executor->getType())));
75-
} else {
76-
// Create the argc and argv arguments.
77-
auto entry = TopLevelSGF.B.getInsertionBB();
78-
auto context = TopLevelSGF.getTypeExpansionContext();
79-
auto paramTypeIter =
80-
TopLevelSGF.F.getConventions().getParameterSILTypes(context).begin();
81-
82-
entry->createFunctionArgument(*paramTypeIter);
83-
entry->createFunctionArgument(*std::next(paramTypeIter));
84-
}
8568

8669
{
8770
Scope S(TopLevelSGF.Cleanups, moduleCleanupLoc);
71+
72+
if (SF->isAsyncContext()) {
73+
// emitAsyncMainThreadStart will create argc and argv.
74+
// Just set the main actor as the expected executor; we should
75+
// already be running on it.
76+
auto mainActorType =
77+
getASTContext().getMainActorType()->getCanonicalType();
78+
TopLevelSGF.ExpectedExecutor.set(
79+
TopLevelSGF.emitGlobalActorIsolation(prologueLoc, mainActorType)
80+
.borrow(TopLevelSGF, prologueLoc)
81+
.getValue());
82+
} else {
83+
// Create the argc and argv arguments.
84+
auto entry = TopLevelSGF.B.getInsertionBB();
85+
auto context = TopLevelSGF.getTypeExpansionContext();
86+
auto paramTypeIter =
87+
TopLevelSGF.F.getConventions().getParameterSILTypes(context).begin();
88+
89+
entry->createFunctionArgument(*paramTypeIter);
90+
entry->createFunctionArgument(*std::next(paramTypeIter));
91+
}
92+
8893
SILGenTopLevel(TopLevelSGF).visitSourceFile(SF);
8994
}
9095

test/SILGen/toplevel_globalactorvars.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
// a
44
// CHECK-LABEL: sil_global hidden @$s24toplevel_globalactorvars1aSivp : $Int
55

6-
// CHECK-LABEL: sil private [ossa] @async_Main
6+
// CHECK-LABEL: sil private [ossa] @async_Main : $@convention(thin) @async () -> () {
77
// CHECK: bb0:
8-
// CHECK-NEXT: [[MAIN:%.*]] = builtin "buildMainActorExecutorRef"() : $Builtin.Executor
9-
// CHECK-NEXT: [[MAIN_OPTIONAL:%[0-9]+]] = enum $Optional<Builtin.Executor>, #Optional.some!enumelt, [[MAIN]]
8+
// CHECK: [[META:%.*]] = metatype $@thick MainActor.Type
9+
// CHECK: [[FUNC:%.*]] = function_ref @$sScM6sharedScMvgZ : $@convention(method) (@thick MainActor.Type) -> @owned MainActor
10+
// CHECK: [[ACTOR_RAW:%.*]] = apply [[FUNC]]([[META]])
11+
// CHECK: [[ACTOR_RAW_E:%.*]] = init_existential_ref [[ACTOR_RAW]]
12+
// CHECK: [[ACTOR_RAW_E_O:%.*]] = enum $Optional<any Actor>, #Optional.some!enumelt, [[ACTOR_RAW_E]]
13+
// CHECK: [[ACTOR:%.*]] = begin_borrow [[ACTOR_RAW_E_O]]
1014

1115
actor MyActorImpl {}
1216

@@ -67,7 +71,7 @@ await printFromMyActor(value: a)
6771
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl
6872
// CHECK: end_borrow [[ACTORREF]]
6973
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]])
70-
// CHECK: hop_to_executor [[MAIN_OPTIONAL]]
74+
// CHECK: hop_to_executor [[ACTOR]]
7175

7276
if a < 10 {
7377
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int
@@ -121,5 +125,12 @@ if a < 10 {
121125
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl
122126
// CHECK: end_borrow [[ACTORREF]]
123127
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]])
124-
// CHECK: hop_to_executor [[MAIN_OPTIONAL]]
128+
// CHECK: hop_to_executor [[ACTOR]]
125129
}
130+
131+
nonisolated(nonsending) func nonisolatedNonSendingFunction() async {}
132+
133+
// CHECK: [[FUNC:%.*]] = function_ref @$s24toplevel_globalactorvars29nonisolatedNonSendingFunctionyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
134+
// CHECK: apply [[FUNC]]([[ACTOR]])
135+
// CHECK-NEXT: hop_to_executor [[ACTOR]]
136+
await nonisolatedNonSendingFunction()

0 commit comments

Comments
 (0)