-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor linear memory to use Atomics #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Found the error. For overlapping source and destination, the order of copy must be so that no byte of src in is overwritten before it was read. |
|
I think we can even take advantage of |
870f4c5 to
d374b17
Compare
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
3e1c1b8 to
7d07198
Compare
d652567 to
4c25ad1
Compare
After an enlightening discussion with Asahi Lina[1], I was left convinced that using atomic operations to implement non-atomic Wasm instructions might be a good idea after all. This commit is the manifestation of this realization. [1] https://lobste.rs/s/cwdone/why_are_we_worried_about_memory_access#c_obd8av Co-authored-by: Florian <[email protected]> Signed-off-by: Wanja Zaeske <[email protected]>
4c25ad1 to
9b8e96e
Compare
Pull Request Overview
After reading an insightful set of comments from @hoshinolina (again: thank you!) on Lobste.rs I got convinced to rewrite the linear memory using
AtomicU8instead ofUnsafeCell<u8>.TODO or Help Wanted
It doesn't work yet 😆.There must be a subtle (to me) bug in the rewrite, causing one of our internal tests (memory_init_test_4) and a handful of thememory_copy.wastandmemory_init.wasttests to fail. I don't have the patience to debug it today.Edit 1:
TESTSUITE_SAVE=1 ALLOW_TEST_PATTERN=memory_copy.wast cargo test --test wasm_spec_testsuite -- --nocapture | grep ❌reveals the specific test statements that fail formem.copy.Edit 2:
all affected functions invokemem.copy, so likely the issue is in that?TESTSUITE_SAVE=1 ALLOW_TEST_PATTERN=memory_(init|copy).wast cargo test --test wasm_spec_testsuite -- --nocapture | grep ❌to get them all.Edit 3:
The problem occurred for
mem.copywithin the same memory if source and destination overlap while the source index is smaller than the destination index. In this case, the copy would overwrite source values before they were read at all, causing havoc. The simple fix: for this specific case, do the copy in reverse order.Checks
nix fmtnix flake check '.?submodules=1'cargo fmtcargo testcargo checkcargo buildcargo docBenchmark Results
This does in fact negatively affect performance. Especially on the memory load/store hungry
fibonacci_loopbenchmark, we see a moderate increase of ~11 % in runtime.Github Issue
This approach presents a path towards solving #162 .