Skip to content

Commit

Permalink
feat: New data-threads attribute for rust assets.
Browse files Browse the repository at this point in the history
This enables the `threads`, `bulk-memory`, and `mutable-globals`
options in `wasm-opt`, which if absent can result in `wasm-opt`
failing to optimize wasm modules compiled with threading features.

Fixes trunk-rs#866.
  • Loading branch information
BGR360 committed Sep 8, 2024
1 parent 14b972f commit fe4fc9d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/wasm_threads/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
rel="rust"
href="Cargo.toml"
data-wasm-opt="z"
data-threads
data-bindgen-target="web"
/>
<link data-trunk rel="copy-file" href="assets/_headers" />
Expand Down
1 change: 1 addition & 0 deletions guide/src/assets/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-weak-refs`: (optional) instruct `wasm-bindgen` to enable [weak references](https://rustwasm.github.io/docs/wasm-bindgen/reference/weak-references.html).
- `data-typescript`: (optional) instruct `wasm-bindgen` to output Typescript bindings. Defaults to false.
- `data-bindgen-target`: (optional) specifies the value of the `wasm-bindgen` [flag `--target`](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html) (see link for possible values). Defaults to `no-modules`. The main use-case is to switch to `web` with `data-type="worker"` which reduces backwards [compatibility](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) but with some [advantages](https://rustwasm.github.io/wasm-bindgen/examples/without-a-bundler.html?highlight=no-modules#using-the-older---target-no-modules).
- `data-threads`: (optional) instruct `wasm-opt` to enable `threads`, `bulk-memory`, and `mutable-globals`. See the [`wasm-bindgen` Parallel Raytracing example](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html) or [this rustwasm blog post](https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html) for more information.
- `data-loader-shim`: (optional) instruct `trunk` to create a loader shim for web workers. Defaults to false.
- `data-cross-origin`: (optional) the `crossorigin` setting when loading the code & script resources. Defaults to plain `anonymous`.
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
Expand Down
1 change: 1 addition & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-weak-refs`: (optional) instruct `wasm-bindgen` to enable [weak references](https://rustwasm.github.io/docs/wasm-bindgen/reference/weak-references.html).
- `data-typescript`: (optional) instruct `wasm-bindgen` to output Typescript bindings. Defaults to false.
- `data-bindgen-target`: (optional) specifies the value of the `wasm-bindgen` [flag `--target`](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html) (see link for possible values). Defaults to `no-modules`. The main use-case is to switch to `web` with `data-type="worker"` which reduces backwards [compatibility](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) but with some [advantages](https://rustwasm.github.io/wasm-bindgen/examples/without-a-bundler.html?highlight=no-modules#using-the-older---target-no-modules).
- `data-threads`: (optional) instruct `wasm-opt` to enable `threads`, `bulk-memory`, and `mutable-globals`. See the [`wasm-bindgen` Parallel Raytracing example](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html) or [this rustwasm blog post](https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html) for more information.
- `data-loader-shim`: (optional) instruct `trunk` to create a loader shim for web workers. Defaults to false.
- `data-cross-origin`: (optional) the `crossorigin` setting when loading the code & script resources. Defaults to plain `anonymous`.
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
Expand Down
10 changes: 10 additions & 0 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct RustApp {
reference_types: bool,
/// An option to instruct wasm-bindgen to enable weak references.
weak_refs: bool,
/// An option to instruct wasm-opt to allow atomics, bulk-memory, and mutable-globals.
threads: bool,
/// An optional optimization setting that enables wasm-opt. Can be nothing, `0` (default), `1`,
/// `2`, `3`, `4`, `s or `z`. Using `0` disables wasm-opt completely.
wasm_opt: WasmOptLevel,
Expand Down Expand Up @@ -156,6 +158,7 @@ impl RustApp {
.unwrap_or(RustAppType::Main);
let reference_types = attrs.contains_key("data-reference-types");
let weak_refs = attrs.contains_key("data-weak-refs");
let threads = attrs.contains_key("data-threads");
let wasm_opt = attrs
.get("data-wasm-opt")
.map(|val| val.parse())
Expand Down Expand Up @@ -256,6 +259,7 @@ impl RustApp {
no_demangle,
reference_types,
weak_refs,
threads,
wasm_opt,
wasm_bindgen_target,
app_type,
Expand Down Expand Up @@ -304,6 +308,7 @@ impl RustApp {
no_demangle: false,
reference_types: false,
weak_refs: false,
threads: false,
wasm_opt: WasmOptLevel::Off,
app_type: RustAppType::Main,
wasm_bindgen_target: WasmBindgenTarget::Web,
Expand Down Expand Up @@ -878,6 +883,11 @@ impl RustApp {
if self.reference_types {
args.push("--enable-reference-types");
}
if self.threads {
args.push("--enable-threads");
args.push("--enable-bulk-memory");
args.push("--enable-mutable-globals");
}

// Invoke wasm-opt.
tracing::debug!("calling wasm-opt");
Expand Down

0 comments on commit fe4fc9d

Please sign in to comment.