Skip to content

Commit

Permalink
Update and revamp wasm32 SIMD intrinsics
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.
  • Loading branch information
alexcrichton committed Jul 13, 2020
1 parent 9faced9 commit 0b5db66
Show file tree
Hide file tree
Showing 22 changed files with 1,204 additions and 797 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.

22 changes: 22 additions & 0 deletions ci/docker/wasm32-wasi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y --no-install-recommends \
ca-certificates \
clang \
cmake \
curl \
git \
libc6-dev \
make \
python \
python3 \
xz-utils

RUN curl -L https://github.com/bytecodealliance/wasmtime/releases/download/dev/wasmtime-dev-x86_64-linux.tar.xz | tar xJf -
ENV PATH=$PATH:/wasmtime-dev-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
8 changes: 1 addition & 7 deletions crates/assert-instr-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,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 +145,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" ]
10 changes: 5 additions & 5 deletions crates/core_arch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
adx_target_feature,
rtm_target_feature,
f16c_target_feature,
external_doc
external_doc,
allow_internal_unstable,
allow_internal_unsafe,
decl_macro
)]
#![cfg_attr(test, feature(test, abi_vectorcall, untagged_unions))]
#![deny(clippy::missing_inline_in_public_items)]
Expand Down Expand Up @@ -66,13 +69,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};
2 changes: 2 additions & 0 deletions crates/core_arch/src/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `core_arch`
#![allow(improper_ctypes_definitions)]

#[macro_use]
mod macros;

Expand Down
2 changes: 0 additions & 2 deletions crates/core_arch/src/wasm32/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#[cfg(test)]
use stdarch_test::assert_instr;
#[cfg(test)]
use wasm_bindgen_test::wasm_bindgen_test;

extern "C" {
#[link_name = "llvm.wasm.atomic.wait.i32"]
Expand Down
2 changes: 0 additions & 2 deletions crates/core_arch/src/wasm32/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(test)]
use stdarch_test::assert_instr;
#[cfg(test)]
use wasm_bindgen_test::wasm_bindgen_test;

extern "C" {
#[link_name = "llvm.wasm.memory.grow.i32"]
Expand Down
4 changes: 2 additions & 2 deletions crates/core_arch/src/wasm32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#[cfg(test)]
use stdarch_test::assert_instr;
#[cfg(test)]
use wasm_bindgen_test::wasm_bindgen_test;

#[cfg(any(target_feature = "atomics", dox))]
mod atomic;
Expand All @@ -12,6 +10,8 @@ pub use self::atomic::*;

#[cfg(any(target_feature = "simd128", dox))]
mod simd128;
// #[cfg(any(target_feature = "simd128", dox))]
// pub use self::simd128::v128_shuffle;
#[cfg(any(target_feature = "simd128", dox))]
pub use self::simd128::*;

Expand Down
Loading

0 comments on commit 0b5db66

Please sign in to comment.