[llvm] SimplifyLibCalls: Don't require ldexp to emit intrinsic in exp2 combine (PR #92707)

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 11:23:14 PDT 2024


andykaylor wrote:

We definitely have a mess with regard to our handling of math library functions. We're nowhere near consistent behavior.

I'm not sure if this is what Matt was alluding to, but the gcc documentation for fno-builtin-* says, "Don’t recognize built-in functions **that do not begin with ‘__builtin_’ as prefix**." (emphasis added) The result of this is that if you call __builtin_ldexp even when compiling with -fno-builtin-ldexp, we will happily translate it to a call to the llvm.ldexp intrinsic, and on X86 that gets lowered to a call to ldexp() (which may or may not be the library call). 

https://godbolt.org/z/95Kb6PjnW

You can make a case that this is user error -- if they didn't want the library function to be called, they shouldn't have used the builtin. It would be much better if we lowered the intrinsics to calls to a compiler runtime library function that we could be certain existed and had control over the accuracy of. This would allow us to actual implement things the way the LLVM IR definition says we will (i.e. not setting errno).

But that's not the current situation, and the example above is different from the front end replacing one call with another on the user's behalf. If the user has passed `-fno-builtin-ldexp` and then called `exp2((double)n)` it seems like a very good bet that they really don't want the ldexp library function to be called.

https://github.com/llvm/llvm-project/pull/92707


More information about the llvm-commits mailing list