blob: ccac42f25cf3003cf7cc4c76247867a3adcd1f3b [file] [log] [blame]
Amit Sabne7905da62019-04-17 19:18:371//===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
2//
Mehdi Amini30857102020-01-26 03:58:303// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Mehdi Amini56222a02019-12-23 17:35:364// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Amit Sabne7905da62019-04-17 19:18:376//
Mehdi Amini56222a02019-12-23 17:35:367//===----------------------------------------------------------------------===//
Amit Sabne7905da62019-04-17 19:18:378//
9// This file implements loop invariant code motion.
10//
11//===----------------------------------------------------------------------===//
12
Amit Sabne7905da62019-04-17 19:18:3713#include "mlir/Transforms/Passes.h"
Stephan Herhutb843cc52019-10-16 11:28:1314
15#include "mlir/IR/Builders.h"
16#include "mlir/IR/Function.h"
17#include "mlir/Pass/Pass.h"
18#include "mlir/Transforms/LoopLikeInterface.h"
Amit Sabne7a43da62019-05-31 20:56:4719#include "llvm/ADT/SmallPtrSet.h"
Amit Sabne7905da62019-04-17 19:18:3720#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
Amit Sabne7905da62019-04-17 19:18:3722
23#define DEBUG_TYPE "licm"
24
Amit Sabne7905da62019-04-17 19:18:3725using namespace mlir;
26
27namespace {
28
Amit Sabne7905da62019-04-17 19:18:3729/// Loop invariant code motion (LICM) pass.
Stephan Herhutb843cc52019-10-16 11:28:1330struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> {
31public:
32 void runOnOperation() override;
Amit Sabne7905da62019-04-17 19:18:3733};
Amit Sabne7905da62019-04-17 19:18:3734
Stephan Herhutb843cc52019-10-16 11:28:1335// Checks whether the given op can be hoisted by checking that
36// - the op and any of its contained operations do not depend on SSA values
37// defined inside of the loop (by means of calling definedOutside).
38// - the op has no side-effects. If sideEffecting is Never, sideeffects of this
39// op and its nested ops are ignored.
40static bool canBeHoisted(Operation *op,
River Riddleb10c6622020-03-09 23:01:4141 function_ref<bool(Value)> definedOutside) {
Stephan Herhutb843cc52019-10-16 11:28:1342 // Check that dependencies are defined outside of loop.
43 if (!llvm::all_of(op->getOperands(), definedOutside))
44 return false;
45 // Check whether this op is side-effect free. If we already know that there
46 // can be no side-effects because the surrounding op has claimed so, we can
47 // (and have to) skip this step.
River Riddleb10c6622020-03-09 23:01:4148 if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
49 if (!memInterface.hasNoEffect())
Amit Sabne7a43da62019-05-31 20:56:4750 return false;
River Riddleb10c6622020-03-09 23:01:4151 } else if (!op->hasNoSideEffect() &&
52 !op->hasTrait<OpTrait::HasRecursiveSideEffects>()) {
53 return false;
Stephan Herhutb843cc52019-10-16 11:28:1354 }
River Riddleb10c6622020-03-09 23:01:4155
56 // If the operation doesn't have side effects and it doesn't recursively
57 // have side effects, it can always be hoisted.
58 if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>())
59 return true;
60
Stephan Herhutb843cc52019-10-16 11:28:1361 // Recurse into the regions for this op and check whether the contained ops
62 // can be hoisted.
63 for (auto &region : op->getRegions()) {
64 for (auto &block : region.getBlocks()) {
River Riddleb10c6622020-03-09 23:01:4165 for (auto &innerOp : block.without_terminator())
66 if (!canBeHoisted(&innerOp, definedOutside))
Amit Sabne7a43da62019-05-31 20:56:4767 return false;
Amit Sabne7905da62019-04-17 19:18:3768 }
69 }
Amit Sabne7a43da62019-05-31 20:56:4770 return true;
71}
72
River Riddleb10c6622020-03-09 23:01:4173static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike) {
Stephan Herhutb843cc52019-10-16 11:28:1374 auto &loopBody = looplike.getLoopBody();
Amit Sabne7a43da62019-05-31 20:56:4775
Stephan Herhutb843cc52019-10-16 11:28:1376 // We use two collections here as we need to preserve the order for insertion
77 // and this is easiest.
78 SmallPtrSet<Operation *, 8> willBeMovedSet;
Amit Sabne7a43da62019-05-31 20:56:4779 SmallVector<Operation *, 8> opsToMove;
80
Stephan Herhutb843cc52019-10-16 11:28:1381 // Helper to check whether an operation is loop invariant wrt. SSA properties.
River Riddlee62a6952019-12-23 22:45:0182 auto isDefinedOutsideOfBody = [&](Value value) {
River Riddle2bdf33c2020-01-11 16:54:0483 auto definingOp = value.getDefiningOp();
Stephan Herhutb843cc52019-10-16 11:28:1384 return (definingOp && !!willBeMovedSet.count(definingOp)) ||
85 looplike.isDefinedOutsideOfLoop(value);
86 };
87
88 // Do not use walk here, as we do not want to go into nested regions and hoist
89 // operations from there. These regions might have semantics unknown to this
90 // rewriting. If the nested regions are loops, they will have been processed.
91 for (auto &block : loopBody) {
92 for (auto &op : block.without_terminator()) {
River Riddleb10c6622020-03-09 23:01:4193 if (canBeHoisted(&op, isDefinedOutsideOfBody)) {
Stephan Herhutb843cc52019-10-16 11:28:1394 opsToMove.push_back(&op);
95 willBeMovedSet.insert(&op);
Amit Sabne7a43da62019-05-31 20:56:4796 }
97 }
98 }
99
Stephan Herhutb843cc52019-10-16 11:28:13100 // For all instructions that we found to be invariant, move outside of the
101 // loop.
102 auto result = looplike.moveOutOfLoop(opsToMove);
103 LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n"));
104 return result;
Amit Sabne7905da62019-04-17 19:18:37105}
106
Stephan Herhutb843cc52019-10-16 11:28:13107} // end anonymous namespace
108
109void LoopInvariantCodeMotion::runOnOperation() {
Stephan Herhutb843cc52019-10-16 11:28:13110 // Walk through all loops in a function in innermost-loop-first order. This
Chris Lattner0134b5d2019-05-11 15:28:15111 // way, we first LICM from the inner loop, and place the ops in
112 // the outer loop, which in turn can be further LICM'ed.
River Riddleb10c6622020-03-09 23:01:41113 getOperation()->walk([&](LoopLikeOpInterface loopLike) {
114 LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop\n"));
115 if (failed(moveLoopInvariantCode(loopLike)))
116 signalPassFailure();
Chris Lattner0134b5d2019-05-11 15:28:15117 });
Amit Sabne7905da62019-04-17 19:18:37118}
119
Stephan Herhutb843cc52019-10-16 11:28:13120// Include the generated code for the loop-like interface here, as it otherwise
121// has no compilation unit. This works as loop-invariant code motion is the
122// only user of that interface.
123#include "mlir/Transforms/LoopLikeInterface.cpp.inc"
124
125std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
126 return std::make_unique<LoopInvariantCodeMotion>();
127}
128
Amit Sabne7905da62019-04-17 19:18:37129static PassRegistration<LoopInvariantCodeMotion>
Stephan Herhutb843cc52019-10-16 11:28:13130 pass("loop-invariant-code-motion",
Amit Sabne7905da62019-04-17 19:18:37131 "Hoist loop invariant instructions outside of the loop");