github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/test/lib/Transforms/TestLoopParametricTiling.cpp (about) 1 //===- TestLoopParametricTiling.cpp --- Parametric loop tiling pass -------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 // 18 // This file implements a pass to parametrically tile nests of standard loops. 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "mlir/Dialect/LoopOps/LoopOps.h" 23 #include "mlir/IR/Builders.h" 24 #include "mlir/Pass/Pass.h" 25 #include "mlir/Transforms/LoopUtils.h" 26 #include "mlir/Transforms/Passes.h" 27 28 using namespace mlir; 29 30 static llvm::cl::list<int> clOuterLoopSizes( 31 "test-outer-loop-sizes", llvm::cl::MiscFlags::CommaSeparated, 32 llvm::cl::desc( 33 "fixed number of iterations that the outer loops should have")); 34 35 namespace { 36 // Extracts fixed-range loops for top-level loop nests with ranges defined in 37 // the pass constructor. Assumes loops are permutable. 38 class SimpleParametricLoopTilingPass 39 : public FunctionPass<SimpleParametricLoopTilingPass> { 40 public: 41 explicit SimpleParametricLoopTilingPass(ArrayRef<int64_t> outerLoopSizes) 42 : sizes(outerLoopSizes.begin(), outerLoopSizes.end()) {} 43 44 void runOnFunction() override { 45 FuncOp func = getFunction(); 46 func.walk([this](loop::ForOp op) { 47 // Ignore nested loops. 48 if (op.getParentRegion()->getParentOfType<loop::ForOp>()) 49 return; 50 extractFixedOuterLoops(op, sizes); 51 }); 52 } 53 54 SmallVector<int64_t, 4> sizes; 55 }; 56 } // end namespace 57 58 std::unique_ptr<FunctionPassBase> 59 mlir::createSimpleParametricTilingPass(ArrayRef<int64_t> outerLoopSizes) { 60 return std::make_unique<SimpleParametricLoopTilingPass>(outerLoopSizes); 61 } 62 63 static PassRegistration<SimpleParametricLoopTilingPass> 64 reg("test-extract-fixed-outer-loops", 65 "test application of parametric tiling to the outer loops so that the " 66 "ranges of outer loops become static", 67 [] { 68 auto pass = std::make_unique<SimpleParametricLoopTilingPass>( 69 ArrayRef<int64_t>{}); 70 pass->sizes.assign(clOuterLoopSizes.begin(), clOuterLoopSizes.end()); 71 return pass; 72 });