public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed 2/2] libstdc++: Simplify constexpr checks in std::char_traits [PR 91488]
Date: Fri, 18 Jun 2021 11:25:51 +0100	[thread overview]
Message-ID: <YMx0r5TlohyVMVs3@redhat.com> (raw)
In-Reply-To: <YMvCkjFzQv2MJQkt@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1799 bytes --]

On 17/06/21 22:45 +0100, Jonathan Wakely wrote:
>This removes the helper functions added by r8-1294 to detect whether the
>char_traits member functions can be evaluated at compile time. Instead,
>we can just use __builtin_constant_evaluated directly, which is well
>supported by non-GCC compilers by now.
>
>As a result, there is a chance that those members will no longer be
>usable in constant expressions when using old versions of non-GCC
>compilers. Make the relevant feature test macros depend on the
>availability of __builtin_constant_evaluated, so they are defined only
>when the feature is actualyl available.
>
>The new testcase from the PR is added to the libitm testsuite, because
>that's where we can be sure it's OK to use the -fgnu-tm option.
>
>Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
>
>	PR libstdc++/91488
>
>libstdc++-v3/ChangeLog:
>
>	* include/bits/basic_string.h (__cpp_lib_constexpr_string): Only
>	define when is_constant_evaluated is available.
>	* include/bits/char_traits.h (__cpp_lib_constexpr_char_traits):
>	Likewise.
>	(__constant_string_p, __constant_array_p): Remove.
>	(char_traits): Use is_constant_evaluated directly.
>	* include/std/version (__cpp_lib_constexpr_char_traits)
>	(__cpp_lib_constexpr_string): Only define when
>	is_constant_evaluated is available.
>
>libitm/ChangeLog:
>
>	* testsuite/libitm.c++/libstdc++-pr91488.C: New test.

The attached patch is the backport. Instead of removing the
__constant_string_p and __constant_array_p functions entirely, just
give them external linkage. That seems to avoid the bug, and means the
release branches can still be used with older non-GCC compilers which
don't support __builtin_is_constant_evaluated yet.

Tested x86_64-linux. Committed to gcc-11 for now, gcc-10 and gcc-9 to
follow soon.




[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 5365 bytes --]

commit 0191c74141cfea8973a6d977ce9944d39f7c4329
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jun 17 14:11:22 2021

    libstdc++: Simplify constexpr checks in std::char_traits [PR 91488]
    
    This removes the 'static' keyword from the helper functions added by
    r8-1294 to detect whether the char_traits member functions can be
    evaluated at compile time. This prevents the "inlining failed" error
    reported in the PR.
    
    The new testcase from the PR is added to the libitm testsuite, because
    that's where we can be sure it's OK to use the -fgnu-tm option.
    
    As a drive-by fix, the feature test macros for C++20 P0980R1 support are
    made to depend on whether __cpp_lib_is_constant_evaluated is defined.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
            PR libstdc++/91488
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/basic_string.h (__cpp_lib_constexpr_string): Only
            define C++20 value when std::is_constant_evaluated is available.
            * include/bits/char_traits.h (__cpp_lib_constexpr_char_traits):
            Likewise.
            (__constant_string_p, __constant_array_p): Give external
            linkage.
            * include/std/version (__cpp_lib_constexpr_char_traits)
            (__cpp_lib_constexpr_string): Only define C++20 values when
            is_constant_evaluated is available.
    
    libitm/ChangeLog:
    
            * testsuite/libitm.c++/libstdc++-pr91488.C: New test.
    
    (cherry picked from commit b376b1ef38971b84975ad1540bf5d2ae0b924e76)

diff --git a/libitm/testsuite/libitm.c++/libstdc++-pr91488.C b/libitm/testsuite/libitm.c++/libstdc++-pr91488.C
new file mode 100644
index 00000000000..e9e82bd1ce2
--- /dev/null
+++ b/libitm/testsuite/libitm.c++/libstdc++-pr91488.C
@@ -0,0 +1,9 @@
+// PR libstdc++/91488 "inlining failed in call to always_inline"
+// { dg-do run }
+// { dg-additional-options "-O1" }
+
+#include <string>
+
+int main() {
+    return std::char_traits<char>::length("");
+}
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 84356adc7ae..037040740ab 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -52,12 +52,13 @@ namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
-#if __cplusplus == 201703L
+#ifdef __cpp_lib_is_constant_evaluated
+// Support P1032R1 in C++20 (but not P0980R1 yet).
+# define __cpp_lib_constexpr_string 201811L
+#elif __cplusplus >= 201703L
 // Support P0426R1 changes to char_traits in C++17.
 # define __cpp_lib_constexpr_string 201611L
 #elif __cplusplus > 201703L
-// Also support P1032R1 in C++20 (but not P0980R1 yet).
-# define __cpp_lib_constexpr_string 201811L
 #endif
 
 #if _GLIBCXX_USE_CXX11_ABI
diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h
index 95fb7c1ee89..c143d74a9ba 100644
--- a/libstdc++-v3/include/bits/char_traits.h
+++ b/libstdc++-v3/include/bits/char_traits.h
@@ -237,12 +237,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if __cplusplus >= 201703L
 
-#if __cplusplus == 201703L
-// Unofficial macro indicating P0426R1 support
-# define __cpp_lib_constexpr_char_traits 201611L
-#else
-// Also support P1032R1 in C++20
+#ifdef __cpp_lib_is_constant_evaluated
+// Unofficial macro indicating P1032R1 support in C++20
 # define __cpp_lib_constexpr_char_traits 201811L
+#else
+// Unofficial macro indicating P0426R1 support in C++17
+# define __cpp_lib_constexpr_char_traits 201611L
 #endif
 
   /**
@@ -253,7 +253,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  Assumes that _CharT is a built-in character type.
    */
   template<typename _CharT>
-    static _GLIBCXX_ALWAYS_INLINE constexpr bool
+    _GLIBCXX_ALWAYS_INLINE constexpr bool
     __constant_string_p(const _CharT* __s)
     {
 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
@@ -276,7 +276,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  Assumes that _CharT is a built-in character type.
    */
   template<typename _CharT>
-    static _GLIBCXX_ALWAYS_INLINE constexpr bool
+    _GLIBCXX_ALWAYS_INLINE constexpr bool
     __constant_char_array_p(const _CharT* __a, size_t __n)
     {
 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index 8d0b2b95f34..5288cc55a0b 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -213,14 +213,18 @@
 // FIXME: #define __cpp_lib_execution 201902L
 #define __cpp_lib_integer_comparison_functions 202002L
 #define __cpp_lib_constexpr_algorithms 201806L
-#define __cpp_lib_constexpr_char_traits 201811L
+#ifdef __cpp_lib_is_constant_evaluated
+# define __cpp_lib_constexpr_char_traits 201811L
+#endif
 #define __cpp_lib_constexpr_complex 201711L
 #define __cpp_lib_constexpr_dynamic_alloc 201907L
 #define __cpp_lib_constexpr_functional 201907L
 #define __cpp_lib_constexpr_iterator 201811L
 #define __cpp_lib_constexpr_memory 201811L
 #define __cpp_lib_constexpr_numeric 201911L
-#define __cpp_lib_constexpr_string 201811L
+#ifdef __cpp_lib_is_constant_evaluated
+# define __cpp_lib_constexpr_string 201811L
+#endif
 #define __cpp_lib_constexpr_string_view 201811L
 #define __cpp_lib_constexpr_tuple 201811L
 #define __cpp_lib_constexpr_utility 201811L

      reply	other threads:[~2021-06-18 10:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-17 21:45 [committed] " Jonathan Wakely
2021-06-18 10:25 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YMx0r5TlohyVMVs3@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).