blob: 337f558b17abe93e01b8f12bd7a1f7a1167ceec4 [file] [log] [blame]
Uday Bondhugula0b4059b2018-07-24 20:01:16 -07001//===- Unroll.cpp - Code to perform loop unrolling ---------------------===//
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
22#include "mlir/IR/Builders.h"
23#include "mlir/IR/CFGFunction.h"
24#include "mlir/IR/MLFunction.h"
25#include "mlir/IR/Module.h"
26#include "mlir/IR/OperationSet.h"
27#include "mlir/IR/Statements.h"
28#include "mlir/IR/StmtVisitor.h"
29#include "mlir/Pass.h"
30#include "mlir/Transforms/Loop.h"
31
32using namespace mlir;
33
34namespace {
35struct LoopUnroll : public MLFunctionPass {
36 bool runOnMLFunction(MLFunction *f);
37 bool runOnForStmt(ForStmt *forStmt);
38 bool runLoopUnroll(MLFunction *f);
39};
40} // namespace
41
42MLFunctionPass *mlir::createLoopUnrollPass() { return new LoopUnroll(); }
43
44/// Unrolls all the innermost loops of this Module.
45bool MLFunctionPass::runOnModule(Module *m) {
46 bool changed = false;
Chris Lattnera8e47672018-07-25 14:08:16 -070047 for (auto &fn : *m) {
48 if (auto *mlFunc = dyn_cast<MLFunction>(&fn))
Uday Bondhugula0b4059b2018-07-24 20:01:16 -070049 changed |= runOnMLFunction(mlFunc);
50 }
51 return changed;
52}
53
54/// Unrolls all the innermost loops of this MLFunction.
55bool LoopUnroll::runOnMLFunction(MLFunction *f) {
56 // Gathers all innermost loops. TODO: change the visitor to post order to make
57 // this linear time / single traversal.
58 struct InnermostLoopGatherer : public StmtVisitor<InnermostLoopGatherer> {
59 std::vector<ForStmt *> loops;
60 InnermostLoopGatherer() {}
61 void visitForStmt(ForStmt *fs) {
62 if (fs->isInnermost())
63 loops.push_back(fs);
64 }
65 };
66
67 InnermostLoopGatherer ilg;
68 ilg.visit(f);
69 auto &loops = ilg.loops;
70 bool changed = false;
71 for (auto *forStmt : loops)
72 changed |= runOnForStmt(forStmt);
73 return changed;
74}
75
76/// Unrolls this loop completely. Returns true if the unrolling happens.
77bool LoopUnroll::runOnForStmt(ForStmt *forStmt) {
78 auto lb = forStmt->getLowerBound()->getValue();
79 auto ub = forStmt->getUpperBound()->getValue();
80 auto step = forStmt->getStep()->getValue();
81 auto trip_count = (ub - lb + 1) / step;
82
83 auto *block = forStmt->getBlock();
84
85 MLFuncBuilder builder(forStmt->Statement::getFunction());
86 builder.setInsertionPoint(block);
87
88 for (int i = 0; i < trip_count; i++) {
89 for (auto &stmt : forStmt->getStatements()) {
90 switch (stmt.getKind()) {
91 case Statement::Kind::For:
92 llvm_unreachable("unrolling loops that have only operations");
93 break;
94 case Statement::Kind::If:
95 llvm_unreachable("unrolling loops that have only operations");
96 break;
97 case Statement::Kind::Operation:
98 auto *op = cast<OperationStmt>(&stmt);
99 builder.createOperation(op->getName(), op->getAttrs());
100 // TODO: loop iterator parsing not yet implemented; replace loop
101 // iterator uses in unrolled body appropriately.
102 break;
103 }
104 }
105 }
106
107 forStmt->eraseFromBlock();
108 return true;
109}