xref: /llvm-project/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp (revision 2be8af8f0e0780901213b6fd3013a5268ddc3359)
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/Interfaces/LoopLikeInterface.h"
16 #include "mlir/Transforms/LoopInvariantCodeMotionUtils.h"
17 #include "mlir/Transforms/SideEffectUtils.h"
18 
19 namespace mlir {
20 #define GEN_PASS_DEF_LOOPINVARIANTCODEMOTIONPASS
21 #include "mlir/Transforms/Passes.h.inc"
22 } // namespace mlir
23 
24 using namespace mlir;
25 
26 namespace {
27 /// Loop invariant code motion (LICM) pass.
28 struct LoopInvariantCodeMotionPass
29     : public impl::LoopInvariantCodeMotionPassBase<
30           LoopInvariantCodeMotionPass> {
31   using LoopInvariantCodeMotionPassBase::LoopInvariantCodeMotionPassBase;
32 
33   void runOnOperation() override;
34 };
35 } // namespace
36 
37 void LoopInvariantCodeMotionPass::runOnOperation() {
38   // Walk through all loops in a function in innermost-loop-first order. This
39   // way, we first LICM from the inner loop, and place the ops in
40   // the outer loop, which in turn can be further LICM'ed.
41   getOperation()->walk(
42       [&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); });
43 }
44