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