Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 1 | //===- 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 Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 22 | #include "mlir/Transforms/Passes.h" |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 23 | |
| 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 Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 32 | |
| 33 | #define DEBUG_TYPE "licm" |
| 34 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 35 | using namespace mlir; |
| 36 | |
| 37 | namespace { |
| 38 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 39 | using SideEffecting = SideEffectsInterface::SideEffecting; |
| 40 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 41 | /// Loop invariant code motion (LICM) pass. |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 42 | struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> { |
| 43 | public: |
| 44 | void runOnOperation() override; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 45 | }; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 46 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 47 | // 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. |
| 52 | static 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 Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 67 | return false; |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 68 | } |
| 69 | // Recurse into the regions for this op and check whether the contained ops |
| 70 | // can be hoisted. |
| 71 | for (auto ®ion : 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 Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 78 | return false; |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 79 | } |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 80 | } |
| 81 | } |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 85 | static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike, |
| 86 | SideEffectsInterface &interface) { |
| 87 | auto &loopBody = looplike.getLoopBody(); |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 88 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 89 | // 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 Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 92 | SmallVector<Operation *, 8> opsToMove; |
| 93 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 94 | // 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 Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 115 | // 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 Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 120 | } |
| 121 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 122 | } // end anonymous namespace |
| 123 | |
| 124 | void LoopInvariantCodeMotion::runOnOperation() { |
| 125 | SideEffectsInterface interface(&getContext()); |
| 126 | // Walk through all loops in a function in innermost-loop-first order. This |
Chris Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 127 | // 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 Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 129 | 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 Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 135 | }); |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 136 | } |
| 137 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 138 | // 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 | |
| 143 | std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { |
| 144 | return std::make_unique<LoopInvariantCodeMotion>(); |
| 145 | } |
| 146 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 147 | static PassRegistration<LoopInvariantCodeMotion> |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame^] | 148 | pass("loop-invariant-code-motion", |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 149 | "Hoist loop invariant instructions outside of the loop"); |