blob: cacb207bc620026af6bfc57c5fb08530ab454649 [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"
Uday Bondhugula84b80952018-08-03 13:22:26 -070028#include "mlir/IR/StandardOps.h"
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070029#include "mlir/IR/Statements.h"
30#include "mlir/IR/StmtVisitor.h"
Uday Bondhugula6c1f6602018-08-13 17:25:13 -070031#include "mlir/Transforms/Pass.h"
Chris Lattneree0c2ae2018-07-29 12:37:35 -070032#include "mlir/Transforms/Passes.h"
Chris Lattnere787b322018-08-08 11:14:57 -070033#include "llvm/ADT/DenseMap.h"
Uday Bondhugula081d9e72018-07-27 10:58:14 -070034#include "llvm/Support/raw_ostream.h"
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070035
36using namespace mlir;
37
38namespace {
39struct LoopUnroll : public MLFunctionPass {
Chris Lattneree0c2ae2018-07-29 12:37:35 -070040 void runOnMLFunction(MLFunction *f) override;
41 void runOnForStmt(ForStmt *forStmt);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070042};
Uday Bondhugula134154e2018-08-06 18:40:34 -070043struct ShortLoopUnroll : public LoopUnroll {
44 const unsigned minTripCount;
45 void runOnMLFunction(MLFunction *f) override;
46 ShortLoopUnroll(unsigned minTripCount) : minTripCount(minTripCount) {}
47};
Chris Lattneree0c2ae2018-07-29 12:37:35 -070048} // end anonymous namespace
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070049
50MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); }
51
Uday Bondhugula134154e2018-08-06 18:40:34 -070052MLFunctionPass *mlir::createLoopUnrollPass(unsigned minTripCount) {
53 return new ShortLoopUnroll(minTripCount);
54}
55
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070056/// Unrolls all the innermost loops of this MLFunction.
Chris Lattneree0c2ae2018-07-29 12:37:35 -070057void LoopUnroll::runOnMLFunction(MLFunction *f) {
Uday Bondhugula081d9e72018-07-27 10:58:14 -070058 // Gathers all innermost loops through a post order pruned walk.
Uday Bondhugula081d9e72018-07-27 10:58:14 -070059 class InnermostLoopGatherer : public StmtWalker<InnermostLoopGatherer, bool> {
60 public:
61 // Store innermost loops as we walk.
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070062 std::vector<ForStmt *> loops;
Uday Bondhugula081d9e72018-07-27 10:58:14 -070063
64 // This method specialized to encode custom return logic.
65 typedef llvm::iplist<Statement> StmtListType;
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070066 bool walkPostOrder(StmtListType::iterator Start,
67 StmtListType::iterator End) {
Uday Bondhugula15984952018-08-01 22:36:12 -070068 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 Bondhugula081d9e72018-07-27 10:58:14 -070072 while (Start != End)
Uday Bondhugula15984952018-08-01 22:36:12 -070073 hasInnerLoops |= walkPostOrder(&(*Start++));
74 return hasInnerLoops;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070075 }
Uday Bondhugula081d9e72018-07-27 10:58:14 -070076
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070077 bool walkForStmtPostOrder(ForStmt *forStmt) {
78 bool hasInnerLoops = walkPostOrder(forStmt->begin(), forStmt->end());
Uday Bondhugula081d9e72018-07-27 10:58:14 -070079 if (!hasInnerLoops)
80 loops.push_back(forStmt);
Chris Lattnere787b322018-08-08 11:14:57 -070081
Uday Bondhugula081d9e72018-07-27 10:58:14 -070082 return true;
83 }
84
Uday Bondhugula8572d1a2018-07-30 10:49:49 -070085 bool walkIfStmtPostOrder(IfStmt *ifStmt) {
Chris Lattnere787b322018-08-08 11:14:57 -070086 bool hasInnerLoops =
87 walkPostOrder(ifStmt->getThen()->begin(), ifStmt->getThen()->end());
88 hasInnerLoops |=
89 walkPostOrder(ifStmt->getElse()->begin(), ifStmt->getElse()->end());
Uday Bondhugula15984952018-08-01 22:36:12 -070090 return hasInnerLoops;
Uday Bondhugula081d9e72018-07-27 10:58:14 -070091 }
92
Uday Bondhugula134154e2018-08-06 18:40:34 -070093 bool visitOperationStmt(OperationStmt *opStmt) { return false; }
Uday Bondhugula081d9e72018-07-27 10:58:14 -070094
Uday Bondhugula134154e2018-08-06 18:40:34 -070095 // 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 Bondhugula8572d1a2018-07-30 10:49:49 -070098 using StmtWalker<InnermostLoopGatherer, bool>::walkPostOrder;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070099 };
100
101 InnermostLoopGatherer ilg;
Uday Bondhugula8572d1a2018-07-30 10:49:49 -0700102 ilg.walkPostOrder(f);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700103 auto &loops = ilg.loops;
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700104 for (auto *forStmt : loops)
Chris Lattneree0c2ae2018-07-29 12:37:35 -0700105 runOnForStmt(forStmt);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700106}
107
Uday Bondhugula134154e2018-08-06 18:40:34 -0700108/// Unrolls all loops with trip count <= minTripCount.
109void 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 Bondhugula15984952018-08-01 22:36:12 -0700117
Uday Bondhugula134154e2018-08-06 18:40:34 -0700118 void visitForStmt(ForStmt *forStmt) {
119 auto lb = forStmt->getLowerBound()->getValue();
120 auto ub = forStmt->getUpperBound()->getValue();
121 auto step = forStmt->getStep()->getValue();
Uday Bondhugula15984952018-08-01 22:36:12 -0700122
Uday Bondhugula134154e2018-08-06 18:40:34 -0700123 if ((ub - lb) / step + 1 <= minTripCount)
124 loops.push_back(forStmt);
Uday Bondhugula15984952018-08-01 22:36:12 -0700125 }
126 };
127
Uday Bondhugula134154e2018-08-06 18:40:34 -0700128 ShortLoopGatherer slg(minTripCount);
129 slg.walk(f);
130 auto &loops = slg.loops;
131 for (auto *forStmt : loops)
132 runOnForStmt(forStmt);
133}
134
Chris Lattnere787b322018-08-08 11:14:57 -0700135/// Unroll this For loop completely.
Chris Lattneree0c2ae2018-07-29 12:37:35 -0700136void LoopUnroll::runOnForStmt(ForStmt *forStmt) {
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700137 auto lb = forStmt->getLowerBound()->getValue();
138 auto ub = forStmt->getUpperBound()->getValue();
139 auto step = forStmt->getStep()->getValue();
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700140
Uday Bondhugula84b80952018-08-03 13:22:26 -0700141 // Builder to add constants need for the unrolled iterator.
Chris Lattnere787b322018-08-08 11:14:57 -0700142 auto *mlFunc = forStmt->findFunction();
143 MLFuncBuilder funcTopBuilder(&mlFunc->front());
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700144
Chris Lattnere787b322018-08-08 11:14:57 -0700145 // 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 Bondhugula84b80952018-08-03 13:22:26 -0700148
149 // Unroll the contents of 'forStmt'.
Uday Bondhugula134154e2018-08-06 18:40:34 -0700150 for (int64_t i = lb; i <= ub; i += step) {
Chris Lattnere787b322018-08-08 11:14:57 -0700151 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 Bondhugula134154e2018-08-06 18:40:34 -0700155 if (!forStmt->use_empty()) {
Chris Lattnere787b322018-08-08 11:14:57 -0700156 auto *ivConst =
157 funcTopBuilder.create<ConstantAffineIntOp>(i)->getResult();
158 operandMapping[forStmt] = cast<MLValue>(ivConst);
Uday Bondhugula134154e2018-08-06 18:40:34 -0700159 }
Uday Bondhugula84b80952018-08-03 13:22:26 -0700160
Chris Lattnere787b322018-08-08 11:14:57 -0700161 // Clone the body of the loop.
162 for (auto &childStmt : *forStmt) {
163 (void)builder.clone(childStmt, operandMapping);
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700164 }
165 }
Uday Bondhugula134154e2018-08-06 18:40:34 -0700166 // Erase the original 'for' stmt from the block.
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700167 forStmt->eraseFromBlock();
Uday Bondhugula0b4059b2018-07-24 20:01:16 -0700168}