2019-04-17 12:18:37 -07:00
|
|
|
//===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
|
|
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-04-17 12:18:37 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-04-17 12:18:37 -07:00
|
|
|
//
|
|
|
|
|
// This file implements loop invariant code motion.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2022-08-31 10:16:29 +02:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
|
|
2023-11-05 11:40:51 +09:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2020-03-14 13:36:42 -07:00
|
|
|
#include "mlir/Interfaces/LoopLikeInterface.h"
|
2022-04-16 00:36:22 +00:00
|
|
|
#include "mlir/Transforms/LoopInvariantCodeMotionUtils.h"
|
2019-04-17 12:18:37 -07:00
|
|
|
|
2022-08-31 10:16:29 +02:00
|
|
|
namespace mlir {
|
|
|
|
|
#define GEN_PASS_DEF_LOOPINVARIANTCODEMOTION
|
2023-11-01 10:57:17 +09:00
|
|
|
#define GEN_PASS_DEF_LOOPINVARIANTSUBSETHOISTING
|
2022-08-31 10:16:29 +02:00
|
|
|
#include "mlir/Transforms/Passes.h.inc"
|
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
2019-04-17 12:18:37 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
/// Loop invariant code motion (LICM) pass.
|
2022-08-30 22:20:36 +02:00
|
|
|
struct LoopInvariantCodeMotion
|
2022-08-31 10:16:29 +02:00
|
|
|
: public impl::LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
|
2019-10-16 04:28:13 -07:00
|
|
|
void runOnOperation() override;
|
2019-04-17 12:18:37 -07:00
|
|
|
};
|
2023-11-01 10:57:17 +09:00
|
|
|
|
|
|
|
|
struct LoopInvariantSubsetHoisting
|
|
|
|
|
: public impl::LoopInvariantSubsetHoistingBase<
|
|
|
|
|
LoopInvariantSubsetHoisting> {
|
|
|
|
|
void runOnOperation() override;
|
|
|
|
|
};
|
2021-12-07 18:27:58 +00:00
|
|
|
} // namespace
|
2019-04-17 12:18:37 -07:00
|
|
|
|
2022-08-30 22:20:36 +02:00
|
|
|
void LoopInvariantCodeMotion::runOnOperation() {
|
2019-10-16 04:28:13 -07:00
|
|
|
// Walk through all loops in a function in innermost-loop-first order. This
|
2019-05-11 08:28:15 -07:00
|
|
|
// way, we first LICM from the inner loop, and place the ops in
|
|
|
|
|
// the outer loop, which in turn can be further LICM'ed.
|
2022-04-16 00:36:22 +00:00
|
|
|
getOperation()->walk(
|
|
|
|
|
[&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); });
|
2019-04-17 12:18:37 -07:00
|
|
|
}
|
2022-08-30 22:20:36 +02:00
|
|
|
|
2023-11-01 10:57:17 +09:00
|
|
|
void LoopInvariantSubsetHoisting::runOnOperation() {
|
2023-11-05 11:40:51 +09:00
|
|
|
IRRewriter rewriter(getOperation()->getContext());
|
2023-11-01 10:57:17 +09:00
|
|
|
// Walk through all loops in a function in innermost-loop-first order. This
|
|
|
|
|
// way, we first hoist from the inner loop, and place the ops in the outer
|
|
|
|
|
// loop, which in turn can be further hoisted from.
|
|
|
|
|
getOperation()->walk([&](LoopLikeOpInterface loopLike) {
|
2023-11-05 11:40:51 +09:00
|
|
|
(void)hoistLoopInvariantSubsets(rewriter, loopLike);
|
2023-11-01 10:57:17 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 22:20:36 +02:00
|
|
|
std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
|
|
|
|
|
return std::make_unique<LoopInvariantCodeMotion>();
|
|
|
|
|
}
|
2023-11-01 10:57:17 +09:00
|
|
|
|
|
|
|
|
std::unique_ptr<Pass> mlir::createLoopInvariantSubsetHoistingPass() {
|
|
|
|
|
return std::make_unique<LoopInvariantSubsetHoisting>();
|
|
|
|
|
}
|