blob: b1bee48470997237569b1dde82aa6afa9b0b1512 [file] [log] [blame]
Amit Sabne7905da62019-04-17 19:18:371//===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
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 invariant code motion.
19//
20//===----------------------------------------------------------------------===//
21
22#include <iomanip>
23#include <sstream>
24
25#include "mlir/AffineOps/AffineOps.h"
26#include "mlir/Analysis/AffineAnalysis.h"
27#include "mlir/Analysis/AffineStructures.h"
28#include "mlir/Analysis/LoopAnalysis.h"
29#include "mlir/Analysis/SliceAnalysis.h"
30#include "mlir/Analysis/Utils.h"
31#include "mlir/IR/AffineExpr.h"
32#include "mlir/IR/AffineMap.h"
33#include "mlir/IR/Builders.h"
34#include "mlir/Pass/Pass.h"
35#include "mlir/StandardOps/Ops.h"
36#include "mlir/Transforms/LoopUtils.h"
37#include "mlir/Transforms/Passes.h"
38#include "mlir/Transforms/Utils.h"
39#include "llvm/ADT/DenseMap.h"
40#include "llvm/ADT/DenseSet.h"
41#include "llvm/ADT/SetVector.h"
42#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/raw_ostream.h"
45
46#define DEBUG_TYPE "licm"
47
48using llvm::SetVector;
49
50using namespace mlir;
51
52namespace {
53
54/// Loop invariant code motion (LICM) pass.
55/// TODO(asabne) : The pass is missing zero-trip tests.
56/// TODO(asabne) : Check for the presence of side effects before hoisting.
57struct LoopInvariantCodeMotion : public FunctionPass<LoopInvariantCodeMotion> {
58 void runOnFunction() override;
59 void runOnAffineForOp(AffineForOp forOp);
60 std::vector<AffineForOp> forOps;
61};
62} // end anonymous namespace
63
64FunctionPassBase *mlir::createLoopInvariantCodeMotionPass() {
65 return new LoopInvariantCodeMotion();
66}
67
68void LoopInvariantCodeMotion::runOnAffineForOp(AffineForOp forOp) {
69 auto *loopBody = forOp.getBody();
70
71 // This is the place where hoisted instructions would reside.
72 FuncBuilder b(forOp.getOperation());
73
74 // This vector is used to place loop invariant operations.
75 SmallVector<Operation *, 8> opsToMove;
76
77 SetVector<Operation *> loopDefinedOps;
78 // Generate forward slice which contains ops that fall under the transitive
79 // definition closure following the loop induction variable.
80 getForwardSlice(forOp, &loopDefinedOps);
81
82 for (auto i : loopDefinedOps) {
83 LLVM_DEBUG(i->print(llvm::dbgs() << "\nLoop-dependent op\n"));
84 }
85
86 for (auto &op : *loopBody) {
87 // If the operation is loop invariant, insert it into opsToMove.
88 if (!op.isa<AffineForOp>() && !op.isa<AffineTerminatorOp>() &&
89 loopDefinedOps.count(&op) != 1) {
90 LLVM_DEBUG(op.print(llvm::dbgs() << "\nLICM'ing op\n"));
91 opsToMove.push_back(&op);
92 }
93 }
94
95 // For all instructions that we found to be invariant, place them sequentially
96 // right before the for loop.
97 for (auto *op : opsToMove) {
98 op->moveBefore(forOp);
99 }
100
101 LLVM_DEBUG(forOp.getOperation()->print(llvm::dbgs() << "\nModified loop\n"));
102
103 // If the for loop body has a single operation (the terminator), erase it.
104 if (forOp.getBody()->getOperations().size() == 1) {
105 assert(forOp.getBody()->getOperations().front().isa<AffineTerminatorOp>());
106 forOp.erase();
107 }
108}
109
110void LoopInvariantCodeMotion::runOnFunction() {
111 forOps.clear();
112
113 // Gather all loops in a function, and order them in innermost-loop-first
114 // order. This way, we first LICM from the inner loop, and place the ops in
115 // the outer loop, which in turn can be further LICM'ed. This saves iterating
116 // on the inner loop operations while LICMing through the outer loop.
117 getFunction().walk<AffineForOp>(
118 [&](AffineForOp forOp) { forOps.push_back(forOp); });
119 // We gather loops first, and then go over them later because we don't want to
120 // mess the iterators up.
121 for (auto forOp : forOps) {
122 auto *forInst = forOp.getOperation();
123 LLVM_DEBUG(forInst->print(llvm::dbgs() << "\nOriginal loop\n"));
124 runOnAffineForOp(forOp);
125 }
126}
127
128static PassRegistration<LoopInvariantCodeMotion>
129 pass("loop-invariant-code-motion",
130 "Hoist loop invariant instructions outside of the loop");