[Mlir-commits] [mlir] [MLIR][CF] Avoid collapsing blocks which participate in cycles (PR #160783)

Matthias Springer llvmlistbot at llvm.org
Sun Sep 28 23:52:37 PDT 2025


================
@@ -122,6 +122,18 @@ static LogicalResult collapseBranch(Block *&successor,
   Block *successorDest = successorBranch.getDest();
   if (successorDest == successor)
     return failure();
+  // Don't try to collapse branches which participate in a cycle.
+  BranchOp nextBranch = dyn_cast<BranchOp>(successorDest->getTerminator());
+  llvm::DenseSet<Block *> visited{successorDest};
+  while (nextBranch) {
+    Block *nextBranchDest = nextBranch.getDest();
+    if (nextBranchDest == successor)
+      return failure();
+    else if (visited.contains(nextBranchDest))
+      break;
----------------
matthias-springer wrote:

The reason why I suggested `failure` instead of `break`: I believe the `break` will still perform some canonicalization, so the function will return `success`. This will make the canonicalization succeed and another iteration of the underlying greedy pattern rewriter will be scheduled. That's problematic because `collapseBranch` can be applied over and over without making any progress.

I would update the expected IR in the Flang test case.


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


More information about the Mlir-commits mailing list