1 //= TestAffineLoopParametricTiling.cpp -- Parametric Affine 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 test pass to test parametric tiling of perfectly
10 // nested affine for loops.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Affine/IR/AffineOps.h"
15 #include "mlir/Dialect/Affine/LoopUtils.h"
16 #include "mlir/Dialect/Affine/Passes.h"
17 #include "mlir/Dialect/Func/IR/FuncOps.h"
18 
19 using namespace mlir;
20 
21 #define DEBUG_TYPE "test-affine-parametric-tile"
22 
23 namespace {
24 struct TestAffineLoopParametricTiling
25     : public PassWrapper<TestAffineLoopParametricTiling,
26                          OperationPass<FuncOp>> {
27   StringRef getArgument() const final { return "test-affine-parametric-tile"; }
28   StringRef getDescription() const final {
29     return "Tile affine loops using SSA values as tile sizes";
30   }
31   void runOnOperation() override;
32 };
33 } // namespace
34 
35 /// Checks if the function enclosing the loop nest has any arguments passed to
36 /// it, which can be used as tiling parameters. Assumes that atleast 'n'
37 /// arguments are passed, where 'n' is the number of loops in the loop nest.
38 static void checkIfTilingParametersExist(ArrayRef<AffineForOp> band) {
39   assert(!band.empty() && "no loops in input band");
40   AffineForOp topLoop = band[0];
41 
42   if (FuncOp funcOp = dyn_cast<FuncOp>(topLoop->getParentOp()))
43     assert(funcOp.getNumArguments() >= band.size() && "Too few tile sizes");
44 }
45 
46 /// Captures tiling parameters, which are expected to be passed as arguments
47 /// to the function enclosing the loop nest. Also checks if the required
48 /// parameters are of index type. This approach is temporary for testing
49 /// purposes.
50 static void getTilingParameters(ArrayRef<AffineForOp> band,
51                                 SmallVectorImpl<Value> &tilingParameters) {
52   AffineForOp topLoop = band[0];
53   Region *funcOpRegion = topLoop->getParentRegion();
54   unsigned nestDepth = band.size();
55 
56   for (BlockArgument blockArgument :
57        funcOpRegion->getArguments().take_front(nestDepth)) {
58     if (blockArgument.getArgNumber() < nestDepth) {
59       assert(blockArgument.getType().isIndex() &&
60              "expected tiling parameters to be of index type.");
61       tilingParameters.push_back(blockArgument);
62     }
63   }
64 }
65 
66 void TestAffineLoopParametricTiling::runOnOperation() {
67   // Bands of loops to tile.
68   std::vector<SmallVector<AffineForOp, 6>> bands;
69   getTileableBands(getOperation(), &bands);
70 
71   // Tile each band.
72   for (MutableArrayRef<AffineForOp> band : bands) {
73     // Capture the tiling parameters from the arguments to the function
74     // enclosing this loop nest.
75     SmallVector<AffineForOp, 6> tiledNest;
76     SmallVector<Value, 6> tilingParameters;
77     // Check if tiling parameters are present.
78     checkIfTilingParametersExist(band);
79 
80     // Get function arguments as tiling parameters.
81     getTilingParameters(band, tilingParameters);
82 
83     (void)tilePerfectlyNestedParametric(band, tilingParameters, &tiledNest);
84   }
85 }
86 
87 namespace mlir {
88 namespace test {
89 void registerTestAffineLoopParametricTilingPass() {
90   PassRegistration<TestAffineLoopParametricTiling>();
91 }
92 } // namespace test
93 } // namespace mlir
94