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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 19 07:20:43 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Damien L-G (dalg24)

<details>
<summary>Changes</summary>

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 

---
Full diff: https://github.com/llvm/llvm-project/pull/99654.diff


1 Files Affected:

- (modified) libcxx/include/__atomic/atomic_ref.h (+5-1) 


``````````diff
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

``````````

</details>


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


More information about the libcxx-commits mailing list