[llvm] Add LoopVectorizer support for `llvm.vector.partial.reduce.fadd` (PR #163975)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 9 06:42:33 PST 2025


================
@@ -2703,11 +2707,60 @@ InstructionCost VPReductionRecipe::computeCost(ElementCount VF,
       CondCost = Ctx.TTI.getCmpSelInstrCost(Instruction::Select, VectorTy,
                                             CondTy, Pred, Ctx.CostKind);
     }
-    return CondCost + Ctx.TTI.getPartialReductionCost(
-                          Opcode, ElementTy, ElementTy, ElementTy, VF,
-                          TargetTransformInfo::PR_None,
-                          TargetTransformInfo::PR_None, std::nullopt,
-                          Ctx.CostKind);
+    if (!match(getVecOp(), m_FMul(m_VPValue(), m_VPValue()))) {
+      auto *PhiType = Ctx.Types.inferScalarType(getChainOp());
+      auto *InputType = Ctx.Types.inferScalarType(getVecOp());
+      return CondCost + Ctx.TTI.getPartialReductionCost(
+                            Opcode, InputType, InputType, PhiType, VF,
+                            TTI::PR_None, TTI::PR_None, {}, Ctx.CostKind,
+                            OptionalFMF);
+    }
+
+    VPRecipeBase *OpR = getVecOp()->getDefiningRecipe();
+    Type *InputTypeA = nullptr, *InputTypeB = nullptr;
+    TTI::PartialReductionExtendKind ExtAType = TTI::PR_None,
+                                    ExtBType = TTI::PR_None;
+
+    auto GetExtendKind = [](VPRecipeBase *R) {
+      if (!R)
+        return TTI::PR_None;
+      auto *WidenCastR = dyn_cast<VPWidenCastRecipe>(R);
+      if (!WidenCastR)
+        return TTI::PR_None;
+      if (WidenCastR->getOpcode() == Instruction::CastOps::ZExt)
+        return TTI::PR_ZeroExtend;
+      if (WidenCastR->getOpcode() == Instruction::CastOps::SExt)
+        return TTI::PR_SignExtend;
+      if (WidenCastR->getOpcode() == Instruction::CastOps::FPExt)
+        return TTI::PR_FPExtend;
+      return TTI::PR_None;
+    };
+
+    if (auto Widen = dyn_cast<VPWidenRecipe>(OpR)) {
+      unsigned WidenOpcode = Widen->getOpcode();
+      VPRecipeBase *ExtAR = Widen->getOperand(0)->getDefiningRecipe();
+      VPRecipeBase *ExtBR = Widen->getOperand(1)->getDefiningRecipe();
----------------
MacDue wrote:

I think this is the wrong approach for trying to hide the extension costs. There is also support for doing this via `VPExpressionRecipe` (see `tryToMatchAndCreateExtendedReduction`). I think that should be updated to handle fadd reductions and fpext.  

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


More information about the llvm-commits mailing list