[llvm-branch-commits] [mlir] [mlir][Analysis] Clean up `visitNonControlFlowArguments` (PR #175978)

Matthias Springer via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 14 08:08:47 PST 2026


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/175978

Depends on #175815.


>From 6d5a0b71a00d3bd9e52cc02047a49666f104c94a Mon Sep 17 00:00:00 2001
From: Matthias Springer <me at m-sp.org>
Date: Wed, 14 Jan 2026 16:07:52 +0000
Subject: [PATCH] [mlir][Analysis] Clean up `visitNonControlFlowArguments`

---
 .../Analysis/DataFlow/IntegerRangeAnalysis.h  |  8 ++--
 .../mlir/Analysis/DataFlow/SparseAnalysis.h   | 41 +++++++++++++------
 .../DataFlow/IntegerRangeAnalysis.cpp         | 10 ++---
 mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp | 15 +++----
 4 files changed, 41 insertions(+), 33 deletions(-)

diff --git a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
index e549a56a6f960..feef04e58e253 100644
--- a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
@@ -66,11 +66,9 @@ class IntegerRangeAnalysis
   /// function calls `InferIntRangeInterface` to provide values for block
   /// arguments or tries to reduce the range on loop induction variables with
   /// known bounds.
-  void
-  visitNonControlFlowArguments(Operation *op, const RegionSuccessor &successor,
-                               ValueRange successorInputs,
-                               ArrayRef<IntegerValueRangeLattice *> argLattices,
-                               unsigned firstIndex) override;
+  void visitNonControlFlowArguments(
+      Operation *op, const RegionSuccessor &successor,
+      ArrayRef<IntegerValueRangeLattice *> argLattices) override;
 };
 
 /// Succeeds if an op can be converted to its unsigned equivalent without
diff --git a/mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h
index 02f699de06f99..e3f9b056a59be 100644
--- a/mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h
@@ -212,11 +212,10 @@ class AbstractSparseForwardDataFlowAnalysis : public DataFlowAnalysis {
   /// Given an operation with region control-flow, the lattices of the operands,
   /// and a region successor, compute the lattice values for block arguments
   /// that are not accounted for by the branching control flow (ex. the bounds
-  /// of loops).
+  /// of loops)
   virtual void visitNonControlFlowArgumentsImpl(
       Operation *op, const RegionSuccessor &successor,
-      ValueRange successorInputs, ArrayRef<AbstractSparseLattice *> argLattices,
-      unsigned firstIndex) = 0;
+      ArrayRef<AbstractSparseLattice *> argLattices) = 0;
 
   /// Get the lattice element of a value.
   virtual AbstractSparseLattice *getLatticeElement(Value value) = 0;
@@ -325,13 +324,31 @@ class SparseForwardDataFlowAnalysis
   /// operands, and a region successor, compute the lattice values for block
   /// arguments that are not accounted for by the branching control flow (ex.
   /// the bounds of loops). By default, this method marks all such lattice
-  /// elements as having reached a pessimistic fixpoint. `firstIndex` is the
-  /// index of the first element of `argLattices` that is set by control-flow.
+  /// elements as having reached a pessimistic fixpoint. If the given operation
+  /// does not implement the `RegionBranchOpInterface`, this method marks all
+  /// lattice elements as having reached a pessimistic fixpoint.
   virtual void visitNonControlFlowArguments(Operation *op,
                                             const RegionSuccessor &successor,
-                                            ValueRange successorInputs,
-                                            ArrayRef<StateT *> argLattices,
-                                            unsigned firstIndex) {
+                                            ArrayRef<StateT *> argLattices) {
+    auto regionBranchOp = dyn_cast<RegionBranchOpInterface>(op);
+    if (!regionBranchOp) {
+      // There are no forwarded arguments. Mark all lattice elements as having
+      // reached a pessimistic fixpoint.
+      setAllToEntryStates(argLattices);
+      return;
+    }
+
+    // Mark only the lattice elements that are not forwarded as having reached
+    // a pessimistic fixpoint.
+    auto successorInputs = regionBranchOp.getSuccessorInputs(successor);
+    unsigned firstIndex = 0;
+    if (!successorInputs.empty() &&
+        isa<BlockArgument>(successorInputs.front())) {
+      firstIndex = cast<BlockArgument>(successorInputs.front()).getArgNumber();
+    } else if (!successorInputs.empty() &&
+               isa<OpResult>(successorInputs.front())) {
+      firstIndex = cast<OpResult>(successorInputs.front()).getResultNumber();
+    }
     setAllToEntryStates(argLattices.take_front(firstIndex));
     setAllToEntryStates(
         argLattices.drop_front(firstIndex + successorInputs.size()));
@@ -385,13 +402,11 @@ class SparseForwardDataFlowAnalysis
   }
   void visitNonControlFlowArgumentsImpl(
       Operation *op, const RegionSuccessor &successor,
-      ValueRange successorInputs, ArrayRef<AbstractSparseLattice *> argLattices,
-      unsigned firstIndex) override {
+      ArrayRef<AbstractSparseLattice *> argLattices) override {
     visitNonControlFlowArguments(
-        op, successor, successorInputs,
+        op, successor,
         {reinterpret_cast<StateT *const *>(argLattices.begin()),
-         argLattices.size()},
-        firstIndex);
+         argLattices.size()});
   }
   void setToEntryState(AbstractSparseLattice *lattice) override {
     return setToEntryState(reinterpret_cast<StateT *>(lattice));
diff --git a/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
index 012d8384d3098..cf2dedc85d05f 100644
--- a/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
@@ -138,8 +138,8 @@ LogicalResult IntegerRangeAnalysis::visitOperation(
 }
 
 void IntegerRangeAnalysis::visitNonControlFlowArguments(
-    Operation *op, const RegionSuccessor &successor, ValueRange successorInputs,
-    ArrayRef<IntegerValueRangeLattice *> argLattices, unsigned firstIndex) {
+    Operation *op, const RegionSuccessor &successor,
+    ArrayRef<IntegerValueRangeLattice *> argLattices) {
   if (auto inferrable = dyn_cast<InferIntRangeInterface>(op)) {
     LDBG() << "Inferring ranges for "
            << OpWithFlags(op, OpPrintingFlags().skipRegions());
@@ -207,8 +207,8 @@ void IntegerRangeAnalysis::visitNonControlFlowArguments(
     std::optional<llvm::SmallVector<Value>> maybeIvs =
         loop.getLoopInductionVars();
     if (!maybeIvs) {
-      return SparseForwardDataFlowAnalysis ::visitNonControlFlowArguments(
-          op, successor, successorInputs, argLattices, firstIndex);
+      return SparseForwardDataFlowAnalysis::visitNonControlFlowArguments(
+          op, successor, argLattices);
     }
     // This shouldn't be returning nullopt if there are indunction variables.
     SmallVector<OpFoldResult> lowerBounds = *loop.getLoopLowerBounds();
@@ -246,5 +246,5 @@ void IntegerRangeAnalysis::visitNonControlFlowArguments(
   }
 
   return SparseForwardDataFlowAnalysis::visitNonControlFlowArguments(
-      op, successor, successorInputs, argLattices, firstIndex);
+      op, successor, argLattices);
 }
diff --git a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
index f86bb55df3ac5..ec2de30a674e8 100644
--- a/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp
@@ -187,8 +187,7 @@ void AbstractSparseForwardDataFlowAnalysis::visitBlock(Block *block) {
 
     // Otherwise, we can't reason about the data-flow.
     return visitNonControlFlowArgumentsImpl(
-        block->getParentOp(), RegionSuccessor(block->getParent()), ValueRange(),
-        argLattices, /*firstIndex=*/0);
+        block->getParentOp(), RegionSuccessor(block->getParent()), argLattices);
   }
 
   // Iterate over the predecessors of the non-entry block.
@@ -314,18 +313,14 @@ void AbstractSparseForwardDataFlowAnalysis::visitRegionSuccessors(
       if (!point->isBlockStart()) {
         if (!inputs.empty())
           firstIndex = cast<OpResult>(inputs.front()).getResultNumber();
-        visitNonControlFlowArgumentsImpl(
-            branch, RegionSuccessor::parent(),
-            branch->getResults().slice(firstIndex, inputs.size()), lattices,
-            firstIndex);
+        visitNonControlFlowArgumentsImpl(branch, RegionSuccessor::parent(),
+                                         lattices);
       } 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);
+        visitNonControlFlowArgumentsImpl(branch, RegionSuccessor(region),
+                                         lattices);
       }
     }
 



More information about the llvm-branch-commits mailing list