[Mlir-commits] [mlir] [mlir][vector] Generalize multi_reduction innerparallel unrolling to N dimensions (PR #182301)

Erick Ochoa Lopez llvmlistbot at llvm.org
Mon Feb 23 06:35:13 PST 2026


================
@@ -486,6 +431,157 @@ struct OneDimMultiReductionToTwoDim
   }
 };
 
+/// Unrolls outermost dimension for vector.multi_reduction.
+/// Matches when the outermost dimension is the only reduction
+/// dimension.
+///
+/// In this case [0] refers to rank at position N, so it is the outermost
+/// dimension.
+///
+/// ```mlir
+/// %res = vector.multi_reduction <add> %src, %acc [0] : vector<NxMx...xf32> to
+/// vector<Mx...xf32>
+/// ```
+///
+/// will extract N vectors from %src and then perform elementwise operations.
+///
+/// ```mlir
+/// %0 = vector.extract %src[0] : vector<Mx...xf32> from vector<NxMx...xf32>
+/// ...
+/// %Nminus1 = vector.extract %src[ [[N-1]] ] : vector<Mx...x.f32> from
+/// vector<NxMx...xf32>
+///
+/// %res0 = arith.addf %0, %acc : vector<Mx...xf32>
+/// ...
+/// %res = arith.addf %Nminus1, %resNminus2 : vector<Mx...xf32>
+/// ```
+struct UnrollMultiReductionInnerParallelBaseCase
+    : public vector::MaskableOpRewritePattern<vector::MultiDimReductionOp> {
+  using MaskableOpRewritePattern::MaskableOpRewritePattern;
+
+  FailureOr<Value>
+  matchAndRewriteMaskableOp(vector::MultiDimReductionOp multiReductionOp,
+                            vector::MaskingOpInterface maskingOp,
+                            PatternRewriter &rewriter) const override {
+    auto srcRank = multiReductionOp.getSourceVectorType().getRank();
+    if (srcRank < 2)
----------------
amd-eochoalo wrote:

Yes, this is correct! In the original PR I did implement a pattern that will change all vector.multi_reduction with rank 1 to vector.reduction. Please note that at the moment we are only replacing TwoDimMultiReductionToElementWise which does not convert vectors of rank < 2 into vector.reduction.

This comment was very useful. It helped me realize that I need to change the transform dialect schedules such that they apply this pattern after applying the other two patterns. That is the downside of this generalization, that it cannot be applied at the same time as the other patterns since multiple patterns may be matched for the same op. (Consider the pattern that transposes vector.multi_reduction vs this one which will unroll it.)

I'll make these changes and the other requested ones. Many thanks for the review :) 

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


More information about the Mlir-commits mailing list