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/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" 17 18 #define PASS_NAME "test-loop-permutation" 19 20 using namespace mlir; 21 22 static llvm::cl::OptionCategory clOptionsCategory(PASS_NAME " options"); 23 24 namespace { 25 26 /// This pass applies the permutation on the first maximal perfect nest. 27 struct TestLoopPermutation 28 : public PassWrapper<TestLoopPermutation, OperationPass<FuncOp>> { 29 StringRef getArgument() const final { return PASS_NAME; } 30 StringRef getDescription() const final { 31 return "Tests affine loop permutation utility"; 32 } 33 TestLoopPermutation() = default; 34 TestLoopPermutation(const TestLoopPermutation &pass) : PassWrapper(pass){}; 35 36 void runOnOperation() override; 37 38 private: 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"), 43 llvm::cl::OneOrMore, llvm::cl::CommaSeparated}; 44 }; 45 46 } // namespace 47 48 void TestLoopPermutation::runOnOperation() { 49 50 SmallVector<unsigned, 4> permMap(permList.begin(), permList.end()); 51 52 SmallVector<AffineForOp, 2> forOps; 53 getOperation().walk([&](AffineForOp forOp) { forOps.push_back(forOp); }); 54 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 60 // permutation. 61 if (nest.size() >= 2 && nest.size() == permMap.size()) { 62 permuteLoops(nest, permMap); 63 } 64 } 65 } 66 67 namespace mlir { 68 void registerTestLoopPermutationPass() { 69 PassRegistration<TestLoopPermutation>(); 70 } 71 } // namespace mlir 72