Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

incorrect optimization with signaling NaN and implicit conversion in fabs: missing exception #38085

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR39112
Status REOPENED
Importance P normal
Reported by Vincent Lefevre (vincent-llvm@vinc17.net)
Reported on 2018-09-28 04:48:14 -0700
Last modified on 2018-09-29 07:49:48 -0700
Version trunk
Hardware PC Linux
CC efriedma@quicinc.com, hfinkel@anl.gov, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
With a code that does something like

  float f = fabs (double_snan)

one does not get the FE_INVALID exception with -O.

Details:

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

typedef union { float f; uint32_t i; } ieee_float_t;

int main (void)
{
  volatile ieee_float_t x, y;
  volatile double d;

  x.i = 0x7f800001;
  printf ("0. %x (%g)\n", x.i, x.f);

  feclearexcept (FE_INVALID);
  y.f = fabs (x.f);
  if (fetestexcept (FE_INVALID))
    printf ("2. FE_INVALID\n");
  printf ("2. %x (%g)\n", y.i, y.f);

  return 0;
}

yields:

cventin% clang-8 snan.c -o snan -lm
cventin% ./snan
0. 7f800001 (nan)
2. FE_INVALID
2. 7fc00001 (nan)

which is correct (due to the float-to-double conversion of sNaN), but

cventin% clang-8 snan.c -o snan -lm -O
cventin% ./snan
0. 7f800001 (nan)
2. 7f800001 (nan)

The FE_INVALID exception is missing, and the result is a signaling NaN instead
of a quiet NaN.

Tested with the clang-8 1:8~svn342638-1 Debian package (x86_64 architecture).
Quuxplusone commented 6 years ago
(In reply to Vincent Lefevre from comment #0)
> With a code that does something like
>
>   float f = fabs (double_snan)
>
> one does not get the FE_INVALID exception with -O.

Oops, I meant:

  float f = fabs (float_snan)

like in the details (I have not tried with double).
Quuxplusone commented 6 years ago

By default, clang assumes you don't access the floating-point environment. Work to implement the FENV_ACCESS pragma is ongoing.

_This bug has been marked as a duplicate of bug 8100_

Quuxplusone commented 6 years ago
No need to access the floating-point environment to reproduce the bug:

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

typedef union { float f; uint32_t i; } ieee_float_t;

int main (void)
{
  volatile ieee_float_t x, y;

  x.i = 0x7f800001;
  printf ("0. %x (%g)\n", x.i, x.f);

  y.f = fabs (x.f);
  printf ("2. %x (%g)\n", y.i, y.f);

  return 0;
}

cventin% clang-8 snan.c -o snan -lm
cventin% ./snan
0. 7f800001 (nan)
2. 7fc00001 (nan)
cventin% clang-8 snan.c -o snan -lm -O
cventin% ./snan
0. 7f800001 (nan)
2. 7f800001 (nan)

With -O, one gets a signaling NaN instead of a quiet NaN.

The bug disappears if one explicitly uses a volatile double as an intermediate
variable (since fabs returns a double, this should not change anything):

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

typedef union { float f; uint32_t i; } ieee_float_t;

int main (void)
{
  volatile ieee_float_t x, y;
  volatile double d;

  x.i = 0x7f800001;
  printf ("0. %x (%g)\n", x.i, x.f);

  d = fabs (x.f);
  y.f = d;
  printf ("2. %x (%g)\n", y.i, y.f);

  return 0;
}

cventin% clang-8 snan.c -o snan -lm
cventin% ./snan
0. 7f800001 (nan)
2. 7fc00001 (nan)
cventin% clang-8 snan.c -o snan -lm -O
cventin% ./snan
0. 7f800001 (nan)
2. 7fc00001 (nan)