Unconditionally expose wasm atomic intrinsics

While they're not very useful in single-threaded mode this makes them
more useful for building libraries because you don't have to always
recompile the standard library to get the desired effect. Additionally
it helps us enable tests on CI for these functions, since the
instructions will now validate without shared memory (thankfully!).
This commit is contained in:
Alex Crichton 2021-03-20 11:01:06 -07:00 committed by Amanieu d'Antras
parent 8ed0d3cbd5
commit 60e8d7766b
4 changed files with 40 additions and 52 deletions

View File

@ -20,5 +20,7 @@ COPY --from=0 /usr/local/cargo/bin/wasmtime /usr/local/bin/wasmtime
ENV CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime \
--enable-simd \
--enable-threads \
--opt-level 0 \
--mapdir .::/checkout/target/wasm32-wasi/release/deps \
--"

View File

@ -59,11 +59,23 @@ pub mod arch {
/// Platform-specific intrinsics for the `wasm32` platform.
///
/// This module provides intrinsics specific to the WebAssembly
/// architecture. Here you'll find intrinsics necessary for leveraging
/// WebAssembly proposals such as [atomics] and [simd]. These proposals are
/// evolving over time and as such the support here is unstable and requires
/// the nightly channel. As WebAssembly proposals stabilize these functions
/// will also become stable.
/// architecture. Here you'll find intrinsics specific to WebAssembly that
/// aren't otherwise surfaced somewhere in a cross-platform abstraction of
/// `std`, and you'll also find functions for leveraging WebAssembly
/// proposals such as [atomics] and [simd].
///
/// Intrinsics in the `wasm32` module are modeled after the WebAssembly
/// instructions that they represent. All functions are named after the
/// instruction they intend to correspond to, and the arguments/results
/// correspond to the type signature of the instruction itself. Stable
/// WebAssembly instructions are [documented online][instrdoc].
///
/// [instrdoc]: https://webassembly.github.io/spec/core/valid/instructions.html
///
/// If a proposal is not yet stable in WebAssembly itself then the functions
/// within this function may be unstable and require the nightly channel of
/// Rust to use. As the proposal itself stabilizes the intrinsics in this
/// module should stabilize as well.
///
/// [atomics]: https://github.com/webassembly/threads
/// [simd]: https://github.com/webassembly/simd
@ -74,18 +86,22 @@ pub mod arch {
/// ## Atomics
///
/// The [threads proposal][atomics] for WebAssembly adds a number of
/// instructions for dealing with multithreaded programs. Atomic
/// instructions can all be generated through `std::sync::atomic` types, but
/// some instructions have no equivalent in Rust such as
/// `memory.atomic.notify` so this module will provide these intrinsics.
/// instructions for dealing with multithreaded programs. Most instructions
/// added in the [atomics] proposal are exposed in Rust through the
/// `std::sync::atomic` module. Some instructions, however, don't have
/// direct equivalents in Rust so they're exposed here instead.
///
/// At this time, however, these intrinsics are only available **when the
/// standard library itself is compiled with atomics**. Compiling with
/// atomics is not enabled by default and requires passing
/// `-Ctarget-feature=+atomics` to rustc. The standard library shipped via
/// `rustup` is not compiled with atomics. To get access to these intrinsics
/// you'll need to compile the standard library from source with the
/// requisite compiler flags.
/// Note that the instructions added in the [atomics] proposal can work in
/// either a context with a shared wasm memory and without. These intrinsics
/// are always available in the standard library, but you likely won't be
/// able to use them too productively unless you recompile the standard
/// library (and all your code) with `-Ctarget-feature=+atomics`.
///
/// It's also worth pointing out that multi-threaded WebAssembly and its
/// story in Rust is still in a somewhat "early days" phase as of the time
/// of this writing. Pieces should mostly work but it generally requires a
/// good deal of manual setup. At this time it's not as simple as "just call
/// `std::thread::spawn`", but it will hopefully get there one day!
///
/// ## SIMD
///

View File

@ -1,13 +1,3 @@
//! Intrinsics associated with WebAssembly's upcoming threads proposal.
//!
//! These intrinsics are all unstable because they're not actually stable in
//! WebAssembly itself yet. The signatures may change as [the
//! specification][spec] is updated.
//!
//! [spec]: https://github.com/WebAssembly/threads
#![cfg(any(target_feature = "atomics", doc))]
#[cfg(test)]
use stdarch_test::assert_instr;
@ -41,16 +31,10 @@ extern "C" {
/// didn't block
/// * 2 - the thread blocked, but the timeout expired.
///
/// # Availability
///
/// This intrinsic is only available **when the standard library itself is
/// compiled with the `atomics` target feature**. This version of the standard
/// library is not obtainable via `rustup`, but rather will require the
/// standard library to be compiled from source.
///
/// [instr]: https://webassembly.github.io/threads/syntax/instructions.html#syntax-instr-atomic-memory
#[inline]
#[cfg_attr(test, assert_instr("i32.atomic.wait"))]
#[cfg_attr(test, assert_instr(memory.atomic.wait32))]
#[target_feature(enable = "atomics")]
pub unsafe fn memory_atomic_wait32(ptr: *mut i32, expression: i32, timeout_ns: i64) -> i32 {
llvm_atomic_wait_i32(ptr, expression, timeout_ns)
}
@ -76,16 +60,10 @@ pub unsafe fn memory_atomic_wait32(ptr: *mut i32, expression: i32, timeout_ns: i
/// didn't block
/// * 2 - the thread blocked, but the timeout expired.
///
/// # Availability
///
/// This intrinsic is only available **when the standard library itself is
/// compiled with the `atomics` target feature**. This version of the standard
/// library is not obtainable via `rustup`, but rather will require the
/// standard library to be compiled from source.
///
/// [instr]: https://webassembly.github.io/threads/syntax/instructions.html#syntax-instr-atomic-memory
#[inline]
#[cfg_attr(test, assert_instr("i64.atomic.wait"))]
#[cfg_attr(test, assert_instr(memory.atomic.wait64))]
#[target_feature(enable = "atomics")]
pub unsafe fn memory_atomic_wait64(ptr: *mut i64, expression: i64, timeout_ns: i64) -> i32 {
llvm_atomic_wait_i64(ptr, expression, timeout_ns)
}
@ -103,16 +81,10 @@ pub unsafe fn memory_atomic_wait64(ptr: *mut i64, expression: i64, timeout_ns: i
///
/// Returns the number of waiters which were actually notified.
///
/// # Availability
///
/// This intrinsic is only available **when the standard library itself is
/// compiled with the `atomics` target feature**. This version of the standard
/// library is not obtainable via `rustup`, but rather will require the
/// standard library to be compiled from source.
///
/// [instr]: https://webassembly.github.io/threads/syntax/instructions.html#syntax-instr-atomic-memory
#[inline]
#[cfg_attr(test, assert_instr("atomic.wake"))]
#[cfg_attr(test, assert_instr(memory.atomic.notify))]
#[target_feature(enable = "atomics")]
pub unsafe fn memory_atomic_notify(ptr: *mut i32, waiters: u32) -> u32 {
llvm_atomic_notify(ptr, waiters as i32) as u32
}

View File

@ -3,9 +3,7 @@
#[cfg(test)]
use stdarch_test::assert_instr;
#[cfg(any(target_feature = "atomics", doc))]
mod atomic;
#[cfg(any(target_feature = "atomics", doc))]
pub use self::atomic::*;
mod simd128;