[llvm] [LoopVectorizer] Add support for partial reductions (PR #92418)

Hassnaa Hamdi via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 09:30:59 PDT 2024


================
@@ -1556,7 +1556,105 @@ class LoopVectorizationCostModel {
   getReductionPatternCost(Instruction *I, ElementCount VF, Type *VectorTy,
                           TTI::TargetCostKind CostKind) const;
 
+  using PartialReductionList = DenseMap<Instruction *, PartialReductionChain>;
+
+  PartialReductionList getPartialReductionChains() {
+    return PartialReductionChains;
+  }
+
+  bool getInstructionsPartialReduction(Instruction *I,
+                                       PartialReductionChain &Chain) const {
+    auto PairIt = PartialReductionChains.find(I);
+    if (PairIt == PartialReductionChains.end())
+      return false;
+    Chain = PairIt->second;
+    return true;
+  }
+
+  void addPartialReductionIfSupported(Instruction *Instr, ElementCount VF) {
+    Value *ExpectedPhi;
+    Value *A, *B;
+
+    using namespace llvm::PatternMatch;
+    auto Pattern =
+        m_BinOp(m_OneUse(m_BinOp(m_OneUse(m_ZExtOrSExt(m_OneUse(m_Value(A)))),
+                                 m_OneUse(m_ZExtOrSExt(m_OneUse(m_Value(B)))))),
----------------
hassnaaHamdi wrote:

I think you don't need `m_OneUse` for the operands, you need it only for the `m_BinOp` that will be eventually lowered, correct ?
If we are doing manual unrolling, the vector will be used multiple times for multiple matrix dimensions, in that case the current pattern will not match.

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


More information about the llvm-commits mailing list