[Mlir-commits] [mlir] [mlir][scf] Expose kPeeledLoopLabel as a public marker attribute (PR #210922)

Shay Kleiman llvmlistbot at llvm.org
Tue Jul 21 08:53:45 PDT 2026


https://github.com/shay-kl updated https://github.com/llvm/llvm-project/pull/210922

>From 5c7a50eb59808b89c343e38fbb1f4ba98027dc05 Mon Sep 17 00:00:00 2001
From: Shay Kleiman <shay.kleiman at mobileye.com>
Date: Tue, 21 Jul 2026 12:15:15 +0300
Subject: [PATCH 1/2] [mlir][scf] Expose kPeeledLoopLabel as a public marker
 attribute

Change-Id: I08fa8bcab6cf76bc36894fb79aa32635c1aebe15
---
 mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h  | 3 +++
 mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp | 9 ++++-----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h b/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
index 00e8572307151..3b96bd77f6928 100644
--- a/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
+++ b/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
@@ -106,6 +106,9 @@ LogicalResult peelForLoopAndSimplifyBounds(RewriterBase &rewriter, ForOp forOp,
 LogicalResult peelForLoopFirstIteration(RewriterBase &rewriter, ForOp forOp,
                                         scf::ForOp &partialIteration);
 
+/// Marker attribute name for loops that have already been peeled.
+static constexpr StringLiteral kPeeledLoopLabel = "__peeled_loop__";
+
 /// Tile a parallel loop of the form
 ///   scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
 ///                                             step (%arg4, %arg5)
diff --git a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
index a39e5520a144b..a8609b2c9eafb 100644
--- a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
@@ -256,7 +256,6 @@ LogicalResult mlir::scf::peelForLoopFirstIteration(RewriterBase &b, ForOp forOp,
   return success();
 }
 
-static constexpr char kPeeledLoopLabel[] = "__peeled_loop__";
 static constexpr char kPartialIterationLabel[] = "__partial_iteration__";
 
 namespace {
@@ -272,7 +271,7 @@ struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
                                          "unsigned loops are not supported");
 
     // Do not peel already peeled loops.
-    if (forOp->hasAttr(kPeeledLoopLabel))
+    if (forOp->hasAttr(scf::kPeeledLoopLabel))
       return failure();
 
     scf::ForOp partialIteration;
@@ -300,11 +299,11 @@ struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
 
     // Apply label, so that the same loop is not rewritten a second time.
     rewriter.modifyOpInPlace(partialIteration, [&]() {
-      partialIteration->setAttr(kPeeledLoopLabel, rewriter.getUnitAttr());
+      partialIteration->setAttr(scf::kPeeledLoopLabel, rewriter.getUnitAttr());
       partialIteration->setAttr(kPartialIterationLabel, rewriter.getUnitAttr());
     });
     rewriter.modifyOpInPlace(forOp, [&]() {
-      forOp->setAttr(kPeeledLoopLabel, rewriter.getUnitAttr());
+      forOp->setAttr(scf::kPeeledLoopLabel, rewriter.getUnitAttr());
     });
     return success();
   }
@@ -351,7 +350,7 @@ struct ForLoopPeeling : public impl::SCFForLoopPeelingBase<ForLoopPeeling> {
 
     // Drop the markers.
     parentOp->walk([](Operation *op) {
-      op->removeAttr(kPeeledLoopLabel);
+      op->removeAttr(scf::kPeeledLoopLabel);
       op->removeAttr(kPartialIterationLabel);
     });
   }

>From 01f25f7b0844c05cca5e70d9314e3ae8ac5f1de7 Mon Sep 17 00:00:00 2001
From: Shay Kleiman <shay.kleiman at mobileye.com>
Date: Tue, 21 Jul 2026 16:14:04 +0300
Subject: [PATCH 2/2] cr

Change-Id: I57556c92a6889719537955f5e92932c7faeef678
---
 mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h |  4 ++--
 .../lib/Dialect/SCF/Transforms/LoopSpecialization.cpp | 11 +++++++----
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h b/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
index 3b96bd77f6928..a11a60629b844 100644
--- a/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
+++ b/mlir/include/mlir/Dialect/SCF/Transforms/Transforms.h
@@ -106,8 +106,8 @@ LogicalResult peelForLoopAndSimplifyBounds(RewriterBase &rewriter, ForOp forOp,
 LogicalResult peelForLoopFirstIteration(RewriterBase &rewriter, ForOp forOp,
                                         scf::ForOp &partialIteration);
 
-/// Marker attribute name for loops that have already been peeled.
-static constexpr StringLiteral kPeeledLoopLabel = "__peeled_loop__";
+/// Attribute name used to mark loops that have already been peeled.
+StringRef getPeeledLoopAttrName();
 
 /// Tile a parallel loop of the form
 ///   scf.parallel (%i0, %i1) = (%arg0, %arg1) to (%arg2, %arg3)
diff --git a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
index a8609b2c9eafb..53359b46bde57 100644
--- a/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/LoopSpecialization.cpp
@@ -258,6 +258,8 @@ LogicalResult mlir::scf::peelForLoopFirstIteration(RewriterBase &b, ForOp forOp,
 
 static constexpr char kPartialIterationLabel[] = "__partial_iteration__";
 
+StringRef mlir::scf::getPeeledLoopAttrName() { return "__peeled_loop__"; }
+
 namespace {
 struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
   ForLoopPeelingPattern(MLIRContext *ctx, bool peelFront, bool skipPartial)
@@ -271,7 +273,7 @@ struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
                                          "unsigned loops are not supported");
 
     // Do not peel already peeled loops.
-    if (forOp->hasAttr(scf::kPeeledLoopLabel))
+    if (forOp->hasAttr(scf::getPeeledLoopAttrName()))
       return failure();
 
     scf::ForOp partialIteration;
@@ -299,11 +301,12 @@ struct ForLoopPeelingPattern : public OpRewritePattern<ForOp> {
 
     // Apply label, so that the same loop is not rewritten a second time.
     rewriter.modifyOpInPlace(partialIteration, [&]() {
-      partialIteration->setAttr(scf::kPeeledLoopLabel, rewriter.getUnitAttr());
+      partialIteration->setAttr(scf::getPeeledLoopAttrName(),
+                                rewriter.getUnitAttr());
       partialIteration->setAttr(kPartialIterationLabel, rewriter.getUnitAttr());
     });
     rewriter.modifyOpInPlace(forOp, [&]() {
-      forOp->setAttr(scf::kPeeledLoopLabel, rewriter.getUnitAttr());
+      forOp->setAttr(scf::getPeeledLoopAttrName(), rewriter.getUnitAttr());
     });
     return success();
   }
@@ -350,7 +353,7 @@ struct ForLoopPeeling : public impl::SCFForLoopPeelingBase<ForLoopPeeling> {
 
     // Drop the markers.
     parentOp->walk([](Operation *op) {
-      op->removeAttr(scf::kPeeledLoopLabel);
+      op->removeAttr(scf::getPeeledLoopAttrName());
       op->removeAttr(kPartialIterationLabel);
     });
   }



More information about the Mlir-commits mailing list