Skip to content

Make Builtin.getCurrentExecutor conservatively not readnone #36770

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

Closed
Closed
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
5 changes: 4 additions & 1 deletion include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,15 @@ BUILTIN_MISC_OPERATION(BuildSerialExecutorRef,
// Retrieve the ExecutorRef on which the current asynchronous
// function is executing.
// Does not retain an actor executor.
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentExecutor, "getCurrentExecutor", "n", Special)
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentExecutor, "getCurrentExecutor", "", Special)

// getCurrentAsyncTask: () -> Builtin.NativeObject
//
// Retrieve the pointer to the task in which the current asynchronous
// function is executing.
//
// This is readnone because, within the world modeled by SIL, the
// current async task of a thread never changes.
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentAsyncTask, "getCurrentAsyncTask", "n", Special)

/// cancelAsyncTask(): (Builtin.NativeObject) -> Void
Expand Down
31 changes: 31 additions & 0 deletions test/SILOptimizer/optimize_hop_to_executor.sil
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,34 @@ bb2:
return %r : $()
}

// This pattern is definitely optimizable, but it's *not* supposed to
// be optimized by simply removing the hop before the builtin.
// CHECK-LABEL: sil [ossa] @handleGetCurrentExecutor : $@convention(method) @async (@guaranteed MyActor, @guaranteed MyActor) -> () {
// CHECK: bb0(%0 : @guaranteed $MyActor, %1 : @guaranteed $MyActor):
// CHECK-NEXT: // function_ref
// CHECK-NEXT: function_ref
// CHECK-NEXT: apply
// CHECK-NEXT: hop_to_executor
// CHECK-NEXT: builtin
// CHECK-NEXT: hop_to_executor
// CHECK-NEXT: // function_ref
// CHECK-NEXT: function_ref
// CHECK-NEXT: apply
// CHECK-NEXT: tuple
// CHECK-NEXT: return
// CHECK: } // end sil function 'handleGetCurrentExecutor'
sil [ossa] @handleGetCurrentExecutor : $@convention(method) @async (@guaranteed MyActor, @guaranteed MyActor) -> () {
bb0(%0 : @guaranteed $MyActor, %1 : @guaranteed $MyActor):
hop_to_executor %0 : $MyActor
%async_f = function_ref @anotherAsyncFunction : $@convention(thin) @async () -> ()
apply %async_f() : $@convention(thin) @async () -> ()
hop_to_executor %0 : $MyActor
%curExec = builtin "getCurrentExecutor"() : $Builtin.Executor
hop_to_executor %1 : $MyActor
%f = function_ref @requiredToRunOnActor : $@convention(method) (@guaranteed MyActor) -> ()
apply %f(%1) : $@convention(method) (@guaranteed MyActor) -> ()
hop_to_executor %0 : $MyActor
%r = tuple ()
return %r : $()
}