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

Finkel, Hal J. via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 16 13:01:10 PDT 2019

Hi, Andy,

This is somewhat tricky. 'afn' is for approximate functions, to "allow substitution of approximate calculations for functions", but in this case, the answers aren't any more approximate than the original function calls. Different, but likely no less accurate. This has long caused these kinds of subtle differences when cross compiling, etc. but it's not clear what the best thing to do actually is. Users often want the constant folding, and I've certainly seen code where the performance depends critically on it, and yet, the compiler will likely never be able to exactly replicate the behavior of whatever libm implementation is used at runtime. Maybe having a dedicated flag to disable just this behavior, aside from suggesting that users use -fno-builtin=..., would be useful for users who depend on the compiler not folding these kinds of expressions in ways that might differ from their runtime libm behavior?


Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

From: llvm-dev <llvm-dev-bounces at lists.llvm.org> on behalf of Kaylor, Andrew via llvm-dev <llvm-dev at lists.llvm.org>
Sent: Tuesday, April 16, 2019 2:23 PM
To: llvm-dev
Subject: [llvm-dev] [FP] Constant folding math library functions

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.




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

More information about the llvm-dev mailing list