Support threaded RTS and SMP parallelism in wasm backend
This issue tracks the feature request of supporting threaded RTS and SMP parallelism in the wasm backend. There's a ton of work to be done here, but most of the building blocks in upstream toolchain are already present: wasi-sdk
already supports the wasm32-wasi-threads
target that has basic threading functionality support (__wasi_thread_spawn
, TLS logic, etc), and wasm shared-memory multi-threading is already supported by major browsers & wasmtime.
A rough list of todos to get us there:
- Fix and test shared libraries for
wasm32-wasi-threads
target, upstream only has static libraries as of now (see here). - Make sure our test runner and dynamic linker support multi-threaded wasm.
- Figure out a way to compile non-thr/thr ways of Haskell libraries as well, not just the RTS. Might need adjustments in hadrian and Cabal as well. Atomic primops used to be lowered to regular memory opcodes in non-thr ways; we can only emit atomic opcodes for thr ways, otherwise for non-thr use cases those opcodes would be rejected by the engine.
- wasm threading doesn't support canceling; check pthread usage in the RTS and ensure only basic threading logic is used for wasm target.
- Audit all existing wasm-specific code for thread safety (e.g. some
unsafeDupablePerformIO
calls should now beunsafePerformIO
).