From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 85A8C384C00B; Mon, 19 Jul 2021 16:06:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85A8C384C00B From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/94295] use __builtin_operator_new and __builtin_operator_delete when available Date: Mon, 19 Jul 2021 16:06:57 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Jul 2021 16:06:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94295 --- Comment #7 from Jonathan Wakely --- Richard S., is there any reason to use the built-ins for the constant evaluation case? I assume not. Currently std::allocator does: [[nodiscard,__gnu__::__always_inline__]] constexpr _Tp* allocate(size_t __n) { #ifdef __cpp_lib_is_constant_evaluated if (std::is_constant_evaluated()) return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); #endif return __allocator_base<_Tp>::allocate(__n, 0); } and my assumption is that there is no reason to change this code, because t= he benefits of __builtin_operator_new are only for run-time uses. The calls to ::operator new in __allocator_base<_Tp>::allocate can use the built-in though e.g. --- a/libstdc++-v3/include/ext/new_allocator.h +++ b/libstdc++-v3/include/ext/new_allocator.h @@ -97,6 +97,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return std::__addressof(__x); } #endif +#if __has_builtin(__builtin_operator_new) >=3D 201802L +# define _GLIBCXX_OPERATOR_NEW __builtin_operator_new +# define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete +#else +# define _GLIBCXX_OPERATOR_NEW ::operator new +# define _GLIBCXX_OPERATOR_DELETE ::operator delete +#endif + // NB: __n is permitted to be 0. The C++ standard says nothing // about what the return value is when __n =3D=3D 0. _GLIBCXX_NODISCARD _Tp* @@ -121,34 +129,38 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { std::align_val_t __al =3D std::align_val_t(alignof(_Tp)); - return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al= )); + return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp= ), + __al)); } #endif - return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); + return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp))); } // __p is not permitted to be a null pointer. void deallocate(_Tp* __p, size_type __t __attribute__ ((__unused__))) { +#if __cpp_sized_deallocation +# define _GLIBCXX_SIZED_DEALLOC(p) p, __t * sizeof(_Tp) +#else +# define _GLIBCXX_SIZED_DEALLOC(p) p +#endif + #if __cpp_aligned_new if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { - ::operator delete(__p, -# if __cpp_sized_deallocation - __t * sizeof(_Tp), -# endif - std::align_val_t(alignof(_Tp))); + _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p), + std::align_val_t(alignof(_Tp))); return; } #endif - ::operator delete(__p -#if __cpp_sized_deallocation - , __t * sizeof(_Tp) -#endif - ); + _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p)); } +#undef _GLIBCXX_SIZED_DEALLOC +#undef _GLIBCXX_OPERATOR_DELETE +#undef _GLIBCXX_OPERATOR_NEW + #if __cplusplus <=3D 201703L size_type max_size() const _GLIBCXX_USE_NOEXCEPT I see no benefit to using __builtin_operator_new in std::pmr::new_delete_resource either, because that will usually be used through virtual calls to std::pmr::memory_resource::do_allocate, and the ac= tual call to ::operator new is inside libstdc++.so, not visible to the compiler.=