[PATCH 0/5] Implement atomic fetch min/max in GCC
soumyaa@nvidia.com
soumyaa@nvidia.com
Mon Jan 19 16:23:35 GMT 2026
From: Soumya AR <soumyaa@nvidia.com>
Hi,
The following are a revised set of patches that implement atomic fetch min/max
in GCC.
These have been revised based on discussions in this thread:
https://gcc.gnu.org/pipermail/gcc-patches/2025-November/699602.html
To summarize, we now define the following builtins:
- __atomic_fetch_min
- __atomic_fetch_max
- __atomic_min_fetch
- __atomic_max_fetch
And resolve them to an internal function: IFN_ATOMIC_FETCH_MINMAX.
If the backend does not support atomic fetch min/max operations, the IFN is
lowered to a compare-and-swap loop. Otherwise, it is expanded to target-specific
optabs.
----
Following discussions with Jonathan here:
https://gcc.gnu.org/pipermail/gcc-patches/2026-January/706015.html
I suppose implementing the op_fetch variants of the builtins is not necessary.
If they are only used to support the libstdc++ operator overloading, then we
can get rid of them in the compiler if we have no use of exposing these.
This would also help clean up a lot of extra-handling in place to ensure the
op_fetch variants of min/max are handled correctly.
----
Patch 2, ie. implementing the builtins in GCC via CAS lowering has had minor
modifications since v1 of it posted here:
https://gcc.gnu.org/pipermail/gcc-patches/2025-November/700787.html
Patch 3 and 4 further extend this interface to expand the IFN to atomic min/max
optabs for aarch64.
----
Patch 5 adds a pass that pattern-matches CAS loops implementing fetch min/max
to IFN_ATOMIC_FETCH_MINMAX.
This pass turned out to be a lot more complex than I was expecting so insights
into its implementation would really help.
I don't expect this patch to go in at Stage 4, but putting it up for feedback
nonetheless, if folks get the time to look at it.
----
Soumya AR (5):
libstdc++: Implement std::atomic::fetch_min/max
middle-end: Add support for atomic fetch min/max builtins via CAS
lowering
expand: Expand IFN_ATOMIC_FETCH_MINMAX to target specific optabs
aarch64: Add backend support for atomic fetch min/max operations
middle-end: Pattern match CAS loops to IFN_ATOMIC_FETCH_MINMAX
gcc/Makefile.in | 2 +
gcc/builtins.cc | 83 +-
gcc/builtins.h | 1 +
gcc/c-family/c-common.cc | 58 +-
gcc/config/aarch64/aarch64-protos.h | 4 +
gcc/config/aarch64/aarch64.cc | 51 ++
gcc/config/aarch64/atomics.md | 54 +-
gcc/config/aarch64/iterators.md | 30 +-
gcc/expr.cc | 88 +-
gcc/ifn-atomic-minmax-lowering.cc | 402 +++++++++
gcc/internal-fn.cc | 8 +
gcc/internal-fn.def | 1 +
gcc/optabs.cc | 133 +++
gcc/optabs.def | 24 +
gcc/passes.def | 2 +
gcc/sync-builtins.def | 15 +
.../g++.dg/tree-ssa/cas-to-minmax-1.C | 23 +
.../g++.dg/tree-ssa/cas-to-minmax-2.C | 23 +
.../g++.dg/tree-ssa/cas-to-minmax-3.C | 21 +
.../g++.dg/tree-ssa/cas-to-minmax-4.C | 22 +
gcc/testsuite/gcc.dg/atomic-op-1.c | 353 ++++++++
gcc/testsuite/gcc.dg/atomic-op-2.c | 353 ++++++++
gcc/testsuite/gcc.dg/atomic-op-3.c | 353 ++++++++
gcc/testsuite/gcc.dg/atomic-op-4.c | 353 ++++++++
gcc/testsuite/gcc.dg/atomic-op-5.c | 355 ++++++++
.../gcc.dg/tree-ssa/cas-to-minmax-1.c | 18 +
.../gcc.dg/tree-ssa/cas-to-minmax-2.c | 18 +
.../gcc.dg/tree-ssa/cas-to-minmax-3.c | 17 +
.../gcc.dg/tree-ssa/cas-to-minmax-4.c | 14 +
.../gcc.dg/tree-ssa/cas-to-minmax-5.c | 19 +
.../gcc.dg/tree-ssa/cas-to-minmax-6.c | 19 +
.../gcc.dg/tree-ssa/cas-to-minmax-run-1.c | 75 ++
.../gcc.dg/tree-ssa/cas-to-minmax-run-2.c | 55 ++
.../gcc.target/aarch64/atomic-minmax-lse.c | 122 +++
.../gcc.target/aarch64/atomic-minmax-nolse.c | 196 +++++
.../gcc.target/aarch64/atomic-minmax.c | 128 +++
.../gcc.target/aarch64/atomic-minmax.x | 185 ++++
gcc/tree-cas-to-minmax.cc | 801 ++++++++++++++++++
gcc/tree-pass.h | 2 +
libgcc/config/aarch64/lse.S | 62 +-
libgcc/config/aarch64/t-lse | 3 +-
libstdc++-v3/include/bits/atomic_base.h | 132 ++-
libstdc++-v3/include/bits/version.def | 8 +
libstdc++-v3/include/bits/version.h | 10 +
libstdc++-v3/include/std/atomic | 57 ++
libstdc++-v3/src/c++23/std.cc.in | 6 +
.../nonmembers_fetch_minmax.cc | 50 ++
.../atomic_ref/integral_fetch_minmax.cc | 118 +++
48 files changed, 4823 insertions(+), 104 deletions(-)
create mode 100644 gcc/ifn-atomic-minmax-lowering.cc
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cas-to-minmax-1.C
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cas-to-minmax-2.C
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cas-to-minmax-3.C
create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cas-to-minmax-4.C
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-2.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-3.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-4.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-5.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-6.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-run-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cas-to-minmax-run-2.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/atomic-minmax-lse.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/atomic-minmax-nolse.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/atomic-minmax.c
create mode 100644 gcc/testsuite/gcc.target/aarch64/atomic-minmax.x
create mode 100644 gcc/tree-cas-to-minmax.cc
create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_integral/nonmembers_fetch_minmax.cc
create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_ref/integral_fetch_minmax.cc
--
2.43.0
More information about the Libstdc++
mailing list