Stream: general

Topic: Calling Wasm logic from multiple threads with shared memory


view this post on Zulip Pēteris Pakalns (Mar 29 2025 at 11:19):

I have successfully integrated Wasm for modding in a game that I am creating. Thank you all for developing wasmtime, wit-bindgen.
Last problem that I am facing is with improving parallelization.

For couple of days I have been researching state of Wasm with threading support (https://github.com/RReverser/wasm-bindgen-rayon).

In my project I need to execute code that accesses/reads the same data. Currently in native code to improve calculation speed I am parallelizing calculations on multiple threads. Would love to move this into Wasm to allow project users to modify calculation logic, but this would require calling the same Wasm logic from multiple host threads to keep comparable performance.

I am trying to set up Wasm instances with the same Wasm module that all use the same shared memory.

For Wasm guest code Rust is used and I try to compile it with following command:

cargo rustc \
  --package=wasm_package \
  --target=wasm32-wasip1-threads \
  -Z build-std=panic_abort,std \
  --release \
  --crate-type=cdylib \
  -- \
  -C target-feature='+atomics,+bulk-memory,+mutable-globals' \
  -C link-arg=--shared-memory

Using wasm-objdump -x wasm_package.wasm
Import section contains line:
memory[0] pages: initial=17 max=16384 shared <- env.memory
So I assume that module is correctly compiled with shared memory.

This compiled module successfully executes using wasmtime, wit-bindgen.

Then I would like to initialize multiple Wasm instances with the same shared memory.

Thank you for your help, I greatly appreciate it.

An adapter for enabling Rayon-based concurrency on the Web with WebAssembly. - RReverser/wasm-bindgen-rayon

view this post on Zulip Pēteris Pakalns (Mar 29 2025 at 13:40):

It would be important to correctly initialize instances and their virtual "threads" like allocating separate stack and correctly using that stack when wasm code is called.
https://github.com/bytecodealliance/wasmtime/issues/5

If SIP really does isolate, then perhaps wasm code shouldn't be able to smash the native stack. Currently Cretonne expects wasm and native code to share the stack so for example wasm code can call ...

view this post on Zulip Pēteris Pakalns (Mar 29 2025 at 13:45):

One more project that can spawn multiple threads on wasm and execute tasks:
https://cjycode.com/flutter_rust_bridge/guides/concurrency/sync-rust

view this post on Zulip Pēteris Pakalns (Mar 29 2025 at 19:18):

Wasm threads on Web: https://web.dev/articles/webassembly-threads with Thread pools ?

view this post on Zulip Pēteris Pakalns (Mar 30 2025 at 09:54):

Found a good example how Thread Local Storage and Shadow Stack in linear memory can be initialized:

https://blog.stackblitz.com/posts/thread-destroyer/

Low-level hacking on multithreaded WebAssembly

view this post on Zulip Pēteris Pakalns (Mar 30 2025 at 10:22):

Good overview from 2018: https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html

view this post on Zulip Pēteris Pakalns (Mar 30 2025 at 14:13):

Looks like module already contains logic to allocate stack in wasm global variable ($allocate_stack). Will try to initialize component with shared memory and see what happens.

view this post on Zulip Valeriooooh (Mar 30 2025 at 15:02):

Hi, I'm a bit new to cranelift, I want to use it to build a frontend for my compiler, do you have a tutorial or examples in rust that I can use? Thanks in advance

view this post on Zulip Pēteris Pakalns (Mar 30 2025 at 15:31):

I created a feature request issue: https://github.com/bytecodealliance/wasmtime/issues/10491

I think that there shouldn't be large problems with initializing stack
Main pain point is how to pass SharedMemory to Wasm Instance under Component Model.

Feature Support SharedMemory for use in component in Component Model (WIT). Benefit Wasm on web supports threaded execution. Wasmtime supports threaded execution. WIT (Component model) simplifies h...

Last updated: Apr 07 2025 at 03:17 UTC