Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 1 | //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===// |
| 2 | // |
Mehdi Amini | 3085710 | 2020-01-26 03:58:30 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
Mehdi Amini | 56222a0 | 2019-12-23 17:35:36 | [diff] [blame] | 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 | |
River Riddle | 1834ad4a | 2020-04-07 20:58:12 | [diff] [blame] | 13 | #include "PassDetail.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 14 | #include "mlir/Transforms/Passes.h" |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 15 | |
| 16 | #include "mlir/IR/Builders.h" |
| 17 | #include "mlir/IR/Function.h" |
River Riddle | 43959a2 | 2020-03-14 20:36:42 | [diff] [blame] | 18 | #include "mlir/Interfaces/LoopLikeInterface.h" |
Stephen Neuendorffer | eb623ae | 2020-05-13 17:27:19 | [diff] [blame] | 19 | #include "mlir/Interfaces/SideEffectInterfaces.h" |
Nicolas Vasilache | 6953cf6 | 2020-06-05 10:35:46 | [diff] [blame] | 20 | #include "mlir/Transforms/LoopUtils.h" |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 24 | |
| 25 | #define DEBUG_TYPE "licm" |
| 26 | |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 27 | using namespace mlir; |
| 28 | |
| 29 | namespace { |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 30 | /// Loop invariant code motion (LICM) pass. |
River Riddle | 80aca1e | 2020-04-07 20:56:16 | [diff] [blame] | 31 | struct LoopInvariantCodeMotion |
River Riddle | 1834ad4a | 2020-04-07 20:58:12 | [diff] [blame] | 32 | : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 33 | void runOnOperation() override; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 34 | }; |
Rahul Joshi | 60f914e | 2020-06-24 03:21:42 | [diff] [blame] | 35 | } // end anonymous namespace |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 36 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 37 | // Checks whether the given op can be hoisted by checking that |
| 38 | // - the op and any of its contained operations do not depend on SSA values |
| 39 | // defined inside of the loop (by means of calling definedOutside). |
| 40 | // - the op has no side-effects. If sideEffecting is Never, sideeffects of this |
| 41 | // op and its nested ops are ignored. |
Rahul Joshi | 60f914e | 2020-06-24 03:21:42 | [diff] [blame] | 42 | static bool canBeHoisted(Operation *op, |
| 43 | function_ref<bool(Value)> definedOutside) { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 44 | // Check that dependencies are defined outside of loop. |
| 45 | if (!llvm::all_of(op->getOperands(), definedOutside)) |
| 46 | return false; |
| 47 | // Check whether this op is side-effect free. If we already know that there |
| 48 | // can be no side-effects because the surrounding op has claimed so, we can |
| 49 | // (and have to) skip this step. |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 50 | if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { |
| 51 | if (!memInterface.hasNoEffect()) |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 52 | return false; |
River Riddle | 0ddba0b | 2020-03-12 21:06:41 | [diff] [blame] | 53 | // If the operation doesn't have side effects and it doesn't recursively |
| 54 | // have side effects, it can always be hoisted. |
| 55 | if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) |
| 56 | return true; |
| 57 | |
| 58 | // Otherwise, if the operation doesn't provide the memory effect interface |
| 59 | // and it doesn't have recursive side effects we treat it conservatively as |
| 60 | // side-effecting. |
| 61 | } else if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) { |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 62 | return false; |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 63 | } |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 64 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 65 | // Recurse into the regions for this op and check whether the contained ops |
| 66 | // can be hoisted. |
| 67 | for (auto ®ion : op->getRegions()) { |
Rahul Joshi | d150662 | 2020-06-19 19:33:21 | [diff] [blame] | 68 | for (auto &block : region) { |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 69 | for (auto &innerOp : block.without_terminator()) |
| 70 | if (!canBeHoisted(&innerOp, definedOutside)) |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 71 | return false; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 72 | } |
| 73 | } |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
Rahul Joshi | e7f7137 | 2020-06-24 00:23:35 | [diff] [blame] | 77 | |
Nicolas Vasilache | 6953cf6 | 2020-06-05 10:35:46 | [diff] [blame] | 78 | LogicalResult mlir::moveLoopInvariantCode(LoopLikeOpInterface looplike) { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 79 | auto &loopBody = looplike.getLoopBody(); |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 80 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 81 | // We use two collections here as we need to preserve the order for insertion |
| 82 | // and this is easiest. |
| 83 | SmallPtrSet<Operation *, 8> willBeMovedSet; |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 84 | SmallVector<Operation *, 8> opsToMove; |
| 85 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 86 | // Helper to check whether an operation is loop invariant wrt. SSA properties. |
River Riddle | e62a695 | 2019-12-23 22:45:01 | [diff] [blame] | 87 | auto isDefinedOutsideOfBody = [&](Value value) { |
River Riddle | 2bdf33c | 2020-01-11 16:54:04 | [diff] [blame] | 88 | auto definingOp = value.getDefiningOp(); |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 89 | return (definingOp && !!willBeMovedSet.count(definingOp)) || |
| 90 | looplike.isDefinedOutsideOfLoop(value); |
| 91 | }; |
| 92 | |
| 93 | // Do not use walk here, as we do not want to go into nested regions and hoist |
| 94 | // operations from there. These regions might have semantics unknown to this |
| 95 | // rewriting. If the nested regions are loops, they will have been processed. |
| 96 | for (auto &block : loopBody) { |
| 97 | for (auto &op : block.without_terminator()) { |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 98 | if (canBeHoisted(&op, isDefinedOutsideOfBody)) { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 99 | opsToMove.push_back(&op); |
| 100 | willBeMovedSet.insert(&op); |
Amit Sabne | 7a43da6 | 2019-05-31 20:56:47 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 105 | // For all instructions that we found to be invariant, move outside of the |
| 106 | // loop. |
| 107 | auto result = looplike.moveOutOfLoop(opsToMove); |
Rahul Joshi | e7f7137 | 2020-06-24 00:23:35 | [diff] [blame] | 108 | LLVM_DEBUG(looplike.print(llvm::dbgs() << "\n\nModified loop:\n")); |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 109 | return result; |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 110 | } |
| 111 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 112 | void LoopInvariantCodeMotion::runOnOperation() { |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 113 | // Walk through all loops in a function in innermost-loop-first order. This |
Chris Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 114 | // way, we first LICM from the inner loop, and place the ops in |
| 115 | // the outer loop, which in turn can be further LICM'ed. |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 116 | getOperation()->walk([&](LoopLikeOpInterface loopLike) { |
Rahul Joshi | e7f7137 | 2020-06-24 00:23:35 | [diff] [blame] | 117 | LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop:\n")); |
River Riddle | b10c662 | 2020-03-09 23:01:41 | [diff] [blame] | 118 | if (failed(moveLoopInvariantCode(loopLike))) |
| 119 | signalPassFailure(); |
Chris Lattner | 0134b5d | 2019-05-11 15:28:15 | [diff] [blame] | 120 | }); |
Amit Sabne | 7905da6 | 2019-04-17 19:18:37 | [diff] [blame] | 121 | } |
| 122 | |
Stephan Herhut | b843cc5 | 2019-10-16 11:28:13 | [diff] [blame] | 123 | std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { |
| 124 | return std::make_unique<LoopInvariantCodeMotion>(); |
| 125 | } |