From: Soumya AR <[email protected]> Hi,
These patches are a continuation of our discussion here: [https://gcc.gnu.org/pipermail/gcc-patches/2025-November/699602.html] I've worked on implementing the CAS expansion for atomic fetch min/max so that the support for the builtins is available alongside the libstdc++ patch that uses them. --- This change consists of the following two patches: 1. middle-end: Add support for atomic fetch min/max builtins via CAS lowering 2. libstdc++: Use new atomic fetch min/max builtins in std::atomic --- The first patch implements the atomic fetch min/max builtins by calling an internal function and lowering it to a CAS loop after IPA. If the backend supports the min/max optabs, the IFN should ideally be expanded to those using expand_ATOMIC_FETCH_MINMAX. However, this patch does not implement expand_ATOMIC_FETCH_MINMAX yet, which will be covered in subsequent patches. This patch has only gone through basic sanity tests on an aarch64 backend. It also needs polishing to add better error handling. I suppose the main purpose here is to align on the approach and implementation of the internal function and the lowering pass. --- The second patch uses the new atomic fetch min/max builtins to define the fetch_min and fetch_max functions in std::atomic. These are implmented using concepts to check if the builtins are supported by the compiler. If not, we fall back to a CAS loop. In order to do this, functions in __atomic_base also need to have access to the __atomic_impl implementation (which contains the actual check). Currently, this is resolved by doing a forward declaration of the min/max functions from __atomic_impl. Is this approach OK? Also, how should these functions be guarded under C++26? --- Soumya AR (2): middle-end: Add support for atomic fetch min/max builtins via CAS lowering libstdc++: Use new atomic fetch min/max builtins in std::atomic gcc/Makefile.in | 1 + gcc/c-family/c-common.cc | 43 +- gcc/ifn-atomic-minmax-lowering.cc | 372 ++++++++++++++++++ gcc/internal-fn.cc | 12 + gcc/internal-fn.def | 1 + gcc/optabs.cc | 36 ++ gcc/optabs.def | 24 ++ gcc/passes.def | 1 + gcc/sync-builtins.def | 15 + 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/tree-pass.h | 1 + libstdc++-v3/include/bits/atomic_base.h | 165 ++++++++ .../include/c_compatibility/stdatomic.h | 4 + libstdc++-v3/include/std/atomic | 52 +++ .../atomic_integral/fetch_minmax.cc | 168 ++++++++ .../atomic_integral/fetch_minmax_order.cc | 116 ++++++ .../29_atomics/atomic_integral/nonmembers.cc | 24 ++ .../29_atomics/atomic_ref/integral.cc | 51 ++- .../headers/stdatomic.h/c_compat.cc | 4 + 23 files changed, 2853 insertions(+), 4 deletions(-) create mode 100644 gcc/ifn-atomic-minmax-lowering.cc create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_integral/fetch_minmax.cc create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_integral/fetch_minmax_order.cc -- 2.44.0
