[Mlir-commits] [mlir] [mlir] Assert region is within config scope in RegionPatternRewriteDriver (PR #193177)

Mehdi Amini llvmlistbot at llvm.org
Tue Apr 21 04:07:25 PDT 2026


https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/193177

>From 3bbe75e8e1d00c0a358fcdb620e5cd6edd118697 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Tue, 21 Apr 2026 02:55:57 -0700
Subject: [PATCH] [mlir] Assert region is within config scope in
 RegionPatternRewriteDriver

Assisted-by: Claude Code
---
 .../mlir/Transforms/GreedyPatternRewriteDriver.h     | 10 ++++++++--
 .../Transforms/Utils/GreedyPatternRewriteDriver.cpp  | 12 +++++++++++-
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
index d56d7e58c35f9..901964d82f62f 100644
--- a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
+++ b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
@@ -92,7 +92,9 @@ class GreedyRewriteConfig {
   /// Only ops within the scope are added to the worklist. If no scope is
   /// specified, the closest enclosing region around the initial list of ops
   /// (or the specified region, depending on which greedy rewrite entry point
-  /// is used) is used as a scope.
+  /// is used) is used as a scope. When using the region-based entry point, the
+  /// scope must enclose the provided region (i.e., the region must be nested
+  /// within the scope, or equal to it).
   Region *getScope() const { return scope; }
   GreedyRewriteConfig &setScope(Region *scope) {
     this->scope = scope;
@@ -167,6 +169,8 @@ class GreedyRewriteConfig {
 /// A region scope can be set in the configuration parameter. By default, the
 /// scope is set to the specified region. Only in-scope ops are added to the
 /// worklist and only in-scope ops are allowed to be modified by the patterns.
+/// If a scope is set explicitly, it must enclose `region` (i.e., `region`
+/// must be nested within the scope, or equal to it).
 ///
 /// Returns "success" if the iterative process converged (i.e., fixpoint was
 /// reached) and no more patterns can be matched within the region. `changed`
@@ -191,7 +195,9 @@ applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns,
 /// specified op. A region scope can be set in the configuration parameter. By
 /// default, the scope is set to the region of the current greedy rewrite. Only
 /// in-scope ops are added to the worklist and only in-scope ops and the
-/// specified op itself are allowed to be modified by the patterns.
+/// specified op itself are allowed to be modified by the patterns. If a scope
+/// is set explicitly, it must enclose `op` (i.e., `op` must be nested within
+/// the scope).
 ///
 /// Note: The specified op may be modified, but it may not be removed by the
 /// patterns.
diff --git a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
index 578e680535bed..ea7c28a07c21b 100644
--- a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
+++ b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
@@ -781,7 +781,8 @@ void GreedyPatternRewriteDriver::notifyMatchFailure(
 //===----------------------------------------------------------------------===//
 
 namespace {
-/// This driver simplfies all ops in a region.
+/// This driver simplfies all ops in a region. If a scope is set in the
+/// config, the provided region must be within that scope.
 class RegionPatternRewriteDriver : public GreedyPatternRewriteDriver {
 public:
   explicit RegionPatternRewriteDriver(MLIRContext *ctx,
@@ -807,6 +808,15 @@ RegionPatternRewriteDriver::RegionPatternRewriteDriver(
   if (config.getStrictness() != GreedyRewriteStrictness::AnyOp) {
     region.walk([&](Operation *op) { strictModeFilteredOps.insert(op); });
   }
+#ifndef NDEBUG
+  // Verify that the region is within the configured scope (if any).
+  if (Region *scope = config.getScope()) {
+    Region *r = ®ion;
+    while (r && r != scope)
+      r = r->getParentRegion();
+    assert(r && "provided region is not within the config scope");
+  }
+#endif
 }
 
 namespace {



More information about the Mlir-commits mailing list