From patchwork Thu Mar 31 15:30:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 52532 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 BC38D3898538 for ; Thu, 31 Mar 2022 15:31:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC38D3898538 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1648740684; bh=2c6zOILxjzvxN+C4zvpy2q4/AXiR2gERh85qCFOzT+c=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=EMreLBjYjxT5QLKrzJ/Y7BUUZhIQ4h/TN3VU/j2azen3bJJMNpUT5B4/BTvTxV56E CuhKEohOc6LERkM0vtz8mKJPEChMb8+X9TB3SMoaIbpCf67vsE1Y2qxF3ZUgmudTKj r1vBPEbGVXg8t4jxyMneEcapC+X/UO4S4LQERmF4= 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 ESMTPS id 3BEB13857C50 for ; Thu, 31 Mar 2022 15:30:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3BEB13857C50 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-111-XHsCT-yWPqKz9lNiHhG5Mw-1; Thu, 31 Mar 2022 11:30:30 -0400 X-MC-Unique: XHsCT-yWPqKz9lNiHhG5Mw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2D7F7185A7A4; Thu, 31 Mar 2022 15:30:30 +0000 (UTC) Received: from localhost (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id E29CA5DDCF7; Thu, 31 Mar 2022 15:30:29 +0000 (UTC) To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Implement std::unreachable() for C++23 (P0627R6) Date: Thu, 31 Mar 2022 16:30:29 +0100 Message-Id: <20220331153029.1898244-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This is a tiny C++23 feature that I plan to add for GCC 12. Does anybody have any comments on the choices below in terms of when to make reaching std::unreachable do an assert/trap/unreachable? My thoughts on avoiding the overhead in the _GLIBCXX_ASSERTIONS case is that this differs from e.g. assertions in operator[] where we want to catch accidental bad indices. A std::unreachable() call should not be reached accidentally. I hope it will only be used for conditions that really are unreachable, and probably quite often where performance matters. If using std::unreachable() increased code size significantly then it would make it much less useful for guiding optimizations. -- >8 -- This defines std::unreachable as an assertion for debug mode, a trap when _GLIBCXX_ASSERTIONS is defined, and __builtin_unreachable() otherwise. The reason for only using __builtin_trap() in the second case is to avoid the overhead of setting up a call to __glibcxx_assert_fail that should never happen. While thinking about what the debug assertion failure should print, I noticed that the __glibcxx_assert_fail function doesn't check for null pointers. This adds a check so we don't try to print them if null. libstdc++-v3/ChangeLog: * include/std/utility (unreachable): Define for C++23. * include/std/version (__cpp_lib_unreachable): Define. * src/c++11/debug.cc (__glibcxx_assert_fail): Check for valid arguments. * testsuite/20_util/unreachable/1.cc: New test. * testsuite/20_util/unreachable/version.cc: New test. --- libstdc++-v3/include/std/utility | 15 +++++++++++++++ libstdc++-v3/include/std/version | 1 + libstdc++-v3/src/c++11/debug.cc | 5 +++-- libstdc++-v3/testsuite/20_util/unreachable/1.cc | 17 +++++++++++++++++ .../testsuite/20_util/unreachable/version.cc | 10 ++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/unreachable/1.cc create mode 100644 libstdc++-v3/testsuite/20_util/unreachable/version.cc diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 0d7f8954c5a..e5b5212381d 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -186,6 +186,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr underlying_type_t<_Tp> to_underlying(_Tp __value) noexcept { return static_cast>(__value); } + +#define __cpp_lib_unreachable 202202L + [[noreturn,__gnu__::__always_inline__]] + void + unreachable() + { +#ifdef _GLIBCXX_DEBUG + std::__glibcxx_assert_fail("", 0, "std::unreachable()", + "inconceivable!"); +#elif defined _GLIBCXX_ASSERTIONS + __builtin_trap(); +#else + __builtin_unreachable(); +#endif + } #endif // C++23 #endif // C++20 #endif // C++17 diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 44b8a9f88b5..51f2110b68e 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -326,6 +326,7 @@ # define __cpp_lib_string_resize_and_overwrite 202110L #endif #define __cpp_lib_to_underlying 202102L +#define __cpp_lib_unreachable 202202L #endif #endif // C++2b #endif // C++20 diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc index 98fe2dcc153..ff3723eb93b 100644 --- a/libstdc++-v3/src/c++11/debug.cc +++ b/libstdc++-v3/src/c++11/debug.cc @@ -52,8 +52,9 @@ namespace std __glibcxx_assert_fail(const char* file, int line, const char* function, const char* condition) noexcept { - fprintf(stderr, "%s:%d: %s: Assertion '%s' failed.\n", - file, line, function, condition); + if (file && function && condition) + fprintf(stderr, "%s:%d: %s: Assertion '%s' failed.\n", + file, line, function, condition); abort(); } } diff --git a/libstdc++-v3/testsuite/20_util/unreachable/1.cc b/libstdc++-v3/testsuite/20_util/unreachable/1.cc new file mode 100644 index 00000000000..0c463d52a48 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unreachable/1.cc @@ -0,0 +1,17 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_unreachable +# error "Feature-test macro for unreachable missing in " +#elif __cpp_lib_unreachable != 202202L +# error "Feature-test macro for unreachable has wrong value in " +#endif + +bool test01(int i) +{ + if (i == 4) + return true; + std::unreachable(); +} // { dg-bogus "control reaches end of non-void function" } diff --git a/libstdc++-v3/testsuite/20_util/unreachable/version.cc b/libstdc++-v3/testsuite/20_util/unreachable/version.cc new file mode 100644 index 00000000000..c7795900c30 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unreachable/version.cc @@ -0,0 +1,10 @@ +// { dg-options "-std=gnu++23" } +// { dg-do preprocess { target c++23 } } + +#include + +#ifndef __cpp_lib_unreachable +# error "Feature-test macro for unreachable missing in " +#elif __cpp_lib_unreachable != 202202L +# error "Feature-test macro for unreachable has wrong value in " +#endif