Skip to content

Commit 3c8e0e1

Browse files
committed
[MLIR][CF] Fix canonicalizer regression with 1-block cycle (#159743)
I caused a regression in flang due to my previous commits, since I did not consider encountering a single-block cycle. I updated my check to look for 1-block cycles in addition to any larger cycles containing the successor block.
1 parent 35ac930 commit 3c8e0e1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

mlir/lib/Dialect/ControlFlow/IR/ControlFlowOps.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ static LogicalResult collapseBranch(Block *&successor,
123123
if (successorDest == successor)
124124
return failure();
125125
// Don't try to collapse branches which participate in a cycle.
126-
BranchOp nextBranch = dyn_cast<BranchOp>(successorDest->getTerminator());
126+
Block *currBlock = successorDest;
127+
BranchOp nextBranch = dyn_cast<BranchOp>(currBlock->getTerminator());
127128
while (nextBranch) {
128129
Block *nextBranchDest = nextBranch.getDest();
129-
if (!nextBranchDest)
130+
if (!nextBranchDest || nextBranchDest == currBlock)
130131
break;
131132
else if (nextBranchDest == successor)
132133
return failure();
133-
nextBranch = dyn_cast<BranchOp>(nextBranchDest->getTerminator());
134+
currBlock = nextBranchDest;
135+
nextBranch = dyn_cast<BranchOp>(currBlock->getTerminator());
134136
}
135137

136138
// Update the operands to the successor. If the branch parent has no

mlir/test/Transforms/control-flow-cycles.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,15 @@ func.func @delayed_3_cycle() {
7979
^bb5:
8080
cf.br ^bb3
8181
}
82+
83+
// CHECK-LABEL: @cycle_1_block
84+
// CHECK: cf.br ^bb1
85+
// CHECK: ^bb1:
86+
// CHECK: cf.br ^bb1
87+
func.func @cycle_1_block() {
88+
cf.br ^bb1
89+
^bb1:
90+
cf.br ^bb2
91+
^bb2:
92+
cf.br ^bb2
93+
}

0 commit comments

Comments
 (0)