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 "PassDetail.h" 14 #include "mlir/Interfaces/LoopLikeInterface.h" 15 #include "mlir/Transforms/LoopInvariantCodeMotionUtils.h" 16 #include "mlir/Transforms/Passes.h" 17 #include "mlir/Transforms/SideEffectUtils.h" 18 19 using namespace mlir; 20 21 namespace { 22 /// Loop invariant code motion (LICM) pass. 23 struct LoopInvariantCodeMotion 24 : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> { 25 void runOnOperation() override; 26 }; 27 } // namespace 28 29 void LoopInvariantCodeMotion::runOnOperation() { 30 // Walk through all loops in a function in innermost-loop-first order. This 31 // way, we first LICM from the inner loop, and place the ops in 32 // the outer loop, which in turn can be further LICM'ed. 33 getOperation()->walk( 34 [&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); }); 35 } 36 37 std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() { 38 return std::make_unique<LoopInvariantCodeMotion>(); 39 } 40