[llvm-dev] [FP] Constant folding math library functions

Kaylor, Andrew via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 16 12:23:36 PDT 2019


Hi everyone,

I noticed today that LLVM's constant folding of math library functions can lead to minor differences in results. A colleague sent me the following test case which demonstrates the issue:

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

typedef union {
  double d;
  unsigned long long i;
} my_dbl;

int main(void) {
  my_dbl res, x;
  x.i = 0x3feeb39556255de2ull;
  res.d = tanh(x.d);
  printf("tanh(%f) = %f = %016LX\n", x.d, res.d, res.i);
  return 0;
}

Compiling with "clang -O2 -g0 -emit-llvm" I get this:

define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = tail call double @tanh(double 0x3FEEB39556255DE2) #2
  %2 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0),
                                                             double 0x3FEEB39556255DE2, double 0x3FE7CF009CE7F169,
                                                             i64 4604876745549017449)
  ret i32 0
}

We're still calling 'tanh' but all the values passed to printf are constant folded. The constant folding is based on a call to tanh made by the compiler. The problem with this is that if I am linking my program against a different version of the math library than was used by the compiler I may get a different result.

I can prevent this constant folding with either the 'nobuiltin' or 'strictfp' attribute. However, it seems to me like this optimization should really be checking the 'afn' fast math flag.

Opinions?

Thanks,
Andy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190416/aa3a458f/attachment.html>


More information about the llvm-dev mailing list