public inbox for gcc-patches@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] libstdc++: Simplify detection of built-in functions
Date: Tue, 1 Dec 2020 14:15:14 +0000	[thread overview]
Message-ID: <20201201141514.GA2754940@redhat.com> (raw)

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

This fixes a regression affecting the Intel compiler. Because that
compiler defines __GNUC__ to match whatever version of GCC it finds on
the host system, it might claim to be a brand new GCC despite not
actually supporting all the built-ins that the latest GCC supports. This
means the config checks for __GNUC__ don't work. Most recently this
broke when r11-3569-g73ae6eb572515ad627b575a7fbdfdd47a4368e1c switched
us from using __is_same_as to __is_same when __GNUC__ >= 11.

Because __has_builtin is supported by all of GCC, Clang, and Intel we can
use that to reliably detect whether a given built-in is supported,
instead of hardcoding anything based on __GNUC__. The big caveat is
that for versions of Clang <= 9.0.0 and for (as far as I can tell) all
released versions of Intel icc, __has_builtin only evaluates to true for
built-ins with a name starting "__builtin_". For __is_aggregate,
__is_same, and __has_unique_object_representations it's necessary to use
__is_identifier to check if it's a valid identifeir token instead.

The solution used in this patch is to define _GLIBCXX_HAS_BUILTIN and
use that instead of using __has_builtin directly. For compilers that
define __is_identifier as well as __has_builtin we use both, so that if
__has_builtin evaluates to false we try again using !__is_identifier.

libstdc++-v3/ChangeLog:

	* include/bits/c++config (_GLIBCXX_HAS_BUILTIN): Define macro to
	work around different implementations of __has_builtin.
	(_GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP)
	(_GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE)
	(_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED)
	(_GLIBCXX_HAVE_BUILTIN_IS_SAME, _GLIBCXX_HAVE_BUILTIN_LAUNDER):
	Define using _GLIBCXX_HAS_BUILTIN.

Tested powerpc64le-linux. Committed to trunk.


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

commit 6aa12274007bccbae2691a9d336c37fe167bb535
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Dec 1 14:14:18 2020

    libstdc++: Simplify detection of built-in functions
    
    This fixes a regression affecting the Intel compiler. Because that
    compiler defines __GNUC__ to match whatever version of GCC it finds on
    the host system, it might claim to be a brand new GCC despite not
    actually supporting all the built-ins that the latest GCC supports. This
    means the config checks for __GNUC__ don't work. Most recently this
    broke when r11-3569-g73ae6eb572515ad627b575a7fbdfdd47a4368e1c switched
    us from using __is_same_as to __is_same when __GNUC__ >= 11.
    
    Because __has_builtin is supported by all of GCC, Clang, and Intel we can
    use that to reliably detect whether a given built-in is supported,
    instead of hardcoding anything based on __GNUC__. The big caveat is
    that for versions of Clang <= 9.0.0 and for (as far as I can tell) all
    released versions of Intel icc, __has_builtin only evaluates to true for
    built-ins with a name starting "__builtin_". For __is_aggregate,
    __is_same, and __has_unique_object_representations it's necessary to use
    __is_identifier to check if it's a valid identifeir token instead.
    
    The solution used in this patch is to define _GLIBCXX_HAS_BUILTIN and
    use that instead of using __has_builtin directly. For compilers that
    define __is_identifier as well as __has_builtin we use both, so that if
    __has_builtin evaluates to false we try again using !__is_identifier.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/c++config (_GLIBCXX_HAS_BUILTIN): Define macro to
            work around different implementations of __has_builtin.
            (_GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP)
            (_GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE)
            (_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED)
            (_GLIBCXX_HAVE_BUILTIN_IS_SAME, _GLIBCXX_HAVE_BUILTIN_LAUNDER):
            Define using _GLIBCXX_HAS_BUILTIN.

diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config
index 2e6c880ad95..27302ed392e 100644
--- a/libstdc++-v3/include/bits/c++config
+++ b/libstdc++-v3/include/bits/c++config
@@ -653,35 +653,36 @@ namespace std
 #define _GLIBCXX_USE_FLOAT128
 #endif
 
-#if __GNUC__ >= 7
-// Assume these are available if the compiler claims to be a recent GCC:
+#ifdef __has_builtin
+# ifdef __is_identifier
+// Intel and older Clang require !__is_identifier for some built-ins:
+#  define _GLIBCXX_HAS_BUILTIN(B) __has_builtin(B) || ! __is_identifier(B)
+# else
+#  define _GLIBCXX_HAS_BUILTIN(B) __has_builtin(B)
+# endif
+#endif
+
+#if _GLIBCXX_HAS_BUILTIN(__has_unique_object_representations)
 # define _GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP 1
+#endif
+
+#if _GLIBCXX_HAS_BUILTIN(__is_aggregate)
 # define _GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE 1
+#endif
+
+#if _GLIBCXX_HAS_BUILTIN(__builtin_is_constant_evaluated)
+#  define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
+#endif
+
+#if _GLIBCXX_HAS_BUILTIN(__is_same)
+#  define _GLIBCXX_HAVE_BUILTIN_IS_SAME 1
+#endif
+
+#if _GLIBCXX_HAS_BUILTIN(__builtin_launder)
 # define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
-# if __GNUC__ >= 9
-#  define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
-# endif
-# if __GNUC__ >= 11
-#  define _GLIBCXX_HAVE_BUILTIN_IS_SAME 1
-# endif
-#elif defined(__is_identifier) && defined(__has_builtin)
-// For non-GNU compilers:
-# if ! __is_identifier(__has_unique_object_representations)
-#  define _GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP 1
-# endif
-# if ! __is_identifier(__is_aggregate)
-#  define _GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE 1
-# endif
-# if __has_builtin(__builtin_launder)
-#  define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
-# endif
-# if __has_builtin(__builtin_is_constant_evaluated)
-#  define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
-# endif
-# if ! __is_identifier(__is_same)
-#  define _GLIBCXX_HAVE_BUILTIN_IS_SAME 1
-# endif
-#endif // GCC
+#endif
+
+#undef _GLIBCXX_HAS_BUILTIN
 
 #if _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
 # define __glibcxx_assert_1(_Condition)		\

                 reply	other threads:[~2020-12-01 14:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20201201141514.GA2754940@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).