The [WebAssembly Component Model](https://component-model.bytecodealliance.org/) has added support for [cooperative multithreading](https://github.com/WebAssembly/component-model/pull/557). This has been implemented in the [Wasmtime engine](https://github.com/bytecodealliance/wasmtime/pull/11751) and is part of the wider project of [WASI preview 3](https://wasi.dev/roadmap#upcoming-wasi-03-releases), which is currently tracked [here](https://github.com/orgs/bytecodealliance/projects/16). These changes require updating the way that `__stack_pointer` and `__tls_base` work purely for a new `wasm32-wasip3` target; other targets will not be touched. Specifically, rather than using a Wasm global for tracking the stack pointer and TLS base, the new [`context.get/set`](https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-contextget) component model builtin functions will be used (the intention being that runtimes will need to aggressively optimize these calls into single load/stores). For justification on this choice rather than switching out the global at context-switch boundaries, see [this comment](https://github.com/WebAssembly/wasi-libc/issues/691#issuecomment-3716405618) and [this comment](https://github.com/WebAssembly/wasi-libc/issues/691#issuecomment-3716916730). This PR adds support for using library calls instead of globals for holding the stack pointer and TLS base. When used, this thread context ABI emits calls to `__wasm_{get,set}_{stack_pointer,tls_base}` when needed. These functions can then be implemented in `libc`. This is enabled only for the WASIp3 target. There is a temporary macro define for `__wasm_libcall_thread_context__` which can be removed once `wasi-libc` has fully migrated to the new ABI for the WASIp3 target.
25 lines
777 B
LLVM
25 lines
777 B
LLVM
; RUN: llc < %s --mtriple=wasm32-wasip3 -asm-verbose=false | FileCheck --check-prefix=LIBCALL %s
|
|
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false | FileCheck --check-prefix=GLOBAL %s
|
|
|
|
declare void @force_sp_save()
|
|
define void @use_stack() #0 {
|
|
%1 = alloca i32, align 4
|
|
%2 = alloca ptr, align 4
|
|
store ptr %1, ptr %2, align 4
|
|
call void @force_sp_save()
|
|
ret void
|
|
}
|
|
|
|
; LIBCALL-LABEL: use_stack:
|
|
; LIBCALL: call __wasm_get_stack_pointer
|
|
; LIBCALL: call __wasm_set_stack_pointer
|
|
; LIBCALL-NOT: global.get __stack_pointer
|
|
; LIBCALL-NOT: global.set __stack_pointer
|
|
|
|
; GLOBAL-LABEL: use_stack:
|
|
; GLOBAL: global.get __stack_pointer
|
|
; GLOBAL: global.set __stack_pointer
|
|
; GLOBAL-NOT: call __wasm_get_stack_pointer
|
|
; GLOBAL-NOT: call __wasm_set_stack_pointer
|
|
|