[llvm] [AMDGPU] Use V_FMAC_F64 in "if (cond) a -= c" (PR #168710)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 30 14:53:45 PST 2026


================
@@ -16284,18 +16284,75 @@ SITargetLowering::performAddCarrySubCarryCombine(SDNode *N,
   return SDValue();
 }
 
-SDValue SITargetLowering::performFAddCombine(SDNode *N,
-                                             DAGCombinerInfo &DCI) const {
-  if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
+// Create fma((cond ? -1.0, 0.0), c, a)
+static SDValue createConditionalSubToFMA(SelectionDAG &DAG, SDLoc SL, EVT VT,
+                                         SDValue Cond, SDValue C, SDValue A) {
+  // FIXME: This should work but currently generates incorrect code.
+  if (isa<ConstantFPSDNode>(C) || isa<ConstantSDNode>(C))
     return SDValue();
+  const SDValue MinusOne = DAG.getConstantFP(-1.0, SL, VT);
+  const SDValue Zero = DAG.getConstantFP(0.0, SL, VT);
+  SDValue Correction = DAG.getNode(ISD::SELECT, SL, VT, Cond, MinusOne, Zero);
+  return DAG.getNode(ISD::FMA, SL, VT, Correction, C, A);
+}
 
+SDValue SITargetLowering::performFAddCombine(SDNode *N,
+                                             DAGCombinerInfo &DCI) const {
   SelectionDAG &DAG = DCI.DAG;
   EVT VT = N->getValueType(0);
 
   SDLoc SL(N);
   SDValue LHS = N->getOperand(0);
   SDValue RHS = N->getOperand(1);
 
+  // Match conditional subtraction patterns for FMA optimization:
+  //   result = a - (cond ? c : 0.0) (see performFSubCombine)
+  //   result = a + (cond ? -c : 0.0)
+  //   result = a + (-(cond ? c : 0.0))
+  // and convert to:
+  //   result = fma((cond ? -1.0, 0.0), c, a)
+  //
+  // This saves one v_cndmask per pattern on a pre-gfx12.
+  // The optimization doesn't make sense on gfx12 due to dual-issued v_cndmask.
+  if (Subtarget->shouldUseConditionalSubToFMAF64() &&
+      VT == MVT::f64) { // Only optimize if FMA is available
----------------
arsenm wrote:

FMA is always available 

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


More information about the llvm-commits mailing list