Repository navigation
wasm: Thread.spawn and Worker under Node, on a shared memory - #818
Merged
Merged
Conversation
wasm32/wasm64 (not Emscripten) compile with the exception-handling feature and the wasm exception model, set before the target machine is made (its MCAsmInfo takes it from there; without it every invoke was lowered to a call). WasmExceptionPass turns each Itanium landing pad into a catch-all catchpad left at once (catchswitch, catchpad [ptr null], llvm.wasm.get.exception, catchret), so the code after the pad runs as ordinary code, as it does on Linux; `resume` becomes llvm.wasm.throw. What the personality would decide is decided after the catch: a pad's selector is the first clause that takes the exception (__tslang_wasm_catches: the same type, or a base class's pointer), a pad of typed clauses only rethrows what none takes, and a matched pointer clause binds the thrown pointer. The C++ runtime's type_infos and type_info vtables get definitions, so each has an address of its own. The JS host imports the __cpp_exception tag (i32 or i64, read from the module by host.instantiate), throws a WebAssembly.Exception of it from __cxa_throw, and keeps begin/end catch and rethrow as libc++abi does. It also maps Node's ERR_FS_* codes and refuses an environment name with '='. run_tests.mjs no longer skips tests that catch; json.ts under none is listed (5 GB of strings). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… host The host implements MLIR's mlirAsyncRuntime* API on the program's one thread, and run() drains the tasks still queued after main. wasm64 function pointers go through a BigInt table index (functionAt). New imports: aligned_alloc, std_this_thread_sleep_for. MemAllocFixPass no longer erases a malloc/realloc/free declaration that is still called (the coroutine frame's free): its calls go to the ts_ function, else it is kept. run_tests.mjs no longer skips async tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nto wasm-async # Conflicts: # docs/how/wasm/test/README.md # docs/how/wasm/test/host.mjs # docs/how/wasm/test/run_tests.mjs # docs/how/wasm/test/wasm_skips.txt
A program that calls tslang_thread_spawn is linked with --shared-memory --import-memory (and a maximum, and __stack_pointer exported); every wasm object is compiled with +atomics,+bulk-memory, which a shared memory needs. The host starts each thread in a Node worker (thread.mjs) on the same module and memory; join waits with Atomics.wait. The host's heap moves into the module's memory behind a lock. Atomics.wait/notify import functions. wasm32: a heap block's header is 8 bytes (CompileOptions::heapBlockHeaderBytes), so a payload of 64-bit elements is 8-aligned: a real 64-bit atomic traps on a 4-aligned address. The count stays an index-sized word at the block's start; the static layouts (string literals, constant arrays, static array headers, stack blocks, the type descriptor's block word) widen. Other targets are unchanged. run_tests.mjs no longer skips thread tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nto wasm-threads # Conflicts: # docs/how/wasm/test/README.md # docs/how/wasm/test/host.mjs # docs/how/wasm/test/run_tests.mjs # docs/how/wasm/test/wasm_skips.txt
This was referenced Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thread.spawn,Worker,Atomics.waitandAtomics.notifynow run on wasm32 and wasm64 under Node. Before this, the wasm test runner skipped every test that used them.Builds on #817 (wasm async), now merged;
mainis merged in.How it works
A program that starts a thread gets a shared memory. Such a program is one whose module calls
tslang_thread_spawn. It is linked with:--shared-memory --import-memory;--max-memory: 4 GB on wasm32, 16 GB on wasm64, V8's limits;--export=__stack_pointer, so each thread can set its own stack.Every other program keeps an unshared memory, so it still runs in a browser page that is not cross-origin isolated.
Each thread is a Node worker (
docs/how/wasm/test/thread.mjs). It instantiates the same compiled module on the same memory and gets a 1 MB stack. When the module has thread-local data, it gets its own copy through__wasm_init_tls. It does not run the module's constructors again.joinwaits on the thread's control block withAtomics.wait. The control block records how the thread ended:exit():jointhen ends the program with that code, as a nativeexit()from a thread does;jointhen throws.Atomics.waitandAtomics.notifyuse the real atomics on a shared memory. On an unshared memory nothing else can run, so a wait checks the value and then sleeps out its timeout. A wait with no timeout fails instead of hanging.Compiler
obj.cpp: every wasm object, the default library's included, is compiled with+atomics,+bulk-memory. A shared memory needs these from everything linked into it. An unshared memory works with them too: I checked that such objects link with and without--shared-memory.exe.cpp: adds the link flags above whenCompileOptions::wasmThreadsis set.obj.cppsets it when the optimized module still callstslang_thread_spawn.Atomics.addon aBigInt64Arraytrapped on wasm32 with "operation does not support unaligned accesses". The index-sized block word put 64-bit elements at a 4-byte offset. Until now that was harmless, because without the atomics feature LLVM lowered atomics to plain loads and stores.CompileOptions::heapBlockHeaderBytes()returns 8 on wasm32, and the index width everywhere else.Host (
docs/how/wasm/test)memoryOf(bytes)also reads an imported memory's limits.instantiatemakes the shared memory, with BigInt page counts on memory64, or takes the one a worker is given.run.mjspasses Node'sworker_threadsandosto the host.readStrcopies the bytes of a shared memory before decoding them, because a browser'sTextDecoderrejects a view of aSharedArrayBuffer.run_tests.mjsno longer skips thread tests.README.mdhas a new "Threads" section, which also states the limits:join, and shares memory only on a cross-origin-isolated page;Default library
No source change, but the wasm archives must be rebuilt with this compiler (
scripts\build_wasm.bat). Archives with the old 4-byte header must not be linked with code built with the 8-byte one. I rebuilt them locally for these runs.Tests
thread_spawn,thread_move,worker_echo,worker_errors,worker_objects,worker_terminateandatomics_wait.atomics.tspasses again on wasm32.Release passes on every leg. Debug passes too, except one failure, which I could not reproduce:
00owned_iteration.ts(wasm32, rc, debug) failed an assertion, the same way in all 5 runs of that one build;My guess is unproven: a constant array's global is named from a hash of attribute pointers (
calc_hash_value), which changes from run to run, so a collision could give one constant array another's data.Browser:
index.htmlstill runs a wasm32 and a wasm64 program that starts no thread, in Chromium (the app's browser pane).🤖 Generated with Claude Code