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" |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 28 | #include "mlir/IR/StandardOps.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 29 | #include "mlir/IR/Statements.h" |
| 30 | #include "mlir/IR/StmtVisitor.h" |
Uday Bondhugula | 6c1f660 | 2018-08-13 17:25:13 -0700 | [diff] [blame] | 31 | #include "mlir/Transforms/Pass.h" |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 32 | #include "mlir/Transforms/Passes.h" |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 35 | |
| 36 | using namespace mlir; |
| 37 | |
| 38 | namespace { |
Uday Bondhugula | 0077e62 | 2018-08-16 13:51:44 -0700 | [diff] [blame] | 39 | /// Loop unrolling pass. For now, this unrolls all the innermost loops of this |
| 40 | /// MLFunction. |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 41 | struct LoopUnroll : public MLFunctionPass { |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 42 | void runOnMLFunction(MLFunction *f) override; |
| 43 | void runOnForStmt(ForStmt *forStmt); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 44 | }; |
Uday Bondhugula | 0077e62 | 2018-08-16 13:51:44 -0700 | [diff] [blame] | 45 | |
| 46 | /// Unrolls all loops with trip count <= minTripCount. |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 47 | struct ShortLoopUnroll : public LoopUnroll { |
| 48 | const unsigned minTripCount; |
| 49 | void runOnMLFunction(MLFunction *f) override; |
| 50 | ShortLoopUnroll(unsigned minTripCount) : minTripCount(minTripCount) {} |
| 51 | }; |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 52 | } // end anonymous namespace |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 53 | |
| 54 | MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); } |
| 55 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 56 | MLFunctionPass *mlir::createLoopUnrollPass(unsigned minTripCount) { |
| 57 | return new ShortLoopUnroll(minTripCount); |
| 58 | } |
| 59 | |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 60 | void LoopUnroll::runOnMLFunction(MLFunction *f) { |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 61 | // Gathers all innermost loops through a post order pruned walk. |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 62 | class InnermostLoopGatherer : public StmtWalker<InnermostLoopGatherer, bool> { |
| 63 | public: |
| 64 | // Store innermost loops as we walk. |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 65 | std::vector<ForStmt *> loops; |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 66 | |
| 67 | // This method specialized to encode custom return logic. |
| 68 | typedef llvm::iplist<Statement> StmtListType; |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 69 | bool walkPostOrder(StmtListType::iterator Start, |
| 70 | StmtListType::iterator End) { |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 71 | bool hasInnerLoops = false; |
| 72 | // We need to walk all elements since all innermost loops need to be |
| 73 | // gathered as opposed to determining whether this list has any inner |
| 74 | // loops or not. |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 75 | while (Start != End) |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 76 | hasInnerLoops |= walkPostOrder(&(*Start++)); |
| 77 | return hasInnerLoops; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 78 | } |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 79 | |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 80 | bool walkForStmtPostOrder(ForStmt *forStmt) { |
| 81 | bool hasInnerLoops = walkPostOrder(forStmt->begin(), forStmt->end()); |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 82 | if (!hasInnerLoops) |
| 83 | loops.push_back(forStmt); |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 84 | |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 88 | bool walkIfStmtPostOrder(IfStmt *ifStmt) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 89 | bool hasInnerLoops = |
| 90 | walkPostOrder(ifStmt->getThen()->begin(), ifStmt->getThen()->end()); |
| 91 | hasInnerLoops |= |
| 92 | walkPostOrder(ifStmt->getElse()->begin(), ifStmt->getElse()->end()); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 93 | return hasInnerLoops; |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 96 | bool visitOperationStmt(OperationStmt *opStmt) { return false; } |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 97 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 98 | // FIXME: can't use base class method for this because that in turn would |
| 99 | // need to use the derived class method above. CRTP doesn't allow it, and |
| 100 | // the compiler error resulting from it is also misleading. |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 101 | using StmtWalker<InnermostLoopGatherer, bool>::walkPostOrder; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | InnermostLoopGatherer ilg; |
Uday Bondhugula | 8572d1a | 2018-07-30 10:49:49 -0700 | [diff] [blame] | 105 | ilg.walkPostOrder(f); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 106 | auto &loops = ilg.loops; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 107 | for (auto *forStmt : loops) |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 108 | runOnForStmt(forStmt); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 111 | void ShortLoopUnroll::runOnMLFunction(MLFunction *f) { |
| 112 | // Gathers all loops with trip count <= minTripCount. |
| 113 | class ShortLoopGatherer : public StmtWalker<ShortLoopGatherer> { |
| 114 | public: |
| 115 | // Store short loops as we walk. |
| 116 | std::vector<ForStmt *> loops; |
| 117 | const unsigned minTripCount; |
| 118 | ShortLoopGatherer(unsigned minTripCount) : minTripCount(minTripCount) {} |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 119 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 120 | void visitForStmt(ForStmt *forStmt) { |
| 121 | auto lb = forStmt->getLowerBound()->getValue(); |
| 122 | auto ub = forStmt->getUpperBound()->getValue(); |
| 123 | auto step = forStmt->getStep()->getValue(); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 124 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 125 | if ((ub - lb) / step + 1 <= minTripCount) |
| 126 | loops.push_back(forStmt); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 127 | } |
| 128 | }; |
| 129 | |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 130 | ShortLoopGatherer slg(minTripCount); |
Uday Bondhugula | 0077e62 | 2018-08-16 13:51:44 -0700 | [diff] [blame] | 131 | // Do a post order walk so that loops are gathered from innermost to |
| 132 | // outermost (or else unrolling an outer one may delete gathered inner ones). |
| 133 | slg.walkPostOrder(f); |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 134 | auto &loops = slg.loops; |
| 135 | for (auto *forStmt : loops) |
| 136 | runOnForStmt(forStmt); |
| 137 | } |
| 138 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 139 | /// Unroll this For loop completely. |
Chris Lattner | ee0c2ae | 2018-07-29 12:37:35 -0700 | [diff] [blame] | 140 | void LoopUnroll::runOnForStmt(ForStmt *forStmt) { |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 141 | auto lb = forStmt->getLowerBound()->getValue(); |
| 142 | auto ub = forStmt->getUpperBound()->getValue(); |
| 143 | auto step = forStmt->getStep()->getValue(); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 144 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 145 | // Builder to add constants need for the unrolled iterator. |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 146 | auto *mlFunc = forStmt->findFunction(); |
| 147 | MLFuncBuilder funcTopBuilder(&mlFunc->front()); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 148 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 149 | // Builder to insert the unrolled bodies. We insert right after the |
| 150 | /// ForStmt we're unrolling. |
| 151 | MLFuncBuilder builder(forStmt->getBlock(), ++StmtBlock::iterator(forStmt)); |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 152 | |
| 153 | // Unroll the contents of 'forStmt'. |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 154 | for (int64_t i = lb; i <= ub; i += step) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 155 | DenseMap<const MLValue *, MLValue *> operandMapping; |
| 156 | |
| 157 | // If the induction variable is used, create a constant for this unrolled |
| 158 | // value and add an operand mapping for it. |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 159 | if (!forStmt->use_empty()) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 160 | auto *ivConst = |
| 161 | funcTopBuilder.create<ConstantAffineIntOp>(i)->getResult(); |
| 162 | operandMapping[forStmt] = cast<MLValue>(ivConst); |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 163 | } |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 164 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 165 | // Clone the body of the loop. |
| 166 | for (auto &childStmt : *forStmt) { |
| 167 | (void)builder.clone(childStmt, operandMapping); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 170 | // Erase the original 'for' stmt from the block. |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 171 | forStmt->eraseFromBlock(); |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 172 | } |