llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] __builtin_isgreater/__builtin_isgreaterequal/__builtin_isless/__builtin_islessequal/__builtin_islessgreater does not handle unordered comparisons

Open zatrazz opened this issue 11 months ago • 8 comments

The following code triggers an invalid exception on ABIs that suppors 128 bit float types (either by long double or extended types like __float128):

$ cat t.c

#include <math.h>
#include <stdio.h>
#include <fenv.h>

int main (int argc, char *argtv[])
{
  feenableexcept (FE_INVALID);

  long double qNaN = __builtin_nanl ("");
  long double zero = 0;

  int r = __builtin_isgreater (zero, qNaN);
  printf ("%d\n", r);
}
$ clang -target aarch64-linux-gnu t.c -o t -D_GNU_SOURCE -lm
$ ./t
Floating point exception (core dumped)

It is not a problem for compiler-rt because it does not support floating point exceptions; but is an issue when using the gcc libgcc. This is triggered by the glibc testsuite when either using clang as the test compiler [1] or by using clang as the built compiler [2].

My plan is to disable an optimization on glibc math.h where isfunction is routed to its builtin conterpart, as least for types different than float and double.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=33322 [2] https://sourceware.org/git/?p=glibc.git;a=commit;h=be48ae12b10b0556895e356c3ae34b8ca229e61b

zatrazz avatar Dec 16 '25 15:12 zatrazz

CC @tahonermann @zahiraam

shafik avatar Dec 16 '25 15:12 shafik

@llvm/issue-subscribers-clang-frontend

Author: Adhemerval Zanella (zatrazz)

The following code triggers an invalid exception on ABIs that suppors 128 bit float types (either by long double or extended types like __float128):

$ cat t.c

#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fenv.h&gt;

int main (int argc, char *argtv[])
{
  feenableexcept (FE_INVALID);

  long double qNaN = __builtin_nanl ("");
  long double zero = 0;

  int r = __builtin_isgreater (zero, qNaN);
  printf ("%d\n", r);
}
$ clang -target aarch64-linux-gnu t.c -o t -D_GNU_SOURCE -lm
$ ./t
Floating point exception (core dumped)

It is not a problem for compiler-rt because it does not support floating point exceptions; but is an issue when using the gcc libgcc. This is triggered by the glibc testsuite when either using clang as the test compiler [1] or by using clang as the built compiler [2].

My plan is to disable an optimization on glibc math.h where isfunction is routed to its builtin conterpart, as least for types different than float and double.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=33322 [2] https://sourceware.org/git/?p=glibc.git;a=commit;h=be48ae12b10b0556895e356c3ae34b8ca229e61b

llvmbot avatar Dec 17 '25 01:12 llvmbot

This is another instance where fenv access is not enabled (we desperately need the warning for this implemented), so calling feenableexcept is illegal

arsenm avatar Dec 18 '25 20:12 arsenm

To clarify, Clang requires explicit -ftrapping-math or -ffp-model=strict on the command-line, or #pragma stdc fenv_access on in the source, for any code which accesses the floating-point environment, so this is not a bug.

Adding a workaround to glibc to avoid the use of these builtins in the headers is not the right thing to do.

jyknight avatar Dec 19 '25 17:12 jyknight

Neither #pragma stdc fenv_access on nor -ftrapping-math/-ffp-model=strict help in this specific case. I found out this after adding experimental support to build glibc with clang, and glibc explicit uses -ftrapping-math. The issue is clang does not check if the argument is unordered, diffrerent than gcc [1] (the extra __unordtf2 call).

[1] https://godbolt.org/z/ddTeo4G96

zatrazz avatar Dec 19 '25 17:12 zatrazz

That doesn't look like it ought to require an extra call, as Clang is using __gttf2, which I believe is already expected to implement the desired semantics, per its documentation.

Am I missing something here?

jyknight avatar Dec 19 '25 18:12 jyknight

The problem is for libcs that aims to support MATH_ERREXCEPT. Implementing isgreater with direct call to __gttf2 will raise FE_INVALID if the rtlib is libgcc. You can check that libgcc explicit handle this [1] where FP_SET_EXCEPTION (FP_EX_INVALID) is called by FP_CMP_Q macro (issued by __getf2).

It is not really a problem for compiler-rt because afaik it does support exceptions for the software implementations. However, if users are targetting libgcc I would expect compiler to respect MATH_ERREXCEPT.

[1] https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libgcc/soft-fp/op-common.h;h=090028f8c7dfed76771598e2188992bee44e13fa;hb=HEAD#l1230

zatrazz avatar Dec 19 '25 18:12 zatrazz

At the LLVM IR level, we have two intrinsics: fcmp, and fcmps. Currently TargetLowering::softenSetCCOperands ignores the difference. It's a bit of a pain to fix this because there is no non-signalling libcall, and we don't support inserting control-flow at the point in the compiler where this lowering happens.

efriedma-quic avatar Dec 19 '25 21:12 efriedma-quic