[libcxx-commits] [libcxx] [libc++] Increase `atomic_ref`'s required alignment for small types (PR #99654)

Damien L-G via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 19 07:26:22 PDT 2024


https://github.com/dalg24 updated https://github.com/llvm/llvm-project/pull/99654

>From 8d1fc929828cbc5a5d9e88180f928b545a703f91 Mon Sep 17 00:00:00 2001
From: Damien L-G <dalg24 at gmail.com>
Date: Fri, 19 Jul 2024 10:09:17 -0400
Subject: [PATCH 1/2] [libc++] Increase atomic_ref's required alignment for
 small types

---
 libcxx/include/__atomic/atomic_ref.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 156f1961151c1..49f6982a27f1f 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -95,10 +95,14 @@ struct __atomic_ref_base {
 
   friend struct __atomic_waitable_traits<__atomic_ref_base<_Tp>>;
 
+  // require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to at least their size to allow them to be
+  // used lock-free
+  static constexpr bool __min_alignement = (sizeof(_Tp) & sizeof(_Tp - 1)) || (sizeof(_Tp) > 16) ? 0 : sizeof(_Tp);
+
 public:
   using value_type = _Tp;
 
-  static constexpr size_t required_alignment = alignof(_Tp);
+  static constexpr size_t required_alignment = alignof(_Tp) > __min_alignement ? alignof(_Tp) : __min_alignement;
 
   // The __atomic_always_lock_free builtin takes into account the alignment of the pointer if provided,
   // so we create a fake pointer with a suitable alignment when querying it. Note that we are guaranteed

>From 8545aeb682c31c7eb43e2eb3ba6f55c84147c4f6 Mon Sep 17 00:00:00 2001
From: Damien L-G <dalg24 at gmail.com>
Date: Fri, 19 Jul 2024 10:26:05 -0400
Subject: [PATCH 2/2] fixup! [libc++] Increase atomic_ref's required alignment
 for small types

---
 libcxx/include/__atomic/atomic_ref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index 49f6982a27f1f..99ec8add82c39 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -97,7 +97,7 @@ struct __atomic_ref_base {
 
   // require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to at least their size to allow them to be
   // used lock-free
-  static constexpr bool __min_alignement = (sizeof(_Tp) & sizeof(_Tp - 1)) || (sizeof(_Tp) > 16) ? 0 : sizeof(_Tp);
+  static constexpr bool __min_alignement = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || (sizeof(_Tp) > 16) ? 0 : sizeof(_Tp);
 
 public:
   using value_type = _Tp;



More information about the libcxx-commits mailing list