unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* __builtin_assume warnings
@ 2020-08-12  9:38 Mattias Engdegård
  2020-08-15  2:32 ` Paul Eggert
  0 siblings, 1 reply; 8+ messages in thread
From: Mattias Engdegård @ 2020-08-12  9:38 UTC (permalink / raw)
  To: Paul Eggert; +Cc: emacs-devel

Building on macOS with Apple clang version 11.0.0 (clang-1100.0.33.17) now gives a stream of warnings about:

In file included from ../../emacs/src/dispnew.c:27:
../../emacs/src/lisp.h:1812:12: warning: the argument to '__builtin_assume' has
      side effects that will be discarded [-Wassume]
  eassume (0 <= i && i < bool_vector_size (a));
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../emacs/src/lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../../emacs/lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^

We could build with -Wno-assume, or alter the definition of 'assume' to

# define assume(R) _Pragma("clang diagnostic push")                     \
                   _Pragma("clang diagnostic ignored \"-Wassume\"")     \
                   __builtin_assume (R)                                 \
                   _Pragma("clang diagnostic pop")

but neither seems entirely satisfactory.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-12  9:38 __builtin_assume warnings Mattias Engdegård
@ 2020-08-15  2:32 ` Paul Eggert
  2020-08-16  8:23   ` Mattias Engdegård
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Eggert @ 2020-08-15  2:32 UTC (permalink / raw)
  To: Mattias Engdegård; +Cc: emacs-devel

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

On 8/12/20 2:38 AM, Mattias Engdegård wrote:
> ../../emacs/src/lisp.h:1812:12: warning: the argument to '__builtin_assume' has
>        side effects that will be discarded [-Wassume]
>    eassume (0 <= i && i < bool_vector_size (a));

Thanks for mentioning that. I installed the attached, which should pacify clang 
there.


[-- Attachment #2: 0001-Pacify-Apple-clang-11-__builtin_assume.patch --]
[-- Type: text/x-patch, Size: 1625 bytes --]

From 3296c0b2d1a61f099aa1d662b0a36e0b8f78ed28 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 14 Aug 2020 19:29:14 -0700
Subject: [PATCH] Pacify Apple clang 11 __builtin_assume
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Mattias Engdegård in:
https://lists.gnu.org/r/emacs-devel/2020-08/msg00300.html
* src/lisp.h (bool_vector_bitref, bool_vector_set):
Use eassert instead of eassume for bool_vector_size checks.
---
 src/lisp.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/lisp.h b/src/lisp.h
index 2962babb4f..eaf1c6ce6d 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -1809,7 +1809,8 @@ bool_vector_uchar_data (Lisp_Object a)
 INLINE bool
 bool_vector_bitref (Lisp_Object a, EMACS_INT i)
 {
-  eassume (0 <= i && i < bool_vector_size (a));
+  eassume (0 <= i);
+  eassert (i < bool_vector_size (a));
   return !! (bool_vector_uchar_data (a)[i / BOOL_VECTOR_BITS_PER_CHAR]
 	     & (1 << (i % BOOL_VECTOR_BITS_PER_CHAR)));
 }
@@ -1825,11 +1826,11 @@ bool_vector_ref (Lisp_Object a, EMACS_INT i)
 INLINE void
 bool_vector_set (Lisp_Object a, EMACS_INT i, bool b)
 {
-  unsigned char *addr;
-
-  eassume (0 <= i && i < bool_vector_size (a));
-  addr = &bool_vector_uchar_data (a)[i / BOOL_VECTOR_BITS_PER_CHAR];
+  eassume (0 <= i);
+  eassert (i < bool_vector_size (a));
 
+  unsigned char *addr
+    = &bool_vector_uchar_data (a)[i / BOOL_VECTOR_BITS_PER_CHAR];
   if (b)
     *addr |= 1 << (i % BOOL_VECTOR_BITS_PER_CHAR);
   else
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-15  2:32 ` Paul Eggert
@ 2020-08-16  8:23   ` Mattias Engdegård
  2020-08-17 14:05     ` Mattias Engdegård
  0 siblings, 1 reply; 8+ messages in thread
From: Mattias Engdegård @ 2020-08-16  8:23 UTC (permalink / raw)
  To: Paul Eggert; +Cc: emacs-devel

15 aug. 2020 kl. 04.32 skrev Paul Eggert <eggert@cs.ucla.edu>:
> 
> On 8/12/20 2:38 AM, Mattias Engdegård wrote:
>> ../../emacs/src/lisp.h:1812:12: warning: the argument to '__builtin_assume' has
>>       side effects that will be discarded [-Wassume]
>>   eassume (0 <= i && i < bool_vector_size (a));
> 
> Thanks for mentioning that. I installed the attached, which should pacify clang there.

Much better, thank you! (For the record, I believe it applies to any Clang version with that builtin and warning, not just the Apple 11 version.)




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-16  8:23   ` Mattias Engdegård
@ 2020-08-17 14:05     ` Mattias Engdegård
  2020-08-17 22:11       ` Paul Eggert
  0 siblings, 1 reply; 8+ messages in thread
From: Mattias Engdegård @ 2020-08-17 14:05 UTC (permalink / raw)
  To: Paul Eggert; +Cc: emacs-devel

16 aug. 2020 kl. 10.23 skrev Mattias Engdegård <mattiase@acm.org>:
> 
> 15 aug. 2020 kl. 04.32 skrev Paul Eggert <eggert@cs.ucla.edu>:
>> 
>> On 8/12/20 2:38 AM, Mattias Engdegård wrote:
>>> ../../emacs/src/lisp.h:1812:12: warning: the argument to '__builtin_assume' has
>>>      side effects that will be discarded [-Wassume]
>>>  eassume (0 <= i && i < bool_vector_size (a));
>> 
>> Thanks for mentioning that. I installed the attached, which should pacify clang there.

Actually, there are warnings in more places. Looks like Clang does not yet look inside inline functions for -Wassume.

frame.c:1686:12: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
  eassume (CONSP (Vframe_list));
           ^~~~~~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^
frame.c:1710:12: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
  eassume (CONSP (Vframe_list));
           ^~~~~~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^
frame.c:2020:16: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
      eassume (CONSP (Vframe_list));
               ^~~~~~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^
character.c:814:16: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
      eassume (CHARACTERP (args[i]));
               ^~~~~~~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^
alloc.c:1935:12: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
  eassume (STRING_MULTIBYTE (string));
           ^~~~~~~~~~~~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^
data.c:3153:12: warning: the argument to '__builtin_assume' has side effects
      that will be discarded [-Wassume]
  eassume (FIXNUMP (value));
           ^~~~~~~~~~~~~~~
./lisp.h:182:32: note: expanded from macro 'eassume'
# define eassume(cond) assume (cond)
                               ^~~~
../lib/verify.h:305:38: note: expanded from macro 'assume'
# define assume(R) __builtin_assume (R)
                                     ^




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-17 14:05     ` Mattias Engdegård
@ 2020-08-17 22:11       ` Paul Eggert
  2020-08-18  7:11         ` Mattias Engdegård
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Eggert @ 2020-08-17 22:11 UTC (permalink / raw)
  To: Mattias Engdegård; +Cc: emacs-devel

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

On 8/17/20 7:05 AM, Mattias Engdegård wrote:
>   Looks like Clang does not yet look inside inline functions for -Wassume.

I guess that for that diagnostic, Clang assumes that every function call has a 
side effect, which is pretty silly. I worked around the bug (and by an another 
bogus-warning bug I ran into on Fedora 31's Clang 9.0.1) by installing the attached.

[-- Attachment #2: 0001-Update-from-Gnulib.patch --]
[-- Type: text/x-patch, Size: 2707 bytes --]

From 352b7dede0b3a28024a41a3da1c340859b110665 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Mon, 17 Aug 2020 15:05:05 -0700
Subject: [PATCH] Update from Gnulib
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This incorporates:
2020-08-17 verify: avoid __built_assume on Clang
2020-08-17 libc-config: avoid Clang’s __diagnose_if__
* lib/cdefs.h, lib/verify.h: Copy from Gnulib.
---
 lib/cdefs.h  |  2 +-
 lib/verify.h | 18 +++++++-----------
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/lib/cdefs.h b/lib/cdefs.h
index 0cc27806a1..b1870fd0a9 100644
--- a/lib/cdefs.h
+++ b/lib/cdefs.h
@@ -148,7 +148,7 @@ #define __bos0(ptr) __builtin_object_size (ptr, 0)
 # define __warnattr(msg) __attribute__((__warning__ (msg)))
 # define __errordecl(name, msg) \
   extern void name (void) __attribute__((__error__ (msg)))
-#elif __glibc_clang_has_attribute (__diagnose_if__)
+#elif __glibc_clang_has_attribute (__diagnose_if__) && 0 /* fails on Fedora 31 with Clang 9.  */
 # define __warndecl(name, msg) \
   extern void name (void) __attribute__((__diagnose_if__ (1, msg, "warning")))
 # define __warnattr(msg) __attribute__((__diagnose_if__ (1, msg, "warning")))
diff --git a/lib/verify.h b/lib/verify.h
index 0ba8d57907..d485a0283a 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -246,13 +246,6 @@ #define _GL_VERIFY_TRUE(R, DIAGNOSTIC) \
 
 /* @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
@@ -312,11 +305,14 @@ #define verify_expr(R, E) \
 
    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.  */
+   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_ASSUME
-# define assume(R) __builtin_assume (R)
-#elif _GL_HAS_BUILTIN_UNREACHABLE
+#if _GL_HAS_BUILTIN_UNREACHABLE
 # define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
 #elif 1200 <= _MSC_VER
 # define assume(R) __assume (R)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-17 22:11       ` Paul Eggert
@ 2020-08-18  7:11         ` Mattias Engdegård
  2020-08-18 22:53           ` Paul Eggert
  0 siblings, 1 reply; 8+ messages in thread
From: Mattias Engdegård @ 2020-08-18  7:11 UTC (permalink / raw)
  To: Paul Eggert; +Cc: emacs-devel

18 aug. 2020 kl. 00.11 skrev Paul Eggert <eggert@cs.ucla.edu>:

> I guess that for that diagnostic, Clang assumes that every function call has a side effect, which is pretty silly. I worked around the bug (and by an another bogus-warning bug I ran into on Fedora 31's Clang 9.0.1) by installing the attached.

Thank you, but aren't we throwing the baby out with the bathwater? Wouldn't -Wno-assume, either locally (clang pragmas around __builtin_assume) or globally (configure?) be more effective?




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-18  7:11         ` Mattias Engdegård
@ 2020-08-18 22:53           ` Paul Eggert
  2020-08-19  8:01             ` Mattias Engdegård
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Eggert @ 2020-08-18 22:53 UTC (permalink / raw)
  To: Mattias Engdegård; +Cc: emacs-devel

On 8/18/20 12:11 AM, Mattias Engdegård wrote:

> Wouldn't -Wno-assume, either locally (clang pragmas around __builtin_assume) or globally (configure?) be more effective?

No, it's the other way around at least for me: having 'assume' use Clang's 
__builtin_assume makes 'assume' slower. Without __builtin_assume, 'assume' falls 
back on __builtin_unreachable, and Clang generates better code for 
__builtin_unreachable than it does __builtin_assume. For the following code:

int x;
static int f (void) { return x; }
int g (void) { __builtin_assume (!f ()); return f (); }
int h (void) { if (f ()) __builtin_unreachable (); return f (); }

clang -O2 generates suboptimal machine code for g (the generated code loads from 
'x') and better machine code for h (the generated code returns 0 without loading 
from 'x'). This is clang version 9.0.1 (Fedora 9.0.1-2.fc31) on 
x86_64-unknown-linux-gnu.

Perhaps someday the Clang folks will get their act together in this department, 
but in the meantime __builtin_unreachable is a perfectly adequate substitute.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: __builtin_assume warnings
  2020-08-18 22:53           ` Paul Eggert
@ 2020-08-19  8:01             ` Mattias Engdegård
  0 siblings, 0 replies; 8+ messages in thread
From: Mattias Engdegård @ 2020-08-19  8:01 UTC (permalink / raw)
  To: Paul Eggert; +Cc: emacs-devel

19 aug. 2020 kl. 00.53 skrev Paul Eggert <eggert@cs.ucla.edu>:

> No, it's the other way around at least for me: having 'assume' use Clang's __builtin_assume makes 'assume' slower. Without __builtin_assume, 'assume' falls back on __builtin_unreachable, and Clang generates better code for __builtin_unreachable than it does __builtin_assume.

Right you are; as far as I can tell, __builtin_assume is strictly less useful. There does not seem to be any point in using it at this time. The only advantage over if(!x)__builtin_unreachable() appears to be that the argument isn't actually evaluated, but I'm not sure when that property would be useful.

Thank you for clearing that up.




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-08-19  8:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-12  9:38 __builtin_assume warnings Mattias Engdegård
2020-08-15  2:32 ` Paul Eggert
2020-08-16  8:23   ` Mattias Engdegård
2020-08-17 14:05     ` Mattias Engdegård
2020-08-17 22:11       ` Paul Eggert
2020-08-18  7:11         ` Mattias Engdegård
2020-08-18 22:53           ` Paul Eggert
2020-08-19  8:01             ` Mattias Engdegård

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).