Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 1 | //===- Unroll.cpp - Code to perform loop unrolling ------------------------===// |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 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 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Attributes.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 23 | #include "mlir/IR/Builders.h" |
| 24 | #include "mlir/IR/CFGFunction.h" |
| 25 | #include "mlir/IR/MLFunction.h" |
| 26 | #include "mlir/IR/Module.h" |
| 27 | #include "mlir/IR/OperationSet.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 28 | #include "mlir/IR/Pass.h" |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 29 | #include "mlir/IR/StandardOps.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 30 | #include "mlir/IR/Statements.h" |
| 31 | #include "mlir/IR/StmtVisitor.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 32 | #include "mlir/Transforms/Passes.h" |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace mlir; |
| 36 | |
| 37 | namespace { |
| 38 | struct LoopUnroll : public MLFunctionPass { |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 39 | void runOnMLFunction(MLFunction *f) override; |
| 40 | void runOnForStmt(ForStmt *forStmt); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 41 | }; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 42 | } // end anonymous namespace |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 43 | |
| 44 | MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); } |
| 45 | |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 46 | /// Unrolls all the innermost loops of this MLFunction. |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 47 | void LoopUnroll::runOnMLFunction(MLFunction *f) { |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 48 | // Gathers all innermost loops through a post order pruned walk. |
| 49 | // TODO: figure out the right reusable template here to better refactor code. |
| 50 | class InnermostLoopGatherer : public StmtWalker<InnermostLoopGatherer, bool> { |
| 51 | public: |
| 52 | // Store innermost loops as we walk. |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 53 | std::vector<ForStmt *> loops; |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 54 | |
| 55 | // This method specialized to encode custom return logic. |
| 56 | typedef llvm::iplist<Statement> StmtListType; |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 57 | bool walkPostOrder(StmtListType::iterator Start, |
| 58 | StmtListType::iterator End) { |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 59 | bool hasInnerLoops = false; |
| 60 | // We need to walk all elements since all innermost loops need to be |
| 61 | // gathered as opposed to determining whether this list has any inner |
| 62 | // loops or not. |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 63 | while (Start != End) |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 64 | hasInnerLoops |= walkPostOrder(&(*Start++)); |
| 65 | return hasInnerLoops; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 66 | } |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 67 | |
| 68 | // FIXME: can't use base class method for this because that in turn would |
| 69 | // need to use the derived class method above. CRTP doesn't allow it, and |
| 70 | // the compiler error resulting from it is also very misleading! |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 71 | void walkPostOrder(MLFunction *f) { walkPostOrder(f->begin(), f->end()); } |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 72 | |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 73 | bool walkForStmtPostOrder(ForStmt *forStmt) { |
| 74 | bool hasInnerLoops = walkPostOrder(forStmt->begin(), forStmt->end()); |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 75 | if (!hasInnerLoops) |
| 76 | loops.push_back(forStmt); |
| 77 | return true; |
| 78 | } |
| 79 | |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 80 | bool walkIfStmtPostOrder(IfStmt *ifStmt) { |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 81 | bool hasInnerLoops = walkPostOrder(ifStmt->getThenClause()->begin(), |
| 82 | ifStmt->getThenClause()->end()); |
| 83 | hasInnerLoops |= walkPostOrder(ifStmt->getElseClause()->begin(), |
| 84 | ifStmt->getElseClause()->end()); |
| 85 | return hasInnerLoops; |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool walkOpStmt(OperationStmt *opStmt) { return false; } |
| 89 | |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 90 | using StmtWalker<InnermostLoopGatherer, bool>::walkPostOrder; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | InnermostLoopGatherer ilg; |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 94 | ilg.walkPostOrder(f); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 95 | auto &loops = ilg.loops; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 96 | for (auto *forStmt : loops) |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 97 | runOnForStmt(forStmt); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 100 | /// Replace all uses of 'oldVal' with 'newVal' in 'stmt' |
| 101 | static void replaceAllStmtUses(Statement *stmt, MLValue *oldVal, |
| 102 | MLValue *newVal) { |
| 103 | struct ReplaceUseWalker : public StmtWalker<ReplaceUseWalker> { |
| 104 | // Value to be replaced. |
| 105 | MLValue *oldVal; |
| 106 | // Value to be replaced with. |
| 107 | MLValue *newVal; |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 108 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 109 | ReplaceUseWalker(MLValue *oldVal, MLValue *newVal) |
| 110 | : oldVal(oldVal), newVal(newVal){}; |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 111 | |
| 112 | void visitOperationStmt(OperationStmt *os) { |
| 113 | for (auto &operand : os->getStmtOperands()) { |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 114 | if (operand.get() == oldVal) |
| 115 | operand.set(newVal); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 120 | ReplaceUseWalker ri(oldVal, newVal); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 121 | ri.walk(stmt); |
| 122 | } |
| 123 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 124 | /// Unroll this 'for stmt' / loop completely. |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 125 | void LoopUnroll::runOnForStmt(ForStmt *forStmt) { |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 126 | auto lb = forStmt->getLowerBound()->getValue(); |
| 127 | auto ub = forStmt->getUpperBound()->getValue(); |
| 128 | auto step = forStmt->getStep()->getValue(); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 129 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 130 | // Builder to add constants need for the unrolled iterator. |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 131 | auto *mlFunc = forStmt->Statement::findFunction(); |
| 132 | MLFuncBuilder funcTopBuilder(mlFunc); |
| 133 | funcTopBuilder.setInsertionPointAtStart(mlFunc); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 134 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 135 | // Builder to insert the unrolled bodies. |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 136 | MLFuncBuilder builder(forStmt->getBlock()); |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 137 | // Set insertion point to right after where the for stmt ends. |
| 138 | builder.setInsertionPoint(forStmt->getBlock(), |
| 139 | ++StmtBlock::iterator(forStmt)); |
| 140 | |
| 141 | // Unroll the contents of 'forStmt'. |
| 142 | for (int i = lb; i <= ub; i += step) { |
| 143 | // TODO(bondhugula): generate constants only when IV actually appears. |
| 144 | auto constOp = funcTopBuilder.create<ConstantIntOp>(i, 32); |
| 145 | auto *ivConst = cast<OperationStmt>(constOp->getOperation())->getResult(0); |
| 146 | |
| 147 | // Iterator pointing to just before 'this' (i^th) unrolled iteration. |
| 148 | StmtBlock::iterator beforeUnrolledBody = --builder.getInsertionPoint(); |
| 149 | |
| 150 | // Pairs of <old op stmt result whose uses need to be replaced, |
| 151 | // new result generated by the corresponding cloned op stmt>. |
| 152 | SmallVector<std::pair<MLValue *, MLValue *>, 8> oldNewResultPairs; |
| 153 | |
| 154 | for (auto &loopBodyStmt : forStmt->getStatements()) { |
| 155 | auto *cloneStmt = builder.clone(loopBodyStmt); |
| 156 | // Replace all uses of the IV in the clone with constant iteration value. |
| 157 | replaceAllStmtUses(cloneStmt, forStmt, ivConst); |
| 158 | |
| 159 | // Whenever we have an op stmt, we'll have a new ML Value defined: replace |
| 160 | // uses of the old result with this one. |
| 161 | if (auto *opStmt = dyn_cast<OperationStmt>(&loopBodyStmt)) { |
| 162 | if (opStmt->getNumResults()) { |
| 163 | auto *cloneOpStmt = cast<OperationStmt>(cloneStmt); |
| 164 | for (unsigned i = 0, e = opStmt->getNumResults(); i < e; i++) { |
| 165 | // Store old/new result pairs. |
| 166 | // TODO *only* if needed later: storing of old/new results can be |
| 167 | // avoided, by cloning the statement list in the reverse direction |
| 168 | // (and running the IR builder in the reverse |
| 169 | // (iplist.insertAfter()). That way, a newly created result can be |
| 170 | // immediately propagated to all its uses, which would already been |
| 171 | // cloned/inserted. |
| 172 | oldNewResultPairs.push_back(std::make_pair( |
| 173 | &opStmt->getStmtResult(i), &cloneOpStmt->getStmtResult(i))); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | // Replace uses of old op results' with the results in the just |
| 179 | // unrolled body. |
| 180 | StmtBlock::iterator endOfUnrolledBody = builder.getInsertionPoint(); |
| 181 | for (auto it = ++beforeUnrolledBody; it != endOfUnrolledBody; it++) { |
| 182 | for (unsigned i = 0; i < oldNewResultPairs.size(); i++) { |
| 183 | replaceAllStmtUses(&(*it), oldNewResultPairs[i].first, |
| 184 | oldNewResultPairs[i].second); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame^] | 188 | // Erase the original for stmt from the block. |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 189 | forStmt->eraseFromBlock(); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 190 | } |