[Mlir-commits] [mlir] [mlir] add getRegionNonforwardedArguments API to RegionSuccessor (PR #175212)

lonely eagle llvmlistbot at llvm.org
Fri Jan 9 09:36:32 PST 2026


https://github.com/linuxlonelyeagle created https://github.com/llvm/llvm-project/pull/175212

The getSuccessorInputs function is used to access successor inputs, but the region's arguments include arguments that are not forwarded, so I added an API to access them.

>From 8b7ec3e2e04bf2258144afb18ceb37523d1e1d16 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 7 Jan 2026 15:41:34 +0000
Subject: [PATCH 1/3] delete visitRegionSuccessors not run code.

---
 mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp | 33 +++++++------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
index b9d861830dd38..02f0e0b77831d 100644
--- a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
@@ -307,29 +307,20 @@ void AbstractSparseForwardDataFlowAnalysis::visitRegionSuccessors(
            "expected the same number of successor inputs as operands");
 
     unsigned firstIndex = 0;
-    if (inputs.size() != lattices.size()) {
-      if (!point->isBlockStart()) {
-        if (!inputs.empty())
-          firstIndex = cast<OpResult>(inputs.front()).getResultNumber();
-        visitNonControlFlowArgumentsImpl(
-            branch,
-            RegionSuccessor(
-                branch, branch->getResults().slice(firstIndex, inputs.size())),
-            lattices, firstIndex);
-      } else {
-        if (!inputs.empty())
-          firstIndex = cast<BlockArgument>(inputs.front()).getArgNumber();
-        Region *region = point->getBlock()->getParent();
-        visitNonControlFlowArgumentsImpl(
-            branch,
-            RegionSuccessor(region, region->getArguments().slice(
-                                        firstIndex, inputs.size())),
-            lattices, firstIndex);
-      }
+    if (inputs.size() != lattices.size() && point->isBlockStart()) {
+      if (!inputs.empty())
+        firstIndex = cast<BlockArgument>(inputs.front()).getArgNumber();
+      Region *region = point->getBlock()->getParent();
+      visitNonControlFlowArgumentsImpl(
+          branch,
+          RegionSuccessor(
+              region, region->getArguments().slice(firstIndex, inputs.size())),
+          lattices, firstIndex);
     }
 
-    for (auto it : llvm::zip(*operands, lattices.drop_front(firstIndex)))
-      join(std::get<1>(it), *getLatticeElementFor(point, std::get<0>(it)));
+    for (auto [lattice, operand] :
+         llvm::zip_equal(lattices.drop_front(firstIndex), *operands))
+      join(lattice, *getLatticeElementFor(point, operand));
   }
 }
 

>From c83202642357242adcbc0ce1c90eb9f89dbca817 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 7 Jan 2026 16:07:34 +0000
Subject: [PATCH 2/3] don't use zip_equal.

---
 mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
index 02f0e0b77831d..695226a49df93 100644
--- a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
@@ -319,7 +319,7 @@ void AbstractSparseForwardDataFlowAnalysis::visitRegionSuccessors(
     }
 
     for (auto [lattice, operand] :
-         llvm::zip_equal(lattices.drop_front(firstIndex), *operands))
+         llvm::zip(lattices.drop_front(firstIndex), *operands))
       join(lattice, *getLatticeElementFor(point, operand));
   }
 }

>From 04e9a03538bdea676071453f367f6ef25b37fdf3 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Fri, 9 Jan 2026 11:03:16 +0000
Subject: [PATCH 3/3] add getRegionNonforwardedArguments API.

---
 .../mlir/Interfaces/ControlFlowInterfaces.h    | 18 ++++++++++++++++++
 mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp  | 12 ++----------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h
index b76c2891fad5a..76aa20f11d84b 100644
--- a/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h
+++ b/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h
@@ -228,6 +228,24 @@ class RegionSuccessor {
   /// the current region.
   ValueRange getSuccessorInputs() const { return inputs; }
 
+  ValueRange getRegionNonforwardedArguments() const {
+    if (isParent())
+      return {};
+    SmallVector<Value> nonForwardArguments;
+    MutableArrayRef<BlockArgument> arguments = getSuccessor()->getArguments();
+    if (arguments.empty())
+      return {};
+    ValueRange inputs = getSuccessorInputs();
+    if (inputs.empty())
+      return arguments;
+    for (BlockArgument argument : arguments) {
+      if (!llvm::is_contained(inputs, argument)) {
+        nonForwardArguments.push_back(argument);
+      }
+    }
+    return nonForwardArguments;
+  }
+
   bool operator==(RegionSuccessor rhs) const {
     return successor == rhs.successor && inputs == rhs.inputs;
   }
diff --git a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
index 695226a49df93..97889415da31a 100644
--- a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
@@ -603,16 +603,8 @@ void AbstractSparseBackwardDataFlowAnalysis::visitRegionSuccessors(
     if (successor.isParent())
       continue;
     SmallVector<BlockArgument> noControlFlowArguments;
-    MutableArrayRef<BlockArgument> arguments =
-        successor.getSuccessor()->getArguments();
-    ValueRange inputs = successor.getSuccessorInputs();
-    for (BlockArgument argument : arguments) {
-      // Visit blockArgument of RegionBranchOp which isn't "control
-      // flow block arguments". For example, the IV of a loop.
-      if (!llvm::is_contained(inputs, argument)) {
-        noControlFlowArguments.push_back(argument);
-      }
-    }
+    for (Value arg : successor.getRegionNonforwardedArguments())
+      noControlFlowArguments.push_back(cast<BlockArgument>(arg));
     visitNonControlFlowArguments(successor, noControlFlowArguments);
   }
 



More information about the Mlir-commits mailing list