Skip to content

More wasm SIMD updates #1092

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
merged 3 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions ci/docker/wasm32-wasi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
FROM rust:1.50.0

# Install wasmtime from source for now while the `experimental_x64` feature is
# not yet the default. (it's not actually that experimental at the time of this
# writing, wasmtime should switch defaults soon and the backend this enables has
# better support for simd instructions)
RUN \
CARGO_INCREMENTAL=0 \
CARGO_PROFILE_DEV_DEBUGINFO=0 \
cargo install wasmtime-cli --features experimental_x64 --debug --vers 0.25.0 --locked

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
xz-utils \
clang

RUN curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v0.24.0/wasmtime-v0.24.0-x86_64-linux.tar.xz | tar xJf -
ENV PATH=$PATH:/wasmtime-v0.24.0-x86_64-linux
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 \
--"
8 changes: 0 additions & 8 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ case ${TARGET} in
export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+avx"
cargo_test "--release"
;;
wasm32*)
# TODO: need to re-enable simd testing for wasm32
# TODO: should enable atomics testing for wasm32
# prev="$RUSTFLAGS"
# export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128,+unimplemented-simd128"
# cargo_test "--release"
# export RUSTFLAGS="$prev"
;;
# FIXME: don't build anymore
#mips-*gnu* | mipsel-*gnu*)
# export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+msa,+fp64,+mips32r5"
Expand Down
4 changes: 2 additions & 2 deletions crates/core_arch/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ macro_rules! static_assert_imm16 {

#[allow(unused)]
macro_rules! static_assert {
($imm:ident : $ty:ty where $e:expr) => {
($imm:ident : $ty:ty where $e:expr) => {{
struct Validate<const $imm: $ty>();
impl<const $imm: $ty> Validate<$imm> {
const VALID: () = {
let _ = 1 / ($e as usize);
};
}
let _ = Validate::<$imm>::VALID;
};
}};
}

#[allow(unused)]
Expand Down
97 changes: 49 additions & 48 deletions crates/core_arch/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,35 +86,35 @@ 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.
///
/// 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`.
///
/// 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.
/// 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
///
/// The [simd proposal][simd] for WebAssembly adds a new `v128` type for a
/// 128-bit SIMD register. It also adds a large array of instructions to
/// operate on the `v128` type to perform data processing. The SIMD proposal
/// has been in progress for quite some time and many instructions have come
/// and gone. This module attempts to keep up with the proposal, but if you
/// notice anything awry please feel free to [open an
/// at the time of this writing is in [phase 4] which means that it's in the
/// standardization phase. It's expected that once some testing on nightly
/// has happened a stabilization proposal will be made for the Rust
/// intrinsics. If you notice anything awry please feel free to [open an
/// issue](https://github.com/rust-lang/stdarch/issues/new).
///
/// It's important to be aware that the current state of development of SIMD
/// in WebAssembly is still somewhat early days. There's lots of pieces to
/// demo and prototype with, but discussions and support are still in
/// progress. There's a number of pitfalls and gotchas in various places,
/// which will attempt to be documented here, but there may be others
/// lurking!
/// [phase 4]: https://github.com/webassembly/proposals
///
/// Using SIMD is intended to be similar to as you would on `x86_64`, for
/// example. You'd write a function such as:
Expand All @@ -118,15 +130,17 @@ pub mod arch {
///
/// Unlike `x86_64`, however, WebAssembly does not currently have dynamic
/// detection at runtime as to whether SIMD is supported (this is one of the
/// motivators for the [conditional sections proposal][condsections], but
/// that is still pretty early days). This means that your binary will
/// either have SIMD and can only run on engines which support SIMD, or it
/// will not have SIMD at all. For compatibility the standard library itself
/// does not use any SIMD internally. Determining how best to ship your
/// WebAssembly binary with SIMD is largely left up to you as it can can be
/// pretty nuanced depending on your situation.
/// motivators for the [conditional sections][condsections] and [feature
/// detection] proposals, but that is still pretty early days). This means
/// that your binary will either have SIMD and can only run on engines
/// which support SIMD, or it will not have SIMD at all. For compatibility
/// the standard library itself does not use any SIMD internally.
/// Determining how best to ship your WebAssembly binary with SIMD is
/// largely left up to you as it can can be pretty nuanced depending on
/// your situation.
///
/// [condsections]: https://github.com/webassembly/conditional-sections
/// [feature detection]: https://github.com/WebAssembly/feature-detection
///
/// To enable SIMD support at compile time you need to do one of two things:
///
Expand All @@ -138,7 +152,9 @@ pub mod arch {
/// * Second you can compile your program with `-Ctarget-feature=+simd128`.
/// This compilation flag blanket enables SIMD support for your entire
/// compilation. Note that this does not include the standard library
/// unless you recompile the standard library.
/// unless you [recompile the standard library][buildstd].
///
/// [buildstd]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std
///
/// If you enable SIMD via either of these routes then you'll have a
/// WebAssembly binary that uses SIMD instructions, and you'll need to ship
Expand All @@ -147,21 +163,6 @@ pub mod arch {
/// generated in your program. This means to generate a binary without SIMD
/// you'll need to avoid both options above plus calling into any intrinsics
/// in this module.
///
/// > **Note**: Due to
/// > [rust-lang/rust#74320](https://github.com/rust-lang/rust/issues/74320)
/// > it's recommended to compile your entire program with SIMD support
/// > (using `RUSTFLAGS`) or otherwise functions may not be inlined
/// > correctly.
///
/// > **Note**: LLVM's SIMD support is actually split into two features:
/// > `simd128` and `unimplemented-simd128`. Rust code can enable `simd128`
/// > with `#[target_feature]` (and test for it with `#[cfg(target_feature =
/// > "simd128")]`, but it cannot enable `unimplemented-simd128`. The only
/// > way to enable this feature is to compile with
/// > `-Ctarget-feature=+simd128,+unimplemented-simd128`. This second
/// > feature enables more recent instructions implemented in LLVM which
/// > haven't always had enough time to make their way to runtimes.
#[cfg(any(target_arch = "wasm32", doc))]
#[doc(cfg(target_arch = "wasm32"))]
#[stable(feature = "simd_wasm32", since = "1.33.0")]
Expand Down
40 changes: 6 additions & 34 deletions crates/core_arch/src/wasm32/atomic.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
20 changes: 6 additions & 14 deletions crates/core_arch/src/wasm32/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use stdarch_test::assert_instr;

extern "C" {
#[link_name = "llvm.wasm.memory.grow.i32"]
fn llvm_memory_grow(mem: i32, pages: i32) -> i32;
fn llvm_memory_grow(mem: u32, pages: i32) -> i32;
#[link_name = "llvm.wasm.memory.size.i32"]
fn llvm_memory_size(mem: i32) -> i32;
fn llvm_memory_size(mem: u32) -> i32;
}

/// Corresponding intrinsic to wasm's [`memory.size` instruction][instr]
Expand All @@ -25,13 +25,8 @@ extern "C" {
#[rustc_legacy_const_generics(0)]
#[stable(feature = "simd_wasm32", since = "1.33.0")]
pub fn memory_size<const MEM: u32>() -> usize {
unsafe {
// FIXME: Consider replacing with a static_assert!
if MEM != 0 {
crate::intrinsics::abort();
}
llvm_memory_size(0) as usize
}
static_assert!(MEM: u32 where MEM == 0);
unsafe { llvm_memory_size(MEM) as usize }
}

/// Corresponding intrinsic to wasm's [`memory.grow` instruction][instr]
Expand All @@ -55,10 +50,7 @@ pub fn memory_size<const MEM: u32>() -> usize {
#[stable(feature = "simd_wasm32", since = "1.33.0")]
pub fn memory_grow<const MEM: u32>(delta: usize) -> usize {
unsafe {
// FIXME: Consider replacing with a static_assert!
if MEM != 0 {
crate::intrinsics::abort();
}
llvm_memory_grow(0, delta as i32) as isize as usize
static_assert!(MEM: u32 where MEM == 0);
llvm_memory_grow(MEM, delta as i32) as isize as usize
}
}
2 changes: 0 additions & 2 deletions crates/core_arch/src/wasm32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading