From patchwork Thu Sep 11 12:16:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 120072 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5B633857354 for ; Thu, 11 Sep 2025 12:18:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id DC8D33858421 for ; Thu, 11 Sep 2025 12:17:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC8D33858421 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DC8D33858421 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757593068; cv=none; b=l7dsbXVKkcbKJOpH7/67599yyXeBOjDFnQ4djwgN0ZTDIZ4Y1kmWLqib1IhZO3jxPnWuHBbOcnH2sO1s7DSexGE8uWa8Qzr8MJr63la8X63J1m8U8AKVw4nYTG1oJS9w1jdlmfqI6vjRUxgP10lNItCvdy1p7G74So/nEgyOwCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757593068; c=relaxed/simple; bh=a2T3nE16LA3NZpzsYum+LoM0NO2DzjZ/KmM3RlIQGO8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=eH0cK3iQBG5y+DQ9OSaK6PNx1z1o3acTLmwcibiykuaiHc6dXlvbAv9y0KShagKEMLG3EsqKMftTjTpU4A1gsJk4aYs3PePsC7hHei4d3tVytvHBS5SK9BmDOymMdIzU/0IcW+FIkl+rEzRJ67gFO7vGV7RnKZgZPGxifUxBAbE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC8D33858421 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=G4D3tKwn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757593066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jKvrZ0FKDu4LLPqPq9UYlUzn65A/GX4u8WzXobmslf8=; b=G4D3tKwnR6oxj4U/cpArfnrZYdsvZbNBlfmAVRdKO4+IQ0oM5u+Cf8HZZUGf5+0I/Rm4u/ 3rgppLTtRb/be7ybcbyCz0XAJ8HNo7ER0npe7EwjL+ywXrhsC5g7WaJuLjD6zoPrYqYUmR AsUoRyS4X49VQLQNAqHD3OuOLx5HXMw= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-219-I3dZIv12NtW_2G4I8hGC5A-1; Thu, 11 Sep 2025 08:17:43 -0400 X-MC-Unique: I3dZIv12NtW_2G4I8hGC5A-1 X-Mimecast-MFC-AGG-ID: I3dZIv12NtW_2G4I8hGC5A_1757593062 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4E65419560B3; Thu, 11 Sep 2025 12:17:42 +0000 (UTC) Received: from zen.kayari.org (unknown [10.42.28.196]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5712A1944CCA; Thu, 11 Sep 2025 12:17:40 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Add always_inline to ranges iterator ops and access functions Date: Thu, 11 Sep 2025 13:16:23 +0100 Message-ID: <20250911121739.1538375-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: KiYkuJ5ptPPoWc3ELLA83PdWXbw5UkXZIXxBqibuvlU_1757593062 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_W, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Most of the basis operations for ranges such as ranges::begin and ranges::next are trivial one-line function bodies, so can be made always_inline to reduce the abstraction penalty for -O0 code. Now that we no longer need to support the -fconcepts-ts grammar, we can also move some [[nodiscard]] attributes to the more natural position before the function declaration, instead of between the declarator-id and the function parameters, e.g. we can use: template requires C [[nodiscard]] auto operator()(T&&) instead of: template requires C auto operator() [[nodiscard]] (T&&) The latter form was necessary because -fconcepts-ts used a different grammar for the requires-clause, parsing 'C[[x]]' as a subscripting operator with an ill-formed argument '[x]'. In the C++20 grammar you would need to use parentheses to use a subscript in a constraint, so without parentheses it's parsed as an attribute. libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (__detail::__to_unsigned_like) (__access::__possible_const_range, __access::__as_const) (__distance_fn::operator(), __next_fn::operator()) (__prev_fn::operator()): Add always_inline attribute. (_Begin::operator(), _End::operator(), _RBegin::operator()) (_REnd::operator(), _Size::operator(), _SSize::operator()) (_Empty::operator(), _Data::operator(), _SSize::operator()): Likewise. Move nodiscard attribute to start of declaration. --- This change was inspired when I was adding more uses of ranges::next and ranges::prev to fix the iterator arithmetic not using difference_type. Tested powerpc64-linux. libstdc++-v3/include/bits/ranges_base.h | 47 ++++++++++++++++--------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 02e13a2027d1..c1a9f6a90009 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -61,29 +61,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace ranges { template inline constexpr bool disable_sized_range = false; template inline constexpr bool enable_borrowed_range = false; namespace __detail { + [[__gnu__::__always_inline__]] constexpr __max_size_type __to_unsigned_like(__max_size_type __t) noexcept { return __t; } + [[__gnu__::__always_inline__]] constexpr __max_size_type __to_unsigned_like(__max_diff_type __t) noexcept { return __max_size_type(__t); } template + [[__gnu__::__always_inline__]] constexpr auto __to_unsigned_like(_Tp __t) noexcept { return static_cast>(__t); } template using __make_unsigned_like_t = decltype(__detail::__to_unsigned_like(std::declval<_Tp>())); // Part of the constraints of ranges::borrowed_range template @@ -111,22 +114,23 @@ namespace ranges else if constexpr (__member_begin<_Tp>) return noexcept(_GLIBCXX_AUTO_CAST(std::declval<_Tp&>().begin())); else return noexcept(_GLIBCXX_AUTO_CAST(begin(std::declval<_Tp&>()))); } public: template<__maybe_borrowed_range _Tp> requires is_array_v> || __member_begin<_Tp> || __adl_begin<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_array_v>) { static_assert(is_lvalue_reference_v<_Tp>); return __t + 0; } else if constexpr (__member_begin<_Tp>) return __t.begin(); else return begin(__t); @@ -161,22 +165,23 @@ namespace ranges else if constexpr (__member_end<_Tp>) return noexcept(_GLIBCXX_AUTO_CAST(std::declval<_Tp&>().end())); else return noexcept(_GLIBCXX_AUTO_CAST(end(std::declval<_Tp&>()))); } public: template<__maybe_borrowed_range _Tp> requires is_bounded_array_v> || __member_end<_Tp> || __adl_end<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_bounded_array_v>) { static_assert(is_lvalue_reference_v<_Tp>); return __t + extent_v>; } else if constexpr (__member_end<_Tp>) return __t.end(); else return end(__t); @@ -225,22 +230,23 @@ namespace ranges return is_nothrow_copy_constructible_v<_It>; } else return false; } } public: template<__maybe_borrowed_range _Tp> requires __member_rbegin<_Tp> || __adl_rbegin<_Tp> || __reversable<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_rbegin<_Tp>) return __t.rbegin(); else if constexpr (__adl_rbegin<_Tp>) return rbegin(__t); else return std::make_reverse_iterator(_End{}(__t)); } }; @@ -282,22 +288,23 @@ namespace ranges return is_nothrow_copy_constructible_v<_It>; } else return false; } } public: template<__maybe_borrowed_range _Tp> requires __member_rend<_Tp> || __adl_rend<_Tp> || __reversable<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_rend<_Tp>) return __t.rend(); else if constexpr (__adl_rend<_Tp>) return rend(__t); else return std::make_reverse_iterator(_Begin{}(__t)); } }; @@ -346,42 +353,44 @@ namespace ranges return noexcept(_GLIBCXX_AUTO_CAST(size(std::declval<_Tp&>()))); else if constexpr (__sentinel_size<_Tp>) return noexcept(_End{}(std::declval<_Tp&>()) - _Begin{}(std::declval<_Tp&>())); } public: template requires is_bounded_array_v> || __member_size<_Tp> || __adl_size<_Tp> || __sentinel_size<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (is_bounded_array_v>) return extent_v>; else if constexpr (__member_size<_Tp>) return __t.size(); else if constexpr (__adl_size<_Tp>) return size(__t); else if constexpr (__sentinel_size<_Tp>) return __detail::__to_unsigned_like(_End{}(__t) - _Begin{}(__t)); } }; struct _SSize { // _GLIBCXX_RESOLVE_LIB_DEFECTS // 3403. Domain of ranges::ssize(E) doesn't match ranges::size(E) template requires requires (_Tp& __t) { _Size{}(__t); } + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(noexcept(_Size{}(__t))) + operator()(_Tp&& __t) const noexcept(noexcept(_Size{}(__t))) { auto __size = _Size{}(__t); using __size_type = decltype(__size); // Return the wider of ptrdiff_t and make-signed-like-t<__size_type>. if constexpr (integral<__size_type>) { using __gnu_cxx::__int_traits; if constexpr (__int_traits<__size_type>::__digits < __int_traits::__digits) return static_cast(__size); @@ -422,22 +431,23 @@ namespace ranges return noexcept(_Size{}(std::declval<_Tp&>()) == 0); else return noexcept(bool(_Begin{}(std::declval<_Tp&>()) == _End{}(std::declval<_Tp&>()))); } public: template requires __member_empty<_Tp> || __size0_empty<_Tp> || __eq_iter_empty<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr bool - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp&>()) { if constexpr (__member_empty<_Tp>) return bool(__t.empty()); else if constexpr (__size0_empty<_Tp>) return _Size{}(__t) == 0; else return bool(_Begin{}(__t) == _End{}(__t)); } }; @@ -463,22 +473,23 @@ namespace ranges { if constexpr (__member_data<_Tp>) return noexcept(_GLIBCXX_AUTO_CAST(std::declval<_Tp&>().data())); else return noexcept(_Begin{}(std::declval<_Tp&>())); } public: template<__maybe_borrowed_range _Tp> requires __member_data<_Tp> || __begin_data<_Tp> + [[nodiscard, __gnu__::__always_inline__]] constexpr auto - operator()[[nodiscard]](_Tp&& __t) const noexcept(_S_noexcept<_Tp>()) + operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp>()) { if constexpr (__member_data<_Tp>) return __t.data(); else return std::to_address(_Begin{}(__t)); } }; } // namespace __access @@ -625,33 +636,35 @@ namespace ranges #if __glibcxx_ranges_as_const // >= C++23 template concept constant_range = input_range<_Tp> && std::__detail::__constant_iterator>; #endif namespace __access { #if __glibcxx_ranges_as_const // >= C++23 template + [[__gnu__::__always_inline__]] constexpr auto& __possibly_const_range(_Range& __r) noexcept { // _GLIBCXX_RESOLVE_LIB_DEFECTS // 4027. possibly-const-range should prefer returning const R& if constexpr (input_range) return const_cast(__r); else return __r; } #else // If _To is an lvalue-reference, return const _Tp&, otherwise const _Tp&&. template + [[__gnu__::__always_inline__]] constexpr decltype(auto) __as_const(_Tp& __t) noexcept { static_assert(std::is_same_v<_To&, _Tp&>); if constexpr (is_lvalue_reference_v<_To>) return const_cast(__t); else return static_cast(__t); } @@ -960,106 +973,106 @@ namespace ranges iter_difference_t<_It> __n = 0; while (__first != __last) { ++__first; ++__n; } return __n; } template> _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr iter_difference_t> operator()(_It&& __first, _Sent __last) const { return __last - static_cast&>(__first); } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr range_difference_t<_Range> operator()(_Range&& __r) const { if constexpr (sized_range<_Range>) return static_cast>(ranges::size(__r)); else return (*this)(ranges::begin(__r), ranges::end(__r)); } void operator&() const = delete; }; inline constexpr __distance_fn distance{}; struct __next_fn final { template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x) const { ++__x; return __x; } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n) const { ranges::advance(__x, __n); return __x; } template _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, _Sent __bound) const { ranges::advance(__x, __bound); return __x; } template _Sent> - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n, _Sent __bound) const { ranges::advance(__x, __n, __bound); return __x; } void operator&() const = delete; }; inline constexpr __next_fn next{}; struct __prev_fn final { template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x) const { --__x; return __x; } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n) const { ranges::advance(__x, -__n); return __x; } template - [[nodiscard]] + [[nodiscard, __gnu__::__always_inline__]] constexpr _It operator()(_It __x, iter_difference_t<_It> __n, _It __bound) const { ranges::advance(__x, -__n, __bound); return __x; } void operator&() const = delete; };