My question is related to memory ordering of atomic operations in LLVM IR, in particular, to a difference between
So, according to the docs,
NonAtomic is designed to match semantics of C/C++ non-atomic accesses — that is any race on these accesses is treated as UB.
Unordered is designed to match semantics of Java plain accesses (and plain accesses in other “safe” languages) so that racy accesses should have “somewhat defined” semantics.
I have several questions with respect to this.
I’ve tried to check source code of several compilers of “safe” languages that emit LLVM IR, and it looks like none of them actually uses
Unordered(instead they compile loads/stores to regular
NonAtomicload/stores). Here are links to source code for GraalVM , GHC , Swift .
So my questions are: should these compilers actually use
Unorderedmemory order? Should usage of
NonAtomicin the examples above be considered a bug? Should it be reported/discussed with the developers of said compilers? Is there any compiler in the wild that actually uses
What exactly are semantics guarantees of
NonAtomic. It is said that racy unordered accesses should have defined semantics, but how it could look like? Am I right that a high-level guarantee that is desirable here is that
Unorderedshould guarantee “type-system soundness” for “safe” languages? In a sense that an
Unorderedload should read value actually belonging to the type of the variable (type in the type system of the compiled language).
How optimizer treats
NonAtomic? What optimizations are applicable to
NonAtomicaccesses but not to
Unordered? LLVM documentation mentions some of these, for example “load rematerialization”. Are there other interesting examples?
I am currently doing some research on LLVM memory model, so the answers to the questions above would be very helpful. In particular I want to understand what exactly the LLVM specification guarantees about “Unordered” and what optimizations on them it performs. I want to try to validate whether these optimizations actually preserve the guarantees provided by the spec.