https://gcc.gnu.org/g:4c273ce785dad77f8a2cc99c3b5a48eb44dd1890

commit r13-10106-g4c273ce785dad77f8a2cc99c3b5a48eb44dd1890
Author: Tomasz Kamiński <[email protected]>
Date:   Thu Jan 29 18:14:47 2026 +0100

    libstdc++: Allow constant initialization of std::atomic of types with 
padding [PR123875]
    
    Currently for the types T that contains padding bits, std::atomic<T>(T)
    constructor was not usable at compile-time in C++14 or later modes. This
    regression caused by use of __builtin_clear_padding introduced in
    r13-2548-g157236dbd62164.
    
    This leads to two regressions when switching from C++11 to C++14
    standard (or switching from GCC-12 to later version for C++14 standard),
    where for type X that contains padding
    * constexpr std::atomic<X> cx(X(...)) becomes ill-formed,
    * std::atomic<X> gx(X(...)) with static storage duration, switch from
      static to dynamic initialization.
    The latter breakage is silent and may introduced very hard to localize
    order of initialization issues.
    
    This patch mitigates above issue by not invoking the 
__builtin_clear_padding,
    during constant initialization (std::__is_constant_evaluated() is false).
    This is considered to be safe, as:
    * for objects with static storage duration, padding bits are already
      cleared by zero-initialization
    * for constexpr objects with non-static storage duration, there is no
      API that would allow user to observe padding bits on const atomic objects
    
    To elaborate on the second point, values of padding bits in atomic can
    be observed by:
    * The compare_exchange_weak/compare_exchange_strong operations are mutating,
      so cannot be invoked on const objects.
    * As atomic<X> is not required to store actual object of type X,
      observing its object representation does (via bitcast, memcpy), does
      not provide values of object representation of X. Furthermore, the
      operations are defined only for trivially_copyable types, and atomic
      specializations meets above requirement only due to bug in libstdc++
      (see PR67572).
    
    Note that above will no longer hold, and the solution will need to be
    revisited during implementation of C++26 paper P3309R3: constexpr
    atomic and atomic_ref (it will be possible to call compare_exchange
    during constant evaluation).
    
            PR libstdc++/123875
    
    libstdc++-v3/ChangeLog:
    
            * include/std/atomic (atomic<_Tp>::atomic(_Tp)): Skip 
__clear_padding
            call for constant evaluation.
            * testsuite/29_atomics/atomic/cons/zero_padding.cc: New test.
    
    Reviewed-by: Patrick Palka  <[email protected]>
    Reviewed-by: Jonathan Wakely <[email protected]>
    Signed-off-by: Tomasz Kamiński <[email protected]>
            (cherry picked with modifications from commits
            6b550d69fe7cb62ea6e240ce7a4ba29ce33aa1b1
            682c95b808724e6f876ea709b873ac6771704d7b
            060d7c2a9c1fe16d23d98a74287fdb7c73ddb784
            f3ba5ad088cebe117f857329a35b95d18d97a808)

Diff:
---
 libstdc++-v3/include/std/atomic                    |  3 +-
 .../29_atomics/atomic/cons/zero_padding.cc         | 79 ++++++++++++++++++++++
 2 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic
index 396e29f3b80c..0b05e054720e 100644
--- a/libstdc++-v3/include/std/atomic
+++ b/libstdc++-v3/include/std/atomic
@@ -234,7 +234,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
 #if __cplusplus >= 201402L && __has_builtin(__builtin_clear_padding)
        if _GLIBCXX17_CONSTEXPR (__atomic_impl::__maybe_has_padding<_Tp>())
-         __builtin_clear_padding(std::__addressof(_M_i));
+         if (!std::__is_constant_evaluated())
+           __builtin_clear_padding(std::__addressof(_M_i));
 #endif
       }
 
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/cons/zero_padding.cc 
b/libstdc++-v3/testsuite/29_atomics/atomic/cons/zero_padding.cc
new file mode 100644
index 000000000000..d57846d60205
--- /dev/null
+++ b/libstdc++-v3/testsuite/29_atomics/atomic/cons/zero_padding.cc
@@ -0,0 +1,79 @@
+// { dg-do run { target c++11 } }
+// { dg-require-atomic-cmpxchg-word "" }
+// { dg-add-options libatomic }
+
+#include <atomic>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+struct TailPadding { int i; char c; };
+TailPadding ztail{1, 2}; // zeroed-padding
+constexpr std::atomic<TailPadding> ctail(TailPadding{1,2});
+std::atomic<TailPadding> gtail(TailPadding{1,2});
+
+struct MidPadding { char c; int x; };
+MidPadding zmid{1, 2}; // zeroed-padding
+constexpr std::atomic<MidPadding> cmid(MidPadding{1,2});
+std::atomic<MidPadding> gmid(MidPadding{1,2});
+
+struct BitPadding { int : 4; int i : 5; int : 4; int j : 5; int : 4;  };
+BitPadding zbit{1, 2}; // zeroed-padding
+constexpr std::atomic<BitPadding> cbit(BitPadding{1,2});
+std::atomic<BitPadding> gbit(BitPadding{1,2});
+
+struct Ctor
+{
+  Ctor() = default;
+
+  constexpr Ctor(char pc, char pi)
+    : c(pc), i(pi)
+  {}
+
+  char c;
+  int i;
+};
+
+Ctor zctor{1, 2}; // zeroed-padding
+constexpr std::atomic<Ctor> cctor(Ctor{1,2});
+std::atomic<Ctor> gctor(Ctor{1,2});
+
+template<typename T>
+void test_struct(std::atomic<T>& g, const T& zp)
+{
+  T const d{3, 4};
+  T t;
+
+  std::memcpy(&t, &zp, sizeof(T));
+  VERIFY( g.compare_exchange_strong(t, d) );
+
+  static std::atomic<T> st(T{1, 2});
+  std::memcpy(&t, &zp, sizeof(T));
+  VERIFY( st.compare_exchange_strong(t, d) );
+
+  thread_local std::atomic<T> tl(T{1, 2});
+  std::memcpy(&t, &zp, sizeof(T));
+  VERIFY( tl.compare_exchange_strong(t, d) );
+
+  std::atomic<T> l(T{1, 2});
+  std::memcpy(&t, &zp, sizeof(T));
+#if __cplusplus >= 201402L // Remove once PR114865 is fixed
+  VERIFY( l.compare_exchange_strong(t, d) );
+#endif
+
+  std::atomic<T>* h = new std::atomic<T>(T{1, 2});
+  std::memcpy(&t, &zp, sizeof(T));
+#if __cplusplus >= 201402L // Remove once PR114865 is fixed
+  VERIFY( h->compare_exchange_strong(t, d) );
+#endif
+  delete h;
+
+  constexpr std::atomic<T> cl(T{1, 2});
+}
+
+int main()
+{
+  test_struct(gtail, ztail);
+  test_struct(gmid, zmid);
+  test_struct(gbit, zbit);
+  test_struct(gctor, zctor);
+}

Reply via email to