[LangRef] adjust IR atomics specification following C++20 model tweaks. (#77263)
authorJames Y Knight <jyknight@google.com>
Tue, 23 Jan 2024 17:18:51 +0000 (12:18 -0500)
committerGitHub <noreply@github.com>
Tue, 23 Jan 2024 17:18:51 +0000 (12:18 -0500)
commit8a45cec934697747ac3d3a18e75833e0058fe9a1
tree002f4f4ded6ecdb7b8046f28851f16d3b35ebe18
parent39420279125c8d2fe0a2dae8b8c2e80d0118789c
[LangRef] adjust IR atomics specification following C++20 model tweaks. (#77263)

C++20 accepted two papers, [P0668](https://wg21.link/P0668) and
[P0982](https://wg21.link/P0982), which changed the atomics memory model
slightly in order to reflect the realities of the existing
implementations.

The rationale for these changes applies as well to the LLVM IR atomics
model. No code changes are expected to be required from this change: it
is primarily a matter of more-correctly-documenting the existing state
of the world.

There's three changes: two of them weaken guarantees, and one
strengthens them:

1. The memory ordering guaranteed by some backends/CPUs when seq_cst
operations are mixed with acquire/release operations on the same
location was weaker than the spec guaranteed. Therefore, the
specification is changed to remove the requirement that seq_cst ordering
is consistent with happens-before, and replaces it with a slightly
weaker requirement of consistency with a new relation named
strongly-happens-before.

2. The rules for a "release sequence" were weakened. Previously, an
acquire synchronizes with an release even if it observes a later
monotonic store from the same thread as the release store. That has now
been removed: now, only read-modify-write operations can extend a
release sequence.

3. The model for a a seq_cst fence is strengthened, such that placing a
seq_cst between monotonic accesses now _is_ sufficient to guarantee
sequential consistency in the model (as it always has been on existing
implementations.)

Note that I've directly referenced the C++ standard's atomics.order
section for the precise semantics of seq_cst, instead of fully
describing them. They are quite complex, and a lot of work has gone into
refining the words in the standard. I'm afraid if I attempt to reiterate
them, I would only introduce errors.
llvm/docs/Atomics.rst
llvm/docs/LangRef.rst
llvm/include/llvm/CodeGen/TargetLowering.h