This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Don't optimize idempotent `atomicrmw <op>, 0` into `load atomic`
ClosedPublic

Authored by qcolombet on Jan 9 2023, 5:33 AM.

Details

Summary

Turning idempotent atomicrmws into load atomic is perfectly legal
with respect to how the loading happens, but it may not be legal for the
whole program semantic.

Indeed, this optimization removes a store that may have some effects on
the legality of other optimizations.
Essentially, we lose some information and depending on the backend
it may or may not produce incorrect code, so don't do it!

This fixes llvm.org/PR56450.

Diff Detail

Event Timeline

qcolombet created this revision.Jan 9 2023, 5:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 5:33 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
qcolombet requested review of this revision.Jan 9 2023, 5:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 9 2023, 5:33 AM
nikic added a subscriber: nikic.Jan 9 2023, 6:38 AM
nikic added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

Just to double check, is this atomicrmw -> store fold fine, or does it also have a similar issue?

qcolombet added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

It might, but I'd like a "language lawyer" to confirm.

It took me some time to get convinced that this optimization is wrong to begin with so I would rather have an actual expert on the matter to comment :).

@RalfJung

RalfJung added inline comments.Jan 9 2023, 8:43 AM
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

I'm afraid I am not a weak memory expert, so I don't know for sure.

But given that RMWs have special semantics that stores don't have, I am at least doubtful of this optimization. I will ask some other people. :)
If I understand the code correctly, this will apply specifically to Xchg whose loaded value is ignored, and whose ordering is Release or Relaxed?

RalfJung added inline comments.Jan 9 2023, 11:50 PM
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

Judging from the discussion at https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/LLVM.20atomic.20optimization.3A.20atomic.20swap.20to.20atomic.20store, the optimization is indeed wrong since it breaks release sequences. If LLVM wants to perform such optimizations, at the very least it would have to define its own weak memory model that is very distinct from the C++ one -- and carefully justify why translating from C++ to that model is correct.

qcolombet added inline comments.Jan 13 2023, 6:31 AM
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

@reames what do you think?

From what @RalfJung is reporting (thanks Ralf BTW), the change that replaces xchg with atomic store is technically incorrect.
I.e., #2 from https://reviews.llvm.org/D58290.

I'm happy to remove that change in that PR too.
Otherwise I would suggest we move with at least the current fix since it produces miscompiles in the wild.

nikic accepted this revision.Jan 17 2023, 12:02 PM

LGTM, might as well just land this part first.

This revision is now accepted and ready to land.Jan 17 2023, 12:02 PM
arsenm added a subscriber: arsenm.Jan 17 2023, 12:11 PM

Can you just insert a fence to go with the load?

Can you just insert a fence to go with the load?

AtomicExpand also tries the same transform (which for some reason is done in an x86 specific way, so this might be incomplete anyway)

AtomicExpand also tries the same transform (which for some reason is done in an x86 specific way, so this might be incomplete anyway)

Yeah, I believe there would be some subtle issues doing that in a generic way.

nikic added inline comments.Jan 19 2023, 1:11 AM
llvm/test/Transforms/InstCombine/atomicrmw.ll
5–6

I think the two comment lines above were supposed to be dropped.

This revision was landed with ongoing or failed builds.Jan 19 2023, 1:20 AM
This revision was automatically updated to reflect the committed changes.
qcolombet added inline comments.Jan 19 2023, 1:22 AM
llvm/test/Transforms/InstCombine/atomicrmw.ll
5–6

Good catch, looks like I screwed up the merge conflict.

qcolombet marked an inline comment as done.Jan 19 2023, 1:23 AM
qcolombet added inline comments.
llvm/test/Transforms/InstCombine/atomicrmw.ll
5–6

Fixed in ffafa0d43d81

qcolombet added inline comments.Jan 19 2023, 2:26 AM
llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
134

Patch for this particular issue is at https://reviews.llvm.org/D142097