[Mlir-commits] [mlir] [mlir][scf] Expose isPerfectlyNestedForLoops (PR #152115)

Shay Kleiman llvmlistbot at llvm.org
Tue Aug 5 03:20:33 PDT 2025


https://github.com/shay-kl created https://github.com/llvm/llvm-project/pull/152115

None

>From 5aa8d0154cd21307751b24810def6539b4c42194 Mon Sep 17 00:00:00 2001
From: Shay Kleiman <shay.kleiman at mobileye.com>
Date: Tue, 29 Jul 2025 18:02:59 +0300
Subject: [PATCH] [mlir][scf] Expose isPerfectlyNestedForLoops

---
 .../mlir/Dialect/SCF/Transforms/TileUsingInterface.h      | 8 ++++++++
 mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp    | 5 ++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Dialect/SCF/Transforms/TileUsingInterface.h b/mlir/include/mlir/Dialect/SCF/Transforms/TileUsingInterface.h
index 3205da6e448fc..1177aee560f6a 100644
--- a/mlir/include/mlir/Dialect/SCF/Transforms/TileUsingInterface.h
+++ b/mlir/include/mlir/Dialect/SCF/Transforms/TileUsingInterface.h
@@ -364,6 +364,14 @@ FailureOr<scf::SCFTilingResult>
 tileReductionUsingScf(RewriterBase &b, PartialReductionOpInterface op,
                       ArrayRef<OpFoldResult> tileSizes);
 
+/// Check if the provided loops are perfectly nested for-loops. Perfect nesting
+/// means:
+/// 1. All loops are scf.for operations
+/// 2. Each outer loop's region iter args match the inner loop's init args
+/// 3. Each outer loop's yields match the inner loop's results
+/// 4. Each region iter arg and result has exactly one use
+bool isPerfectlyNestedForLoops(MutableArrayRef<LoopLikeOpInterface> loops);
+
 } // namespace scf
 } // namespace mlir
 
diff --git a/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp b/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp
index c0e47ee1e74fc..0a717de3bda0d 100644
--- a/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp
@@ -1927,8 +1927,7 @@ static FailureOr<OpOperand *> getConsumerFromLoopUses(RewriterBase &rewriter,
 ///    yield %1
 /// ```
 /// Here loops should be [%0, %1].
-static bool
-isPerfectlyNestedForLoops(MutableArrayRef<LoopLikeOpInterface> loops) {
+bool mlir::scf::isPerfectlyNestedForLoops(MutableArrayRef<LoopLikeOpInterface> loops) {
   assert(!loops.empty() && "unexpected empty loop nest");
   if (loops.size() == 1) {
     return isa_and_nonnull<scf::ForOp>(loops.front().getOperation());
@@ -1991,7 +1990,7 @@ getUntiledConsumerFromSlice(RewriterBase &rewriter,
   }
 
   // 2. Check that the loop is perfectly nested.
-  if (!isPerfectlyNestedForLoops(loops)) {
+  if (!mlir::scf::isPerfectlyNestedForLoops(loops)) {
     return rewriter.notifyMatchFailure(
         candidateSliceOp, "expected passed loops to be perfectly nested.");
   }



More information about the Mlir-commits mailing list