mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This patch updates the asynchronous main function to run the first thunk
of the function synchronously through a call to `swift_job_run`.
The runloop is killed by exiting or aborting the task that it is running
on. As such, we need to ensure that the task contains an async function
that either calls exit explicitly or aborts. The AsyncEntryPoint, that
contains this code, was added in the previous patch. This patch adds the
pieces for the actual implementation of this behaviour as well as adding
the necessary code to start the runloop.
There are now four layers of main functions before hitting the "real"
code.
@main: This is the actual main entrypoint of the program. This
constructs the task containing @async_main, grabs the main executor,
runs swift_job_run to run the first part synchronously, and finally
kicks off the runloop with a call to _asyncMainDrainQueue. This is
generated in the call to `emitAsyncMainThreadStart`.
@async_main: This thunk exists to ensure that the main function calls
`exit` at some point so that the runloop stops. It also handles emitting
an error if the user-written main function throws.
e.g:
```
func async_main() async -> () {
do {
try await Main.$main()
exit(0)
} catch {
_errorInMain(error)
}
}
```
Main.$main(): This still has the same behaviour as with the
synchronous case. It just calls `try await Main.main()` and exists to
simplify typechecking.
Main.main(): This is the actual user-specified main. It serves the same
purpose as in the synchronous, allowing the programmer to write code,
but it's async!
The control flow in `emitFunctionDefinition` is a little confusing (to
me anyway), so here it is spelled out:
If the main function is synchronous, the `constant.kind` will be a
`SILDeclRef::Kind::EntryPoint`, but the `decl` won't be async, so it
drops down to `emitArtificalTopLevel` anyway.
If the main function is async and we're generating `@main`, the
`constant.kind` will be `SILDeclRef::Kind::AsyncEntryPoint`, so we also
call `emitArtificalTopLevel`. `emitArtificalTopLevel` is responsible for
detecting whether the decl is async and deciding whether to emit code to
extract the argc/argv variables that get passed into the actual main
entrypoint to the program. If we're generating the `@async_main` body,
the kind will be `SILDeclRef::Kind::EntryPoint` and the `decl` will be
async, so we grab the mainEntryPoint decl and call
`emitAsyncMainThreadStart` to generate the wrapping code.
Note; there is a curious change in `SILLocation::getSourceLoc()`
where instead of simply checking `isFilenameAndLocation()`, I change it
to `getStorageKind() == FilenameAndLocationKind`. This is because the
SILLocation returned is to a FilenameAndLocationKind, but the actual
storage returns true for the call to `isNull()` inside of the
`isFilenameAndLocation()` call. This results in us incorrectly falling
through to the `getASTNode()` call below that, which asserts when asked
to get the AST node of a location.
I also did a little bit of refactoring in the SILGenModule for grabbing
intrinsics. Previously, there was only a `getConcurrencyIntrinsic`
function, which would only load FuncDecls out of the concurrency
module. The `exit` function is in the concurrency shims module, so I
refactored the load code to take a ModuleDecl to search from.
The emitBuiltinCreateAsyncTask function symbol is exposed from
SILGenBuiltin so that it is available from SILGenFunction. There is a
fair bit of work involved going from what is available at the SGF to
what is needed for actually calling the CreateAsyncTask builtin, so in
order to avoid additional maintenance, it's good to re-use that.
82 lines
4.5 KiB
Swift
82 lines
4.5 KiB
Swift
// RUN: %target-swift-frontend -emit-sil -disable-availability-checking -parse-as-library %s | %FileCheck %s --check-prefix=CHECK-SIL
|
|
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -Xfrontend -parse-as-library %s -o %t_binary
|
|
// RUN: %target-run %t_binary | %FileCheck %s --check-prefix=CHECK-EXEC
|
|
|
|
// REQUIRES: concurrency
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: OS=macosx || OS=ios
|
|
|
|
// rdar://76038845
|
|
// REQUIRES: concurrency_runtime
|
|
// UNSUPPORTED: back_deployment_runtime
|
|
|
|
func asyncFunc() async {
|
|
print("Hello World!")
|
|
}
|
|
|
|
@main struct MyProgram {
|
|
static func main() async throws {
|
|
await asyncFunc()
|
|
}
|
|
}
|
|
|
|
// CHECK-EXEC: Hello World!
|
|
|
|
// static MyProgram.main()
|
|
// CHECK-SIL-LABEL: sil hidden @$s10async_main9MyProgramV0B0yyYaKFZ : $@convention(method) @async (@thin MyProgram.Type) -> @error Error
|
|
|
|
|
|
// static MyProgram.$main()
|
|
// CHECK-SIL-LABEL: sil hidden @$s10async_main9MyProgramV5$mainyyYaKFZ : $@convention(method) @async (@thin MyProgram.Type) -> @error Error
|
|
|
|
|
|
// async_Main
|
|
// CHECK-SIL_LABEL: sil hidden @async_Main : $@convention(thin) @async () -> () {
|
|
// call main
|
|
// CHECK-SIL: %0 = metatype $@thin MyProgram.Type // user: %2
|
|
// CHECK-SIL-NEXT: // function_ref static MyProgram.$main()
|
|
// CHECK-SIL-NEXT: %1 = function_ref @$s10async_main9MyProgramV5$mainyyYaKFZ : $@convention(method) @async (@thin MyProgram.Type) -> @error Error // user: %2
|
|
// CHECK-SIL-NEXT: try_apply %1(%0) : $@convention(method) @async (@thin MyProgram.Type) -> @error Error, normal bb1, error bb2 // id: %2
|
|
|
|
// unwrap error and exit or explode
|
|
// CHECK-SIL: bb1(%3 : $()):
|
|
// CHECK-SIL-NEXT: %4 = integer_literal $Builtin.Int32, 0
|
|
// CHECK-SIL-NEXT: %5 = struct $Int32 (%4 : $Builtin.Int32)
|
|
// CHECK-SIL-NEXT: // function_ref exit
|
|
// CHECK-SIL-NEXT: %6 = function_ref @exit : $@convention(c) (Int32) -> Never
|
|
// CHECK-SIL-NEXT: %7 = apply %6(%5) : $@convention(c) (Int32) -> Never
|
|
// CHECK-SIL-NEXT: unreachable
|
|
|
|
// CHECK-SIL: bb2(%9 : $Error):
|
|
// CHECK-SIL-NEXT: %10 = builtin "errorInMain"(%9 : $Error) : $()
|
|
// CHECK-SIL-NEXT: unreachable
|
|
|
|
// main
|
|
// CHECK-SIL-LABEL: sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
|
|
|
|
// CHECK-SIL: // function_ref async_Main
|
|
// CHECK-SIL-NEXT: %2 = function_ref @async_Main : $@convention(thin) @async () -> ()
|
|
// CHECK-SIL-NEXT: %3 = integer_literal $Builtin.Int64, 21
|
|
// CHECK-SIL-NEXT: %4 = struct $Int (%3 : $Builtin.Int64)
|
|
// CHECK-SIL-NEXT: %5 = metatype $@thick ().Type
|
|
// CHECK-SIL-NEXT: %6 = init_existential_metatype %5 : $@thick ().Type, $@thick Any.Type
|
|
// CHECK-SIL-NEXT: // function_ref thunk for @escaping @convention(thin) @async () -> ()
|
|
// CHECK-SIL-NEXT: %7 = function_ref @$sIetH_yts5Error_pIegHrzo_TR : $@convention(thin) @async (@convention(thin) @async () -> ()) -> (@out (), @error Error)
|
|
// CHECK-SIL-NEXT: %8 = partial_apply [callee_guaranteed] %7(%2) : $@convention(thin) @async (@convention(thin) @async () -> ()) -> (@out (), @error Error)
|
|
// CHECK-SIL-NEXT: %9 = convert_function %8 : $@async @callee_guaranteed () -> (@out (), @error Error) to $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <()>
|
|
// CHECK-SIL-NEXT: %10 = builtin "createAsyncTask"<()>(%4 : $Int, %6 : $@thick Any.Type, %9 : $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <()>) : $(Builtin.NativeObject, Builtin.RawPointer)
|
|
// CHECK-SIL-NEXT: %11 = tuple_extract %10 : $(Builtin.NativeObject, Builtin.RawPointer), 0
|
|
// CHECK-SIL-NEXT: // function_ref swift_job_run
|
|
// CHECK-SIL-NEXT: %12 = function_ref @swift_job_run : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
|
|
// CHECK-SIL-NEXT: %13 = builtin "convertTaskToJob"(%11 : $Builtin.NativeObject) : $Builtin.Job
|
|
// CHECK-SIL-NEXT: %14 = unchecked_trivial_bit_cast %13 : $Builtin.Job to $UnownedJob
|
|
// CHECK-SIL-NEXT: // function_ref swift_task_getMainExecutor
|
|
// CHECK-SIL-NEXT: %15 = function_ref @swift_task_getMainExecutor : $@convention(thin) () -> Builtin.Executor
|
|
// CHECK-SIL-NEXT: %16 = apply %15() : $@convention(thin) () -> Builtin.Executor
|
|
// CHECK-SIL-NEXT: %17 = unchecked_trivial_bit_cast %16 : $Builtin.Executor to $UnownedSerialExecutor
|
|
// CHECK-SIL-NEXT: %18 = apply %12(%14, %17) : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
|
|
// CHECK-SIL-NEXT: // function_ref swift_task_asyncMainDrainQueue
|
|
// CHECK-SIL-NEXT: %19 = function_ref @swift_task_asyncMainDrainQueue : $@convention(thin) () -> Never
|
|
// CHECK-SIL-NEXT: %20 = apply %19() : $@convention(thin) () -> Never
|
|
// CHECK-SIL-NEXT: unreachable
|