xref: /llvm-project/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp (revision b9fe461e7382875156890b286d53b4429fcaa588)
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 
1367d0d7acSMichele Scuttari #include "mlir/Transforms/Passes.h"
1467d0d7acSMichele Scuttari 
15*b9fe461eSMatthias Springer #include "mlir/IR/PatternMatch.h"
1643959a25SRiver Riddle #include "mlir/Interfaces/LoopLikeInterface.h"
17fc367dfaSMahesh Ravishankar #include "mlir/Interfaces/SideEffectInterfaces.h"
18fa26c7ffSMogball #include "mlir/Transforms/LoopInvariantCodeMotionUtils.h"
197905da65SAmit Sabne 
2067d0d7acSMichele Scuttari namespace mlir {
2167d0d7acSMichele Scuttari #define GEN_PASS_DEF_LOOPINVARIANTCODEMOTION
222164a449SMatthias Springer #define GEN_PASS_DEF_LOOPINVARIANTSUBSETHOISTING
2367d0d7acSMichele Scuttari #include "mlir/Transforms/Passes.h.inc"
2467d0d7acSMichele Scuttari } // namespace mlir
2567d0d7acSMichele Scuttari 
267905da65SAmit Sabne using namespace mlir;
277905da65SAmit Sabne 
287905da65SAmit Sabne namespace {
297905da65SAmit Sabne /// Loop invariant code motion (LICM) pass.
30039b969bSMichele Scuttari struct LoopInvariantCodeMotion
3167d0d7acSMichele Scuttari     : public impl::LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
32b843cc5dSStephan Herhut   void runOnOperation() override;
337905da65SAmit Sabne };
342164a449SMatthias Springer 
352164a449SMatthias Springer struct LoopInvariantSubsetHoisting
362164a449SMatthias Springer     : public impl::LoopInvariantSubsetHoistingBase<
372164a449SMatthias Springer           LoopInvariantSubsetHoisting> {
382164a449SMatthias Springer   void runOnOperation() override;
392164a449SMatthias Springer };
40be0a7e9fSMehdi Amini } // namespace
417905da65SAmit Sabne 
runOnOperation()42039b969bSMichele Scuttari void LoopInvariantCodeMotion::runOnOperation() {
430134b5dfSChris Lattner   // Walk through all loops in a function in innermost-loop-first order. This
440134b5dfSChris Lattner   // way, we first LICM from the inner loop, and place the ops in
450134b5dfSChris Lattner   // the outer loop, which in turn can be further LICM'ed.
46fa26c7ffSMogball   getOperation()->walk(
47fa26c7ffSMogball       [&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); });
487905da65SAmit Sabne }
49039b969bSMichele Scuttari 
runOnOperation()502164a449SMatthias Springer void LoopInvariantSubsetHoisting::runOnOperation() {
51*b9fe461eSMatthias Springer   IRRewriter rewriter(getOperation()->getContext());
522164a449SMatthias Springer   // Walk through all loops in a function in innermost-loop-first order. This
532164a449SMatthias Springer   // way, we first hoist from the inner loop, and place the ops in the outer
542164a449SMatthias Springer   // loop, which in turn can be further hoisted from.
552164a449SMatthias Springer   getOperation()->walk([&](LoopLikeOpInterface loopLike) {
56*b9fe461eSMatthias Springer     (void)hoistLoopInvariantSubsets(rewriter, loopLike);
572164a449SMatthias Springer   });
582164a449SMatthias Springer }
592164a449SMatthias Springer 
createLoopInvariantCodeMotionPass()60039b969bSMichele Scuttari std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
61039b969bSMichele Scuttari   return std::make_unique<LoopInvariantCodeMotion>();
62039b969bSMichele Scuttari }
632164a449SMatthias Springer 
createLoopInvariantSubsetHoistingPass()642164a449SMatthias Springer std::unique_ptr<Pass> mlir::createLoopInvariantSubsetHoistingPass() {
652164a449SMatthias Springer   return std::make_unique<LoopInvariantSubsetHoisting>();
662164a449SMatthias Springer }
67