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

Matthias Springer llvmlistbot at llvm.org
Fri Sep 26 00:16: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.
+  Block *currBlock = successorDest;
+  BranchOp nextBranch = dyn_cast<BranchOp>(currBlock->getTerminator());
+  while (nextBranch) {
----------------
matthias-springer wrote:

Is it possible that this `while` loop runs into an endless loop? E.g.:

```
   A  ->  B  -> C  <-
                |     \
                v      \
                D   ->  E
```

Assuming that we call `collapseBranch(A, ...)`.

I feel like `nextBranchDest == successor` is not sufficient and we need a `DenseSet<Block *> visited` instead?


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


More information about the Mlir-commits mailing list