# Using LLVM's "unordered" reads and writes

**URL:** <https://internals.rust-lang.org/t/using-llvms-unordered-reads-and-writes/9472>\
**Category:** Uncategorized\
**Created:** [February 18, 2019, 2:01pm UTC](https://internals.rust-lang.org/t/using-llvms-unordered-reads-and-writes/9472 "2019-02-18T14:01:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsivonen](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hsivonen/32/4080_2.png) [@hsivonen](https://internals.rust-lang.org/u/hsivonen)\
**Post date:** [February 18, 2019, 2:01pm UTC](https://internals.rust-lang.org/t/using-llvms-unordered-reads-and-writes/9472/1 "2019-02-18T14:01:00Z")

</div>

LLVM provides an atomic mode called [“unordered”](http://llvm.org/docs/Atomics.html#unordered) which is documented as UB-free. However, it comes from Java and not from C++11/C11, so [`Ordered`](https://doc.rust-lang.org/std/sync/atomic/enum.Ordering.html) doesn’t have it.

Does Rust provide reads/writes of `u8`, reads/writes of aligned `usize` and reads/writes of `u8x16` in LLVM’s “unordered” mode via some other API than the atomics API?

Use case: Implementing host services for multithreaded Wasm. Freedom from data races can’t be guaranteed. If another thread of the Wasm program concurrently writes to a part of the Wasm heap that the host is accessing, it’s OK for the part of the Wasm heap to yield garbage value to the host or the host to write garbage to the buffer, but it’s not OK for the data race to result in broader UB, such as out-of-bounds reads/writes by the host.

For example, if I do a pointer-sized read and mask to value to see if any of the bytes has the high bit set, if in the case that masking shows that at least one byte has its high bit set, if I then read the memory byte by byte and look for a byte with the high bit set, the compiler must not assume it gets to eliminate other termination conditions on the logic that the loop must terminate by finding a byte with the high bit set. (Another Wasm thread might have changed the memory.)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 19, 2019, 2:13pm UTC](https://internals.rust-lang.org/t/using-llvms-unordered-reads-and-writes/9472/2 "2019-05-19T14:13:58Z")

</div>

Closed on May 19, 2019, 2:13pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
