Skip to content

wasm: copies and fills of plain loads and stores, not memory.copy and memory.fill - #838

Merged
ASDAlexander77 merged 2 commits into
mainfrom
wasm-no-bulk-copy
Nov 13, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
wasm-no-bulk-copy

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

The v0.0-pre-alpha126 release run failed one test: the default library's message_queue.ts under wasm32 -mm=own debug trapped with RuntimeError: memory access out of bounds in MessageQueue<string>.take. The trap is at the memory.copy of items.shift(). This build's binary matches CI's, with the same function indexes and call offsets.

Cause: V8 checks memory.copy/fill against a stale size

After another thread grows a shared memory, V8 (Node 26) checks memory.copy and memory.fill against the size this thread last saw. Plain loads and stores see the new size at once. In the failing run, the producer thread's push reallocated the queue's array into freshly grown pages, and the consumer's shift copied within it before its isolate had caught up.

A two-thread test of a bare shared memory reproduces it. The consumer spins in one wasm function, then touches an address in a page the other thread has just grown:

op wasm32 wasm64
memory.copy 112 traps / 8000 85 traps / 8000
i32.load 0 / 8000 0 / 8000
memory.copy after "past memory.size? memory.grow 0" 0 / 8000 (twice; the refresh ran 25 and 34 times)

memory.size returns the same stale size that memory.copy is checked against, and memory.grow 0 brings it up to date.

Fix: refresh the size first when a copy or fill reaches past it

LLVM emits memory.copy/memory.fill for every copy and fill it doesn't expand into loads and stores, once bulk-memory is on. A shared memory's passive data segments need bulk-memory, and WebAssemblySubtarget turns bulk-memory-opt on with it, so there is no switch for these two instructions alone.

So obj.cpp now does this for wasm (not Emscripten), after the optimizations:

  • Each llvm.memcpy, memmove and memset of a run-time size, or a constant size over 32 bytes, becomes a call to an internal function: __tslang_wasm_memmove or __tslang_wasm_memset.
  • These run memory.grow 0 when the copy or fill would reach past memory.size. Then they run the native memory.copy or memory.fill.
  • The common case costs one extra load and compare. Copies smaller than that stay inline loads and stores, as before.

This applies to every wasm object, because default-library objects can't know whether the program will start a thread.

The first version of this PR copied with plain load/store loops. Those were too slow for big copies: CI timed out json.ts on wasm32 rc/own and wasm64 rc. With the native copy back, it runs in about 6 s locally on each of those legs.

Tests

  • New ctests test-compile-wasm-no-bulk-copy-{wasm32,wasm64}-unknown-unknown: the program's copies call __tslang_wasm_memmove, memory.grow appears, and only one memory.copy and one memory.fill remain (the helpers').
  • New test 00wasm_no_bulk_copy.ts (shift, splice both ways, spread, string building) runs natively (compile, jit, rc/none corpus) and under the wasm runner.
  • Every wasm default library (2 triples × 3 memory models × release/debug) was rebuilt with the fix. Full run_tests.mjs results:
    • tester: 656/656 for none and rc, 490 for own;
    • defaultlib: 206/207 for each leg, own 206;
    • 0 failures and 0 timeouts in all 24 legs.
  • Windows ctest: 7064/7064 pass (on the first version; obj.cpp's change affects wasm only).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 13, 2026 10:44
… memory.fill

The release run for v0.0-pre-alpha126 failed one test: the default library's message_queue.ts under
wasm32 -mm=own debug trapped "memory access out of bounds" at the memory.copy of items.shift() in
MessageQueue<string>.take.

V8 (Node 26, wasm32 and wasm64 alike) checks memory.copy and memory.fill against the size of a shared
memory as this thread last saw it. Right after another thread grew the memory - the producer's push
reallocating the queue's array into new pages - they trap on that block, while plain loads and
stores see the new size at once. A two-thread test of a shared memory reproduces it: memory.copy
trapped 112 times in 8000 grows (wasm64: 85), i32.load never.

LLVM emits memory.copy and memory.fill for every copy and fill it doesn't expand once bulk-memory is
on, which a shared memory's passive data segments need, and has no switch for them alone
(WebAssemblySubtarget turns bulk-memory-opt on with bulk-memory). So after the optimizations each
llvm.memcpy, memmove and memset of a run-time or large size becomes a call to an internal function
of 8-byte and 1-byte loads and stores (__tslang_wasm_memmove, __tslang_wasm_memset). Only the
linker's __wasm_init_memory keeps memory.fill, before any thread starts.

test-compile-wasm-no-bulk-copy-{wasm32,wasm64} check the assembly; 00wasm_no_bulk_copy.ts runs
natively and under the wasm runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The loops of 8-byte and 1-byte loads and stores were too slow for big copies: CI timed json.ts out
(defaultlib wasm32 rc and own, wasm64 rc), which builds long strings.

memory.size gives the same stale size V8 checks memory.copy against, and memory.grow by 0 brings it
up to date: with that check before memory.copy, the two-thread test of a shared memory trapped 0
times in 8000 grows (twice; memory.grow 0 ran 25 and 34 times), where the bare memory.copy trapped
38 times. So __tslang_wasm_memmove and __tslang_wasm_memset now run memory.grow 0 when the copy or
fill reaches past memory.size, then the native memory.copy or memory.fill: a load and a compare
more than before for the common case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 88407d8 into main Nov 13, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the wasm-no-bulk-copy branch November 13, 2026 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant