blob: 27bb43f7dee7accdfa12fc97b7a5aa387503ed65 [file] [log] [blame]
Chris Lattneree0c2ae2018-07-29 12:37:35 -07001//===- Unroll.cpp - Code to perform loop unrolling ------------------------===//
Uday Bondhugula0b4059b2018-07-24 20:01:16 -07002//
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 Bondhugula15984952018-08-01 22:36:12 -070022#include "mlir/IR/Attributes.h"
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070023#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 Lattneree0c2ae2018-07-29 12:37:35 -070028#include "mlir/IR/Pass.h"
Uday Bondhugula84b80952018-08-03 13:22:26 -070029#include "mlir/IR/StandardOps.h"
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070030#include "mlir/IR/Statements.h"
31#include "mlir/IR/StmtVisitor.h"
Chris Lattneree0c2ae2018-07-29 12:37:35 -070032#include "mlir/Transforms/Passes.h"
Uday Bondhugula081d9e72018-07-27 10:58:14 -070033#include "llvm/Support/raw_ostream.h"
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070034
35using namespace mlir;
36
37namespace {
38struct LoopUnroll : public MLFunctionPass {
Chris Lattneree0c2ae2018-07-29 12:37:35 -070039 void runOnMLFunction(MLFunction *f) override;
40 void runOnForStmt(ForStmt *forStmt);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070041};
Chris Lattneree0c2ae2018-07-29 12:37:35 -070042} // end anonymous namespace
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070043
44MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); }
45
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070046/// Unrolls all the innermost loops of this MLFunction.
Chris Lattneree0c2ae2018-07-29 12:37:35 -070047void LoopUnroll::runOnMLFunction(MLFunction *f) {
Uday Bondhugula081d9e72018-07-27 10:58:14 -070048 // 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 Bondhugula0b4059b2018-07-24 20:01:16 -070053 std::vector<ForStmt *> loops;
Uday Bondhugula081d9e72018-07-27 10:58:14 -070054
55 // This method specialized to encode custom return logic.
56 typedef llvm::iplist<Statement> StmtListType;
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070057 bool walkPostOrder(StmtListType::iterator Start,
58 StmtListType::iterator End) {
Uday Bondhugula15984952018-08-01 22:36:12 -070059 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 Bondhugula081d9e72018-07-27 10:58:14 -070063 while (Start != End)
Uday Bondhugula15984952018-08-01 22:36:12 -070064 hasInnerLoops |= walkPostOrder(&(*Start++));
65 return hasInnerLoops;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070066 }
Uday Bondhugula081d9e72018-07-27 10:58:14 -070067
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 Bondhugula8572d1a2018-07-30 10:49:49 -070071 void walkPostOrder(MLFunction *f) { walkPostOrder(f->begin(), f->end()); }
Uday Bondhugula081d9e72018-07-27 10:58:14 -070072
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070073 bool walkForStmtPostOrder(ForStmt *forStmt) {
74 bool hasInnerLoops = walkPostOrder(forStmt->begin(), forStmt->end());
Uday Bondhugula081d9e72018-07-27 10:58:14 -070075 if (!hasInnerLoops)
76 loops.push_back(forStmt);
77 return true;
78 }
79
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070080 bool walkIfStmtPostOrder(IfStmt *ifStmt) {
Uday Bondhugula15984952018-08-01 22:36:12 -070081 bool hasInnerLoops = walkPostOrder(ifStmt->getThenClause()->begin(),
82 ifStmt->getThenClause()->end());
83 hasInnerLoops |= walkPostOrder(ifStmt->getElseClause()->begin(),
84 ifStmt->getElseClause()->end());
85 return hasInnerLoops;
Uday Bondhugula081d9e72018-07-27 10:58:14 -070086 }
87
88 bool walkOpStmt(OperationStmt *opStmt) { return false; }
89
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070090 using StmtWalker<InnermostLoopGatherer, bool>::walkPostOrder;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070091 };
92
93 InnermostLoopGatherer ilg;
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070094 ilg.walkPostOrder(f);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070095 auto &loops = ilg.loops;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070096 for (auto *forStmt : loops)
Chris Lattneree0c2ae2018-07-29 12:37:35 -070097 runOnForStmt(forStmt);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070098}
99
Uday Bondhugula84b80952018-08-03 13:22:26 -0700100/// Replace all uses of 'oldVal' with 'newVal' in 'stmt'
101static 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 Bondhugula15984952018-08-01 22:36:12 -0700108
Uday Bondhugula84b80952018-08-03 13:22:26 -0700109 ReplaceUseWalker(MLValue *oldVal, MLValue *newVal)
110 : oldVal(oldVal), newVal(newVal){};
Uday Bondhugula15984952018-08-01 22:36:12 -0700111
112 void visitOperationStmt(OperationStmt *os) {
113 for (auto &operand : os->getStmtOperands()) {
Uday Bondhugula84b80952018-08-03 13:22:26 -0700114 if (operand.get() == oldVal)
115 operand.set(newVal);
Uday Bondhugula15984952018-08-01 22:36:12 -0700116 }
117 }
118 };
119
Uday Bondhugula84b80952018-08-03 13:22:26 -0700120 ReplaceUseWalker ri(oldVal, newVal);
Uday Bondhugula15984952018-08-01 22:36:12 -0700121 ri.walk(stmt);
122}
123
Uday Bondhugula84b80952018-08-03 13:22:26 -0700124/// Unroll this 'for stmt' / loop completely.
Chris Lattneree0c2ae2018-07-29 12:37:35 -0700125void LoopUnroll::runOnForStmt(ForStmt *forStmt) {
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700126 auto lb = forStmt->getLowerBound()->getValue();
127 auto ub = forStmt->getUpperBound()->getValue();
128 auto step = forStmt->getStep()->getValue();
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700129
Uday Bondhugula84b80952018-08-03 13:22:26 -0700130 // Builder to add constants need for the unrolled iterator.
Uday Bondhugula15984952018-08-01 22:36:12 -0700131 auto *mlFunc = forStmt->Statement::findFunction();
132 MLFuncBuilder funcTopBuilder(mlFunc);
133 funcTopBuilder.setInsertionPointAtStart(mlFunc);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700134
Uday Bondhugula84b80952018-08-03 13:22:26 -0700135 // Builder to insert the unrolled bodies.
Uday Bondhugula15984952018-08-01 22:36:12 -0700136 MLFuncBuilder builder(forStmt->getBlock());
Uday Bondhugula84b80952018-08-03 13:22:26 -0700137 // 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 Bondhugula0b4059b2018-07-24 20:01:16 -0700185 }
186 }
187 }
Uday Bondhugula84b80952018-08-03 13:22:26 -0700188 // Erase the original for stmt from the block.
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700189 forStmt->eraseFromBlock();
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700190}