1 //===- TestLoopPermutation.cpp - Test affine loop permutation -------------===//
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 pass to test the affine for op permutation utility.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/Dialect/Affine/Analysis/Utils.h"
14 #include "mlir/Dialect/Affine/IR/AffineOps.h"
15 #include "mlir/Dialect/Affine/LoopUtils.h"
16 #include "mlir/Pass/Pass.h"
18 #define PASS_NAME "test-loop-permutation"
24 /// This pass applies the permutation on the first maximal perfect nest.
25 struct TestLoopPermutation
26 : public PassWrapper
<TestLoopPermutation
, OperationPass
<>> {
27 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLoopPermutation
)
29 StringRef
getArgument() const final
{ return PASS_NAME
; }
30 StringRef
getDescription() const final
{
31 return "Tests affine loop permutation utility";
33 TestLoopPermutation() = default;
34 TestLoopPermutation(const TestLoopPermutation
&pass
) : PassWrapper(pass
){};
36 void runOnOperation() override
;
39 /// Permutation specifying loop i is mapped to permList[i] in
40 /// transformed nest (with i going from outermost to innermost).
41 ListOption
<unsigned> permList
{*this, "permutation-map",
42 llvm::cl::desc("Specify the loop permutation"),
48 void TestLoopPermutation::runOnOperation() {
50 SmallVector
<unsigned, 4> permMap(permList
.begin(), permList
.end());
52 SmallVector
<AffineForOp
, 2> forOps
;
53 getOperation()->walk([&](AffineForOp forOp
) { forOps
.push_back(forOp
); });
55 for (auto forOp
: forOps
) {
56 SmallVector
<AffineForOp
, 6> nest
;
57 // Get the maximal perfect nest.
58 getPerfectlyNestedLoops(nest
, forOp
);
59 // Permute if the nest's size is consistent with the specified
61 if (nest
.size() >= 2 && nest
.size() == permMap
.size()) {
62 permuteLoops(nest
, permMap
);
68 void registerTestLoopPermutationPass() {
69 PassRegistration
<TestLoopPermutation
>();