Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strengthen atomic_compare_exchange seq_cst failure mapping #445

Conversation

patrick-rivos
Copy link
Contributor

The Zacas extension defines different ordering behavior when an amocas fails:

The memory operation performed by an AMOCAS.W/D/Q, when not successful, has
acquire semantics if aq bit is 1 but does not have release semantics,
regardless of rl.

This requires a leading fence to maintain A6C compatability for both the RVWMO and Ztso memory models. The A7 mappings can use the non-fenced versions.

See issue #444 for more context and litmus tests.

Resolves #444.

@patrick-rivos
Copy link
Contributor Author

Happy to update formatting/verbage. An alternative format would to
introduce another table like this:

[cols="<20,<20,<4",options="header",]
|===
|C/{Cpp} Construct |RVWMO AMO Mapping |Notes

|`atomic_compare_exchange(memory_order_relaxed)` |`amo<op>.{w\|d}` |4,7

|`atomic_compare_exchange(memory_order_acquire)` |`amo<op>.{w\|d}.aq` |4,7

|`atomic_compare_exchange(memory_order_release)` |`amo<op>.{w\|d}.rl` |4,7

|`atomic_compare_exchange(memory_order_acq_rel)` |`amo<op>.{w\|d}.aqrl` |4,7

|`atomic_compare_exchange(memory_order_*, memory_order_seq_cst)` |`fence rw,rw; amo<op>.{w\|d}.aqrl` |4

|`atomic_compare_exchange(memory_order_*, memory_order_seq_cst)` |`amo<op>.{w\|d}.aqrl` |3,4

|`atomic_compare_exchange(memory_order_seq_cst, memory_order_{relaxed\|acquire\|release\|acq_rel})` |`amo<op>.{w\|d}.aqrl` |3,4

|===

7) The memory model listed is the stronger of the given success and
failure memory oderings. If acquire and release are used for success and
failure then the memory ordering is mapped to be acq_rel.

Copy link
Contributor

@aswaterman aswaterman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this version is clear and concise.

@patrick-rivos
Copy link
Contributor Author

Thanks for the review!
I'm going to convert this to a draft so it's not accidentally merged before the discussion in the related issue is complete.

@patrick-rivos patrick-rivos marked this pull request as draft July 15, 2024 21:52
@enh-google
Copy link

@hboehm

The Zacas extension defines different ordering behavior when an amocas fails:
> The memory operation performed by an AMOCAS.W/D/Q, when not successful, has
> acquire semantics if aq bit is 1 but does not have release semantics,
> regardless of rl.

This requires a leading fence to maintain A6C compatability for both the RVWMO
and Ztso memory models. The A7 mappings can use the non-fenced versions.

See issue riscv-non-isa#444 for more context and litmus tests.

Resolves riscv-non-isa#444.
@patrick-rivos patrick-rivos force-pushed the patrick/cas-seq-cst-failure-ordering branch from da2ed14 to f90df2d Compare July 18, 2024 15:55
@patrick-rivos
Copy link
Contributor Author

Updated to address @hboehm's review by adding note-3 to the Ztso table.
#444 (comment)

@patrick-rivos patrick-rivos marked this pull request as ready for review July 18, 2024 15:57
@patrick-rivos
Copy link
Contributor Author

The discussion has concluded so I think this is OK to land.
#444 (comment)

Copy link
Collaborator

@kito-cheng kito-cheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM since @hboehm say OK :P

@kito-cheng kito-cheng merged commit 17038f1 into riscv-non-isa:master Jul 23, 2024
4 checks passed
asb added a commit to asb/llvm-project that referenced this pull request Jul 24, 2024
… to ABI change

The psABI was updated to note that a leading fence must be present for
amocas with seq_cst failure ordering.
<riscv-non-isa/riscv-elf-psabi-doc#445>.

shouldInsertFencesForAtomic only returns true for AtomicCmpXchg if Zacas
is present, because doing so otherwise changes codegen due to how the
atomic expansion pass handles this hook.
asb added a commit to asb/llvm-project that referenced this pull request Jul 29, 2024
…hange

A recent atomics ABI change / fix requiresthat for the "A6C" and A6S"
atomics ABIs (i.e. both of those supported by LLVM currently), an
additional fence is inserted for an atomic_compare_exchange with seq_cst
failure ordering.
<riscv-non-isa/riscv-elf-psabi-doc#445>

This isn't trivial to support through the hooks used by AtomicExpandPass
because that pass assumes that when fences are inserted, the original
atomics ordering information can be removed from the instruction. Rather
than try to change and complicate that API, this patch implements the
needed fence insertion in RISCVCodeGenPrepare.
asb added a commit to asb/llvm-project that referenced this pull request Jul 29, 2024
…hange

A recent atomics ABI change / fix requiresthat for the "A6C" and A6S"
atomics ABIs (i.e. both of those supported by LLVM currently), an
additional fence is inserted for an atomic_compare_exchange with seq_cst
failure ordering.
<riscv-non-isa/riscv-elf-psabi-doc#445>

This isn't trivial to support through the hooks used by AtomicExpandPass
because that pass assumes that when fences are inserted, the original
atomics ordering information can be removed from the instruction. Rather
than try to change and complicate that API, this patch implements the
needed fence insertion in RISCVCodeGenPrepare.
asb added a commit to llvm/llvm-project that referenced this pull request Sep 19, 2024
…hange (#101023)

A recent atomics ABI change / fix requires that for the "A6C" and A6S"
atomics ABIs (i.e. both of those supported by LLVM currently), an
additional fence is inserted for an atomic_compare_exchange with seq_cst
failure ordering.
<riscv-non-isa/riscv-elf-psabi-doc#445>

This isn't trivial to support through the hooks used by AtomicExpandPass
because that pass assumes that when fences are inserted, the original
atomics ordering information can be removed from the instruction. Rather
than try to change and complicate that API, this patch implements the
needed fence insertion through a small special purpose pass.
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
…hange (llvm#101023)

A recent atomics ABI change / fix requires that for the "A6C" and A6S"
atomics ABIs (i.e. both of those supported by LLVM currently), an
additional fence is inserted for an atomic_compare_exchange with seq_cst
failure ordering.
<riscv-non-isa/riscv-elf-psabi-doc#445>

This isn't trivial to support through the hooks used by AtomicExpandPass
because that pass assumes that when fences are inserted, the original
atomics ordering information can be removed from the instruction. Rather
than try to change and complicate that API, this patch implements the
needed fence insertion through a small special purpose pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AMOCAS recommended compatibility mappings are too weak for the c11 memory model
4 participants