Skip to content

Conversation

shay-kl
Copy link
Contributor

@shay-kl shay-kl commented Aug 5, 2025

The function isPerfectlyNestedForLoops is useful on its own and so I'm exposing it for downstream use.

@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-mlir-scf

@llvm/pr-subscribers-mlir

Author: Shay Kleiman (shay-kl)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/152115.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/SCF/Transforms/TileUsingInterface.h (+8)
  • (modified) mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp (+2-3)
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.");
   }

Copy link

github-actions bot commented Aug 5, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@shay-kl shay-kl force-pushed the shay-kl/expose_isPerfectlyNestedForLoops branch from 5aa8d01 to 9e9093f Compare August 5, 2025 10:23
@amirBish amirBish requested review from MaheshRavishankar, Groverkss and Abhishek-Varma and removed request for MaheshRavishankar and Groverkss August 6, 2025 09:28
@shay-kl shay-kl self-assigned this Aug 10, 2025
@shay-kl shay-kl requested review from ftynse and joker-eph August 13, 2025 05:36
@shay-kl
Copy link
Contributor Author

shay-kl commented Aug 24, 2025

Ping

@Abhishek-Varma
Copy link
Contributor

Hi @shay-kl - if the utility has to be moved/exposed, I'd rather it be done as part of https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/SCF/Utils/Utils.h instead.

CC: @MaheshRavishankar

shay-kl and others added 2 commits August 26, 2025 10:39
@shay-kl shay-kl merged commit 0cbb6e7 into llvm:main Aug 26, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants