Skip to content

Commit e748aef

Browse files
[WASM] fix unconditional SIMD inclusion in WASM binary (#16)
### Context I made a mistake using `#[target_feature(enable = "simd128")]` because it means the enclosed code is compiled using the simd128 feature flag. This can result in binaries containing SIMD. Even if the SIMD is never executed, older browsers may fail to validate the WASM binary if it includes SIMD instructions. Thus, this PR removes all usages of target_feature for WASM, instead opting into conditional compilation. ### Test plan This really needs an automated test, but I haven't found a good way to test this yet. I manually tested by running the following command: ``` cargo build --example srgb --target=wasm32-unknown-unknown && wasm2wat ./target/wasm32-unknown-unknown/debug/examples/srgb.wasm | grep -q v128 && echo "SIMD FOUND" || echo "NO SIMD" ``` Now returns `NO SIMD` , when previously it returned `SIMD FOUND`. ``` RUSTFLAGS=-Ctarget-feature=+simd128 cargo build --example srgb --target=wasm32-unknown-unknown && wasm2wat ./target/wasm32-unknown-unknown/debug/examples/srgb.wasm | grep -q v128 && echo "SIMD FOUND" || echo "NO SIMD" ``` Returns `SIMD FOUND` because the `+simd128` feature was requested.
1 parent 00dd1b7 commit e748aef

File tree

7 files changed

+18
-19
lines changed

7 files changed

+18
-19
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
tool: wasm-pack
7474

7575
- name: Run fearless_simd_tests on Chrome
76-
run: wasm-pack test --headless --chrome
76+
run: RUSTFLAGS=-Ctarget-feature=+simd128 wasm-pack test --headless --chrome
7777
working-directory: fearless_simd_tests
7878

7979
build-no-std:

fearless_simd/src/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ mod neon;
1111
mod ops;
1212
mod simd_trait;
1313
mod simd_types;
14-
#[cfg(target_arch = "wasm32")]
14+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
1515
mod wasm;
1616

1717
pub use fallback::*;
1818
#[cfg(all(feature = "std", target_arch = "aarch64"))]
1919
pub use neon::*;
2020
pub use simd_trait::*;
2121
pub use simd_types::*;
22-
#[cfg(target_arch = "wasm32")]
22+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
2323
pub use wasm::*;

fearless_simd/src/generated/wasm.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl Simd for WasmSimd128 {
3636
}
3737
#[inline]
3838
fn vectorize<F: FnOnce() -> R, R>(self, f: F) -> R {
39-
#[target_feature(enable = "simd128")]
4039
#[inline]
4140
unsafe fn vectorize_simd128<F: FnOnce() -> R, R>(f: F) -> R {
4241
f()

fearless_simd/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub mod aarch64 {
3434
pub use crate::generated::Neon;
3535
}
3636

37-
#[cfg(target_arch = "wasm32")]
37+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
3838
pub mod wasm32 {
3939
pub use crate::generated::WasmSimd128;
4040
}
@@ -45,7 +45,7 @@ pub enum Level {
4545
Fallback(Fallback),
4646
#[cfg(all(feature = "std", target_arch = "aarch64"))]
4747
Neon(Neon),
48-
#[cfg(target_arch = "wasm32")]
48+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
4949
WasmSimd128(WasmSimd128),
5050
}
5151

@@ -70,7 +70,7 @@ impl Level {
7070
}
7171
}
7272

73-
#[cfg(target_arch = "wasm32")]
73+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
7474
#[inline]
7575
pub fn as_wasm_simd128(self) -> Option<WasmSimd128> {
7676
match self {
@@ -94,8 +94,7 @@ impl Level {
9494
f.with_simd(neon)
9595
}
9696

97-
#[cfg(target_arch = "wasm32")]
98-
#[target_feature(enable = "simd128")]
97+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
9998
#[inline]
10099
fn dispatch_simd128<W: WithSimd>(f: W, simd128: WasmSimd128) -> W::Output {
101100
f.with_simd(simd128)
@@ -109,7 +108,7 @@ impl Level {
109108
match self {
110109
#[cfg(all(feature = "std", target_arch = "aarch64"))]
111110
Level::Neon(neon) => unsafe { dispatch_neon(f, neon) },
112-
#[cfg(target_arch = "wasm32")]
111+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
113112
Level::WasmSimd128(simd128) => dispatch_simd128(f, simd128),
114113
Level::Fallback(fallback) => dispatch_fallback(f, fallback),
115114
}

fearless_simd/src/macros.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ macro_rules! simd_dispatch {
1919
unsafe fn inner_neon(neon: $crate::aarch64::Neon $( , $arg: $ty )* ) $( -> $ret )? {
2020
$inner( neon $( , $arg )* )
2121
}
22-
#[cfg(target_arch = "wasm32")]
23-
#[target_feature(enable = "simd128")]
22+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
2423
#[inline]
2524
unsafe fn inner_wasm_simd128(simd128: $crate::wasm32::WasmSimd128 $( , $arg: $ty )* ) $( -> $ret )? {
25+
println!("GOT IN HERE WHAT THE!");
2626
$inner( simd128 $( , $arg )* )
2727
}
2828
match level {
29-
Level::Fallback(fb) => $inner(fb $( , $arg )* ),
29+
Level::Fallback(fb) => {
30+
println!("fallback dispatched");
31+
$inner(fb $( , $arg )* )
32+
},
3033
#[cfg(target_arch = "aarch64")]
3134
Level::Neon(neon) => unsafe { inner_neon (neon $( , $arg )* ) }
32-
#[cfg(target_arch = "wasm32")]
35+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
3336
Level::WasmSimd128(wasm) => unsafe { inner_wasm_simd128 (wasm $( , $arg )* ) }
3437
}
3538
}
@@ -46,15 +49,14 @@ macro_rules! simd_dispatch {
4649
) => {
4750
$( #[$meta] )* $vis
4851
fn $func(level: $crate::Level $(, $arg: $ty )*) $( -> $ret )? {
49-
#[cfg(target_arch = "wasm32")]
50-
#[target_feature(enable = "simd128")]
52+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
5153
#[inline]
5254
unsafe fn inner_wasm_simd128(simd128: $crate::wasm32::WasmSimd128 $( , $arg: $ty )* ) $( -> $ret )? {
5355
$inner( simd128 $( , $arg )* )
5456
}
5557
match level {
5658
Level::Fallback(fb) => $inner(fb $( , $arg )* ),
57-
#[cfg(target_arch = "wasm32")]
59+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
5860
Level::WasmSimd128(wasm) => unsafe { inner_wasm_simd128 (wasm $( , $arg )* ) }
5961
}
6062
}

fearless_simd_gen/src/mk_wasm.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ fn mk_simd_impl(level: Level) -> TokenStream {
257257

258258
#[inline]
259259
fn vectorize<F: FnOnce() -> R, R>(self, f: F) -> R {
260-
#[target_feature(enable = "simd128")]
261260
#[inline]
262261
// unsafe not needed here with tf11, but can be justified
263262
unsafe fn vectorize_simd128<F: FnOnce() -> R, R>(f: F) -> R {

fearless_simd_tests/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
#[cfg(target_arch = "wasm32")]
55
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
66

7-
#[cfg(target_arch = "wasm32")]
7+
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
88
mod wasm;

0 commit comments

Comments
 (0)