[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
totalorder, totalorderf: Avoid miscompilation by clang on OpenBSD/i386
From: |
Bruno Haible |
Subject: |
totalorder, totalorderf: Avoid miscompilation by clang on OpenBSD/i386 |
Date: |
Tue, 09 Apr 2024 14:19:13 +0200 |
On OpenBSD 7.5/i386, the two tests still fail, except when compiling
without optimization (e.g. with CFLAGS="-ggdb").
So, it's a compiler optimization bug, as can be seen by the code
generated by the OpenBSD compiler (a clang 16.0.6 derivative).
Cf. the attached totalorderf.O0.s, totalorderf.O2.s.
The patch below avoids the compiler bug, cf. the attached
totalorderf.O2-fixed.s.
2024-04-09 Bruno Haible <bruno@clisp.org>
totalorder, totalorderf: Avoid miscompilation by clang on OpenBSD/i386.
* lib/totalorder.c (totalorder): Insert a compiler optimization barrier.
* lib/totalorderf.c (totalorderf): Likewise.
diff --git a/lib/totalorder.c b/lib/totalorder.c
index 635e3cb276..eaef9a582d 100644
--- a/lib/totalorder.c
+++ b/lib/totalorder.c
@@ -57,6 +57,11 @@ totalorder (double const *x, double const *y)
xu.f = *x;
yu.f = *y;
#else
+# ifdef __clang__
+ /* Prevent clang 16.0.6 on OpenBSD 7.5 from reusing the values of *x and *y
+ (fetched above) in optimized inlined memcpy expansions. */
+ __asm__ __volatile__ ("" : : : "memory");
+# endif
/* On 32-bit x86 processors, as well as on x86_64 processors with
CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through
an 'fldl' instruction, which converts a signalling NaN to a quiet NaN. See
diff --git a/lib/totalorderf.c b/lib/totalorderf.c
index 75024b6839..f5f1b40112 100644
--- a/lib/totalorderf.c
+++ b/lib/totalorderf.c
@@ -57,6 +57,11 @@ totalorderf (float const *x, float const *y)
xu.f = *x;
yu.f = *y;
#else
+# ifdef __clang__
+ /* Prevent clang 16.0.6 on OpenBSD 7.5 from reusing the values of *x and *y
+ (fetched above) in optimized inlined memcpy expansions. */
+ __asm__ __volatile__ ("" : : : "memory");
+# endif
/* On 32-bit x86 processors, as well as on x86_64 processors with
CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through
an 'flds' instruction, which converts a signalling NaN to a quiet NaN. See
totalorderf.O0.s
Description: Text document
totalorderf.O2.s
Description: Text document
totalorderf.O2-fixed.s
Description: Text document