Skip to content

[mlir][Transforms] Add loop-invariant subset hoisting (LISH) transformation #70619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions mlir/include/mlir/Transforms/LoopInvariantCodeMotionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,45 @@ size_t moveLoopInvariantCode(
/// methods provided by the interface.
size_t moveLoopInvariantCode(LoopLikeOpInterface loopLike);

/// Hoist loop-invariant tensor subsets (subset extraction and subset insertion
/// ops) from loop-like ops. Extraction ops are moved before the loop. Insertion
/// ops are moved after the loop. The loop body operates on newly added region
/// iter_args (one per extraction-insertion pair).
///
/// A subset extraction op (`SubsetExtractionOpInterface`) extracts from a
/// tensor value at a subset. The result of the op may have an arbitrary type,
/// i.e., not necessarily a tensor type. Example: "tensor.extract_slice".
///
/// A subset insertion op (`SubsetInsertionOpInterface`) inserts into a tensor
/// value ("destination") at a subset. Example: "tensor.insert_slice".
///
/// Matching extraction-insertion subset ops can be hoisted from a loop if there
/// are no other ops within the loop that operate on the same or on an
/// overlapping subset. In particular, non-subset ops can prevent hoisting
/// because the analysis does not know what subset they operate on.
///
/// Example:
/// ```
/// %r = scf.for ... iter_args(%t = %a) -> (tensor<?xf32>) {
/// %0 = tensor.extract_slice %t[0][5][1] : tensor<?xf32> to tensor<5xf32>
/// %1 = "test.foo"(%0) : (tensor<5xf32>) -> (tensor<5xf32>)
/// %2 = tensor.insert_slice %1 into %t[0][5][1]
/// : tensor<5xf32> into tensor<?xf32>
/// scf.yield %2 : tensor<?xf32>
/// }
/// ```
/// Is rewritten to:
/// ```
/// %0 = tensor.extract_slice %a[0][5][1] : tensor<?xf32> to tensor<5xf32>
/// %new_loop:2 = scf.for ... iter_args(%t = %a, %h = %0) -> (tensor<?xf32>) {
/// %1 = "test.foo"(%h) : (tensor<5xf32>) -> (tensor<5xf32>)
/// scf.yield %t, %2 : tensor<?xf32>, tensor<5xf32>
/// }
/// %r = tensor.insert_slice %new_loop#1 into %new_loop#0
/// : tensor<5xf32> into tensor<?xf32>
/// ```
LoopLikeOpInterface hoistLoopInvariantSubsets(LoopLikeOpInterface loopLike);

} // end namespace mlir

#endif // MLIR_TRANSFORMS_LOOPINVARIANTCODEMOTIONUTILS_H
3 changes: 3 additions & 0 deletions mlir/include/mlir/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ std::unique_ptr<Pass> createGenerateRuntimeVerificationPass();
/// instructions out of the loop.
std::unique_ptr<Pass> createLoopInvariantCodeMotionPass();

/// Creates a pass that hoists loop-invariant subset ops.
std::unique_ptr<Pass> createLoopInvariantSubsetHoistingPass();

/// Creates a pass to strip debug information from a function.
std::unique_ptr<Pass> createStripDebugInfoPass();

Expand Down
5 changes: 5 additions & 0 deletions mlir/include/mlir/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ def LoopInvariantCodeMotion : Pass<"loop-invariant-code-motion"> {
let constructor = "mlir::createLoopInvariantCodeMotionPass()";
}

def LoopInvariantSubsetHoisting : Pass<"loop-invariant-subset-hoisting"> {
let summary = "Hoist loop invariant subset ops outside of the loop";
let constructor = "mlir::createLoopInvariantSubsetHoistingPass()";
}

def Mem2Reg : Pass<"mem2reg"> {
let summary = "Promotes memory slots into values.";
let description = [{
Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/Transforms/LoopInvariantCodeMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace mlir {
#define GEN_PASS_DEF_LOOPINVARIANTCODEMOTION
#define GEN_PASS_DEF_LOOPINVARIANTSUBSETHOISTING
#include "mlir/Transforms/Passes.h.inc"
} // namespace mlir

Expand All @@ -29,6 +30,12 @@ struct LoopInvariantCodeMotion
: public impl::LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
void runOnOperation() override;
};

struct LoopInvariantSubsetHoisting
: public impl::LoopInvariantSubsetHoistingBase<
LoopInvariantSubsetHoisting> {
void runOnOperation() override;
};
} // namespace

void LoopInvariantCodeMotion::runOnOperation() {
Expand All @@ -39,6 +46,19 @@ void LoopInvariantCodeMotion::runOnOperation() {
[&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); });
}

void LoopInvariantSubsetHoisting::runOnOperation() {
// 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) {
(void)hoistLoopInvariantSubsets(loopLike);
});
}

std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
return std::make_unique<LoopInvariantCodeMotion>();
}

std::unique_ptr<Pass> mlir::createLoopInvariantSubsetHoistingPass() {
return std::make_unique<LoopInvariantSubsetHoisting>();
}
1 change: 1 addition & 0 deletions mlir/lib/Transforms/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ add_mlir_library(MLIRTransformUtils
MLIRFunctionInterfaces
MLIRLoopLikeInterface
MLIRSideEffectInterfaces
MLIRSubsetOpInterface
MLIRRewrite
)
Loading