Skip to content

Commit 73b86d1

Browse files
[mlir][Transforms] GreedyPatternRewriteDriver: verify IR (#74270)
This commit adds an additional "expensive check" that verifies the IR before starting a greedy pattern rewriter, after every pattern application and after every folding. (Only if `MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS` is set.) It also adds an assertion that the `scope` region (part of `GreedyRewriteConfig`) is not being erased as part of the greedy pattern rewrite. That would break the scoping mechanism and the expensive checks. This commit does not fix any patterns, this is done in separate commits.
1 parent a15532d commit 73b86d1

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/Config/mlir-config.h"
1616
#include "mlir/IR/Action.h"
1717
#include "mlir/IR/Matchers.h"
18+
#include "mlir/IR/Verifier.h"
1819
#include "mlir/Interfaces/SideEffectInterfaces.h"
1920
#include "mlir/Rewrite/PatternApplicator.h"
2021
#include "mlir/Transforms/FoldUtils.h"
@@ -432,6 +433,10 @@ bool GreedyPatternRewriteDriver::processWorklist() {
432433
if (succeeded(folder.tryToFold(op))) {
433434
LLVM_DEBUG(logResultWithLine("success", "operation was folded"));
434435
changed = true;
436+
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
437+
if (config.scope && failed(verify(config.scope->getParentOp())))
438+
llvm::report_fatal_error("IR failed to verify after folding");
439+
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
435440
continue;
436441
}
437442

@@ -464,26 +469,34 @@ bool GreedyPatternRewriteDriver::processWorklist() {
464469
#endif
465470

466471
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
467-
debugFingerPrints.computeFingerPrints(
468-
/*topLevel=*/config.scope ? config.scope->getParentOp() : op);
472+
if (config.scope) {
473+
debugFingerPrints.computeFingerPrints(config.scope->getParentOp());
474+
}
469475
auto clearFingerprints =
470476
llvm::make_scope_exit([&]() { debugFingerPrints.clear(); });
471477
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
472478

473479
LogicalResult matchResult =
474480
matcher.matchAndRewrite(op, *this, canApply, onFailure, onSuccess);
475481

482+
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
483+
if (config.scope && failed(verify(config.scope->getParentOp())))
484+
llvm::report_fatal_error("IR failed to verify after pattern application");
485+
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
486+
476487
if (succeeded(matchResult)) {
477488
LLVM_DEBUG(logResultWithLine("success", "pattern matched"));
478489
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
479-
debugFingerPrints.notifyRewriteSuccess();
490+
if (config.scope)
491+
debugFingerPrints.notifyRewriteSuccess();
480492
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
481493
changed = true;
482494
++numRewrites;
483495
} else {
484496
LLVM_DEBUG(logResultWithLine("failure", "pattern failed to match"));
485497
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
486-
debugFingerPrints.notifyRewriteFailure();
498+
if (config.scope)
499+
debugFingerPrints.notifyRewriteFailure();
487500
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
488501
}
489502
}
@@ -562,6 +575,18 @@ void GreedyPatternRewriteDriver::notifyOperationRemoved(Operation *op) {
562575
logger.startLine() << "** Erase : '" << op->getName() << "'(" << op
563576
<< ")\n";
564577
});
578+
579+
#ifndef NDEBUG
580+
// Only ops that are within the configured scope are added to the worklist of
581+
// the greedy pattern rewriter. Moreover, the parent op of the scope region is
582+
// the part of the IR that is taken into account for the "expensive checks".
583+
// A greedy pattern rewrite is not allowed to erase the parent op of the scope
584+
// region, as that would break the worklist handling and the expensive checks.
585+
if (config.scope && config.scope->getParentOp() == op)
586+
llvm_unreachable(
587+
"scope region must not be erased during greedy pattern rewrite");
588+
#endif // NDEBUG
589+
565590
if (config.listener)
566591
config.listener->notifyOperationRemoved(op);
567592

@@ -721,6 +746,12 @@ mlir::applyPatternsAndFoldGreedily(Region &region,
721746
if (!config.scope)
722747
config.scope = &region;
723748

749+
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
750+
if (failed(verify(config.scope->getParentOp())))
751+
llvm::report_fatal_error(
752+
"greedy pattern rewriter input IR failed to verify");
753+
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
754+
724755
// Start the pattern driver.
725756
RegionPatternRewriteDriver driver(region.getContext(), patterns, config,
726757
region);
@@ -846,6 +877,12 @@ LogicalResult mlir::applyOpPatternsAndFold(
846877
#endif // NDEBUG
847878
}
848879

880+
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
881+
if (config.scope && failed(verify(config.scope->getParentOp())))
882+
llvm::report_fatal_error(
883+
"greedy pattern rewriter input IR failed to verify");
884+
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
885+
849886
// Start the pattern driver.
850887
llvm::SmallDenseSet<Operation *, 4> surviving;
851888
MultiOpPatternRewriteDriver driver(ops.front()->getContext(), patterns,

0 commit comments

Comments
 (0)