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

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 19 00:06:42 PST 2024


================
@@ -334,6 +337,59 @@ bool AMDGPUPostLegalizerCombinerImpl::matchRcpSqrtToRsq(
   return false;
 }
 
+bool AMDGPUPostLegalizerCombinerImpl::matchFDivSqrt(
+    MachineInstr &MI,
+    std::function<void(MachineIRBuilder &)> &MatchInfo) const {
+
+  // TODO: Can I match fdiv 1.0 / sqrt(x) from here?
+  // My apologies, this code is still a mess. Trying to figure out
+  // what value MI should hold when getting to this point
----------------
Pierre-vh wrote:

If you do MIR patterns this code goes away but I'll review it:

> Trying to figure out what value MI should hold when getting to this point

MI here should be a MachineInstr for the root of the match. There was a mistake in the TableGen: `(wip_match_opcode G_FSQRT, G_FDIV)` - this should only contain the root instruction you're trying to match, so `G_FDIV`.

If you use that, the combiner will call your function every time it sees a `G_FDIV`, and `MI` would be any  `G_FDIV` in the code

Now your matcher code needs to:

- Check that `MI` has FmContract
- Check that the first operand of `MI` is 1.0 
- Check that the second operand of `MI` is an instruction that's a `G_FSQRT` and also has FmContract.

`GlobalISel/Utils.h` has helpers for the last 2 points - to get a `ConstantFP` and to find a Def of an instruction, ignoring any `COPY` in the process. You could also use `mi_match` but it's a bit more complicated

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


More information about the llvm-commits mailing list