Skip to content

Commit ecb21fa

Browse files
address comments
1 parent a230a52 commit ecb21fa

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

mlir/include/mlir/IR/Block.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "mlir/IR/BlockSupport.h"
1717
#include "mlir/IR/Visitors.h"
1818

19+
#include "llvm/ADT/SmallPtrSet.h"
20+
1921
namespace llvm {
2022
class BitVector;
2123
class raw_ostream;
@@ -267,7 +269,11 @@ class alignas(8) Block : public IRObjectWithUseList<BlockOperand>,
267269
/// Return "true" if there is a path from this block to the given block
268270
/// (according to the successors relationship). Both blocks must be in the
269271
/// same region. Paths that contain a block from `except` do not count.
270-
bool isReachable(Block *other, ArrayRef<Block *> except = {});
272+
/// This function returns "false" if `other` is in `except`.
273+
///
274+
/// Note: This function performs a block graph traversal and its complexity
275+
/// linear in the number of blocks in the parent region.
276+
bool isReachable(Block *other, SmallPtrSet<Block *, 16> &&except = {});
271277

272278
//===--------------------------------------------------------------------===//
273279
// Walkers

mlir/lib/IR/Block.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,20 @@ SuccessorRange::SuccessorRange(Operation *term) : SuccessorRange() {
352352
base = term->getBlockOperands().data();
353353
}
354354

355-
bool Block::isReachable(Block *other, ArrayRef<Block *> except) {
355+
bool Block::isReachable(Block *other, SmallPtrSet<Block *, 16> &&except) {
356356
assert(getParent() == other->getParent() && "expected same region");
357+
if (except.contains(other)) {
358+
// Fast path: If `other` is in the `except` set, there can be no path from
359+
// "this" to `other` (that does not pass through an excluded block).
360+
return false;
361+
}
357362
SmallVector<Block *> worklist(succ_begin(), succ_end());
358-
SmallPtrSet<Block *, 16> visited;
359363
while (!worklist.empty()) {
360364
Block *next = worklist.pop_back_val();
361-
if (llvm::is_contained(except, next))
362-
continue;
363365
if (next == other)
364366
return true;
365-
if (!visited.insert(next).second)
367+
// Note: `except` keeps track of already visited blocks.
368+
if (!except.insert(next).second)
366369
continue;
367370
worklist.append(next->succ_begin(), next->succ_end());
368371
}

0 commit comments

Comments
 (0)