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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Sep 28 00:56:48 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;
----------------
benwu25 wrote:

Update:

After adding this change to bail out early, I caused a regression in flang: `test/Lower/OpenMP/infinite-loop-in-construct.f90`.

I broke this test with my initial PR because there was an infinite loop. Now, it seems like the infinite loop is less optimized than expected for this testcase. I suppose I could fix this by updating this testcase to expect a different infinite loop?

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


More information about the Mlir-commits mailing list