From patchwork Thu Jan 11 18:03:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 83917 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 8841138708E2 for ; Thu, 11 Jan 2024 18:05:27 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id E3FD3386C584 for ; Thu, 11 Jan 2024 18:04:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E3FD3386C584 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 E3FD3386C584 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704996290; cv=none; b=MQ9/87sTN6BR4XjoRGeQpayMUt4m3308smtKB3U2L38NkazhxfJN0XzZtsndYXJmumPsUxg8IiTNSFdblsFgdoFFpUJVpi8xUi2NqOTJNZf/9cS/QTyIduPzdetUBLjUc/oz5bTuWnY5TCqlxuY4gd23MWXBRvugt0xb2ak53sQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704996290; c=relaxed/simple; bh=qZSNcbMob9fNv/7fsKdvPUePzuq+UceUK4HIFhz6Xr0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Kls+EB/z7DIizFcNtqFWSqmUGEKmWife7uiHMHDnqDE6r2GOkaLj+pagOTxHMXyTYO5v9INkj+dHPmC6zhoG0mkViSkx/xq5UOBHL5CJav4q8IRoVnoTOJHXaV6x7w2DmH/ax3gSboDshOiAsqYZ7Wu8CXuY3r1zeSRxi49WIHM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704996288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2BlR6dCpwkydoaK/e8fCJtqzblYb3eyUXxpBYDUjVqU=; b=YxwOu0hM4lOx6ZLKpCywnt3YKuNfpwHOD5QWP1LSyf6+tFEab2Bh/kx8sV5vxQswyzBzvY fjqeOQkgz+hSdmXbAqkDiQO1WojsESzXTDiyC1vJDXtkttcLxeN4nXsSSOmkMzIgywo1MY 0yDub+5HMOoh+JU3IiWTtr70psXnzZs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-n21PdG05P1KQcaIA4fD0hg-1; Thu, 11 Jan 2024 13:04:43 -0500 X-MC-Unique: n21PdG05P1KQcaIA4fD0hg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A69B29AA2CC; Thu, 11 Jan 2024 18:04:43 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFB402026D66; Thu, 11 Jan 2024 18:04:42 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Tom Rodgers , gonzalo.gadeschi@gmail.com Subject: [PATCH] libstdc++: Make PSTL algorithms accept C++20 iterators [PR110512] Date: Thu, 11 Jan 2024 18:03:21 +0000 Message-ID: <20240111180442.421194-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 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_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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 Tested x86_64-linux and aarch64-linux, with TBB 2020.3 only. Reviews requested. -- >8 -- This is a step towards implementing the C++23 change P2408R5, "Ranges iterators as inputs to non-Ranges algorithms". C++20 random access iterators which do not meet the C==17RandomAccessIterator requirements will now be recognized by the PSTL algorithms. We can also optimize the C++17 implementation by using std::__or_, and use std::__remove_cvref_t and std::__iter_category_t for readability. This diverges from the upstream PSTL, but since libc++ is no longer using that upstream (so we're the only consumer of this code) I think it's reasonable to use libstdc++ extensions in localized places like this. Rebasing this small header on upstream should not be difficult. libstdc++-v3/ChangeLog: PR libstdc++/110512 * include/pstl/execution_impl.h (__are_random_access_iterators): Recognize C++20 random access iterators, and use more efficient implementations. * testsuite/25_algorithms/pstl/110512.cc: New test. --- libstdc++-v3/include/pstl/execution_impl.h | 21 ++++++++++--- .../testsuite/25_algorithms/pstl/110512.cc | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/pstl/110512.cc diff --git a/libstdc++-v3/include/pstl/execution_impl.h b/libstdc++-v3/include/pstl/execution_impl.h index 64f6cc4357a..c84061848b9 100644 --- a/libstdc++-v3/include/pstl/execution_impl.h +++ b/libstdc++-v3/include/pstl/execution_impl.h @@ -19,13 +19,24 @@ namespace __pstl { namespace __internal { - -template -using __are_iterators_of = std::conjunction< - std::is_base_of<_IteratorTag, typename std::iterator_traits>::iterator_category>...>; +#if __glibcxx_concepts +template + concept __is_random_access_iter + = std::is_base_of_v> + || std::random_access_iterator<_Iter>; template -using __are_random_access_iterators = __are_iterators_of; + using __are_random_access_iterators + = std::bool_constant<(__is_random_access_iter> && ...)>; +#else +template +using __are_random_access_iterators + = std::__and_< + std::is_base_of>>... + >; +#endif struct __serial_backend_tag { diff --git a/libstdc++-v3/testsuite/25_algorithms/pstl/110512.cc b/libstdc++-v3/testsuite/25_algorithms/pstl/110512.cc new file mode 100644 index 00000000000..188c7c915e5 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/pstl/110512.cc @@ -0,0 +1,31 @@ +// { dg-do compile { target c++17 } } + +// Bug 110512 - C++20 random access iterators run sequentially with PSTL + +#include +#include +#include +#include + +using InputIter = __gnu_test::input_iterator_wrapper; +using FwdIter = __gnu_test::forward_iterator_wrapper; +using RAIter = __gnu_test::random_access_iterator_wrapper; + +template +constexpr bool all_random_access + = __pstl::__internal::__are_random_access_iterators::value; + +using __pstl::__internal::__are_random_access_iterators; +static_assert( all_random_access ); +static_assert( all_random_access ); +static_assert( ! all_random_access ); +static_assert( ! all_random_access ); + +#if __cpp_lib_ranges +using IotaIter = std::ranges::iterator_t>; +static_assert( std::random_access_iterator ); +static_assert( all_random_access ); +static_assert( all_random_access ); +static_assert( all_random_access ); +static_assert( ! all_random_access ); +#endif