Mattias Engdegård wrote:
__builtin_mul_overflow(long long) does expand to a call to __mulodi4 when
building for 32-bit x86 even with current Clang; either this function is absent
from libgcc and these platforms do not link to compiler-rt, or their
compiler-rt is somehow built without it. The former seems more likely.
Did the bug report include any Clang/LLVM version information?
No, he built with lots of clangs on lots of platforms, many with clang locally
installed instead of being packaged by the vendor, and he typically didn't give
version numbers.
He did mention that openSUSE 13 had the relevant library here:
/usr/local/ashare/llvm/llvm-20140716/lib/clang/3.5.0/lib/linux/libclang_rt.builtins-x86_64.a
but it wasn't clear whether this library was from an optional package as opposed
to the barebones Clang package. He also said that this particular library "is
absent from some later clang releases" - not sure whether that means absent from
that package in openSUSE, or something else.
In many of the builds he could get clang to work by configuring with CC=clang
LDFLAGS=--rtlib=compiler-rt. However, this wasn't always enough. Eventually he
gave up on using clang to compile GNU sed because of the __builtin_mul_overflow
hassle.
We can safely assume that macOS will use compiler-rt by default rather than
libgcc
In that case I installed the attached, which uses __builtin_mul_overflow with
Clang on macOS.
>From 991c85087796af881057deffa85c0c8e99d76167 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Tue, 27 Aug 2019 04:23:44 -0700
Subject: [PATCH] Speed up INT_MULTIPLY_WRAPV on macOS
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Assume that __builtin_mul_overflow works OK with Clang on macOS.
Mattias EngdegÃ¥rd says itâs safe to assume the relevant library
is always available there.
* lib/intprops.h (_GL_HAS___builtin_mul_overflow):
New temporary internal macro.
(_GL_HAS_BUILTIN_MUL_OVERFLOW):
No need to work around the Clang bug on macOS.
---
ChangeLog | 11 +++++++++++
lib/intprops.h | 13 ++++++++-----
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index e6f5a096a..9976e1833 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2019-08-27 Paul Eggert <[email protected]>
+
+ Speed up INT_MULTIPLY_WRAPV on macOS
+ Assume that __builtin_mul_overflow works OK with Clang on macOS.
+ Mattias EngdegÃ¥rd says itâs safe to assume the relevant library
+ is always available there.
+ * lib/intprops.h (_GL_HAS___builtin_mul_overflow):
+ New temporary internal macro.
+ (_GL_HAS_BUILTIN_MUL_OVERFLOW):
+ No need to work around the Clang bug on macOS.
+
2019-08-25 Paul Eggert <[email protected]>
intprops.h, verify.h: port better to clang
diff --git a/lib/intprops.h b/lib/intprops.h
index ffd737028..2cb6ada51 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -31,6 +31,7 @@
# else
# define _GL_HAS___builtin_add_overflow 0
# endif
+# define _GL_HAS___builtin_mul_overflow _GL_HAS___builtin_add_overflow
# define _GL_TEMPDEF___has_builtin
#endif
@@ -240,12 +241,13 @@
# define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
#endif
-/* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
-#ifdef __clang__
-/* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
-# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
+/* True if __builtin_mul_overflow (A, B, P) works when P is non-null.
+ Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
+#if (__has_builtin (__builtin_mul_overflow) \
+ && (!defined __clang__ || (defined __APPLE__ && defined __MACH__)))
+# define _GL_HAS_BUILTIN_MUL_OVERFLOW 1
#else
-# define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
+# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
#endif
/* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
@@ -589,6 +591,7 @@
#ifdef _GL_TEMPDEF___has_builtin
# undef __has_builtin
# undef _GL_HAS___builtin_add_overflow
+# undef _GL_HAS___builtin_mul_overflow
# undef _GL_TEMPDEF___has_builtin
#endif
--
2.17.1