[libcxx-commits] [libcxx] [libcxx] Avoid __cxa_atexit with -O0 (PR #89882)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Apr 24 00:04:16 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Vitaly Buka (vitalybuka)
<details>
<summary>Changes</summary>
Empty destructor will register __cxa_atexit.
https://gcc.godbolt.org/z/hce587b65
We can not remove the destructor with union where
_Tp can have non-trivial destructor.
But we can remove destructor if we use in-place
new instead of union.
https://gcc.godbolt.org/z/Yqxx57eEd - empty even with -O0.
---
Full diff: https://github.com/llvm/llvm-project/pull/89882.diff
1 Files Affected:
- (modified) libcxx/include/__utility/no_destroy.h (+10-18)
``````````diff
diff --git a/libcxx/include/__utility/no_destroy.h b/libcxx/include/__utility/no_destroy.h
index f9c1eb7bed4569..833e16116c42d7 100644
--- a/libcxx/include/__utility/no_destroy.h
+++ b/libcxx/include/__utility/no_destroy.h
@@ -29,33 +29,25 @@ struct __uninitialized_tag {};
// initialization using __emplace.
template <class _Tp>
struct __no_destroy {
- _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI explicit __no_destroy(__uninitialized_tag) : __dummy_() {
- if (__libcpp_is_constant_evaluated()) {
- __dummy_ = char();
- }
- }
- _LIBCPP_HIDE_FROM_ABI ~__no_destroy() {
- // nothing
- }
+ _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI explicit __no_destroy(__uninitialized_tag) {}
template <class... _Args>
- _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI explicit __no_destroy(_Args&&... __args)
- : __obj_(std::forward<_Args>(__args)...) {}
+ _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI explicit __no_destroy(_Args&&... __args) {
+ new (&__obj_) _Tp(std::forward<_Args>(__args)...);
+ }
template <class... _Args>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp& __emplace(_Args&&... __args) {
- new (&__obj_) _Tp(std::forward<_Args>(__args)...);
- return __obj_;
+ return *(new (&__obj_) _Tp(std::forward<_Args>(__args)...));
}
- _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp& __get() { return __obj_; }
- _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp const& __get() const { return __obj_; }
+ _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp& __get() { return *reinterpret_cast<_Tp*>(__obj_); }
+ _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp const& __get() const {
+ return *reinterpret_cast<const _Tp*>(__obj_);
+ }
private:
- union {
- _Tp __obj_;
- char __dummy_; // so we can initialize a member even with __uninitialized_tag for constexpr-friendliness
- };
+ alignas(_Tp) unsigned char __obj_[sizeof(_Tp)] = {};
};
_LIBCPP_END_NAMESPACE_STD
``````````
</details>
https://github.com/llvm/llvm-project/pull/89882
More information about the libcxx-commits
mailing list