Skip to content

Commit

Permalink
Update and revamp wasm32 SIMD intrinsics (#874)
Browse files Browse the repository at this point in the history
Lots of time and lots of things have happened since the simd128 support
was first added to this crate. Things are starting to settle down now so
this commit syncs the Rust intrinsic definitions with the current
specification (https://github.com/WebAssembly/simd). Unfortuantely not
everything can be enabled just yet but everything is in the pipeline for
getting enabled soon.

This commit also applies a major revamp to how intrinsics are tested.
The intention is that the setup should be much more lightweight and/or
easy to work with after this commit.

At a high-level, the changes here are:

* Testing with node.js and `#[wasm_bindgen]` has been removed. Instead
  intrinsics are tested with Wasmtime which has a nearly complete
  implementation of the SIMD spec (and soon fully complete!)

* Testing is switched to `wasm32-wasi` to make idiomatic Rust bits a bit
  easier to work with (e.g. `panic!)`

* Testing of this crate's simd128 feature for wasm is re-enabled. This
  will run on CI and both compile and execute intrinsics. This should
  bring wasm intrinsics to the same level of parity as x86 intrinsics,
  for example.

* New wasm intrinsics have been added:
  * `iNNxMM_loadAxA_{s,u}`
  * `vNNxMM_load_splat`
  * `v8x16_swizzle`
  * `v128_andnot`
  * `iNNxMM_abs`
  * `iNNxMM_narrow_*_{u,s}`
  * `iNNxMM_bitmask` - commented out until LLVM is updated to LLVM 11
  * `iNNxMM_widen_*_{u,s}` - commented out until
    bytecodealliance/wasmtime#1994 lands
  * `iNNxMM_{max,min}_{u,s}`
  * `iNNxMM_avgr_u`

* Some wasm intrinsics have been removed:
  * `i64x2_trunc_*`
  * `f64x2_convert_*`
  * `i8x16_mul`

* The `v8x16.shuffle` instruction is exposed. This is done through a
  `macro` (not `macro_rules!`, but `macro`). This is intended to be
  somewhat experimental and unstable until we decide otherwise. This
  instruction has 16 immediate-mode expressions and is as a result
  unsuited to the existing `constify_*` logic of this crate. I'm hoping
  that we can game out over time what a macro might look like and/or
  look for better solutions. For now, though, what's implemented is the
  first of its kind in this crate (an architecture-specific macro), so
  some extra scrutiny looking at it would be appreciated.

* Lots of `assert_instr` annotations have been fixed for wasm.

* All wasm simd128 tests are uncommented and passing now.

This is still missing tests for new intrinsics and it's also missing
tests for various corner cases. I hope to get to those later as the
upstream spec itself gets closer to stabilization.

In the meantime, however, I went ahead and updated the `hex.rs` example
with a wasm implementation using intrinsics. With it I got some very
impressive speedups using Wasmtime:

    test benches::large_default  ... bench:     213,961 ns/iter (+/- 5,108) = 4900 MB/s
    test benches::large_fallback ... bench:   3,108,434 ns/iter (+/- 75,730) = 337 MB/s
    test benches::small_default  ... bench:          52 ns/iter (+/- 0) = 2250 MB/s
    test benches::small_fallback ... bench:         358 ns/iter (+/- 0) = 326 MB/s

or otherwise using Wasmtime hex encoding using SIMD is 15x faster on 1MB
chunks or 7x faster on small <128byte chunks.

All of these intrinsics are still unstable and will continue to be so
presumably until the simd proposal in wasm itself progresses to a later
stage. Additionaly we'll still want to sync with clang on intrinsic
names (or decide not to) at some point in the future.

* wasm: Unconditionally expose SIMD functions

This commit unconditionally exposes SIMD functions from the `wasm32`
module. This is done in such a way that the standard library does not
need to be recompiled to access SIMD intrinsics and use them. This,
hopefully, is the long-term story for SIMD in WebAssembly in Rust.

It's unlikely that all WebAssembly runtimes will end up implementing
SIMD so the standard library is unlikely to use SIMD any time soon, but
we want to make sure it's easily available to folks! This commit enables
all this by ensuring that SIMD is available to the standard library,
regardless of compilation flags.

This'll come with the same caveats as x86 support, where it doesn't make
sense to call these functions unless you're enabling simd support one
way or another locally. Additionally, as with x86, if you don't call
these functions then the instructions won't show up in your binary.

While I was here I went ahead and expanded the WebAssembly-specific
documentation for the wasm32 module as well, ensuring that the current
state of SIMD/Atomics are documented.
  • Loading branch information
alexcrichton authored Jul 18, 2020
1 parent de984bc commit 56ee497
Show file tree
Hide file tree
Showing 22 changed files with 1,984 additions and 1,217 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- mips64-unknown-linux-gnuabi64
- mips64el-unknown-linux-gnuabi64
- s390x-unknown-linux-gnu
- wasm32-unknown-unknown
- wasm32-wasi
- i586-unknown-linux-gnu
- x86_64-linux-android
- arm-linux-androideabi
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
disable_assert_instr: true
- target: s390x-unknown-linux-gnu
os: ubuntu-latest
- target: wasm32-unknown-unknown
- target: wasm32-wasi
os: ubuntu-latest
- target: aarch64-unknown-linux-gnu
os: ubuntu-latest
Expand Down
25 changes: 0 additions & 25 deletions ci/docker/wasm32-unknown-unknown/Dockerfile

This file was deleted.

15 changes: 0 additions & 15 deletions ci/docker/wasm32-unknown-unknown/wasm-entrypoint.sh

This file was deleted.

16 changes: 16 additions & 0 deletions ci/docker/wasm32-wasi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.19.0/wasmtime-v0.19.0-x86_64-linux.tar.xz | tar xJf -
ENV PATH=$PATH:/wasmtime-v0.19.0-x86_64-linux

ENV CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime \
--enable-simd \
--mapdir .::/checkout/target/wasm32-wasi/release/deps \
--"
31 changes: 16 additions & 15 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ cargo_test() {
fi
cmd="$cmd ${subcmd} --target=$TARGET $1"
cmd="$cmd -- $2"

# wasm targets can't catch panics so if a test failures make sure the test
# harness isn't trying to capture output, otherwise we won't get any useful
# output.
case ${TARGET} in
wasm32*)
cmd="$cmd --nocapture"
;;
esac

$cmd
}

Expand Down Expand Up @@ -72,20 +82,11 @@ case ${TARGET} in
export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+avx"
cargo_test "--release"
;;
wasm32-unknown-unknown*)
# Attempt to actually run some SIMD tests in node.js. Unfortunately
# though node.js (transitively through v8) doesn't have support for the
# full SIMD spec yet, only some functions. As a result only pass in
# some target features and a special `--cfg`
# FIXME: broken
#export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128 --cfg only_node_compatible_functions"
#cargo_test "--release"

# After that passes make sure that all intrinsics compile, passing in
# the extra feature to compile in non-node-compatible SIMD.
# FIXME: broken
#export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128,+unimplemented-simd128"
#cargo_test "--release --no-run"
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*)
Expand All @@ -111,7 +112,7 @@ case ${TARGET} in

esac

if [ "$NORUN" != "1" ] && [ "$NOSTD" != 1 ] && [ "$TARGET" != "wasm32-unknown-unknown" ]; then
if [ "$NORUN" != "1" ] && [ "$NOSTD" != 1 ]; then
# Test examples
(
cd examples
Expand Down
15 changes: 8 additions & 7 deletions crates/assert-instr-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ pub fn assert_instr(
// generate some code that's hopefully very tight in terms of
// codegen but is otherwise unique to prevent code from being
// folded.
//
// This is avoided on Wasm32 right now since these functions aren't
// inlined which breaks our tests since each intrinsic looks like it
// calls functions. Turns out functions aren't similar enough to get
// merged on wasm32 anyway. This bug is tracked at
// rust-lang/rust#74320.
#[cfg(not(target_arch = "wasm32"))]
::stdarch_test::_DONT_DEDUP.store(
std::mem::transmute(#shim_name_str.as_bytes().as_ptr()),
std::sync::atomic::Ordering::Relaxed,
Expand All @@ -131,8 +138,7 @@ pub fn assert_instr(
};

let tokens: TokenStream = quote! {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[test]
#[allow(non_snake_case)]
fn #assert_name() {
#to_test
Expand All @@ -146,11 +152,6 @@ pub fn assert_instr(
#instr);
}
};
// why? necessary now to get tests to work?
let tokens: TokenStream = tokens
.to_string()
.parse()
.expect("cannot parse tokenstream");

let tokens: TokenStream = quote! {
#item
Expand Down
3 changes: 0 additions & 3 deletions crates/core_arch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ maintenance = { status = "experimental" }
stdarch-test = { version = "0.*", path = "../stdarch-test" }
std_detect = { version = "0.*", path = "../std_detect" }

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.2.47"

[package.metadata.docs.rs]
rustdoc-args = [ "--cfg", "dox" ]
14 changes: 14 additions & 0 deletions crates/core_arch/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
use std::env;

fn main() {
println!("cargo:rustc-cfg=core_arch_docs");

// Used to tell our `#[assert_instr]` annotations that all simd intrinsics
// are available to test their codegen, since some are gated behind an extra
// `-Ctarget-feature=+unimplemented-simd128` that doesn't have any
// equivalent in `#[target_feature]` right now.
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
if env::var("RUSTFLAGS")
.unwrap_or_default()
.contains("unimplemented-simd128")
{
println!("cargo:rustc-cfg=all_simd");
}
}
13 changes: 8 additions & 5 deletions crates/core_arch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![doc(include = "core_arch_docs.md")]
#![allow(improper_ctypes_definitions)]
#![allow(dead_code)]
#![allow(unused_features)]
#![allow(incomplete_features)]
#![feature(
const_fn,
const_fn_union,
const_generics,
custom_inner_attributes,
link_llvm_intrinsics,
platform_intrinsics,
Expand Down Expand Up @@ -32,9 +35,12 @@
adx_target_feature,
rtm_target_feature,
f16c_target_feature,
external_doc
external_doc,
allow_internal_unstable,
decl_macro
)]
#![cfg_attr(test, feature(test, abi_vectorcall, untagged_unions))]
#![cfg_attr(all(test, target_arch = "wasm32"), feature(wasm_simd))]
#![deny(clippy::missing_inline_in_public_items)]
#![allow(
clippy::inline_always,
Expand Down Expand Up @@ -66,13 +72,10 @@ extern crate std_detect;
#[cfg(test)]
extern crate stdarch_test;

#[cfg(all(test, target_arch = "wasm32"))]
extern crate wasm_bindgen_test;

#[path = "mod.rs"]
mod core_arch;

pub use self::core_arch::arch::*;
pub use self::core_arch::arch;

#[allow(unused_imports)]
use core::{ffi, hint, intrinsics, marker, mem, ops, ptr, sync};
110 changes: 103 additions & 7 deletions crates/core_arch/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,110 @@ pub mod arch {

/// Platform-specific intrinsics for the `wasm32` platform.
///

/// # Availability
/// 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.
///
/// Note that intrinsics gated by `target_feature = "atomics"` or `target_feature = "simd128"`
/// are only available **when the standard library itself is compiled with the the respective
/// 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.
/// See the [module documentation](../index.html) for more details.
/// [atomics]: https://github.com/webassembly/threads
/// [simd]: https://github.com/webassembly/simd
///
/// See the [module documentation](../index.html) for general information
/// about the `arch` module and platform intrinsics.
///
/// ## 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.
///
/// 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.
///
/// ## 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
/// 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!
///
/// Using SIMD is intended to be similar to as you would on `x86_64`, for
/// example. You'd write a function such as:
///
/// ```rust,ignore
/// #[cfg(target_arch = "wasm32")]
/// #[target_feature(enable = "simd128")]
/// unsafe fn uses_simd() {
/// use std::arch::wasm32::*;
/// // ...
/// }
/// ```
///
/// 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.
///
/// [condsections]: https://github.com/webassembly/conditional-sections
///
/// To enable SIMD support at compile time you need to do one of two things:
///
/// * First you can annotate functions with `#[target_feature(enable =
/// "simd128")]`. This causes just that one function to have SIMD support
/// available to it, and intrinsics will get inlined as usual in this
/// situation.
///
/// * 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.
///
/// 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
/// that accordingly. Also note that if you call SIMD intrinsics but don't
/// enable SIMD via either of these mechanisms, you'll still have SIMD
/// 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", dox))]
#[doc(cfg(target_arch = "wasm32"))]
#[stable(feature = "simd_wasm32", since = "1.33.0")]
Expand Down
Loading

0 comments on commit 56ee497

Please sign in to comment.