blob: 738524aa6ec97c46bf6f49eab0a9b3c8215271f4 [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
Amit Sabne7905da62019-04-17 19:18:3722#include "mlir/Transforms/Passes.h"
Stephan Herhutb843cc52019-10-16 11:28:1323
24#include "mlir/IR/Builders.h"
25#include "mlir/IR/Function.h"
26#include "mlir/Pass/Pass.h"
27#include "mlir/Transforms/LoopLikeInterface.h"
28#include "mlir/Transforms/SideEffectsInterface.h"
Amit Sabne7a43da62019-05-31 20:56:4729#include "llvm/ADT/SmallPtrSet.h"
Amit Sabne7905da62019-04-17 19:18:3730#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
Amit Sabne7905da62019-04-17 19:18:3732
33#define DEBUG_TYPE "licm"
34
Amit Sabne7905da62019-04-17 19:18:3735using namespace mlir;
36
37namespace {
38
Stephan Herhutb843cc52019-10-16 11:28:1339using SideEffecting = SideEffectsInterface::SideEffecting;
40
Amit Sabne7905da62019-04-17 19:18:3741/// Loop invariant code motion (LICM) pass.
Stephan Herhutb843cc52019-10-16 11:28:1342struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> {
43public:
44 void runOnOperation() override;
Amit Sabne7905da62019-04-17 19:18:3745};
Amit Sabne7905da62019-04-17 19:18:3746
Stephan Herhutb843cc52019-10-16 11:28:1347// Checks whether the given op can be hoisted by checking that
48// - the op and any of its contained operations do not depend on SSA values
49// defined inside of the loop (by means of calling definedOutside).
50// - the op has no side-effects. If sideEffecting is Never, sideeffects of this
51// op and its nested ops are ignored.
52static bool canBeHoisted(Operation *op,
53 llvm::function_ref<bool(Value *)> definedOutside,
54 SideEffecting sideEffecting,
55 SideEffectsInterface &interface) {
56 // Check that dependencies are defined outside of loop.
57 if (!llvm::all_of(op->getOperands(), definedOutside))
58 return false;
59 // Check whether this op is side-effect free. If we already know that there
60 // can be no side-effects because the surrounding op has claimed so, we can
61 // (and have to) skip this step.
62 auto thisOpIsSideEffecting = sideEffecting;
63 if (thisOpIsSideEffecting != SideEffecting::Never) {
64 thisOpIsSideEffecting = interface.isSideEffecting(op);
65 // If the op always has sideeffects, we cannot hoist.
66 if (thisOpIsSideEffecting == SideEffecting::Always)
Amit Sabne7a43da62019-05-31 20:56:4767 return false;
Stephan Herhutb843cc52019-10-16 11:28:1368 }
69 // Recurse into the regions for this op and check whether the contained ops
70 // can be hoisted.
71 for (auto &region : op->getRegions()) {
72 for (auto &block : region.getBlocks()) {
73 for (auto &innerOp : block) {
74 if (innerOp.isKnownTerminator())
75 continue;
76 if (!canBeHoisted(&innerOp, definedOutside, thisOpIsSideEffecting,
77 interface))
Amit Sabne7a43da62019-05-31 20:56:4778 return false;
Amit Sabne7a43da62019-05-31 20:56:4779 }
Amit Sabne7905da62019-04-17 19:18:3780 }
81 }
Amit Sabne7a43da62019-05-31 20:56:4782 return true;
83}
84
Stephan Herhutb843cc52019-10-16 11:28:1385static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike,
86 SideEffectsInterface &interface) {
87 auto &loopBody = looplike.getLoopBody();
Amit Sabne7a43da62019-05-31 20:56:4788
Stephan Herhutb843cc52019-10-16 11:28:1389 // We use two collections here as we need to preserve the order for insertion
90 // and this is easiest.
91 SmallPtrSet<Operation *, 8> willBeMovedSet;
Amit Sabne7a43da62019-05-31 20:56:4792 SmallVector<Operation *, 8> opsToMove;
93
Stephan Herhutb843cc52019-10-16 11:28:1394 // Helper to check whether an operation is loop invariant wrt. SSA properties.
95 auto isDefinedOutsideOfBody = [&](Value *value) {
96 auto definingOp = value->getDefiningOp();
97 return (definingOp && !!willBeMovedSet.count(definingOp)) ||
98 looplike.isDefinedOutsideOfLoop(value);
99 };
100
101 // Do not use walk here, as we do not want to go into nested regions and hoist
102 // operations from there. These regions might have semantics unknown to this
103 // rewriting. If the nested regions are loops, they will have been processed.
104 for (auto &block : loopBody) {
105 for (auto &op : block.without_terminator()) {
106 if (canBeHoisted(&op, isDefinedOutsideOfBody,
107 mlir::SideEffectsDialectInterface::Recursive,
108 interface)) {
109 opsToMove.push_back(&op);
110 willBeMovedSet.insert(&op);
Amit Sabne7a43da62019-05-31 20:56:47111 }
112 }
113 }
114
Stephan Herhutb843cc52019-10-16 11:28:13115 // For all instructions that we found to be invariant, move outside of the
116 // loop.
117 auto result = looplike.moveOutOfLoop(opsToMove);
118 LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n"));
119 return result;
Amit Sabne7905da62019-04-17 19:18:37120}
121
Stephan Herhutb843cc52019-10-16 11:28:13122} // end anonymous namespace
123
124void LoopInvariantCodeMotion::runOnOperation() {
125 SideEffectsInterface interface(&getContext());
126 // Walk through all loops in a function in innermost-loop-first order. This
Chris Lattner0134b5d2019-05-11 15:28:15127 // way, we first LICM from the inner loop, and place the ops in
128 // the outer loop, which in turn can be further LICM'ed.
Stephan Herhutb843cc52019-10-16 11:28:13129 getOperation()->walk([&](Operation *op) {
130 if (auto looplike = dyn_cast<LoopLikeOpInterface>(op)) {
131 LLVM_DEBUG(op->print(llvm::dbgs() << "\nOriginal loop\n"));
132 if (failed(moveLoopInvariantCode(looplike, interface)))
133 signalPassFailure();
134 }
Chris Lattner0134b5d2019-05-11 15:28:15135 });
Amit Sabne7905da62019-04-17 19:18:37136}
137
Stephan Herhutb843cc52019-10-16 11:28:13138// Include the generated code for the loop-like interface here, as it otherwise
139// has no compilation unit. This works as loop-invariant code motion is the
140// only user of that interface.
141#include "mlir/Transforms/LoopLikeInterface.cpp.inc"
142
143std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
144 return std::make_unique<LoopInvariantCodeMotion>();
145}
146
Amit Sabne7905da62019-04-17 19:18:37147static PassRegistration<LoopInvariantCodeMotion>
Stephan Herhutb843cc52019-10-16 11:28:13148 pass("loop-invariant-code-motion",
Amit Sabne7905da62019-04-17 19:18:37149 "Hoist loop invariant instructions outside of the loop");