1 //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===// 2 // 3 // Part of the LLVM 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements loop invariant code motion. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Transforms/Passes.h" 14 15 #include "mlir/IR/PatternMatch.h" 16 #include "mlir/Interfaces/LoopLikeInterface.h" 17 #include "mlir/Interfaces/SideEffectInterfaces.h" 18 #include "mlir/Transforms/LoopInvariantCodeMotionUtils.h" 19 20 namespace mlir { 21 #define GEN_PASS_DEF_LOOPINVARIANTCODEMOTION 22 #define GEN_PASS_DEF_LOOPINVARIANTSUBSETHOISTING 23 #include "mlir/Transforms/Passes.h.inc" 24 } // namespace mlir 25 26 using namespace mlir; 27 28 namespace { 29 /// Loop invariant code motion (LICM) pass. 30 struct LoopInvariantCodeMotion 31 : public impl::LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> { 32 void runOnOperation() override; 33 }; 34 35 struct LoopInvariantSubsetHoisting 36 : public impl::LoopInvariantSubsetHoistingBase< 37 LoopInvariantSubsetHoisting> { 38 void runOnOperation() override; 39 }; 40 } // namespace 41 runOnOperation()42void LoopInvariantCodeMotion::runOnOperation() { 43 // Walk through all loops in a function in innermost-loop-first order. This 44 // way, we first LICM from the inner loop, and place the ops in 45 // the outer loop, which in turn can be further LICM'ed. 46 getOperation()->walk( 47 [&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); }); 48 } 49 runOnOperation()50void LoopInvariantSubsetHoisting::runOnOperation() { 51 IRRewriter rewriter(getOperation()->getContext()); 52 // Walk through all loops in a function in innermost-loop-first order. This 53 // way, we first hoist from the inner loop, and place the ops in the outer 54 // loop, which in turn can be further hoisted from. 55 getOperation()->walk([&](LoopLikeOpInterface loopLike) { 56 (void)hoistLoopInvariantSubsets(rewriter, loopLike); 57 }); 58 } 59 createLoopInvariantCodeMotionPass()60std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { 61 return std::make_unique<LoopInvariantCodeMotion>(); 62 } 63 createLoopInvariantSubsetHoistingPass()64std::unique_ptr<Pass> mlir::createLoopInvariantSubsetHoistingPass() { 65 return std::make_unique<LoopInvariantSubsetHoisting>(); 66 } 67