Skip to content

Commit 46f65e4

Browse files
authored
[mlir]use correct iterator when eraseOp (#83444)
#66771 introduce `llvm::post_order(&r.front())` which is equal to `r.front().getSuccessor(...)`. It will visit the succ block of current block. But actually here need to visit all block of region in reverse order. Fixes: #77420.
1 parent 1837579 commit 46f65e4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

mlir/lib/IR/PatternMatch.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ void RewriterBase::eraseOp(Operation *op) {
229229
// until the region is empty. (The block graph could be disconnected.)
230230
while (!r.empty()) {
231231
SmallVector<Block *> erasedBlocks;
232-
for (Block *b : llvm::post_order(&r.front())) {
232+
// Some blocks may have invalid successor, use a set including nullptr
233+
// to avoid null pointer.
234+
llvm::SmallPtrSet<Block *, 4> visited{nullptr};
235+
for (Block *b : llvm::post_order_ext(&r.front(), visited)) {
233236
// Visit ops in reverse order.
234237
for (Operation &op :
235238
llvm::make_early_inc_range(ReverseIterator::makeIterable(*b)))

mlir/test/Transforms/gh-77420.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: mlir-opt --canonicalize %s | FileCheck %s
2+
3+
4+
module {
5+
6+
// CHECK: func.func @f() {
7+
// CHECK-NEXT: return
8+
// CHECK-NEXT: }
9+
func.func @f() {
10+
return
11+
^bb1: // no predecessors
12+
omp.parallel {
13+
%0 = llvm.intr.stacksave : !llvm.ptr
14+
llvm.br ^bb1
15+
^bb1: // pred: ^bb0
16+
omp.terminator
17+
}
18+
return
19+
}
20+
21+
}

0 commit comments

Comments
 (0)