[libcxx-commits] [libcxx] [libc++] make std::atomic works with types with paddings (PR #76180)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 19 09:27:38 PST 2024


================
@@ -26,6 +27,32 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR void __clear_padding_if_needed(_Tp* __ptr) noexcept {
+#if _LIBCPP_STD_VER >= 20
+  constexpr bool __needs_clear_padding =
+      !__has_unique_object_representations(_Tp) && !is_same<_Tp, float>::value && !is_same<_Tp, double>::value;
+  if constexpr (__needs_clear_padding) {
+    if (!__builtin_is_constant_evaluated()) {
+      __builtin_clear_padding(__ptr);
+    }
+  }
+#endif
+}
+
+template <typename _Tp, typename _ImplFunc>
+_LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_compare_exchange_impl(_Tp* __expected, _Tp __value, _ImplFunc&& __impl_fun) {
+  std::__clear_padding_if_needed(std::addressof(__value));
+  _Tp __expected_copy = *__expected;
----------------
ldionne wrote:

We should have a test that fails if you forget to make a copy here. This would require a situation where we clear the padding bits of `__expected`.

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


More information about the libcxx-commits mailing list