Skip to content

[mlir] Assert region is within config scope in RegionPatternRewriteDriver#193177

Merged
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:assert-regionpatterndriver
Apr 21, 2026
Merged

[mlir] Assert region is within config scope in RegionPatternRewriteDriver#193177
joker-eph merged 1 commit into
llvm:mainfrom
joker-eph:assert-regionpatterndriver

Conversation

@joker-eph
Copy link
Copy Markdown
Contributor

Assisted-by: Claude Code

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Apr 21, 2026

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Mehdi Amini (joker-eph)

Changes

Assisted-by: Claude Code


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

2 Files Affected:

  • (modified) mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h (+5-1)
  • (modified) mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp (+9-1)
diff --git a/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h b/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
index d56d7e58c35f9..b21efc25cb963 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 not be narrower than 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 not be narrower than `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`
diff --git a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
index 578e680535bed..165e9971a4ac4 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,13 @@ RegionPatternRewriteDriver::RegionPatternRewriteDriver(
   if (config.getStrictness() != GreedyRewriteStrictness::AnyOp) {
     region.walk([&](Operation *op) { strictModeFilteredOps.insert(op); });
   }
+  // Verify that the region is within the configured scope (if any).
+  if (Region *scope = config.getScope()) {
+    Region *r = &region;
+    while (r && r != scope)
+      r = r->getParentRegion();
+    assert(r && "provided region is not within the config scope");
+  }
 }
 
 namespace {

Comment thread mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h Outdated
Comment thread mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
@joker-eph joker-eph force-pushed the assert-regionpatterndriver branch from caba644 to 3bbe75e Compare April 21, 2026 11:07
@joker-eph joker-eph force-pushed the assert-regionpatterndriver branch from 3bbe75e to 3a7e33f Compare April 21, 2026 11:23
@joker-eph joker-eph enabled auto-merge (squash) April 21, 2026 11:23
@joker-eph joker-eph merged commit ed34ee3 into llvm:main Apr 21, 2026
10 checks passed
linuxlonelyeagle pushed a commit to linuxlonelyeagle/llvm-project that referenced this pull request Apr 23, 2026
KHicketts pushed a commit to KHicketts/llvm-project that referenced this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants