[Mlir-commits] [mlir] [mlir][dataflow] Fix LivenessAnalysis/RemoveDeadValues handling of loop induction variables (PR #161117)

Mehdi Amini llvmlistbot at llvm.org
Mon Oct 20 08:04:51 PDT 2025


================
@@ -165,6 +165,28 @@ void LivenessAnalysis::visitBranchOperand(OpOperand &operand) {
           blocks.push_back(&block);
       }
     }
+
+    // In the block of the successor block argument of RegionBranchOpInterface,
+    // there may be arguments of RegionBranchOpInterface, such as the IV of
+    // scf.forOp. Explicitly set this argument to live.
+    auto regionBranchOp = cast<RegionBranchOpInterface>(op);
+    for (size_t i = 0, e = op->getNumRegions(); i < e; ++i) {
+      SmallVector<RegionSuccessor> successors;
+      regionBranchOp.getSuccessorRegions(op->getRegion(i), successors);
+      for (RegionSuccessor successor : successors) {
+        if (successor.isParent())
+          continue;
+        auto arguments = successor.getSuccessor()->getArguments();
+        ValueRange regionInputs = successor.getSuccessorInputs();
+        for (auto argument : arguments) {
+          if (llvm::find(regionInputs, argument) == regionInputs.end()) {
+            (void)getLatticeElement(argument)->markLive();
+            LDBG() << "Marking RegionBranchOp's success argument live: "
+                   << argument;
+          }
+        }
+      }
+    }
----------------
joker-eph wrote:

That is: if the loop had a result but unused, we would want to be able to delete it.

In this loop:
```
  %loop_ret = scf.for %iv = %lb to %ub step %step iter_args(%iter = %b) -> (i1) {
   cf.assert %b, "loop not dead"
    scf.yield %b : i1
  }
```

If `%loop_ret` has no use, then the loop can be turned into:

```
  scf.for %iv = %lb to %ub step %step {
    cf.assert %b, "loop not dead"
    scf.yield
  }
```


But maybe RemoveDeadValues can't remove results of an operation like scf.for?

https://github.com/llvm/llvm-project/pull/161117


More information about the Mlir-commits mailing list