-
Notifications
You must be signed in to change notification settings - Fork 13.4k
WASI threads, implementation of wasm32-wasi-preview1-threads target #112922
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
+473
−31
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
compiler/rustc_target/src/spec/wasm32_wasi_preview1_threads.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//! The `wasm32-wasi-preview1-threads` target is a new and still (as of July 2023) an | ||
//! experimental target. The definition in this file is likely to be tweaked | ||
//! over time and shouldn't be relied on too much. | ||
//! | ||
//! The `wasi-threads` target is a proposal to define a standardized set of syscalls | ||
//! that WebAssembly files can interoperate with. This set of syscalls is | ||
//! intended to empower WebAssembly binaries with native capabilities such as | ||
//! threads, filesystem access, network access, etc. | ||
//! | ||
//! You can see more about the proposal at <https://github.com/WebAssembly/wasi-threads>. | ||
//! | ||
//! The Rust target definition here is interesting in a few ways. We want to | ||
//! serve two use cases here with this target: | ||
//! | ||
//! * First, we want Rust usage of the target to be as hassle-free as possible, | ||
//! ideally avoiding the need to configure and install a local wasm32-wasi-preview1-threads | ||
//! toolchain. | ||
//! | ||
//! * Second, one of the primary use cases of LLVM's new wasm backend and the | ||
//! wasm support in LLD is that any compiled language can interoperate with | ||
//! any other. To that the `wasm32-wasi-preview1-threads` target is the first with a viable C | ||
//! standard library and sysroot common definition, so we want Rust and C/C++ | ||
//! code to interoperate when compiled to `wasm32-unknown-unknown`. | ||
//! | ||
//! You'll note, however, that the two goals above are somewhat at odds with one | ||
//! another. To attempt to solve both use cases in one go we define a target | ||
//! that (ab)uses the `crt-static` target feature to indicate which one you're | ||
//! in. | ||
//! | ||
//! ## No interop with C required | ||
//! | ||
//! By default the `crt-static` target feature is enabled, and when enabled | ||
//! this means that the bundled version of `libc.a` found in `liblibc.rlib` | ||
//! is used. This isn't intended really for interoperation with a C because it | ||
//! may be the case that Rust's bundled C library is incompatible with a | ||
//! foreign-compiled C library. In this use case, though, we use `rust-lld` and | ||
//! some copied crt startup object files to ensure that you can download the | ||
//! wasi target for Rust and you're off to the races, no further configuration | ||
//! necessary. | ||
//! | ||
//! All in all, by default, no external dependencies are required. You can | ||
//! compile `wasm32-wasi-preview1-threads` binaries straight out of the box. You can't, however, | ||
//! reliably interoperate with C code in this mode (yet). | ||
//! | ||
//! ## Interop with C required | ||
//! | ||
//! For the second goal we repurpose the `target-feature` flag, meaning that | ||
//! you'll need to do a few things to have C/Rust code interoperate. | ||
//! | ||
//! 1. All Rust code needs to be compiled with `-C target-feature=-crt-static`, | ||
//! indicating that the bundled C standard library in the Rust sysroot will | ||
//! not be used. | ||
//! | ||
//! 2. If you're using rustc to build a linked artifact then you'll need to | ||
//! specify `-C linker` to a `clang` binary that supports | ||
//! `wasm32-wasi-preview1-threads` and is configured with the `wasm32-wasi-preview1-threads` sysroot. This | ||
//! will cause Rust code to be linked against the libc.a that the specified | ||
//! `clang` provides. | ||
//! | ||
//! 3. If you're building a staticlib and integrating Rust code elsewhere, then | ||
//! compiling with `-C target-feature=-crt-static` is all you need to do. | ||
//! | ||
//! You can configure the linker via Cargo using the | ||
//! `CARGO_TARGET_WASM32_WASI_LINKER` env var. Be sure to also set | ||
//! `CC_wasm32-wasi-preview1-threads` if any crates in the dependency graph are using the `cc` | ||
//! crate. | ||
//! | ||
//! ## Remember, this is all in flux | ||
//! | ||
//! The wasi target is **very** new in its specification. It's likely going to | ||
//! be a long effort to get it standardized and stable. We'll be following it as | ||
//! best we can with this target. Don't start relying on too much here unless | ||
//! you know what you're getting in to! | ||
|
||
use super::crt_objects::{self, LinkSelfContainedDefault}; | ||
use super::{wasm_base, Cc, LinkerFlavor, Target}; | ||
|
||
pub fn target() -> Target { | ||
let mut options = wasm_base::options(); | ||
|
||
options.os = "wasi".into(); | ||
|
||
options.add_pre_link_args( | ||
LinkerFlavor::WasmLld(Cc::No), | ||
&["--import-memory", "--export-memory", "--shared-memory"], | ||
); | ||
options.add_pre_link_args( | ||
LinkerFlavor::WasmLld(Cc::Yes), | ||
&[ | ||
"--target=wasm32-wasi-threads", | ||
"-Wl,--import-memory", | ||
"-Wl,--export-memory,", | ||
"-Wl,--shared-memory", | ||
], | ||
); | ||
|
||
options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained(); | ||
options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained(); | ||
|
||
// FIXME: Figure out cases in which WASM needs to link with a native toolchain. | ||
options.link_self_contained = LinkSelfContainedDefault::True; | ||
|
||
// Right now this is a bit of a workaround but we're currently saying that | ||
// the target by default has a static crt which we're taking as a signal | ||
// for "use the bundled crt". If that's turned off then the system's crt | ||
// will be used, but this means that default usage of this target doesn't | ||
// need an external compiler but it's still interoperable with an external | ||
// compiler if configured correctly. | ||
options.crt_static_default = true; | ||
options.crt_static_respected = true; | ||
|
||
// Allow `+crt-static` to create a "cdylib" output which is just a wasm file | ||
// without a main function. | ||
options.crt_static_allows_dylibs = true; | ||
|
||
// WASI's `sys::args::init` function ignores its arguments; instead, | ||
// `args::args()` makes the WASI API calls itself. | ||
options.main_needs_argc_argv = false; | ||
|
||
// And, WASI mangles the name of "main" to distinguish between different | ||
// signatures. | ||
options.entry_name = "__main_void".into(); | ||
|
||
options.singlethread = false; | ||
options.features = "+atomics,+bulk-memory,+mutable-globals".into(); | ||
|
||
Target { | ||
llvm_target: "wasm32-wasi".into(), | ||
pointer_width: 32, | ||
data_layout: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20".into(), | ||
arch: "wasm32".into(), | ||
options, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
|
||
set -ex | ||
|
||
# Originally from https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.4/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04.tar.xz | ||
curl https://ci-mirrors.rust-lang.org/rustc/2023-05-17-clang%2Bllvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04.tar.xz | \ | ||
tar xJf - | ||
bin="$PWD/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin" | ||
|
||
git clone https://github.com/WebAssembly/wasi-libc | ||
|
||
cd wasi-libc | ||
git reset --hard 7018e24d8fe248596819d2e884761676f3542a04 | ||
make -j$(nproc) \ | ||
CC="$bin/clang" \ | ||
NM="$bin/llvm-nm" \ | ||
AR="$bin/llvm-ar" \ | ||
THREAD_MODEL=posix \ | ||
INSTALL_DIR=/wasm32-wasi-preview1-threads \ | ||
install | ||
|
||
cd .. | ||
rm -rf wasi-libc | ||
rm -rf clang+llvm* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# `wasm32-wasi-preview1-threads` | ||
|
||
**Tier: 3** | ||
wesleywiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `wasm32-wasi-preview1-threads` target is a new and still (as of July 2023) an | ||
experimental target. This target is an extension to `wasm32-wasi-preview1` target, | ||
originally known as `wasm32-wasi`. It extends the original target with a | ||
standardized set of syscalls that are intended to empower WebAssembly binaries with | ||
native multi threading capabilities. | ||
|
||
[wasi-threads]: https://github.com/WebAssembly/wasi-threads | ||
[threads]: https://github.com/WebAssembly/threads | ||
|
||
|
||
## Target maintainers | ||
|
||
- Georgii Rylov, https://github.com/g0djan | ||
- Alex Crichton, https://github.com/alexcrichton | ||
- Andrew Brown, https://github.com/abrown | ||
- Marcin Kolny, https://github.com/loganek | ||
|
||
## Requirements | ||
|
||
This target is cross-compiled. The target supports `std` fully. | ||
|
||
The Rust target definition here is interesting in a few ways. We want to | ||
serve two use cases here with this target: | ||
* First, we want Rust usage of the target to be as hassle-free as possible, | ||
ideally avoiding the need to configure and install a local wasm32-wasi-preview1-threads | ||
toolchain. | ||
* Second, one of the primary use cases of LLVM's new wasm backend and the | ||
wasm support in LLD is that any compiled language can interoperate with | ||
any other. The `wasm32-wasi-preview1-threads` target is the first with a viable C | ||
standard library and sysroot common definition, so we want Rust and C/C++ | ||
code to interoperate when compiled to `wasm32-unknown-unknown`. | ||
|
||
|
||
You'll note, however, that the two goals above are somewhat at odds with one | ||
another. To attempt to solve both use cases in one go we define a target | ||
that (ab)uses the `crt-static` target feature to indicate which one you're | ||
in. | ||
### No interop with C required | ||
By default the `crt-static` target feature is enabled, and when enabled | ||
this means that the bundled version of `libc.a` found in `liblibc.rlib` | ||
is used. This isn't intended really for interoperation with a C because it | ||
may be the case that Rust's bundled C library is incompatible with a | ||
foreign-compiled C library. In this use case, though, we use `rust-lld` and | ||
some copied crt startup object files to ensure that you can download the | ||
wasi target for Rust and you're off to the races, no further configuration | ||
necessary. | ||
All in all, by default, no external dependencies are required. You can | ||
compile `wasm32-wasi-preview1-threads` binaries straight out of the box. You can't, however, | ||
reliably interoperate with C code in this mode (yet). | ||
### Interop with C required | ||
For the second goal we repurpose the `target-feature` flag, meaning that | ||
you'll need to do a few things to have C/Rust code interoperate. | ||
1. All Rust code needs to be compiled with `-C target-feature=-crt-static`, | ||
indicating that the bundled C standard library in the Rust sysroot will | ||
not be used. | ||
2. If you're using rustc to build a linked artifact then you'll need to | ||
specify `-C linker` to a `clang` binary that supports | ||
`wasm32-wasi-preview1-threads` and is configured with the `wasm32-wasi-preview1-threads` sysroot. This | ||
will cause Rust code to be linked against the libc.a that the specified | ||
`clang` provides. | ||
3. If you're building a staticlib and integrating Rust code elsewhere, then | ||
compiling with `-C target-feature=-crt-static` is all you need to do. | ||
|
||
All in all, by default, no external dependencies are required. You can | ||
compile `wasm32-wasi-preview1-threads` binaries straight out of the box. You can't, however, | ||
reliably interoperate with C code in this mode (yet). | ||
|
||
|
||
This target is not a stable target. This means that there are not many engines | ||
which implement the `wasi-threads` feature and if they do they're likely behind a | ||
flag, for example: | ||
|
||
* Wasmtime - `--wasm-features=threads --wasi-modules=experimental-wasi-threads` | ||
|
||
Also note that at this time the `wasm32-wasi-preview1-threads` target assumes the | ||
presence of other merged wasm proposals such as (with their LLVM feature flags): | ||
|
||
* [Bulk memory] - `+bulk-memory` | ||
* Mutable imported globals - `+mutable-globals` | ||
* Atomics - `+atomics` | ||
|
||
[Bulk memory]: https://github.com/WebAssembly/spec/blob/main/proposals/bulk-memory-operations/Overview.md | ||
|
||
LLVM 16 is required for this target. The reason is related to linker flags: prior to LLVM 16, --import-memory and --export-memory were not allowed together. The reason both are needed is an artifact of how WASI currently does things; see https://github.com/WebAssembly/WASI/issues/502 for more details. | ||
|
||
The target intends to match the corresponding Clang target for its `"C"` ABI. | ||
|
||
> **Note**: due to the relatively early-days nature of this target when working | ||
> with this target you may encounter LLVM bugs. If an assertion hit or a bug is | ||
> found it's recommended to open an issue either with rust-lang/rust or ideally | ||
> with LLVM itself. | ||
## Building the target | ||
|
||
Users need to install or built wasi-sdk since release 20.0 | ||
https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-20 | ||
and specify path to *wasi-root* `.cargo/config.toml` | ||
|
||
```toml | ||
[target.wasm32-wasi-preview1-threads] | ||
wasi-root = ".../wasi-libc/sysroot" | ||
``` | ||
|
||
After that users can build this by adding it to the `target` list in | ||
`config.toml`, or with `-Zbuild-std`. | ||
|
||
## Building Rust programs | ||
|
||
Since it is Tier 3, rust doesn't ship pre-compiled artifacts for this target. | ||
|
||
Specify `wasi-root` as explained in the previous section and then use the `build-std` | ||
nightly cargo feature to build the standard library: | ||
```shell | ||
cargo +nightly build --target=wasm32-wasi-preview1-threads -Zbuild-std | ||
``` | ||
|
||
## Cross-compilation | ||
|
||
This target can be cross-compiled from any hosts. | ||
|
||
## Testing | ||
|
||
Currently testing is not well supported for `wasm32-wasi-preview1-threads` and the | ||
Rust project doesn't run any tests for this target. However the UI testsuite can be run | ||
manually following this instructions: | ||
|
||
0. Ensure [wamr](https://github.com/bytecodealliance/wasm-micro-runtime), [wasmtime](https://github.com/bytecodealliance/wasmtime) | ||
or another engine that supports `wasi-threads` is installed and can be found in the `$PATH` env variable. | ||
1. Clone master branch. | ||
2. Apply such [a change](https://github.com/g0djan/rust/compare/godjan/wasi-threads...g0djan:rust:godjan/wasi-run-ui-tests?expand=1) with an engine from the step 1. | ||
3. Run `./x.py test --target wasm32-wasi-preview1-threads tests/ui` and save the list of failed tests. | ||
4. Checkout branch with your changes. | ||
5. Apply such [a change](https://github.com/g0djan/rust/compare/godjan/wasi-threads...g0djan:rust:godjan/wasi-run-ui-tests?expand=1) with an engine from the step 1. | ||
6. Run `./x.py test --target wasm32-wasi-preview1-threads tests/ui` and save the list of failed tests. | ||
7. For both lists of failed tests run `cat list | sort > sorted_list` and compare it with `diff sorted_list1 sorted_list2`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.