17905da65SAmit Sabne //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===// 27905da65SAmit Sabne // 330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information. 556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67905da65SAmit Sabne // 756222a06SMehdi Amini //===----------------------------------------------------------------------===// 87905da65SAmit Sabne // 97905da65SAmit Sabne // This file implements loop invariant code motion. 107905da65SAmit Sabne // 117905da65SAmit Sabne //===----------------------------------------------------------------------===// 127905da65SAmit Sabne 137905da65SAmit Sabne #include "mlir/Transforms/Passes.h" 14b843cc5dSStephan Herhut 15b843cc5dSStephan Herhut #include "mlir/IR/Builders.h" 16b843cc5dSStephan Herhut #include "mlir/IR/Function.h" 17b843cc5dSStephan Herhut #include "mlir/Pass/Pass.h" 18b843cc5dSStephan Herhut #include "mlir/Transforms/LoopLikeInterface.h" 197a43da60SAmit Sabne #include "llvm/ADT/SmallPtrSet.h" 207905da65SAmit Sabne #include "llvm/Support/CommandLine.h" 217905da65SAmit Sabne #include "llvm/Support/Debug.h" 227905da65SAmit Sabne 237905da65SAmit Sabne #define DEBUG_TYPE "licm" 247905da65SAmit Sabne 257905da65SAmit Sabne using namespace mlir; 267905da65SAmit Sabne 277905da65SAmit Sabne namespace { 287905da65SAmit Sabne 297905da65SAmit Sabne /// Loop invariant code motion (LICM) pass. 30b843cc5dSStephan Herhut struct LoopInvariantCodeMotion : public OperationPass<LoopInvariantCodeMotion> { 31b843cc5dSStephan Herhut public: 32b843cc5dSStephan Herhut void runOnOperation() override; 337905da65SAmit Sabne }; 347905da65SAmit Sabne 35b843cc5dSStephan Herhut // Checks whether the given op can be hoisted by checking that 36b843cc5dSStephan Herhut // - the op and any of its contained operations do not depend on SSA values 37b843cc5dSStephan Herhut // defined inside of the loop (by means of calling definedOutside). 38b843cc5dSStephan Herhut // - the op has no side-effects. If sideEffecting is Never, sideeffects of this 39b843cc5dSStephan Herhut // op and its nested ops are ignored. 40b843cc5dSStephan Herhut static bool canBeHoisted(Operation *op, 41*b10c6625SRiver Riddle function_ref<bool(Value)> definedOutside) { 42b843cc5dSStephan Herhut // Check that dependencies are defined outside of loop. 43b843cc5dSStephan Herhut if (!llvm::all_of(op->getOperands(), definedOutside)) 44b843cc5dSStephan Herhut return false; 45b843cc5dSStephan Herhut // Check whether this op is side-effect free. If we already know that there 46b843cc5dSStephan Herhut // can be no side-effects because the surrounding op has claimed so, we can 47b843cc5dSStephan Herhut // (and have to) skip this step. 48*b10c6625SRiver Riddle if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { 49*b10c6625SRiver Riddle if (!memInterface.hasNoEffect()) 50*b10c6625SRiver Riddle return false; 51*b10c6625SRiver Riddle } else if (!op->hasNoSideEffect() && 52*b10c6625SRiver Riddle !op->hasTrait<OpTrait::HasRecursiveSideEffects>()) { 537a43da60SAmit Sabne return false; 547a43da60SAmit Sabne } 55*b10c6625SRiver Riddle 56*b10c6625SRiver Riddle // If the operation doesn't have side effects and it doesn't recursively 57*b10c6625SRiver Riddle // have side effects, it can always be hoisted. 58*b10c6625SRiver Riddle if (!op->hasTrait<OpTrait::HasRecursiveSideEffects>()) 59*b10c6625SRiver Riddle return true; 60*b10c6625SRiver Riddle 61b843cc5dSStephan Herhut // Recurse into the regions for this op and check whether the contained ops 62b843cc5dSStephan Herhut // can be hoisted. 63b843cc5dSStephan Herhut for (auto ®ion : op->getRegions()) { 64b843cc5dSStephan Herhut for (auto &block : region.getBlocks()) { 65*b10c6625SRiver Riddle for (auto &innerOp : block.without_terminator()) 66*b10c6625SRiver Riddle if (!canBeHoisted(&innerOp, definedOutside)) 677a43da60SAmit Sabne return false; 687a43da60SAmit Sabne } 697a43da60SAmit Sabne } 707a43da60SAmit Sabne return true; 717a43da60SAmit Sabne } 727a43da60SAmit Sabne 73*b10c6625SRiver Riddle static LogicalResult moveLoopInvariantCode(LoopLikeOpInterface looplike) { 74b843cc5dSStephan Herhut auto &loopBody = looplike.getLoopBody(); 757a43da60SAmit Sabne 76b843cc5dSStephan Herhut // We use two collections here as we need to preserve the order for insertion 77b843cc5dSStephan Herhut // and this is easiest. 78b843cc5dSStephan Herhut SmallPtrSet<Operation *, 8> willBeMovedSet; 797905da65SAmit Sabne SmallVector<Operation *, 8> opsToMove; 807905da65SAmit Sabne 81b843cc5dSStephan Herhut // Helper to check whether an operation is loop invariant wrt. SSA properties. 82e62a6956SRiver Riddle auto isDefinedOutsideOfBody = [&](Value value) { 832bdf33ccSRiver Riddle auto definingOp = value.getDefiningOp(); 84b843cc5dSStephan Herhut return (definingOp && !!willBeMovedSet.count(definingOp)) || 85b843cc5dSStephan Herhut looplike.isDefinedOutsideOfLoop(value); 86b843cc5dSStephan Herhut }; 87b843cc5dSStephan Herhut 88b843cc5dSStephan Herhut // Do not use walk here, as we do not want to go into nested regions and hoist 89b843cc5dSStephan Herhut // operations from there. These regions might have semantics unknown to this 90b843cc5dSStephan Herhut // rewriting. If the nested regions are loops, they will have been processed. 91b843cc5dSStephan Herhut for (auto &block : loopBody) { 92b843cc5dSStephan Herhut for (auto &op : block.without_terminator()) { 93*b10c6625SRiver Riddle if (canBeHoisted(&op, isDefinedOutsideOfBody)) { 947905da65SAmit Sabne opsToMove.push_back(&op); 95b843cc5dSStephan Herhut willBeMovedSet.insert(&op); 967905da65SAmit Sabne } 977a43da60SAmit Sabne } 987a43da60SAmit Sabne } 997905da65SAmit Sabne 100b843cc5dSStephan Herhut // For all instructions that we found to be invariant, move outside of the 101b843cc5dSStephan Herhut // loop. 102b843cc5dSStephan Herhut auto result = looplike.moveOutOfLoop(opsToMove); 103b843cc5dSStephan Herhut LLVM_DEBUG(looplike.print(llvm::dbgs() << "Modified loop\n")); 104b843cc5dSStephan Herhut return result; 1057905da65SAmit Sabne } 1067905da65SAmit Sabne 107b843cc5dSStephan Herhut } // end anonymous namespace 1087905da65SAmit Sabne 109b843cc5dSStephan Herhut void LoopInvariantCodeMotion::runOnOperation() { 1100134b5dfSChris Lattner // Walk through all loops in a function in innermost-loop-first order. This 1110134b5dfSChris Lattner // way, we first LICM from the inner loop, and place the ops in 1120134b5dfSChris Lattner // the outer loop, which in turn can be further LICM'ed. 113*b10c6625SRiver Riddle getOperation()->walk([&](LoopLikeOpInterface loopLike) { 114*b10c6625SRiver Riddle LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop\n")); 115*b10c6625SRiver Riddle if (failed(moveLoopInvariantCode(loopLike))) 116b843cc5dSStephan Herhut signalPassFailure(); 1170134b5dfSChris Lattner }); 1187905da65SAmit Sabne } 1197905da65SAmit Sabne 120b843cc5dSStephan Herhut // Include the generated code for the loop-like interface here, as it otherwise 121b843cc5dSStephan Herhut // has no compilation unit. This works as loop-invariant code motion is the 122b843cc5dSStephan Herhut // only user of that interface. 123b843cc5dSStephan Herhut #include "mlir/Transforms/LoopLikeInterface.cpp.inc" 124b843cc5dSStephan Herhut 125b843cc5dSStephan Herhut std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { 126b843cc5dSStephan Herhut return std::make_unique<LoopInvariantCodeMotion>(); 127b843cc5dSStephan Herhut } 128b843cc5dSStephan Herhut 1297905da65SAmit Sabne static PassRegistration<LoopInvariantCodeMotion> 130b843cc5dSStephan Herhut pass("loop-invariant-code-motion", 1317905da65SAmit Sabne "Hoist loop invariant instructions outside of the loop"); 132