Skip to content

[6.2][silgen] Make async_Main compatible with calling nonisolated(nonsending) functions. #82388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions lib/SILGen/SILGenTopLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,31 @@ void SILGenModule::emitEntryPoint(SourceFile *SF, SILFunction *TopLevel) {

auto prologueLoc = RegularLocation::getModuleLocation();
prologueLoc.markAsPrologue();
if (SF->isAsyncContext()) {
// emitAsyncMainThreadStart will create argc and argv.
// Just set the main actor as the expected executor; we should
// already be running on it.
SILValue executor = TopLevelSGF.emitMainExecutor(prologueLoc);
TopLevelSGF.ExpectedExecutor.set(TopLevelSGF.B.createOptionalSome(
prologueLoc, executor, SILType::getOptionalType(executor->getType())));
} else {
// Create the argc and argv arguments.
auto entry = TopLevelSGF.B.getInsertionBB();
auto context = TopLevelSGF.getTypeExpansionContext();
auto paramTypeIter =
TopLevelSGF.F.getConventions().getParameterSILTypes(context).begin();

entry->createFunctionArgument(*paramTypeIter);
entry->createFunctionArgument(*std::next(paramTypeIter));
}

{
Scope S(TopLevelSGF.Cleanups, moduleCleanupLoc);

if (SF->isAsyncContext()) {
// emitAsyncMainThreadStart will create argc and argv.
// Just set the main actor as the expected executor; we should
// already be running on it.
auto mainActorType =
getASTContext().getMainActorType()->getCanonicalType();
TopLevelSGF.ExpectedExecutor.set(
TopLevelSGF.emitGlobalActorIsolation(prologueLoc, mainActorType)
.borrow(TopLevelSGF, prologueLoc)
.getValue());
} else {
// Create the argc and argv arguments.
auto entry = TopLevelSGF.B.getInsertionBB();
auto context = TopLevelSGF.getTypeExpansionContext();
auto paramTypeIter =
TopLevelSGF.F.getConventions().getParameterSILTypes(context).begin();

entry->createFunctionArgument(*paramTypeIter);
entry->createFunctionArgument(*std::next(paramTypeIter));
}

SILGenTopLevel(TopLevelSGF).visitSourceFile(SF);
}

Expand Down
21 changes: 16 additions & 5 deletions test/SILGen/toplevel_globalactorvars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
// a
// CHECK-LABEL: sil_global hidden @$s24toplevel_globalactorvars1aSivp : $Int

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

actor MyActorImpl {}

Expand Down Expand Up @@ -67,7 +71,7 @@ await printFromMyActor(value: a)
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl
// CHECK: end_borrow [[ACTORREF]]
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]])
// CHECK: hop_to_executor [[MAIN_OPTIONAL]]
// CHECK: hop_to_executor [[ACTOR]]

if a < 10 {
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int
Expand Down Expand Up @@ -121,5 +125,12 @@ if a < 10 {
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl
// CHECK: end_borrow [[ACTORREF]]
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]])
// CHECK: hop_to_executor [[MAIN_OPTIONAL]]
// CHECK: hop_to_executor [[ACTOR]]
}

nonisolated(nonsending) func nonisolatedNonSendingFunction() async {}

// CHECK: [[FUNC:%.*]] = function_ref @$s24toplevel_globalactorvars29nonisolatedNonSendingFunctionyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: apply [[FUNC]]([[ACTOR]])
// CHECK-NEXT: hop_to_executor [[ACTOR]]
await nonisolatedNonSendingFunction()