Skip to content

Commit 892abd3

Browse files
authored
[mlir][IR] Make verifyDominanceOfContainedRegions iterative (#74428)
This commit refactors `verifyDominanceOfContainedRegions` to iterative algorithms similar to https://reviews.llvm.org/D154925 to fix stack overflow for deeply nested regions (e.g. llvm/circt#5316). There should be no functional change except that this could result in slightly different order of verification.
1 parent eaba81f commit 892abd3

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

mlir/lib/IR/Verifier.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -378,39 +378,39 @@ static void diagnoseInvalidOperandDominance(Operation &op, unsigned operandNo) {
378378
LogicalResult
379379
OperationVerifier::verifyDominanceOfContainedRegions(Operation &op,
380380
DominanceInfo &domInfo) {
381-
for (Region &region : op.getRegions()) {
382-
// Verify the dominance of each of the held operations.
383-
for (Block &block : region) {
384-
// Dominance is only meaningful inside reachable blocks.
385-
bool isReachable = domInfo.isReachableFromEntry(&block);
386-
387-
for (Operation &op : block) {
388-
if (isReachable) {
389-
// Check that operands properly dominate this use.
390-
for (const auto &operand : llvm::enumerate(op.getOperands())) {
391-
if (domInfo.properlyDominates(operand.value(), &op))
392-
continue;
393-
394-
diagnoseInvalidOperandDominance(op, operand.index());
395-
return failure();
381+
llvm::SmallVector<Operation *, 8> worklist{&op};
382+
while (!worklist.empty()) {
383+
auto *op = worklist.pop_back_val();
384+
for (auto &region : op->getRegions())
385+
for (auto &block : region.getBlocks()) {
386+
// Dominance is only meaningful inside reachable blocks.
387+
bool isReachable = domInfo.isReachableFromEntry(&block);
388+
for (auto &op : block) {
389+
if (isReachable) {
390+
// Check that operands properly dominate this use.
391+
for (const auto &operand : llvm::enumerate(op.getOperands())) {
392+
if (domInfo.properlyDominates(operand.value(), &op))
393+
continue;
394+
395+
diagnoseInvalidOperandDominance(op, operand.index());
396+
return failure();
397+
}
396398
}
397-
}
398399

399-
// Recursively verify dominance within each operation in the block, even
400-
// if the block itself is not reachable, or we are in a region which
401-
// doesn't respect dominance.
402-
if (verifyRecursively && op.getNumRegions() != 0) {
403-
// If this operation is IsolatedFromAbove, then we'll handle it in the
404-
// outer verification loop.
405-
if (op.hasTrait<OpTrait::IsIsolatedFromAbove>())
406-
continue;
407-
408-
if (failed(verifyDominanceOfContainedRegions(op, domInfo)))
409-
return failure();
400+
// Recursively verify dominance within each operation in the block,
401+
// even if the block itself is not reachable, or we are in a region
402+
// which doesn't respect dominance.
403+
if (verifyRecursively && op.getNumRegions() != 0) {
404+
// If this operation is IsolatedFromAbove, then we'll handle it in
405+
// the outer verification loop.
406+
if (op.hasTrait<OpTrait::IsIsolatedFromAbove>())
407+
continue;
408+
worklist.push_back(&op);
409+
}
410410
}
411411
}
412-
}
413412
}
413+
414414
return success();
415415
}
416416

0 commit comments

Comments
 (0)