Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 1 | //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===// |
| 2 | // |
Mehdi Amini | 56222a0 | 2019-12-23 17:35:36 | [diff] [blame^] | 3 | // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 6 | // |
Mehdi Amini | 56222a0 | 2019-12-23 17:35:36 | [diff] [blame^] | 7 | //===----------------------------------------------------------------------===// |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 8 | // |
| 9 | // This file implements loop invariant code motion. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 13 | #include "mlir/Transforms/Passes.h" |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 14 | |
| 15 | #include "mlir/IR/Builders.h" |
| 16 | #include "mlir/IR/Function.h" |
| 17 | #include "mlir/Pass/Pass.h" |
| 18 | #include "mlir/Transforms/LoopLikeInterface.h" |
| 19 | #include "mlir/Transforms/SideEffectsInterface.h" |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Debug.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 23 | |
| 24 | #define DEBUG_TYPE "licm" |
| 25 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 26 | using namespace mlir; |
| 27 | |
| 28 | namespace { |
| 29 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 30 | using SideEffecting = SideEffectsInterface::SideEffecting; |
| 31 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 32 | /// Loop invariant code motion (LICM) pass. |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 33 | struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> { |
| 34 | public: |
| 35 | void runOnOperation() override; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 36 | }; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 37 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 38 | // Checks whether the given op can be hoisted by checking that |
| 39 | // - the op and any of its contained operations do not depend on SSA values |
| 40 | // defined inside of the loop (by means of calling definedOutside). |
| 41 | // - the op has no side-effects. If sideEffecting is Never, sideeffects of this |
| 42 | // op and its nested ops are ignored. |
| 43 | static bool canBeHoisted(Operation *op, |
River Riddle | 35807bc | 2019-12-23 05:59:55 | [diff] [blame] | 44 | function_ref<bool(ValuePtr)> definedOutside, |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 45 | SideEffecting sideEffecting, |
| 46 | SideEffectsInterface &interface) { |
| 47 | // Check that dependencies are defined outside of loop. |
| 48 | if (!llvm::all_of(op->getOperands(), definedOutside)) |
| 49 | return false; |
| 50 | // Check whether this op is side-effect free. If we already know that there |
| 51 | // can be no side-effects because the surrounding op has claimed so, we can |
| 52 | // (and have to) skip this step. |
| 53 | auto thisOpIsSideEffecting = sideEffecting; |
| 54 | if (thisOpIsSideEffecting != SideEffecting::Never) { |
| 55 | thisOpIsSideEffecting = interface.isSideEffecting(op); |
| 56 | // If the op always has sideeffects, we cannot hoist. |
| 57 | if (thisOpIsSideEffecting == SideEffecting::Always) |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 58 | return false; |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 59 | } |
| 60 | // Recurse into the regions for this op and check whether the contained ops |
| 61 | // can be hoisted. |
| 62 | for (auto ®ion : op->getRegions()) { |
| 63 | for (auto &block : region.getBlocks()) { |
| 64 | for (auto &innerOp : block) { |
| 65 | if (innerOp.isKnownTerminator()) |
| 66 | continue; |
| 67 | if (!canBeHoisted(&innerOp, definedOutside, thisOpIsSideEffecting, |
| 68 | interface)) |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 69 | return false; |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 70 | } |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 71 | } |
| 72 | } |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 76 | static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike, |
| 77 | SideEffectsInterface &interface) { |
| 78 | auto &loopBody = looplike.getLoopBody(); |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 79 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 80 | // We use two collections here as we need to preserve the order for insertion |
| 81 | // and this is easiest. |
| 82 | SmallPtrSet<Operation *, 8> willBeMovedSet; |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 83 | SmallVector<Operation *, 8> opsToMove; |
| 84 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 85 | // Helper to check whether an operation is loop invariant wrt. SSA properties. |
River Riddle | 35807bc | 2019-12-23 05:59:55 | [diff] [blame] | 86 | auto isDefinedOutsideOfBody = [&](ValuePtr value) { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 87 | auto definingOp = value->getDefiningOp(); |
| 88 | return (definingOp && !!willBeMovedSet.count(definingOp)) || |
| 89 | looplike.isDefinedOutsideOfLoop(value); |
| 90 | }; |
| 91 | |
| 92 | // Do not use walk here, as we do not want to go into nested regions and hoist |
| 93 | // operations from there. These regions might have semantics unknown to this |
| 94 | // rewriting. If the nested regions are loops, they will have been processed. |
| 95 | for (auto &block : loopBody) { |
| 96 | for (auto &op : block.without_terminator()) { |
| 97 | if (canBeHoisted(&op, isDefinedOutsideOfBody, |
| 98 | mlir::SideEffectsDialectInterface::Recursive, |
| 99 | interface)) { |
| 100 | opsToMove.push_back(&op); |
| 101 | willBeMovedSet.insert(&op); |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 106 | // For all instructions that we found to be invariant, move outside of the |
| 107 | // loop. |
| 108 | auto result = looplike.moveOutOfLoop(opsToMove); |
| 109 | LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n")); |
| 110 | return result; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 111 | } |
| 112 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 113 | } // end anonymous namespace |
| 114 | |
| 115 | void LoopInvariantCodeMotion::runOnOperation() { |
| 116 | SideEffectsInterface interface(&getContext()); |
| 117 | // Walk through all loops in a function in innermost-loop-first order. This |
Chris Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 118 | // way, we first LICM from the inner loop, and place the ops in |
| 119 | // the outer loop, which in turn can be further LICM'ed. |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 120 | getOperation()->walk([&](Operation *op) { |
| 121 | if (auto looplike = dyn_cast<LoopLikeOpInterface>(op)) { |
| 122 | LLVM_DEBUG(op->print(llvm::dbgs() << "\nOriginal loop\n")); |
| 123 | if (failed(moveLoopInvariantCode(looplike, interface))) |
| 124 | signalPassFailure(); |
| 125 | } |
Chris Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 126 | }); |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 127 | } |
| 128 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 129 | // Include the generated code for the loop-like interface here, as it otherwise |
| 130 | // has no compilation unit. This works as loop-invariant code motion is the |
| 131 | // only user of that interface. |
| 132 | #include "mlir/Transforms/LoopLikeInterface.cpp.inc" |
| 133 | |
| 134 | std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { |
| 135 | return std::make_unique<LoopInvariantCodeMotion>(); |
| 136 | } |
| 137 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 138 | static PassRegistration<LoopInvariantCodeMotion> |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 139 | pass("loop-invariant-code-motion", |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 140 | "Hoist loop invariant instructions outside of the loop"); |