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

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 25 10:56:55 PDT 2024


================
@@ -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 be potentially
+  // used lock-free
+  static constexpr size_t __min_alignment = (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_alignment ? alignof(_Tp) : __min_alignment;
----------------
EricWF wrote:

I think we should calculate the alignment using something like this?

```
template <class T>
constexpr size_t calc_alignment() {
    size_t align = alignof(T);
    while (align <= alignof(max_align_t)) {
        if (__atomic_always_lock_free(sizeof(T), align))
            return align;
        align *= 2;
    }
    return alignof(T);
}
```

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


More information about the libcxx-commits mailing list