1 //===- TestVectorTransforms.cpp - Test Vector transforms and lowerings ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <type_traits>
10 
11 #include "mlir/Analysis/SliceAnalysis.h"
12 #include "mlir/Dialect/Affine/IR/AffineOps.h"
13 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
14 #include "mlir/Dialect/Linalg/IR/Linalg.h"
15 #include "mlir/Dialect/Linalg/Passes.h"
16 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
17 #include "mlir/Dialect/MemRef/IR/MemRef.h"
18 #include "mlir/Dialect/SCF/SCF.h"
19 #include "mlir/Dialect/StandardOps/IR/Ops.h"
20 #include "mlir/Dialect/Vector/VectorTransforms.h"
21 #include "mlir/Pass/Pass.h"
22 #include "mlir/Pass/PassManager.h"
23 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
24 
25 using namespace mlir;
26 using namespace mlir::linalg;
27 using namespace mlir::vector;
28 
29 namespace {
30 
31 struct TestVectorToVectorLowering
32     : public PassWrapper<TestVectorToVectorLowering, FunctionPass> {
33   TestVectorToVectorLowering() = default;
34   TestVectorToVectorLowering(const TestVectorToVectorLowering &pass)
35       : PassWrapper(pass) {}
36   StringRef getArgument() const final {
37     return "test-vector-to-vector-lowering";
38   }
39   StringRef getDescription() const final {
40     return "Test lowering patterns between ops in the vector dialect";
41   }
42 
43   void getDependentDialects(DialectRegistry &registry) const override {
44     registry.insert<AffineDialect>();
45   }
46 
47   Option<bool> unroll{*this, "unroll", llvm::cl::desc("Include unrolling"),
48                       llvm::cl::init(false)};
49 
50   void runOnFunction() override {
51     auto *ctx = &getContext();
52     RewritePatternSet patterns(ctx);
53     if (unroll) {
54       populateVectorUnrollPatterns(
55           patterns,
56           UnrollVectorOptions().setNativeShapeFn(getShape).setFilterConstraint(
57               filter));
58     }
59     populateVectorToVectorCanonicalizationPatterns(patterns);
60     populateBubbleVectorBitCastOpPatterns(patterns);
61     populateCastAwayVectorLeadingOneDimPatterns(patterns);
62     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
63   }
64 
65 private:
66   // Return the target shape based on op type.
67   static Optional<SmallVector<int64_t, 4>> getShape(Operation *op) {
68     if (isa<arith::AddFOp, SelectOp, arith::CmpFOp>(op))
69       return SmallVector<int64_t, 4>(2, 2);
70     if (isa<vector::ContractionOp>(op))
71       return SmallVector<int64_t, 4>(3, 2);
72     // For transfer ops, just propagate the shape coming from
73     // InsertStridedSlices/ExtractStridedSlices.
74     if (auto readOp = dyn_cast<vector::TransferReadOp>(op)) {
75       VectorType dstVec;
76       for (Operation *users : readOp->getUsers()) {
77         auto extract = dyn_cast<ExtractStridedSliceOp>(users);
78         if (!extract)
79           return llvm::None;
80         auto vecType = extract.getResult().getType().cast<VectorType>();
81         if (dstVec && dstVec != vecType)
82           return llvm::None;
83         dstVec = vecType;
84       }
85       return SmallVector<int64_t, 4>(dstVec.getShape().begin(),
86                                      dstVec.getShape().end());
87     }
88     if (auto writeOp = dyn_cast<vector::TransferWriteOp>(op)) {
89       auto insert = writeOp.vector().getDefiningOp<InsertStridedSliceOp>();
90       if (!insert)
91         return llvm::None;
92       ArrayRef<int64_t> shape = insert.getSourceVectorType().getShape();
93       return SmallVector<int64_t, 4>(shape.begin(), shape.end());
94     }
95     return llvm::None;
96   }
97 
98   static LogicalResult filter(Operation *op) {
99     return success(isa<arith::AddFOp, SelectOp, arith::CmpFOp, ContractionOp,
100                        TransferReadOp, TransferWriteOp>(op));
101   }
102 };
103 
104 struct TestVectorContractionLowering
105     : public PassWrapper<TestVectorContractionLowering, FunctionPass> {
106   StringRef getArgument() const final {
107     return "test-vector-contraction-lowering";
108   }
109   StringRef getDescription() const final {
110     return "Test lowering patterns that lower contract ops in the vector "
111            "dialect";
112   }
113   TestVectorContractionLowering() = default;
114   TestVectorContractionLowering(const TestVectorContractionLowering &pass)
115       : PassWrapper(pass) {}
116 
117   Option<bool> lowerToFlatMatrix{
118       *this, "vector-lower-matrix-intrinsics",
119       llvm::cl::desc("Lower vector.contract to llvm.intr.matrix.multiply"),
120       llvm::cl::init(false)};
121   Option<bool> lowerToOuterProduct{
122       *this, "vector-outerproduct",
123       llvm::cl::desc("Lower vector.contract to vector.outerproduct"),
124       llvm::cl::init(false)};
125   Option<bool> lowerToFilterOuterProduct{
126       *this, "vector-filter-outerproduct",
127       llvm::cl::desc("Lower vector.contract to vector.outerproduct but not for "
128                      "vectors of size 4."),
129       llvm::cl::init(false)};
130 
131   void runOnFunction() override {
132     RewritePatternSet patterns(&getContext());
133 
134     // Test on one pattern in isolation.
135     if (lowerToOuterProduct) {
136       VectorContractLowering lowering = VectorContractLowering::OuterProduct;
137       VectorTransformsOptions options{lowering};
138       patterns.add<ContractionOpToOuterProductOpLowering>(options,
139                                                           &getContext());
140       (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
141       return;
142     }
143 
144     // Test on one pattern in isolation.
145     if (lowerToFilterOuterProduct) {
146       VectorContractLowering lowering = VectorContractLowering::OuterProduct;
147       VectorTransformsOptions options{lowering};
148       patterns.add<ContractionOpToOuterProductOpLowering>(
149           options, &getContext(), [](vector::ContractionOp op) {
150             // Only lowers vector.contract where the lhs as a type vector<MxNx?>
151             // where M is not 4.
152             if (op.getRhsType().getShape()[0] == 4)
153               return failure();
154             return success();
155           });
156       (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
157       return;
158     }
159 
160     // Test on all contract lowering patterns.
161     VectorContractLowering contractLowering = VectorContractLowering::Dot;
162     if (lowerToFlatMatrix)
163       contractLowering = VectorContractLowering::Matmul;
164     VectorMultiReductionLowering vectorMultiReductionLowering =
165         VectorMultiReductionLowering::InnerParallel;
166     VectorTransformsOptions options{contractLowering,
167                                     vectorMultiReductionLowering,
168                                     VectorTransposeLowering()};
169     populateVectorBroadcastLoweringPatterns(patterns);
170     populateVectorContractLoweringPatterns(patterns, options);
171     populateVectorMaskOpLoweringPatterns(patterns);
172     populateVectorShapeCastLoweringPatterns(patterns);
173     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
174   }
175 };
176 
177 struct TestVectorTransposeLowering
178     : public PassWrapper<TestVectorTransposeLowering, FunctionPass> {
179   StringRef getArgument() const final {
180     return "test-vector-transpose-lowering";
181   }
182   StringRef getDescription() const final {
183     return "Test lowering patterns that lower contract ops in the vector "
184            "dialect";
185   }
186   TestVectorTransposeLowering() = default;
187   TestVectorTransposeLowering(const TestVectorTransposeLowering &pass)
188       : PassWrapper(pass) {}
189 
190   Option<bool> lowerToEltwise{
191       *this, "eltwise",
192       llvm::cl::desc("Lower 2-D vector.transpose to eltwise insert/extract"),
193       llvm::cl::init(false)};
194   Option<bool> lowerToFlatTranspose{
195       *this, "flat",
196       llvm::cl::desc("Lower 2-D vector.transpose to vector.flat_transpose"),
197       llvm::cl::init(false)};
198   Option<bool> lowerToShuffleTranspose{
199       *this, "shuffle",
200       llvm::cl::desc("Lower 2-D vector.transpose to shape_cast + shuffle"),
201       llvm::cl::init(false)};
202   Option<bool> lowerToAvx2{
203       *this, "avx2",
204       llvm::cl::desc("Lower vector.transpose to avx2-specific patterns"),
205       llvm::cl::init(false)};
206 
207   void getDependentDialects(DialectRegistry &registry) const override {
208     registry.insert<LLVM::LLVMDialect>();
209   }
210 
211   void runOnFunction() override {
212     RewritePatternSet patterns(&getContext());
213 
214     // Test on one pattern in isolation.
215     // Explicitly disable shape_cast lowering.
216     LinalgVectorLoweringOptions options = LinalgVectorLoweringOptions()
217                                               .enableVectorTransposeLowering()
218                                               .enableShapeCastLowering(false);
219     if (lowerToEltwise) {
220       options = options.setVectorTransformsOptions(
221           VectorTransformsOptions().setVectorTransposeLowering(
222               VectorTransposeLowering::EltWise));
223     }
224     if (lowerToFlatTranspose) {
225       options = options.setVectorTransformsOptions(
226           VectorTransformsOptions().setVectorTransposeLowering(
227               VectorTransposeLowering::Flat));
228     }
229     if (lowerToShuffleTranspose) {
230       options = options.setVectorTransformsOptions(
231           VectorTransformsOptions().setVectorTransposeLowering(
232               VectorTransposeLowering::Shuffle));
233     }
234     if (lowerToAvx2) {
235       options = options.enableAVX2Lowering().setAVX2LoweringOptions(
236           x86vector::avx2::LoweringOptions().setTransposeOptions(
237               x86vector::avx2::TransposeLoweringOptions()
238                   .lower4x8xf32()
239                   .lower8x8xf32()));
240     }
241 
242     OpPassManager dynamicPM("builtin.func");
243     dynamicPM.addPass(createLinalgStrategyLowerVectorsPass(options));
244     if (failed(runPipeline(dynamicPM, getFunction())))
245       return signalPassFailure();
246   }
247 };
248 
249 struct TestVectorUnrollingPatterns
250     : public PassWrapper<TestVectorUnrollingPatterns, FunctionPass> {
251   StringRef getArgument() const final {
252     return "test-vector-unrolling-patterns";
253   }
254   StringRef getDescription() const final {
255     return "Test lowering patterns to unroll contract ops in the vector "
256            "dialect";
257   }
258   TestVectorUnrollingPatterns() = default;
259   TestVectorUnrollingPatterns(const TestVectorUnrollingPatterns &pass)
260       : PassWrapper(pass) {}
261   void runOnFunction() override {
262     MLIRContext *ctx = &getContext();
263     RewritePatternSet patterns(ctx);
264     populateVectorUnrollPatterns(
265         patterns, UnrollVectorOptions()
266                       .setNativeShape(ArrayRef<int64_t>{2, 2})
267                       .setFilterConstraint([](Operation *op) {
268                         return success(isa<arith::AddFOp, vector::FMAOp>(op));
269                       }));
270 
271     if (unrollBasedOnType) {
272       UnrollVectorOptions::NativeShapeFnType nativeShapeFn =
273           [](Operation *op) -> Optional<SmallVector<int64_t, 4>> {
274         vector::ContractionOp contractOp = cast<vector::ContractionOp>(op);
275         SmallVector<int64_t, 4> nativeShape = {4, 4, 2};
276         if (auto floatType = contractOp.getLhsType()
277                                  .getElementType()
278                                  .dyn_cast<FloatType>()) {
279           if (floatType.getWidth() == 16) {
280             nativeShape[2] = 4;
281           }
282         }
283         return nativeShape;
284       };
285       populateVectorUnrollPatterns(patterns,
286                                    UnrollVectorOptions()
287                                        .setNativeShapeFn(nativeShapeFn)
288                                        .setFilterConstraint([](Operation *op) {
289                                          return success(isa<ContractionOp>(op));
290                                        }));
291     } else {
292       populateVectorUnrollPatterns(
293           patterns, UnrollVectorOptions()
294                         .setNativeShape(ArrayRef<int64_t>{2, 2, 2})
295                         .setFilterConstraint([](Operation *op) {
296                           return success(isa<ContractionOp>(op));
297                         }));
298     }
299     populateVectorToVectorCanonicalizationPatterns(patterns);
300     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
301   }
302 
303   Option<bool> unrollBasedOnType{
304       *this, "unroll-based-on-type",
305       llvm::cl::desc("Set the unroll factor based on type of the operation"),
306       llvm::cl::init(false)};
307 };
308 
309 struct TestVectorDistributePatterns
310     : public PassWrapper<TestVectorDistributePatterns, FunctionPass> {
311   StringRef getArgument() const final {
312     return "test-vector-distribute-patterns";
313   }
314   StringRef getDescription() const final {
315     return "Test lowering patterns to distribute vector ops in the vector "
316            "dialect";
317   }
318   TestVectorDistributePatterns() = default;
319   TestVectorDistributePatterns(const TestVectorDistributePatterns &pass)
320       : PassWrapper(pass) {}
321   void getDependentDialects(DialectRegistry &registry) const override {
322     registry.insert<VectorDialect>();
323     registry.insert<AffineDialect>();
324   }
325   ListOption<int32_t> multiplicity{
326       *this, "distribution-multiplicity", llvm::cl::MiscFlags::CommaSeparated,
327       llvm::cl::desc("Set the multiplicity used for distributing vector")};
328 
329   void runOnFunction() override {
330     MLIRContext *ctx = &getContext();
331     RewritePatternSet patterns(ctx);
332     FuncOp func = getFunction();
333     func.walk([&](arith::AddFOp op) {
334       OpBuilder builder(op);
335       if (auto vecType = op.getType().dyn_cast<VectorType>()) {
336         SmallVector<int64_t, 2> mul;
337         SmallVector<AffineExpr, 2> perm;
338         SmallVector<Value, 2> ids;
339         unsigned count = 0;
340         // Remove the multiplicity of 1 and calculate the affine map based on
341         // the multiplicity.
342         SmallVector<int32_t, 4> m(multiplicity.begin(), multiplicity.end());
343         for (unsigned i = 0, e = vecType.getRank(); i < e; i++) {
344           if (i < m.size() && m[i] != 1 && vecType.getDimSize(i) % m[i] == 0) {
345             mul.push_back(m[i]);
346             ids.push_back(func.getArgument(count++));
347             perm.push_back(getAffineDimExpr(i, ctx));
348           }
349         }
350         auto map = AffineMap::get(op.getType().cast<VectorType>().getRank(), 0,
351                                   perm, ctx);
352         Optional<mlir::vector::DistributeOps> ops = distributPointwiseVectorOp(
353             builder, op.getOperation(), ids, mul, map);
354         if (ops.hasValue()) {
355           SmallPtrSet<Operation *, 1> extractOp({ops->extract, ops->insert});
356           op.getResult().replaceAllUsesExcept(ops->insert.getResult(),
357                                               extractOp);
358         }
359       }
360     });
361     populatePropagateVectorDistributionPatterns(patterns);
362     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
363   }
364 };
365 
366 struct TestVectorToLoopPatterns
367     : public PassWrapper<TestVectorToLoopPatterns, FunctionPass> {
368   StringRef getArgument() const final { return "test-vector-to-forloop"; }
369   StringRef getDescription() const final {
370     return "Test lowering patterns to break up a vector op into a for loop";
371   }
372   TestVectorToLoopPatterns() = default;
373   TestVectorToLoopPatterns(const TestVectorToLoopPatterns &pass)
374       : PassWrapper(pass) {}
375   void getDependentDialects(DialectRegistry &registry) const override {
376     registry.insert<VectorDialect>();
377     registry.insert<AffineDialect>();
378   }
379   Option<int32_t> multiplicity{
380       *this, "distribution-multiplicity",
381       llvm::cl::desc("Set the multiplicity used for distributing vector"),
382       llvm::cl::init(32)};
383   void runOnFunction() override {
384     MLIRContext *ctx = &getContext();
385     RewritePatternSet patterns(ctx);
386     FuncOp func = getFunction();
387     func.walk([&](arith::AddFOp op) {
388       // Check that the operation type can be broken down into a loop.
389       VectorType type = op.getType().dyn_cast<VectorType>();
390       if (!type || type.getRank() != 1 ||
391           type.getNumElements() % multiplicity != 0)
392         return mlir::WalkResult::advance();
393       auto filterAlloc = [](Operation *op) {
394         if (isa<arith::ConstantOp, memref::AllocOp, CallOp>(op))
395           return false;
396         return true;
397       };
398       auto dependentOps = getSlice(op, filterAlloc);
399       // Create a loop and move instructions from the Op slice into the loop.
400       OpBuilder builder(op);
401       auto zero = builder.create<arith::ConstantIndexOp>(op.getLoc(), 0);
402       auto one = builder.create<arith::ConstantIndexOp>(op.getLoc(), 1);
403       auto numIter =
404           builder.create<arith::ConstantIndexOp>(op.getLoc(), multiplicity);
405       auto forOp = builder.create<scf::ForOp>(op.getLoc(), zero, numIter, one);
406       for (Operation *it : dependentOps) {
407         it->moveBefore(forOp.getBody()->getTerminator());
408       }
409       auto map = AffineMap::getMultiDimIdentityMap(1, ctx);
410       // break up the original op and let the patterns propagate.
411       Optional<mlir::vector::DistributeOps> ops = distributPointwiseVectorOp(
412           builder, op.getOperation(), {forOp.getInductionVar()}, {multiplicity},
413           map);
414       if (ops.hasValue()) {
415         SmallPtrSet<Operation *, 1> extractOp({ops->extract, ops->insert});
416         op.getResult().replaceAllUsesExcept(ops->insert.getResult(), extractOp);
417       }
418       return mlir::WalkResult::interrupt();
419     });
420     populatePropagateVectorDistributionPatterns(patterns);
421     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
422   }
423 };
424 
425 struct TestVectorTransferUnrollingPatterns
426     : public PassWrapper<TestVectorTransferUnrollingPatterns, FunctionPass> {
427   void getDependentDialects(DialectRegistry &registry) const override {
428     registry.insert<AffineDialect>();
429   }
430   StringRef getArgument() const final {
431     return "test-vector-transfer-unrolling-patterns";
432   }
433   StringRef getDescription() const final {
434     return "Test lowering patterns to unroll transfer ops in the vector "
435            "dialect";
436   }
437   void runOnFunction() override {
438     MLIRContext *ctx = &getContext();
439     RewritePatternSet patterns(ctx);
440     populateVectorUnrollPatterns(
441         patterns,
442         UnrollVectorOptions()
443             .setNativeShape(ArrayRef<int64_t>{2, 2})
444             .setFilterConstraint([](Operation *op) {
445               return success(
446                   isa<vector::TransferReadOp, vector::TransferWriteOp>(op));
447             }));
448     populateVectorToVectorCanonicalizationPatterns(patterns);
449     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
450   }
451 };
452 
453 struct TestVectorTransferFullPartialSplitPatterns
454     : public PassWrapper<TestVectorTransferFullPartialSplitPatterns,
455                          FunctionPass> {
456   StringRef getArgument() const final {
457     return "test-vector-transfer-full-partial-split";
458   }
459   StringRef getDescription() const final {
460     return "Test lowering patterns to split "
461            "transfer ops via scf.if + linalg ops";
462   }
463   TestVectorTransferFullPartialSplitPatterns() = default;
464   TestVectorTransferFullPartialSplitPatterns(
465       const TestVectorTransferFullPartialSplitPatterns &pass)
466       : PassWrapper(pass) {}
467 
468   void getDependentDialects(DialectRegistry &registry) const override {
469     registry.insert<AffineDialect, linalg::LinalgDialect, memref::MemRefDialect,
470                     scf::SCFDialect>();
471   }
472 
473   Option<bool> useLinalgOps{
474       *this, "use-linalg-copy",
475       llvm::cl::desc("Split using a unmasked vector.transfer + linalg.fill + "
476                      "linalg.copy operations."),
477       llvm::cl::init(false)};
478   void runOnFunction() override {
479     MLIRContext *ctx = &getContext();
480     RewritePatternSet patterns(ctx);
481     VectorTransformsOptions options;
482     if (useLinalgOps)
483       options.setVectorTransferSplit(VectorTransferSplit::LinalgCopy);
484     else
485       options.setVectorTransferSplit(VectorTransferSplit::VectorTransfer);
486     patterns.add<VectorTransferFullPartialRewriter>(ctx, options);
487     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
488   }
489 };
490 
491 struct TestVectorTransferOpt
492     : public PassWrapper<TestVectorTransferOpt, FunctionPass> {
493   StringRef getArgument() const final { return "test-vector-transferop-opt"; }
494   StringRef getDescription() const final {
495     return "Test optimization transformations for transfer ops";
496   }
497   void runOnFunction() override { transferOpflowOpt(getFunction()); }
498 };
499 
500 struct TestVectorTransferLoweringPatterns
501     : public PassWrapper<TestVectorTransferLoweringPatterns, FunctionPass> {
502   void getDependentDialects(DialectRegistry &registry) const override {
503     registry.insert<tensor::TensorDialect, memref::MemRefDialect>();
504   }
505   StringRef getArgument() const final {
506     return "test-vector-transfer-lowering-patterns";
507   }
508   StringRef getDescription() const final {
509     return "Test lowering patterns to lower transfer ops to other vector ops";
510   }
511   void runOnFunction() override {
512     RewritePatternSet patterns(&getContext());
513     populateVectorTransferLoweringPatterns(patterns);
514     populateVectorTransferPermutationMapLoweringPatterns(patterns);
515     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
516   }
517 };
518 
519 struct TestVectorMultiReductionLoweringPatterns
520     : public PassWrapper<TestVectorMultiReductionLoweringPatterns,
521                          FunctionPass> {
522   TestVectorMultiReductionLoweringPatterns() = default;
523   TestVectorMultiReductionLoweringPatterns(
524       const TestVectorMultiReductionLoweringPatterns &pass)
525       : PassWrapper(pass) {}
526   void getDependentDialects(DialectRegistry &registry) const override {
527     registry.insert<memref::MemRefDialect>();
528   }
529   StringRef getArgument() const final {
530     return "test-vector-multi-reduction-lowering-patterns";
531   }
532   StringRef getDescription() const final {
533     return "Test lowering patterns to lower vector.multi_reduction to other "
534            "vector ops";
535   }
536   Option<bool> useOuterReductions{
537       *this, "use-outer-reductions",
538       llvm::cl::desc("Move reductions to outer most dimensions"),
539       llvm::cl::init(false)};
540   void runOnFunction() override {
541     RewritePatternSet patterns(&getContext());
542     populateVectorMultiReductionLoweringPatterns(
543         patterns, useOuterReductions
544                       ? vector::VectorMultiReductionLowering::InnerParallel
545                       : vector::VectorMultiReductionLowering::InnerReduction);
546     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
547   }
548 };
549 
550 struct TestVectorTransferCollapseInnerMostContiguousDims
551     : public PassWrapper<TestVectorTransferCollapseInnerMostContiguousDims,
552                          FunctionPass> {
553   TestVectorTransferCollapseInnerMostContiguousDims() = default;
554   TestVectorTransferCollapseInnerMostContiguousDims(
555       const TestVectorTransferCollapseInnerMostContiguousDims &pass)
556       : PassWrapper(pass) {}
557 
558   void getDependentDialects(DialectRegistry &registry) const override {
559     registry.insert<memref::MemRefDialect, AffineDialect>();
560   }
561 
562   StringRef getArgument() const final {
563     return "test-vector-transfer-collapse-inner-most-dims";
564   }
565 
566   StringRef getDescription() const final {
567     return "Test lowering patterns that reducedes the rank of the vector "
568            "transfer memory and vector operands.";
569   }
570 
571   void runOnFunction() override {
572     RewritePatternSet patterns(&getContext());
573     populateVectorTransferCollapseInnerMostContiguousDimsPatterns(patterns);
574     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
575   }
576 };
577 
578 struct TestVectorReduceToContractPatternsPatterns
579     : public PassWrapper<TestVectorReduceToContractPatternsPatterns,
580                          FunctionPass> {
581   StringRef getArgument() const final {
582     return "test-vector-reduction-to-contract-patterns";
583   }
584   StringRef getDescription() const final {
585     return "Test patterns to convert multireduce op to contract and combine "
586            "broadcast/transpose to contract";
587   }
588   void runOnFunction() override {
589     RewritePatternSet patterns(&getContext());
590     populateVectorReductionToContractPatterns(patterns);
591     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
592   }
593 };
594 
595 struct TestVectorTransferDropUnitDimsPatterns
596     : public PassWrapper<TestVectorTransferDropUnitDimsPatterns, FunctionPass> {
597   StringRef getArgument() const final {
598     return "test-vector-transfer-drop-unit-dims-patterns";
599   }
600   void getDependentDialects(DialectRegistry &registry) const override {
601     registry.insert<memref::MemRefDialect>();
602   }
603   void runOnFunction() override {
604     RewritePatternSet patterns(&getContext());
605     populateVectorTransferDropUnitDimsPatterns(patterns);
606     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
607   }
608 };
609 
610 struct TestFlattenVectorTransferPatterns
611     : public PassWrapper<TestFlattenVectorTransferPatterns, FunctionPass> {
612   StringRef getArgument() const final {
613     return "test-vector-transfer-flatten-patterns";
614   }
615   StringRef getDescription() const final {
616     return "Test patterns to rewrite contiguous row-major N-dimensional "
617            "vector.transfer_{read,write} ops into 1D transfers";
618   }
619   void getDependentDialects(DialectRegistry &registry) const override {
620     registry.insert<memref::MemRefDialect>();
621   }
622   void runOnFunction() override {
623     RewritePatternSet patterns(&getContext());
624     populateFlattenVectorTransferPatterns(patterns);
625     (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
626   }
627 };
628 
629 } // namespace
630 
631 namespace mlir {
632 namespace test {
633 void registerTestVectorLowerings() {
634   PassRegistration<TestVectorToVectorLowering>();
635 
636   PassRegistration<TestVectorContractionLowering>();
637 
638   PassRegistration<TestVectorTransposeLowering>();
639 
640   PassRegistration<TestVectorUnrollingPatterns>();
641 
642   PassRegistration<TestVectorTransferUnrollingPatterns>();
643 
644   PassRegistration<TestVectorTransferFullPartialSplitPatterns>();
645 
646   PassRegistration<TestVectorDistributePatterns>();
647 
648   PassRegistration<TestVectorToLoopPatterns>();
649 
650   PassRegistration<TestVectorTransferOpt>();
651 
652   PassRegistration<TestVectorTransferLoweringPatterns>();
653 
654   PassRegistration<TestVectorMultiReductionLoweringPatterns>();
655 
656   PassRegistration<TestVectorTransferCollapseInnerMostContiguousDims>();
657 
658   PassRegistration<TestVectorReduceToContractPatternsPatterns>();
659 
660   PassRegistration<TestVectorTransferDropUnitDimsPatterns>();
661 
662   PassRegistration<TestFlattenVectorTransferPatterns>();
663 }
664 } // namespace test
665 } // namespace mlir
666