From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 2925E3857C4C; Fri, 26 Nov 2021 23:07:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2925E3857C4C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-5552] libstdc++: Fix trivial relocation for constexpr std::vector X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 76c6be48b7841524974754f8ea7533b82c7de77e X-Git-Newrev: 33adfd0d42e54c809b0c53212abe66e8c874b2f8 Message-Id: <20211126230747.2925E3857C4C@sourceware.org> Date: Fri, 26 Nov 2021 23:07:47 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2021 23:07:47 -0000 https://gcc.gnu.org/g:33adfd0d42e54c809b0c53212abe66e8c874b2f8 commit r12-5552-g33adfd0d42e54c809b0c53212abe66e8c874b2f8 Author: Jonathan Wakely Date: Fri Nov 26 21:34:17 2021 +0000 libstdc++: Fix trivial relocation for constexpr std::vector When implementing constexpr std::vector I added a check for constant evaluation in vector::_S_use_relocate(), so that we would not try to relocate trivial objects by using memmove. But I put it in the constexpr function that decides whether to relocate or not, and calls to that function are always constant evaluated. This had the effect of disabling relocation entirely, even in non-constexpr vectors. This removes the check in _S_use_relocate() and modifies the actual relocation algorithm, __relocate_a_1, to use the non-trivial implementation instead of memmove when called during constant evaluation. libstdc++-v3/ChangeLog: * include/bits/stl_uninitialized.h (__relocate_a_1): Do not use memmove during constant evaluation. * include/bits/stl_vector.h (vector::_S_use_relocate()): Do not check is_constant_evaluated in always-constexpr function. Diff: --- libstdc++-v3/include/bits/stl_uninitialized.h | 45 ++++++++++++++++++--------- libstdc++-v3/include/bits/stl_vector.h | 4 --- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index 673f6a07675..f2786c5a461 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -1051,6 +1051,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @cond undocumented template + _GLIBCXX20_CONSTEXPR inline void __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig, _Allocator& __alloc) @@ -1070,18 +1071,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct __is_bitwise_relocatable : is_trivial<_Tp> { }; - template - _GLIBCXX20_CONSTEXPR - inline __enable_if_t::value, _Tp*> - __relocate_a_1(_Tp* __first, _Tp* __last, - _Tp* __result, allocator<_Up>&) noexcept - { - ptrdiff_t __count = __last - __first; - if (__count > 0) - __builtin_memmove(__result, __first, __count * sizeof(_Tp)); - return __result + __count; - } - template _GLIBCXX20_CONSTEXPR @@ -1105,6 +1094,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __cur; } + template + _GLIBCXX20_CONSTEXPR + inline __enable_if_t::value, _Tp*> + __relocate_a_1(_Tp* __first, _Tp* __last, + _Tp* __result, + [[__maybe_unused__]] allocator<_Up>& __alloc) noexcept + { + ptrdiff_t __count = __last - __first; + if (__count > 0) + { +#ifdef __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + { + // Can't use memmove. Wrap the pointer so that __relocate_a_1 + // resolves to the non-trivial overload above. + __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result); + __out = std::__relocate_a_1(__first, __last, __out, __alloc); + return __out.base(); + } +#endif + __builtin_memmove(__result, __first, __count * sizeof(_Tp)); + } + return __result + __count; + } + + template _GLIBCXX20_CONSTEXPR @@ -1115,9 +1130,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::__niter_base(__last), std::__niter_base(__result), __alloc))) { - return __relocate_a_1(std::__niter_base(__first), - std::__niter_base(__last), - std::__niter_base(__result), __alloc); + return std::__relocate_a_1(std::__niter_base(__first), + std::__niter_base(__last), + std::__niter_base(__result), __alloc); } /// @endcond diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index bd15b6bd8a8..4587757637e 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -475,10 +475,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER static constexpr bool _S_use_relocate() { -#if __cplusplus >= 202002L && __has_builtin(__builtin_is_constant_evaluated) - if (__builtin_is_constant_evaluated()) - return false; // Cannot use memcpy in constant evaluation contexts. -#endif // Instantiating std::__relocate_a might cause an error outside the // immediate context (in __relocate_object_a's noexcept-specifier), // so only do it if we know the type can be move-inserted into *this.