mirror-llvm-project/llvm/test/CodeGen/WebAssembly/thread_pointer.ll
Sy Brand 577e9a7cc8
[WebAssembly] WASIP3 Library Call Thread Context Support (#175800)
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.
2026-05-26 22:46:13 +00:00

29 lines
1.1 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=wasm32-unknown-unknown | FileCheck %s --check-prefix=WASM32
; RUN: llc < %s -mtriple=wasm64-unknown-unknown | FileCheck %s --check-prefix=WASM64
; RUN: llc < %s -mtriple=wasm32-wasip3 | FileCheck %s --check-prefix=WASM32-LIBCALL
declare ptr @llvm.thread.pointer()
define ptr @thread_pointer() nounwind {
; WASM32-LABEL: thread_pointer:
; WASM32: .functype thread_pointer () -> (i32)
; WASM32-NEXT: # %bb.0:
; WASM32-NEXT: global.get __tls_base
; WASM32-NEXT: # fallthrough-return
;
; WASM64-LABEL: thread_pointer:
; WASM64: .functype thread_pointer () -> (i64)
; WASM64-NEXT: # %bb.0:
; WASM64-NEXT: global.get __tls_base
; WASM64-NEXT: # fallthrough-return
;
; WASM32-LIBCALL-LABEL: thread_pointer:
; WASM32-LIBCALL: .functype thread_pointer () -> (i32)
; WASM32-LIBCALL-NEXT: # %bb.0:
; WASM32-LIBCALL-NEXT: call __wasm_get_tls_base
; WASM32-LIBCALL-NEXT: # fallthrough-return
;
%1 = tail call ptr @llvm.thread.pointer()
ret ptr %1
}