Using LLVM's "unordered" reads and writes


LLVM provides an atomic mode called “unordered” which is documented as UB-free. However, it comes from Java and not from C++11/C11, so Ordered 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.)

1 Like