This is an archive of the discontinued LLVM Phabricator instance.

[MemoryDependency] Relax the re-ordering of atomic store and unordered load/store
ClosedPublic

Authored by skatkov on Feb 15 2022, 7:34 AM.

Details

Summary

Atomic store with Release semantic allows re-ordering of unordered load/store before the store.
Implement it.

Diff Detail

Event Timeline

skatkov created this revision.Feb 15 2022, 7:34 AM
skatkov requested review of this revision.Feb 15 2022, 7:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 7:34 AM
reames requested changes to this revision.Feb 15 2022, 9:26 AM
reames added inline comments.
llvm/lib/Analysis/MemoryDependenceAnalysis.cpp
565–573

I don't this comment holds. We can get here with SI being e.g. a ceq_cst store. Reordering that with just about anything is illegal.

The prior change to check is non-unordered instead of non-simple looks reasonable, but the change below doesn't seem to follow.

This revision now requires changes to proceed.Feb 15 2022, 9:26 AM
skatkov added inline comments.Feb 15 2022, 6:39 PM
llvm/lib/Analysis/MemoryDependenceAnalysis.cpp
565–573

Thank you, Philip for starting review.

I tend to disagree with you. This is how I read specs.
Lang Ref:
seq_cst (sequentially consistent)
In addition to the guarantees of acq_rel (acquire for an operation that only reads, release for an operation that only writes), there is a global total order on all sequentially-consistent operations on all addresses, which is consistent with the happens-before partial order and with the modification orders of all the affected addresses. Each sequentially-consistent read sees the last preceding write to the same address in this global order. This corresponds to the C++0x/C1x memory_order_seq_cst and Java volatile.

c++(https://en.cppreference.com/w/c/atomic/memory_order):
memory_order_seq_cst A load operation with this memory order performs an acquire operation, a store performs a release operation, and read-modify-write performs both an acquire operation and a release operation, plus a single total order exists in which all threads observe all modifications in the same order (see Sequentially-consistent ordering below)

So Store with seq_cst is a Store Release and prohibit re-ordering with any another seq_sct instruction.

Do I miss anything?

skatkov updated this revision to Diff 409142.Feb 15 2022, 8:53 PM

test update

skatkov updated this revision to Diff 409153.Feb 15 2022, 10:10 PM

update a comment with explicitly mentioning of seq_cst.

reames accepted this revision.Feb 16 2022, 7:57 AM

LGTM

I'm convinced on the seq_cst case.

I went ahead and landed an expanded version of your new test file. Please rebase and sanity check that none of the new tests give unexpected results before committing. No further review needed unless you see something weird.

This revision is now accepted and ready to land.Feb 16 2022, 7:57 AM
skatkov updated this revision to Diff 409297.Feb 16 2022, 9:28 AM
This revision was landed with ongoing or failed builds.Feb 16 2022, 8:20 PM
This revision was automatically updated to reflect the committed changes.