Skip to content

Commit 47e3245

Browse files
committed
Update and revamp wasm32 SIMD intrinsics
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.
1 parent 9faced9 commit 47e3245

File tree

22 files changed

+1200
-797
lines changed

22 files changed

+1200
-797
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
- mips64-unknown-linux-gnuabi64
7878
- mips64el-unknown-linux-gnuabi64
7979
- s390x-unknown-linux-gnu
80-
- wasm32-unknown-unknown
80+
- wasm32-wasi
8181
- i586-unknown-linux-gnu
8282
- x86_64-linux-android
8383
- arm-linux-androideabi
@@ -129,7 +129,7 @@ jobs:
129129
disable_assert_instr: true
130130
- target: s390x-unknown-linux-gnu
131131
os: ubuntu-latest
132-
- target: wasm32-unknown-unknown
132+
- target: wasm32-wasi
133133
os: ubuntu-latest
134134
- target: aarch64-unknown-linux-gnu
135135
os: ubuntu-latest

ci/docker/wasm32-unknown-unknown/Dockerfile

-25
This file was deleted.

ci/docker/wasm32-unknown-unknown/wasm-entrypoint.sh

-15
This file was deleted.

ci/docker/wasm32-wasi/Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM ubuntu:20.04
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
5+
ca-certificates \
6+
clang \
7+
cmake \
8+
curl \
9+
git \
10+
libc6-dev \
11+
make \
12+
python \
13+
python3 \
14+
xz-utils
15+
16+
RUN curl -L https://github.com/bytecodealliance/wasmtime/releases/download/dev/wasmtime-dev-x86_64-linux.tar.xz | tar xJf -
17+
ENV PATH=$PATH:/wasmtime-dev-x86_64-linux
18+
19+
ENV CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime \
20+
--enable-simd \
21+
--mapdir .::/checkout/target/wasm32-wasi/release/deps \
22+
--"

ci/run.sh

+16-15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ cargo_test() {
4444
fi
4545
cmd="$cmd ${subcmd} --target=$TARGET $1"
4646
cmd="$cmd -- $2"
47+
48+
# wasm targets can't catch panics so if a test failures make sure the test
49+
# harness isn't trying to capture output, otherwise we won't get any useful
50+
# output.
51+
case ${TARGET} in
52+
wasm32*)
53+
cmd="$cmd --nocapture"
54+
;;
55+
esac
56+
4757
$cmd
4858
}
4959

@@ -72,20 +82,11 @@ case ${TARGET} in
7282
export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+avx"
7383
cargo_test "--release"
7484
;;
75-
wasm32-unknown-unknown*)
76-
# Attempt to actually run some SIMD tests in node.js. Unfortunately
77-
# though node.js (transitively through v8) doesn't have support for the
78-
# full SIMD spec yet, only some functions. As a result only pass in
79-
# some target features and a special `--cfg`
80-
# FIXME: broken
81-
#export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128 --cfg only_node_compatible_functions"
82-
#cargo_test "--release"
83-
84-
# After that passes make sure that all intrinsics compile, passing in
85-
# the extra feature to compile in non-node-compatible SIMD.
86-
# FIXME: broken
87-
#export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128,+unimplemented-simd128"
88-
#cargo_test "--release --no-run"
85+
wasm32*)
86+
prev="$RUSTFLAGS"
87+
export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+simd128,+unimplemented-simd128"
88+
cargo_test "--release"
89+
export RUSTFLAGS="$prev"
8990
;;
9091
# FIXME: don't build anymore
9192
#mips-*gnu* | mipsel-*gnu*)
@@ -111,7 +112,7 @@ case ${TARGET} in
111112

112113
esac
113114

114-
if [ "$NORUN" != "1" ] && [ "$NOSTD" != 1 ] && [ "$TARGET" != "wasm32-unknown-unknown" ]; then
115+
if [ "$NORUN" != "1" ] && [ "$NOSTD" != 1 ]; then
115116
# Test examples
116117
(
117118
cd examples

crates/assert-instr-macro/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ pub fn assert_instr(
131131
};
132132

133133
let tokens: TokenStream = quote! {
134-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
135-
#[cfg_attr(not(target_arch = "wasm32"), test)]
134+
#[test]
136135
#[allow(non_snake_case)]
137136
fn #assert_name() {
138137
#to_test
@@ -146,11 +145,6 @@ pub fn assert_instr(
146145
#instr);
147146
}
148147
};
149-
// why? necessary now to get tests to work?
150-
let tokens: TokenStream = tokens
151-
.to_string()
152-
.parse()
153-
.expect("cannot parse tokenstream");
154148

155149
let tokens: TokenStream = quote! {
156150
#item

crates/core_arch/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,5 @@ maintenance = { status = "experimental" }
2626
stdarch-test = { version = "0.*", path = "../stdarch-test" }
2727
std_detect = { version = "0.*", path = "../std_detect" }
2828

29-
[target.wasm32-unknown-unknown.dev-dependencies]
30-
wasm-bindgen-test = "0.2.47"
31-
3229
[package.metadata.docs.rs]
3330
rustdoc-args = [ "--cfg", "dox" ]

crates/core_arch/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
adx_target_feature,
3333
rtm_target_feature,
3434
f16c_target_feature,
35-
external_doc
35+
external_doc,
36+
allow_internal_unstable,
37+
decl_macro
3638
)]
3739
#![cfg_attr(test, feature(test, abi_vectorcall, untagged_unions))]
3840
#![deny(clippy::missing_inline_in_public_items)]
@@ -66,13 +68,10 @@ extern crate std_detect;
6668
#[cfg(test)]
6769
extern crate stdarch_test;
6870

69-
#[cfg(all(test, target_arch = "wasm32"))]
70-
extern crate wasm_bindgen_test;
71-
7271
#[path = "mod.rs"]
7372
mod core_arch;
7473

75-
pub use self::core_arch::arch::*;
74+
pub use self::core_arch::arch;
7675

7776
#[allow(unused_imports)]
7877
use core::{ffi, hint, intrinsics, marker, mem, ops, ptr, sync};

crates/core_arch/src/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! `core_arch`
22
3+
#![allow(improper_ctypes_definitions)]
4+
35
#[macro_use]
46
mod macros;
57

crates/core_arch/src/wasm32/atomic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#[cfg(test)]
1212
use stdarch_test::assert_instr;
13-
#[cfg(test)]
14-
use wasm_bindgen_test::wasm_bindgen_test;
1513

1614
extern "C" {
1715
#[link_name = "llvm.wasm.atomic.wait.i32"]

crates/core_arch/src/wasm32/memory.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#[cfg(test)]
22
use stdarch_test::assert_instr;
3-
#[cfg(test)]
4-
use wasm_bindgen_test::wasm_bindgen_test;
53

64
extern "C" {
75
#[link_name = "llvm.wasm.memory.grow.i32"]

crates/core_arch/src/wasm32/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
#[cfg(test)]
44
use stdarch_test::assert_instr;
5-
#[cfg(test)]
6-
use wasm_bindgen_test::wasm_bindgen_test;
75

86
#[cfg(any(target_feature = "atomics", dox))]
97
mod atomic;

0 commit comments

Comments
 (0)