Repository navigation
wasm: copies and fills of plain loads and stores, not memory.copy and memory.fill - #838
Merged
Merged
Conversation
… 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>
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.
The
v0.0-pre-alpha126release run failed one test: the default library'smessage_queue.tsunder wasm32-mm=owndebug trapped withRuntimeError: memory access out of boundsinMessageQueue<string>.take. The trap is at thememory.copyofitems.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.copyandmemory.fillagainst the size this thread last saw. Plain loads and stores see the new size at once. In the failing run, the producer thread'spushreallocated the queue's array into freshly grown pages, and the consumer'sshiftcopied 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:
memory.copyi32.loadmemory.copyafter "pastmemory.size?memory.grow 0"memory.sizereturns the same stale size thatmemory.copyis checked against, andmemory.grow 0brings it up to date.Fix: refresh the size first when a copy or fill reaches past it
LLVM emits
memory.copy/memory.fillfor every copy and fill it doesn't expand into loads and stores, oncebulk-memoryis on. A shared memory's passive data segments needbulk-memory, andWebAssemblySubtargetturnsbulk-memory-opton with it, so there is no switch for these two instructions alone.So
obj.cppnow does this for wasm (not Emscripten), after the optimizations:llvm.memcpy,memmoveandmemsetof a run-time size, or a constant size over 32 bytes, becomes a call to an internal function:__tslang_wasm_memmoveor__tslang_wasm_memset.memory.grow 0when the copy or fill would reach pastmemory.size. Then they run the nativememory.copyormemory.fill.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.tson wasm32 rc/own and wasm64 rc. With the native copy back, it runs in about 6 s locally on each of those legs.Tests
test-compile-wasm-no-bulk-copy-{wasm32,wasm64}-unknown-unknown: the program's copies call__tslang_wasm_memmove,memory.growappears, and only onememory.copyand onememory.fillremain (the helpers').00wasm_no_bulk_copy.ts(shift, splice both ways, spread, string building) runs natively (compile, jit, rc/none corpus) and under the wasm runner.run_tests.mjsresults:obj.cpp's change affects wasm only).🤖 Generated with Claude Code