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