[llvm] GlobalISel needs fdiv 1 / sqrt(x) to rsq combine (PR #78673)

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 28 22:22:12 PST 2024


================
@@ -33,6 +33,14 @@ def rcp_sqrt_to_rsq : GICombineRule<
          [{ return matchRcpSqrtToRsq(*${rcp}, ${matchinfo}); }]),
   (apply [{ Helper.applyBuildFn(*${rcp}, ${matchinfo}); }])>;
 
+def fdiv_1_by_sqrt_to_rsq : GICombineRule<
+  (defs root:$root),
+  (match (G_FSQRT $sqrt, $x, (MIFlags FmContract)),
+         (G_FCONSTANT $one, $fpimm),
+         (G_FDIV $dst, $one, $sqrt, (MIFlags FmContract)):$root,
+         [{ return ${fpimm}.getFPImm()->isExactlyValue(1.0)
+            || ${fpimm}.getFPImm()->isExactlyValue(-1.0); }]),
----------------
Pierre-vh wrote:

> > Could we do -1/sqrt(x) to fneg(rsq(x))?
> 
> Yes, especially since fneg is cheap since it can be folded into lots of other (AMDGPU) instructions. But it does make me wonder again, if you're going to generalise, why not generalise to any value? I.e. y/sqrt(x) -> y*rsq(x)

I agree generalizing makes sense if you're going to handle more than 1/sqrt - you can simplify the apply function at least to just emit `y * rsq(x)` and let other combine eliminate a `1*rsq`, or transform `-1*rsq` into `fneg(rsq)`

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


More information about the llvm-commits mailing list