Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/Transforms/WalkPatternRewriteDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace mlir {
/// This is intended as the simplest and most lightweight pattern rewriter in
/// cases when a simple walk gets the job done.
///
/// The driver will skip unreachable blocks.
///
/// Note: Does not apply patterns to the given operation itself.
void walkAndApplyPatterns(Operation *op,
const FrozenRewritePatternSet &patterns,
Expand Down
27 changes: 27 additions & 0 deletions mlir/lib/Transforms/Utils/WalkPatternRewriteDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@

namespace mlir {

// Find all reachable blocks in the region and add them to the visitedBlocks
// set.
static void findReachableBlocks(Region &region,
DenseSet<Block *> &reachableBlocks) {
Block *entryBlock = &region.front();
reachableBlocks.insert(entryBlock);
// Traverse the CFG and add all reachable blocks to the blockList.
SmallVector<Block *> worklist({entryBlock});
while (!worklist.empty()) {
Block *block = worklist.pop_back_val();
Operation *terminator = &block->back();
for (Block *successor : terminator->getSuccessors()) {
if (reachableBlocks.contains(successor))
continue;
worklist.push_back(successor);
reachableBlocks.insert(successor);
}
}
}

namespace {
struct WalkAndApplyPatternsAction final
: tracing::ActionImpl<WalkAndApplyPatternsAction> {
Expand Down Expand Up @@ -98,13 +118,18 @@ void walkAndApplyPatterns(Operation *op,
regionIt = region->begin();
if (regionIt != region->end())
blockIt = regionIt->begin();
if (!llvm::hasSingleElement(*region))
findReachableBlocks(*region, reachableBlocks);
}
// Advance the iterator to the next reachable operation.
void advance() {
assert(regionIt != region->end());
hasVisitedRegions = false;
if (blockIt == regionIt->end()) {
++regionIt;
while (regionIt != region->end() &&
!reachableBlocks.contains(&*regionIt))
++regionIt;
if (regionIt != region->end())
blockIt = regionIt->begin();
return;
Expand All @@ -121,6 +146,8 @@ void walkAndApplyPatterns(Operation *op,
Region::iterator regionIt;
// The Operation currently being iterated over.
Block::iterator blockIt;
// The set of blocks that are reachable in the current region.
DenseSet<Block *> reachableBlocks;
// Whether we've visited the nested regions of the current op already.
bool hasVisitedRegions = false;
};
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/IR/test-walk-pattern-rewrite-driver.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ func.func @erase_nested_block() -> i32 {
}): () -> (i32)
return %a : i32
}


// CHECK-LABEL: func.func @unreachable_replace_with_new_op
// CHECK: "test.new_op"
// CHECK: "test.replace_with_new_op"
// CHECK-SAME: unreachable
// CHECK: "test.new_op"
func.func @unreachable_replace_with_new_op() {
"test.br"()[^bb1] : () -> ()
^bb1:
%a = "test.replace_with_new_op"() : () -> (i32)
"test.br"()[^end] : () -> () // Test jumping over the unreachable block is visited as well.
^unreachable:
%b = "test.replace_with_new_op"() {test.unreachable} : () -> (i32)
return
^end:
%c = "test.replace_with_new_op"() : () -> (i32)
return
}