Skip to content

[libc++] Simplify aligned_storage #114665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

philnik777
Copy link
Contributor

The main template of aligned_storage is only ever used when we have extremely overaligned types (> 16384), so we effectively only ever use the specializations currently. This means that we only instantiate the main template for overaligned types. Instead of doing this dance, we can just define the main template to use _ALIGNAS, just like the specializations. This makes the implementation of aligned_storage significantly less confusing.

@philnik777 philnik777 requested a review from a team as a code owner November 2, 2024 13:22
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 2, 2024

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

The main template of aligned_storage is only ever used when we have extremely overaligned types (> 16384), so we effectively only ever use the specializations currently. This means that we only instantiate the main template for overaligned types. Instead of doing this dance, we can just define the main template to use _ALIGNAS, just like the specializations. This makes the implementation of aligned_storage significantly less confusing.


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

1 Files Affected:

  • (modified) libcxx/include/__type_traits/aligned_storage.h (+1-49)
diff --git a/libcxx/include/__type_traits/aligned_storage.h b/libcxx/include/__type_traits/aligned_storage.h
index 49b4e971bbb675..2e39afb7f88088 100644
--- a/libcxx/include/__type_traits/aligned_storage.h
+++ b/libcxx/include/__type_traits/aligned_storage.h
@@ -11,7 +11,6 @@
 
 #include <__config>
 #include <__cstddef/size_t.h>
-#include <__type_traits/conditional.h>
 #include <__type_traits/integral_constant.h>
 #include <__type_traits/nat.h>
 #include <__type_traits/type_list.h>
@@ -50,22 +49,6 @@ typedef __type_list<__align_type<unsigned char>,
         > > > > > > > > > > __all_types;
 // clang-format on
 
-template <size_t _Align>
-struct _ALIGNAS(_Align) __fallback_overaligned {};
-
-template <class _TL, size_t _Align>
-struct __find_pod;
-
-template <class _Hp, size_t _Align>
-struct __find_pod<__type_list<_Hp, __nat>, _Align> {
-  typedef __conditional_t<_Align == _Hp::value, typename _Hp::type, __fallback_overaligned<_Align> > type;
-};
-
-template <class _Hp, class _Tp, size_t _Align>
-struct __find_pod<__type_list<_Hp, _Tp>, _Align> {
-  typedef __conditional_t<_Align == _Hp::value, typename _Hp::type, typename __find_pod<_Tp, _Align>::type> type;
-};
-
 template <class _TL, size_t _Len>
 struct __find_max_align;
 
@@ -88,9 +71,7 @@ struct __find_max_align<__type_list<_Hp, _Tp>, _Len>
 
 template <size_t _Len, size_t _Align = __find_max_align<__all_types, _Len>::value>
 struct _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_TEMPLATE_VIS aligned_storage {
-  typedef typename __find_pod<__all_types, _Align>::type _Aligner;
-  union type {
-    _Aligner __align;
+  union _ALIGNAS(_Align) type {
     unsigned char __data[(_Len + _Align - 1) / _Align * _Align];
   };
 };
@@ -104,35 +85,6 @@ _LIBCPP_SUPPRESS_DEPRECATED_POP
 
 #endif
 
-#define _CREATE_ALIGNED_STORAGE_SPECIALIZATION(n)                                                                      \
-  template <size_t _Len>                                                                                               \
-  struct _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_TEMPLATE_VIS aligned_storage<_Len, n> {                                   \
-    struct _ALIGNAS(n) type {                                                                                          \
-      unsigned char __lx[(_Len + n - 1) / n * n];                                                                      \
-    };                                                                                                                 \
-  }
-
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x1);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x2);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x4);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x8);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x10);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x20);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x40);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x80);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x100);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x200);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x400);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x800);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x1000);
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x2000);
-// PE/COFF does not support alignment beyond 8192 (=0x2000)
-#if !defined(_LIBCPP_OBJECT_FORMAT_COFF)
-_CREATE_ALIGNED_STORAGE_SPECIALIZATION(0x4000);
-#endif // !defined(_LIBCPP_OBJECT_FORMAT_COFF)
-
-#undef _CREATE_ALIGNED_STORAGE_SPECIALIZATION
-
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___TYPE_TRAITS_ALIGNED_STORAGE_H

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@philnik777 philnik777 merged commit 7c69491 into llvm:main Nov 4, 2024
64 checks passed
@philnik777 philnik777 deleted the simplify_aligned_storage branch November 4, 2024 16:48
PhilippRados pushed a commit to PhilippRados/llvm-project that referenced this pull request Nov 6, 2024
The main template of `aligned_storage` is only ever used when we have
extremely overaligned types (> 16384), so we effectively only ever use
the specializations currently. This means that we only instantiate the
main template for overaligned types. Instead of doing this dance, we can
just define the main template to use `_ALIGNAS`, just like the
specializations. This makes the implementation of `aligned_storage`
significantly less confusing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants