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