[llvm] [InstCombine] Add log-pow simplification for FP exponent edge case. (PR #76641)

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 10 17:05:52 PST 2024


================
@@ -12,6 +12,28 @@ define double @log_pow(double %x, double %y) {
   ret double %log
 }
 
+define double @log_powi_const(double %x) {
+; CHECK-LABEL: @log_powi_const(
+; CHECK-NEXT:    [[LOG1:%.*]] = call fast double @llvm.log.f64(double [[X:%.*]])
+; CHECK-NEXT:    [[MUL:%.*]] = fmul fast double [[LOG1]], -3.000000e+00
+; CHECK-NEXT:    ret double [[MUL]]
+;
+  %pow = call fast double @llvm.powi.f64.i32(double %x, i32 -3)
+  %log = call fast double @log(double %pow)
----------------
andykaylor wrote:

@arsenm What flags do you think this should require?

I was talking to @jcranmer-intel about a similar case last week, and it seems like we don't really have a good fit. I suppose 'afn' covers this, but that's a very vaguely defined flag. What we're doing here isn't really approximating either the pow or log calls. I think what we really need is a flag that specifically allows arbitrary algebraic transformations.

GCC will perform this transformation with "-funsafe-math-optimizations -fno-math-errno" but not if I replace "-funsafe-math-optimizations" with the other flags that it implies. I guess it also has its own setting. I don't think GCC has a separate option that would correspond to 'afn' but I don't know the internals well enough to say that's what they're thinking here.

I think we're right to say that the -ffast-math option (assuming we came through clang) should allow this. It should also be allowed with "-funsafe-math-optimizations -fno-math-errno" but it won't be with the current checks.

>From a practical perspective, I'd be happy to say that 'afn' is sufficient here, but from a pedantic perspective, I don't like that.

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


More information about the llvm-commits mailing list