Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 1 | //===- Unroll.cpp - Code to perform loop unrolling ---------------------===// |
| 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 loop unrolling. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "mlir/IR/Builders.h" |
| 23 | #include "mlir/IR/CFGFunction.h" |
| 24 | #include "mlir/IR/MLFunction.h" |
| 25 | #include "mlir/IR/Module.h" |
| 26 | #include "mlir/IR/OperationSet.h" |
| 27 | #include "mlir/IR/Statements.h" |
| 28 | #include "mlir/IR/StmtVisitor.h" |
| 29 | #include "mlir/Pass.h" |
| 30 | #include "mlir/Transforms/Loop.h" |
| 31 | |
| 32 | using namespace mlir; |
| 33 | |
| 34 | namespace { |
| 35 | struct LoopUnroll : public MLFunctionPass { |
| 36 | bool runOnMLFunction(MLFunction *f); |
| 37 | bool runOnForStmt(ForStmt *forStmt); |
| 38 | bool runLoopUnroll(MLFunction *f); |
| 39 | }; |
| 40 | } // namespace |
| 41 | |
| 42 | MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); } |
| 43 | |
| 44 | /// Unrolls all the innermost loops of this Module. |
| 45 | bool MLFunctionPass::runOnModule(Module *m) { |
| 46 | bool changed = false; |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 47 | for (auto &fn : *m) { |
| 48 | if (auto *mlFunc = dyn_cast<MLFunction>(&fn)) |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 49 | changed |= runOnMLFunction(mlFunc); |
| 50 | } |
| 51 | return changed; |
| 52 | } |
| 53 | |
| 54 | /// Unrolls all the innermost loops of this MLFunction. |
| 55 | bool LoopUnroll::runOnMLFunction(MLFunction *f) { |
| 56 | // Gathers all innermost loops. TODO: change the visitor to post order to make |
| 57 | // this linear time / single traversal. |
| 58 | struct InnermostLoopGatherer : public StmtVisitor<InnermostLoopGatherer> { |
| 59 | std::vector<ForStmt *> loops; |
| 60 | InnermostLoopGatherer() {} |
| 61 | void visitForStmt(ForStmt *fs) { |
| 62 | if (fs->isInnermost()) |
| 63 | loops.push_back(fs); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | InnermostLoopGatherer ilg; |
| 68 | ilg.visit(f); |
| 69 | auto &loops = ilg.loops; |
| 70 | bool changed = false; |
| 71 | for (auto *forStmt : loops) |
| 72 | changed |= runOnForStmt(forStmt); |
| 73 | return changed; |
| 74 | } |
| 75 | |
| 76 | /// Unrolls this loop completely. Returns true if the unrolling happens. |
| 77 | bool LoopUnroll::runOnForStmt(ForStmt *forStmt) { |
| 78 | auto lb = forStmt->getLowerBound()->getValue(); |
| 79 | auto ub = forStmt->getUpperBound()->getValue(); |
| 80 | auto step = forStmt->getStep()->getValue(); |
| 81 | auto trip_count = (ub - lb + 1) / step; |
| 82 | |
| 83 | auto *block = forStmt->getBlock(); |
| 84 | |
| 85 | MLFuncBuilder builder(forStmt->Statement::getFunction()); |
| 86 | builder.setInsertionPoint(block); |
| 87 | |
| 88 | for (int i = 0; i < trip_count; i++) { |
| 89 | for (auto &stmt : forStmt->getStatements()) { |
| 90 | switch (stmt.getKind()) { |
| 91 | case Statement::Kind::For: |
| 92 | llvm_unreachable("unrolling loops that have only operations"); |
| 93 | break; |
| 94 | case Statement::Kind::If: |
| 95 | llvm_unreachable("unrolling loops that have only operations"); |
| 96 | break; |
| 97 | case Statement::Kind::Operation: |
| 98 | auto *op = cast<OperationStmt>(&stmt); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame^] | 99 | // TODO: clone operands and result types. |
| 100 | builder.createOperation(op->getName(), /*operands*/ {}, |
| 101 | /*resultTypes*/ {}, op->getAttrs()); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 102 | // TODO: loop iterator parsing not yet implemented; replace loop |
| 103 | // iterator uses in unrolled body appropriately. |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | forStmt->eraseFromBlock(); |
| 110 | return true; |
| 111 | } |