about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bootstrap/compile.rs13
-rw-r--r--src/ci/docker/host-x86_64/dist-various-2/Dockerfile5
-rwxr-xr-xsrc/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh24
-rwxr-xr-xsrc/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh6
-rw-r--r--src/doc/rustc/src/SUMMARY.md1
-rw-r--r--src/doc/rustc/src/platform-support.md1
-rw-r--r--src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md139
-rw-r--r--src/tools/build-manifest/src/main.rs1
8 files changed, 183 insertions, 7 deletions
diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
index 400b07b1882..7a4abcd1dc0 100644
--- a/src/bootstrap/compile.rs
+++ b/src/bootstrap/compile.rs
@@ -287,13 +287,14 @@ fn copy_self_contained_objects(
             let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
             target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
         }
-    } else if target.ends_with("-wasi") {
+    } else if target.contains("-wasi") {
         let srcdir = builder
             .wasi_root(target)
             .unwrap_or_else(|| {
                 panic!("Target {:?} does not have a \"wasi-root\" key", target.triple)
             })
-            .join("lib/wasm32-wasi");
+            .join("lib")
+            .join(target.to_string().replace("-preview1", ""));
         for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
             copy_and_stamp(
                 builder,
@@ -393,9 +394,13 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
             }
         }
 
-        if target.ends_with("-wasi") {
+        if target.contains("-wasi") {
             if let Some(p) = builder.wasi_root(target) {
-                let root = format!("native={}/lib/wasm32-wasi", p.to_str().unwrap());
+                let root = format!(
+                    "native={}/lib/{}",
+                    p.to_str().unwrap(),
+                    target.to_string().replace("-preview1", "")
+                );
                 cargo.rustflag("-L").rustflag(&root);
             }
         }
diff --git a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile
index dd1c5fced0e..9bc074237e6 100644
--- a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile
+++ b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile
@@ -93,6 +93,9 @@ RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh
 COPY host-x86_64/dist-various-2/build-wasi-toolchain.sh /tmp/
 RUN /tmp/build-wasi-toolchain.sh
 
+COPY host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh /tmp/
+RUN /tmp/build-wasi-threads-toolchain.sh
+
 COPY scripts/freebsd-toolchain.sh /tmp/
 RUN /tmp/freebsd-toolchain.sh i686
 
@@ -114,6 +117,7 @@ ENV TARGETS=x86_64-unknown-fuchsia
 ENV TARGETS=$TARGETS,aarch64-unknown-fuchsia
 ENV TARGETS=$TARGETS,wasm32-unknown-unknown
 ENV TARGETS=$TARGETS,wasm32-wasi
+ENV TARGETS=$TARGETS,wasm32-wasi-preview1-threads
 ENV TARGETS=$TARGETS,sparcv9-sun-solaris
 ENV TARGETS=$TARGETS,x86_64-pc-solaris
 ENV TARGETS=$TARGETS,x86_64-sun-solaris
@@ -136,6 +140,7 @@ RUN ln -s /usr/include/asm-generic /usr/local/include/asm
 
 ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --disable-docs \
   --set target.wasm32-wasi.wasi-root=/wasm32-wasi \
+  --set target.wasm32-wasi-preview1-threads.wasi-root=/wasm32-wasi-preview1-threads \
   --musl-root-armv7=/musl-armv7
 
 ENV SCRIPT python3 ../x.py dist --host='' --target $TARGETS
diff --git a/src/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh b/src/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh
new file mode 100755
index 00000000000..7dae90f4403
--- /dev/null
+++ b/src/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh
@@ -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*
diff --git a/src/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh b/src/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh
index b867db6a1b5..45174e708dc 100755
--- a/src/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh
+++ b/src/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh
@@ -2,10 +2,10 @@
 
 set -ex
 
-# Originally from https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.6/clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.xz
-curl https://ci-mirrors.rust-lang.org/rustc/2022-12-06-clang%2Bllvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.xz | \
+# 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-15.0.6-x86_64-linux-gnu-ubuntu-18.04/bin"
+bin="$PWD/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin"
 
 git clone https://github.com/WebAssembly/wasi-libc
 
diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md
index 7d9db743233..851be1c0a2c 100644
--- a/src/doc/rustc/src/SUMMARY.md
+++ b/src/doc/rustc/src/SUMMARY.md
@@ -47,6 +47,7 @@
     - [\*-unknown-netbsd\*](platform-support/netbsd.md)
     - [*-unknown-openbsd](platform-support/openbsd.md)
     - [\*-unknown-uefi](platform-support/unknown-uefi.md)
+    - [wasm32-wasi-preview1-threads](platform-support/wasm32-wasi-preview1-threads.md)
     - [wasm64-unknown-unknown](platform-support/wasm64-unknown-unknown.md)
     - [x86_64-fortanix-unknown-sgx](platform-support/x86_64-fortanix-unknown-sgx.md)
     - [x86_64-unknown-none](platform-support/x86_64-unknown-none.md)
diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md
index 8c5dc11eeef..db4a26937d5 100644
--- a/src/doc/rustc/src/platform-support.md
+++ b/src/doc/rustc/src/platform-support.md
@@ -318,6 +318,7 @@ target | std | host | notes
 `thumbv7a-pc-windows-msvc` | ? |  |
 `thumbv7a-uwp-windows-msvc` | ✓ |  |
 `thumbv7neon-unknown-linux-musleabihf` | ? |  | Thumb2-mode ARMv7-A Linux with NEON, MUSL
+[`wasm32-wasi-preview1-threads`](platform-support/wasm32-wasi-preview1-threads.md) | ✓ | WebAssembly with WASI Preview 1 and threads
 [`wasm64-unknown-unknown`](platform-support/wasm64-unknown-unknown.md) | ? |  | WebAssembly
 `x86_64-apple-ios-macabi` | ✓ |  | Apple Catalyst on x86_64
 [`x86_64-apple-tvos`](platform-support/apple-tvos.md) | ? | | x86 64-bit tvOS
diff --git a/src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md b/src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md
new file mode 100644
index 00000000000..b3eb34de638
--- /dev/null
+++ b/src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md
@@ -0,0 +1,139 @@
+# `wasm32-wasi-preview1-threads`
+
+**Tier: 3**
+
+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`.
diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs
index a02ce68903d..88f8770029e 100644
--- a/src/tools/build-manifest/src/main.rs
+++ b/src/tools/build-manifest/src/main.rs
@@ -139,6 +139,7 @@ static TARGETS: &[&str] = &[
     "wasm32-unknown-emscripten",
     "wasm32-unknown-unknown",
     "wasm32-wasi",
+    "wasm32-wasi-preview1-threads",
     "x86_64-apple-darwin",
     "x86_64-apple-ios",
     "x86_64-fortanix-unknown-sgx",