[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:20:15 PDT 2024


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

Background discussion here https://github.com/llvm/llvm-project/pull/99570#issuecomment-2237668661

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.

cc @jyknight @ldionne 

>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] [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



More information about the libcxx-commits mailing list