1 //= TestAffineLoopParametricTiling.cpp -- Parametric Affine loop tiling pass =//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a test pass to test parametric tiling of perfectly
10 // nested affine for loops.
12 //===----------------------------------------------------------------------===//
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"
21 #define DEBUG_TYPE "test-affine-parametric-tile"
24 struct TestAffineLoopParametricTiling
25 : public PassWrapper
<TestAffineLoopParametricTiling
,
26 OperationPass
<func::FuncOp
>> {
27 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestAffineLoopParametricTiling
)
29 StringRef
getArgument() const final
{ return "test-affine-parametric-tile"; }
30 StringRef
getDescription() const final
{
31 return "Tile affine loops using SSA values as tile sizes";
33 void runOnOperation() override
;
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.
40 static void checkIfTilingParametersExist(ArrayRef
<AffineForOp
> band
) {
41 assert(!band
.empty() && "no loops in input band");
42 AffineForOp topLoop
= band
[0];
44 if (func::FuncOp funcOp
= dyn_cast
<func::FuncOp
>(topLoop
->getParentOp()))
45 assert(funcOp
.getNumArguments() >= band
.size() && "Too few tile sizes");
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
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();
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
);
68 void TestAffineLoopParametricTiling::runOnOperation() {
69 // Bands of loops to tile.
70 std::vector
<SmallVector
<AffineForOp
, 6>> bands
;
71 getTileableBands(getOperation(), &bands
);
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
);
82 // Get function arguments as tiling parameters.
83 getTilingParameters(band
, tilingParameters
);
85 (void)tilePerfectlyNestedParametric(band
, tilingParameters
, &tiledNest
);
91 void registerTestAffineLoopParametricTilingPass() {
92 PassRegistration
<TestAffineLoopParametricTiling
>();