blob: dacd688a7f369574eceaa4ecf5f83414c6ff64df [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
River Riddle1834ad4a2020-04-07 20:58:1213#include "PassDetail.h"
Amit Sabne7905da62019-04-17 19:18:3714#include "mlir/Transforms/Passes.h"
Stephan Herhutb843cc52019-10-16 11:28:1315
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/Function.h"
River Riddle43959a22020-03-14 20:36:4218#include "mlir/Interfaces/LoopLikeInterface.h"
River Riddle153720a2020-03-10 19:25:0819#include "mlir/Interfaces/SideEffects.h"
Amit Sabne7a43da62019-05-31 20:56:4720#include "llvm/ADT/SmallPtrSet.h"
Amit Sabne7905da62019-04-17 19:18:3721#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Debug.h"
Amit Sabne7905da62019-04-17 19:18:3723
24#define DEBUG_TYPE "licm"
25
Amit Sabne7905da62019-04-17 19:18:3726using namespace mlir;
27
28namespace {
Amit Sabne7905da62019-04-17 19:18:3729/// Loop invariant code motion (LICM) pass.
River Riddle80aca1e2020-04-07 20:56:1630struct LoopInvariantCodeMotion
River Riddle1834ad4a2020-04-07 20:58:1231 : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
Stephan Herhutb843cc52019-10-16 11:28:1332 void runOnOperation() override;
Amit Sabne7905da62019-04-17 19:18:3733};
River Riddle43959a22020-03-14 20:36:4234} // end anonymous namespace
Amit Sabne7905da62019-04-17 19:18:3735
Stephan Herhutb843cc52019-10-16 11:28:1336// Checks whether the given op can be hoisted by checking that
37// - the op and any of its contained operations do not depend on SSA values
38// defined inside of the loop (by means of calling definedOutside).
39// - the op has no side-effects. If sideEffecting is Never, sideeffects of this
40// op and its nested ops are ignored.
41static bool canBeHoisted(Operation *op,
River Riddleb10c6622020-03-09 23:01:4142 function_ref<bool(Value)> definedOutside) {
Stephan Herhutb843cc52019-10-16 11:28:1343 // Check that dependencies are defined outside of loop.
44 if (!llvm::all_of(op->getOperands(), definedOutside))
45 return false;
46 // Check whether this op is side-effect free. If we already know that there
47 // can be no side-effects because the surrounding op has claimed so, we can
48 // (and have to) skip this step.
River Riddleb10c6622020-03-09 23:01:4149 if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
50 if (!memInterface.hasNoEffect())
Amit Sabne7a43da62019-05-31 20:56:4751 return false;
River Riddle0ddba0b2020-03-12 21:06:4152 // If the operation doesn't have side effects and it doesn't recursively
53 // have side effects, it can always be hoisted.
54 if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>())
55 return true;
56
57 // Otherwise, if the operation doesn't provide the memory effect interface
58 // and it doesn't have recursive side effects we treat it conservatively as
59 // side-effecting.
60 } else if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) {
River Riddleb10c6622020-03-09 23:01:4161 return false;
Stephan Herhutb843cc52019-10-16 11:28:1362 }
River Riddleb10c6622020-03-09 23:01:4163
Stephan Herhutb843cc52019-10-16 11:28:1364 // Recurse into the regions for this op and check whether the contained ops
65 // can be hoisted.
66 for (auto &region : op->getRegions()) {
67 for (auto &block : region.getBlocks()) {
River Riddleb10c6622020-03-09 23:01:4168 for (auto &innerOp : block.without_terminator())
69 if (!canBeHoisted(&innerOp, definedOutside))
Amit Sabne7a43da62019-05-31 20:56:4770 return false;
Amit Sabne7905da62019-04-17 19:18:3771 }
72 }
Amit Sabne7a43da62019-05-31 20:56:4773 return true;
74}
75
River Riddleb10c6622020-03-09 23:01:4176static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike) {
Stephan Herhutb843cc52019-10-16 11:28:1377 auto &loopBody = looplike.getLoopBody();
Amit Sabne7a43da62019-05-31 20:56:4778
Stephan Herhutb843cc52019-10-16 11:28:1379 // We use two collections here as we need to preserve the order for insertion
80 // and this is easiest.
81 SmallPtrSet<Operation *, 8> willBeMovedSet;
Amit Sabne7a43da62019-05-31 20:56:4782 SmallVector<Operation *, 8> opsToMove;
83
Stephan Herhutb843cc52019-10-16 11:28:1384 // Helper to check whether an operation is loop invariant wrt. SSA properties.
River Riddlee62a6952019-12-23 22:45:0185 auto isDefinedOutsideOfBody = [&](Value value) {
River Riddle2bdf33c2020-01-11 16:54:0486 auto definingOp = value.getDefiningOp();
Stephan Herhutb843cc52019-10-16 11:28:1387 return (definingOp && !!willBeMovedSet.count(definingOp)) ||
88 looplike.isDefinedOutsideOfLoop(value);
89 };
90
91 // Do not use walk here, as we do not want to go into nested regions and hoist
92 // operations from there. These regions might have semantics unknown to this
93 // rewriting. If the nested regions are loops, they will have been processed.
94 for (auto &block : loopBody) {
95 for (auto &op : block.without_terminator()) {
River Riddleb10c6622020-03-09 23:01:4196 if (canBeHoisted(&op, isDefinedOutsideOfBody)) {
Stephan Herhutb843cc52019-10-16 11:28:1397 opsToMove.push_back(&op);
98 willBeMovedSet.insert(&op);
Amit Sabne7a43da62019-05-31 20:56:4799 }
100 }
101 }
102
Stephan Herhutb843cc52019-10-16 11:28:13103 // For all instructions that we found to be invariant, move outside of the
104 // loop.
105 auto result = looplike.moveOutOfLoop(opsToMove);
106 LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n"));
107 return result;
Amit Sabne7905da62019-04-17 19:18:37108}
109
Stephan Herhutb843cc52019-10-16 11:28:13110void LoopInvariantCodeMotion::runOnOperation() {
Stephan Herhutb843cc52019-10-16 11:28:13111 // Walk through all loops in a function in innermost-loop-first order. This
Chris Lattner0134b5d2019-05-11 15:28:15112 // way, we first LICM from the inner loop, and place the ops in
113 // the outer loop, which in turn can be further LICM'ed.
River Riddleb10c6622020-03-09 23:01:41114 getOperation()->walk([&](LoopLikeOpInterface loopLike) {
115 LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop\n"));
116 if (failed(moveLoopInvariantCode(loopLike)))
117 signalPassFailure();
Chris Lattner0134b5d2019-05-11 15:28:15118 });
Amit Sabne7905da62019-04-17 19:18:37119}
120
Stephan Herhutb843cc52019-10-16 11:28:13121std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
122 return std::make_unique<LoopInvariantCodeMotion>();
123}