24#define GEN_PASS_DEF_LOWERVECTORMULTIREDUCTION
25#include "mlir/Dialect/Vector/Transforms/Passes.h.inc"
29#define DEBUG_TYPE "vector-multi-reduction"
39class InnerOuterDimReductionConversion
44 explicit InnerOuterDimReductionConversion(
48 useInnerDimsForReduction(
49 options == vector::VectorMultiReductionLowering::InnerReduction) {}
51 LogicalResult matchAndRewrite(vector::MultiDimReductionOp multiReductionOp,
56 cast<vector::MaskableOpInterface>(multiReductionOp.getOperation());
58 if (maskableOp.isMasked()) {
60 rootOp = maskableOp.getMaskingOp();
62 rootOp = multiReductionOp;
65 auto src = multiReductionOp.getSource();
66 auto loc = multiReductionOp.
getLoc();
67 auto srcRank = multiReductionOp.getSourceVectorType().getRank();
71 llvm::SmallDenseSet<int64_t> reductionDimsSet(reductionDims.begin(),
73 int64_t reductionSize = reductionDims.size();
75 for (
int64_t i = 0; i < srcRank; ++i)
76 if (!reductionDimsSet.contains(i))
77 parallelDims.push_back(i);
81 if (parallelDims.empty())
83 if (useInnerDimsForReduction &&
85 llvm::to_vector<4>(llvm::seq<int64_t>(0, parallelDims.size()))))
88 if (!useInnerDimsForReduction &&
89 (parallelDims == llvm::to_vector<4>(llvm::seq<int64_t>(
91 parallelDims.size() + reductionDims.size()))))
95 if (useInnerDimsForReduction) {
96 indices.append(parallelDims.begin(), parallelDims.end());
97 indices.append(reductionDims.begin(), reductionDims.end());
99 indices.append(reductionDims.begin(), reductionDims.end());
100 indices.append(parallelDims.begin(), parallelDims.end());
104 Value transposedMask;
105 if (maskableOp.isMasked()) {
106 transposedMask = vector::TransposeOp::create(
107 rewriter, loc, maskableOp.getMaskingOp().getMask(),
indices);
111 auto transposeOp = vector::TransposeOp::create(rewriter, loc, src,
indices);
113 for (
int i = 0; i < reductionSize; ++i) {
114 if (useInnerDimsForReduction)
115 reductionMask[srcRank - i - 1] =
true;
117 reductionMask[i] =
true;
120 Operation *newMultiRedOp = vector::MultiDimReductionOp::create(
121 rewriter, multiReductionOp.getLoc(), transposeOp.getResult(),
122 multiReductionOp.getAcc(), reductionMask, multiReductionOp.getKind(),
123 multiReductionOp.getFastmath());
132 const bool useInnerDimsForReduction;
148class FlattenMultiReduction
153 explicit FlattenMultiReduction(
MLIRContext *context,
154 vector::VectorMultiReductionLowering
options,
157 useInnerDimsForReduction(
158 options == vector::VectorMultiReductionLowering::InnerReduction) {}
160 LogicalResult matchAndRewrite(vector::MultiDimReductionOp multiReductionOp,
165 cast<vector::MaskableOpInterface>(multiReductionOp.getOperation());
167 if (maskableOp.isMasked()) {
169 rootOp = maskableOp.getMaskingOp();
171 rootOp = multiReductionOp;
174 auto srcRank = multiReductionOp.getSourceVectorType().getRank();
175 auto srcShape = multiReductionOp.getSourceVectorType().getShape();
176 auto srcScalableDims =
177 multiReductionOp.getSourceVectorType().getScalableDims();
178 auto loc = multiReductionOp.
getLoc();
186 if (llvm::count(srcScalableDims,
true) > 1)
191 if (srcRank == 2 && reductionMask.front() != reductionMask.back())
198 bool isReductionDimScalable =
false;
199 for (
const auto &it : llvm::enumerate(reductionMask)) {
201 bool isReduction = it.value();
203 reductionDims.push_back(i);
204 reductionShapes.push_back(srcShape[i]);
205 isReductionDimScalable |= srcScalableDims[i];
207 parallelDims.push_back(i);
208 parallelShapes.push_back(srcShape[i]);
209 parallelScalableDims.push_back(srcScalableDims[i]);
214 int flattenedParallelDim = 0;
215 int flattenedReductionDim = 0;
216 if (!parallelShapes.empty()) {
217 flattenedParallelDim = 1;
218 for (
auto d : parallelShapes)
219 flattenedParallelDim *= d;
221 if (!reductionShapes.empty()) {
222 flattenedReductionDim = 1;
223 for (
auto d : reductionShapes)
224 flattenedReductionDim *= d;
227 assert((flattenedParallelDim || flattenedReductionDim) &&
228 "expected at least one parallel or reduction dim");
233 if (useInnerDimsForReduction &&
234 llvm::any_of(parallelDims, [&](
int64_t i) {
return i != counter++; }))
237 counter = reductionDims.size();
238 if (!useInnerDimsForReduction &&
239 llvm::any_of(parallelDims, [&](
int64_t i) {
return i != counter++; }))
247 bool isParallelDimScalable = llvm::is_contained(parallelScalableDims,
true);
248 if (flattenedParallelDim) {
249 mask.push_back(
false);
251 scalableDims.push_back(isParallelDimScalable);
253 if (flattenedReductionDim) {
254 mask.push_back(
true);
256 scalableDims.push_back(isReductionDimScalable);
258 if (!useInnerDimsForReduction &&
vectorShape.size() == 2) {
259 std::swap(mask.front(), mask.back());
261 std::swap(scalableDims.front(), scalableDims.back());
265 if (maskableOp.isMasked()) {
266 Value vectorMask = maskableOp.getMaskingOp().getMask();
267 auto maskCastedType = VectorType::get(
269 llvm::cast<VectorType>(vectorMask.
getType()).getElementType(),
271 newVectorMask = vector::ShapeCastOp::create(rewriter, loc, maskCastedType,
275 auto castedType = VectorType::get(
276 vectorShape, multiReductionOp.getSourceVectorType().getElementType(),
278 Value cast = vector::ShapeCastOp::create(rewriter, loc, castedType,
279 multiReductionOp.getSource());
281 Value acc = multiReductionOp.getAcc();
282 if (flattenedParallelDim) {
283 auto accType = VectorType::get(
284 {flattenedParallelDim},
286 {isParallelDimScalable});
287 acc = vector::ShapeCastOp::create(rewriter, loc, accType,
acc);
291 Operation *newMultiDimRedOp = vector::MultiDimReductionOp::create(
292 rewriter, loc, cast,
acc, mask, multiReductionOp.getKind(),
293 multiReductionOp.getFastmath());
299 if (parallelShapes.empty()) {
306 VectorType outputCastedType = VectorType::get(
307 parallelShapes, multiReductionOp.getSourceVectorType().getElementType(),
308 parallelScalableDims);
310 rootOp, outputCastedType, newMultiDimRedOp->
getResult(0));
315 const bool useInnerDimsForReduction;
344struct TwoDimMultiReductionToElementWise
346 using MaskableOpRewritePattern::MaskableOpRewritePattern;
349 matchAndRewriteMaskableOp(vector::MultiDimReductionOp multiReductionOp,
350 vector::MaskingOpInterface maskingOp,
352 auto srcRank = multiReductionOp.getSourceVectorType().getRank();
357 if (multiReductionOp.isReducedDim(1) || !multiReductionOp.isReducedDim(0))
360 Value mask = maskingOp ? maskingOp.getMask() :
Value();
362 auto loc = multiReductionOp.getLoc();
363 Value source = multiReductionOp.getSource();
365 multiReductionOp.getSourceVectorType().getShape();
366 int outerDim = srcShape[0];
369 for (
int64_t i = 0; i < outerDim; i++) {
370 auto v = vector::ExtractOp::create(rewriter, loc, source, i);
371 Value m = mask ?
Value(vector::ExtractOp::create(rewriter, loc, mask, i))
374 makeArithReduction(rewriter, loc, multiReductionOp.getKind(), v,
375 result, multiReductionOp.getFastmathAttr(), m);
402struct TwoDimMultiReductionToReduction
404 using MaskableOpRewritePattern::MaskableOpRewritePattern;
407 matchAndRewriteMaskableOp(vector::MultiDimReductionOp multiReductionOp,
408 vector::MaskingOpInterface maskingOp,
410 auto srcRank = multiReductionOp.getSourceVectorType().getRank();
415 if (multiReductionOp.isReducedDim(0) || !multiReductionOp.isReducedDim(1))
418 Value mask = maskingOp ? maskingOp.getMask() :
nullptr;
420 auto loc = multiReductionOp.
getLoc();
421 Value source = multiReductionOp.getSource();
422 Value acc = multiReductionOp.getAcc();
423 int outerDim = multiReductionOp.getSourceVectorType().getShape()[0];
426 rewriter, loc, multiReductionOp.getDestType(),
427 rewriter.
getZeroAttr(multiReductionOp.getDestType()));
430 for (
int64_t i = 0; i < outerDim; ++i) {
431 Value v = vector::ExtractOp::create(rewriter, loc, source, i);
432 Value a = vector::ExtractOp::create(rewriter, loc,
acc, i);
435 vector::ReductionOp::create(rewriter, loc, multiReductionOp.getKind(),
436 v, a, multiReductionOp.getFastmath());
439 Value m = vector::ExtractOp::create(rewriter, loc, mask, i);
443 result = vector::InsertOp::create(rewriter, loc,
461struct OneDimMultiReductionToReduction
463 using MaskableOpRewritePattern::MaskableOpRewritePattern;
466 matchAndRewriteMaskableOp(vector::MultiDimReductionOp multiReductionOp,
467 vector::MaskingOpInterface maskingOp,
469 auto srcRank = multiReductionOp.getSourceVectorType().getRank();
473 if (!multiReductionOp.isReducedDim(0))
476 auto loc = multiReductionOp.getLoc();
477 Value mask = maskingOp ? maskingOp.getMask() :
Value();
479 Operation *reductionOp = vector::ReductionOp::create(
480 rewriter, loc, multiReductionOp.getKind(), multiReductionOp.getSource(),
481 multiReductionOp.getAcc(), multiReductionOp.getFastmath());
490struct LowerVectorMultiReductionPass
491 :
public vector::impl::LowerVectorMultiReductionBase<
492 LowerVectorMultiReductionPass> {
493 LowerVectorMultiReductionPass(vector::VectorMultiReductionLowering option) {
494 this->loweringStrategy = option;
497 void runOnOperation()
override {
503 patterns, this->loweringStrategy);
509 flatteningPatterns, this->loweringStrategy);
515 unrollingPatterns, this->loweringStrategy);
521 registry.
insert<vector::VectorDialect>();
543 patterns.
add<OneDimMultiReductionToReduction>(patterns.
getContext(), benefit);
544 if (
options == VectorMultiReductionLowering ::InnerReduction)
545 patterns.
add<TwoDimMultiReductionToReduction>(patterns.
getContext(),
548 patterns.
add<TwoDimMultiReductionToElementWise>(patterns.
getContext(),
553 vector::VectorMultiReductionLowering option) {
554 return std::make_unique<LowerVectorMultiReductionPass>(option);
static llvm::ManagedStatic< PassManagerOptions > options
static std::optional< VectorShape > vectorShape(Type type)
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
TypedAttr getZeroAttr(Type type)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
RAII guard to reset the insertion point of the builder when destroyed.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
MLIRContext * getContext()
Return the context this operation is associated with.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Location getLoc() const
Return the location of this value.
Operation * maskOperation(OpBuilder &builder, Operation *maskableOp, Value mask, Value passthru=Value())
Creates a vector.mask operation around a maskable operation.
std::unique_ptr< Pass > createLowerVectorMultiReductionPass(VectorMultiReductionLowering option=VectorMultiReductionLowering::InnerParallel)
Creates an instance of the vector.multi_reduction lowering pass.
void populateVectorMultiReductionUnrollingPatterns(RewritePatternSet &patterns, VectorMultiReductionLowering options, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
void populateVectorMultiReductionFlatteningPatterns(RewritePatternSet &patterns, VectorMultiReductionLowering options, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
void populateVectorMultiReductionReorderPatterns(RewritePatternSet &patterns, VectorMultiReductionLowering options, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
A pattern for ops that implement MaskableOpInterface and that might be masked (i.e.