[InstCombine] Don't replace unused `atomicrmw xchg` with `atomic store`
authorQuentin Colombet <quentin.colombet@gmail.com>
Thu, 19 Jan 2023 09:56:57 +0000 (10:56 +0100)
committerQuentin Colombet <quentin.colombet@gmail.com>
Wed, 1 Feb 2023 10:48:43 +0000 (11:48 +0100)
commit583488e4a787e400b6077b64677d23855a3c9538
tree153adf4a1439497ddddff19ed302ce71f6b03649
parent87cd6f934650c2403d776be0cc7f5c065479f77d
[InstCombine] Don't replace unused `atomicrmw xchg` with `atomic store`

Following the discussion from https://reviews.llvm.org/D141277 and in
particular Ralf Jung's comment at
https://reviews.llvm.org/D141277#inline-1365148, replacing an unused `atomicrmw
xchg` into an `atomic store` is illegal even for release ordering.

Quoting Connor Horman from the rust lang discussion linked in that comment:
"An acquire operation A only synchronizes-with a release operation R if it
takes its value from R, or any store in the release sequence headed by R, which
is R, followed by the longest continuous sequence of read-modify-write
operations.
A regular store following R in the modification order would break the release
sequence, and if an acquire operation reads that store or something later, then
it loses any synchronization it might have already had."

This fixes https://github.com/llvm/llvm-project/issues/60418

Differential Revision: https://reviews.llvm.org/D142097
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
llvm/test/Transforms/InstCombine/atomicrmw.ll