1 //===- TestLoopParametricTiling.cpp --- Parametric loop tiling pass -------===//
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 // This file implements a pass to parametrically tile nests of standard loops.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/SCF/SCF.h"
14 #include "mlir/Dialect/SCF/Utils/Utils.h"
15 #include "mlir/IR/Builders.h"
16 #include "mlir/Pass/Pass.h"
17 
18 using namespace mlir;
19 
20 namespace {
21 
22 // Extracts fixed-range loops for top-level loop nests with ranges defined in
23 // the pass constructor.  Assumes loops are permutable.
24 class SimpleParametricLoopTilingPass
25     : public PassWrapper<SimpleParametricLoopTilingPass,
26                          OperationPass<FuncOp>> {
27 public:
28   StringRef getArgument() const final {
29     return "test-extract-fixed-outer-loops";
30   }
31   StringRef getDescription() const final {
32     return "test application of parametric tiling to the outer loops so that "
33            "the ranges of outer loops become static";
34   }
35   SimpleParametricLoopTilingPass() = default;
36   SimpleParametricLoopTilingPass(const SimpleParametricLoopTilingPass &) {}
37   explicit SimpleParametricLoopTilingPass(ArrayRef<int64_t> outerLoopSizes) {
38     sizes = outerLoopSizes;
39   }
40 
41   void runOnOperation() override {
42     FuncOp func = getOperation();
43     func.walk([this](scf::ForOp op) {
44       // Ignore nested loops.
45       if (op->getParentRegion()->getParentOfType<scf::ForOp>())
46         return;
47       extractFixedOuterLoops(op, sizes);
48     });
49   }
50 
51   ListOption<int64_t> sizes{
52       *this, "test-outer-loop-sizes", llvm::cl::MiscFlags::CommaSeparated,
53       llvm::cl::desc(
54           "fixed number of iterations that the outer loops should have")};
55 };
56 } // namespace
57 
58 namespace mlir {
59 namespace test {
60 void registerSimpleParametricTilingPass() {
61   PassRegistration<SimpleParametricLoopTilingPass>();
62 }
63 } // namespace test
64 } // namespace mlir
65