bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: bug-gnulib@gnu.org
Subject: Re: Clang __builtin_assume
Date: Sun, 23 Aug 2020 01:01:40 +0200	[thread overview]
Message-ID: <8048804.nLtAeEzrYs@omega> (raw)
In-Reply-To: <4d87cd4d-f02c-761c-216e-65157754d039@cs.ucla.edu>

> This is OK, but please also mention in the comment that __builtin_assume 
> generates better code for Clang 8 than __builtin_unreachable does, as that's the 
> only reason to put up with all this mess.

Done, as follows:


2020-08-22  Bruno Haible  <bruno@clisp.org>

	verify: Do use __builtin_assume on clang.
	* lib/verify.h (assume): Use clang’s __builtin_assume, with a temporary
	variable in a statement expression.

diff --git a/lib/verify.h b/lib/verify.h
index d485a02..04bb2df 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -246,6 +246,13 @@ template <int w>
 
 /* @assert.h omit start@  */
 
+#if defined __has_builtin
+/* <https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions> */
+# define _GL_HAS_BUILTIN_ASSUME __has_builtin (__builtin_assume)
+#else
+# define _GL_HAS_BUILTIN_ASSUME 0
+#endif
+
 #if 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))
 # define _GL_HAS_BUILTIN_TRAP 1
 #elif defined __has_builtin
@@ -305,14 +312,21 @@ template <int w>
 
    Although assuming R can help a compiler generate better code or
    diagnostics, performance can suffer if R uses hard-to-optimize
-   features such as function calls not inlined by the compiler.
-
-   Avoid Clang’s __builtin_assume, as clang 9.0.1 -Wassume can
-   generate a bogus diagnostic "the argument to '__builtin_assume' has
-   side effects that will be discarded" even when the argument has no
-   side effects.  */
-
-#if _GL_HAS_BUILTIN_UNREACHABLE
+   features such as function calls not inlined by the compiler.  */
+
+/* Use __builtin_assume in preference to __builtin_unreachable, because
+   in clang versions 8.0.x and older, the definition based on
+   __builtin_assume has an effect on optimizations, whereas the definition
+   based on __builtin_unreachable does not.  (GCC so far has only
+   __builtin_unreachable.)  */
+#if _GL_HAS_BUILTIN_ASSUME
+/* Use a temporary variable, to avoid a clang warning
+   "the argument to '__builtin_assume' has side effects that will be discarded"
+   if R contains invocations of functions not marked as 'const'.  */
+# define assume(R) \
+    ((void) ({ __typeof__ (R) _gl_verify_temp = (R); \
+               __builtin_assume (_gl_verify_temp); }))
+#elif _GL_HAS_BUILTIN_UNREACHABLE
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)



  reply	other threads:[~2020-08-22 23:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17 22:02 fixes for Clang builtins when compiling Emacs on Fedora Paul Eggert
2020-08-17 23:37 ` Bruno Haible
2020-08-18  1:10   ` Paul Eggert
2020-08-22 10:30     ` Clang __built_assume Bruno Haible
2020-08-22 17:32       ` Paul Eggert
2020-08-22 23:01         ` Bruno Haible [this message]
2020-08-23 13:46 ` clang's __diagnose_if__ and glibc fortify Bruno Haible

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

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=8048804.nLtAeEzrYs@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /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).