1 //===- TestLoopPermutation.cpp - Test affine loop permutation -------------===//
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 pass to test the affine for op permutation utility.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Analysis/Utils.h"
14 #include "mlir/Dialect/Affine/IR/AffineOps.h"
15 #include "mlir/Pass/Pass.h"
16 #include "mlir/Transforms/LoopUtils.h"
17 #include "mlir/Transforms/Passes.h"
18 
19 #define PASS_NAME "test-loop-permutation"
20 
21 using namespace mlir;
22 
23 static llvm::cl::OptionCategory clOptionsCategory(PASS_NAME " options");
24 
25 namespace {
26 
27 /// This pass applies the permutation on the first maximal perfect nest.
28 struct TestLoopPermutation
29     : public PassWrapper<TestLoopPermutation, FunctionPass> {
30   TestLoopPermutation() = default;
31   TestLoopPermutation(const TestLoopPermutation &pass){};
32 
33   void runOnFunction() override;
34 
35 private:
36   /// Permutation specifying loop i is mapped to permList[i] in
37   /// transformed nest (with i going from outermost to innermost).
38   ListOption<unsigned> permList{*this, "permutation-map",
39                                 llvm::cl::desc("Specify the loop permutation"),
40                                 llvm::cl::OneOrMore, llvm::cl::CommaSeparated};
41 };
42 
43 } // end anonymous namespace
44 
45 void TestLoopPermutation::runOnFunction() {
46   // Get the first maximal perfect nest.
47   SmallVector<AffineForOp, 6> nest;
48   for (auto &op : getFunction().front()) {
49     if (auto forOp = dyn_cast<AffineForOp>(op)) {
50       getPerfectlyNestedLoops(nest, forOp);
51       break;
52     }
53   }
54 
55   // Nothing to do.
56   if (nest.size() < 2)
57     return;
58 
59   SmallVector<unsigned, 4> permMap(permList.begin(), permList.end());
60   permuteLoops(nest, permMap);
61 }
62 
63 namespace mlir {
64 void registerTestLoopPermutationPass() {
65   PassRegistration<TestLoopPermutation>(
66       PASS_NAME, "Tests affine loop permutation utility");
67 }
68 } // namespace mlir
69