|
15 | 15 | #include "mlir/Config/mlir-config.h" |
16 | 16 | #include "mlir/IR/Action.h" |
17 | 17 | #include "mlir/IR/Matchers.h" |
| 18 | +#include "mlir/IR/Verifier.h" |
18 | 19 | #include "mlir/Interfaces/SideEffectInterfaces.h" |
19 | 20 | #include "mlir/Rewrite/PatternApplicator.h" |
20 | 21 | #include "mlir/Transforms/FoldUtils.h" |
@@ -432,6 +433,10 @@ bool GreedyPatternRewriteDriver::processWorklist() { |
432 | 433 | if (succeeded(folder.tryToFold(op))) { |
433 | 434 | LLVM_DEBUG(logResultWithLine("success", "operation was folded")); |
434 | 435 | 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 |
435 | 440 | continue; |
436 | 441 | } |
437 | 442 |
|
@@ -464,26 +469,34 @@ bool GreedyPatternRewriteDriver::processWorklist() { |
464 | 469 | #endif |
465 | 470 |
|
466 | 471 | #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 | + } |
469 | 475 | auto clearFingerprints = |
470 | 476 | llvm::make_scope_exit([&]() { debugFingerPrints.clear(); }); |
471 | 477 | #endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS |
472 | 478 |
|
473 | 479 | LogicalResult matchResult = |
474 | 480 | matcher.matchAndRewrite(op, *this, canApply, onFailure, onSuccess); |
475 | 481 |
|
| 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 | + |
476 | 487 | if (succeeded(matchResult)) { |
477 | 488 | LLVM_DEBUG(logResultWithLine("success", "pattern matched")); |
478 | 489 | #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS |
479 | | - debugFingerPrints.notifyRewriteSuccess(); |
| 490 | + if (config.scope) |
| 491 | + debugFingerPrints.notifyRewriteSuccess(); |
480 | 492 | #endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS |
481 | 493 | changed = true; |
482 | 494 | ++numRewrites; |
483 | 495 | } else { |
484 | 496 | LLVM_DEBUG(logResultWithLine("failure", "pattern failed to match")); |
485 | 497 | #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS |
486 | | - debugFingerPrints.notifyRewriteFailure(); |
| 498 | + if (config.scope) |
| 499 | + debugFingerPrints.notifyRewriteFailure(); |
487 | 500 | #endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS |
488 | 501 | } |
489 | 502 | } |
@@ -562,6 +575,18 @@ void GreedyPatternRewriteDriver::notifyOperationRemoved(Operation *op) { |
562 | 575 | logger.startLine() << "** Erase : '" << op->getName() << "'(" << op |
563 | 576 | << ")\n"; |
564 | 577 | }); |
| 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 | + |
565 | 590 | if (config.listener) |
566 | 591 | config.listener->notifyOperationRemoved(op); |
567 | 592 |
|
@@ -721,6 +746,12 @@ mlir::applyPatternsAndFoldGreedily(Region ®ion, |
721 | 746 | if (!config.scope) |
722 | 747 | config.scope = ®ion; |
723 | 748 |
|
| 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 | + |
724 | 755 | // Start the pattern driver. |
725 | 756 | RegionPatternRewriteDriver driver(region.getContext(), patterns, config, |
726 | 757 | region); |
@@ -846,6 +877,12 @@ LogicalResult mlir::applyOpPatternsAndFold( |
846 | 877 | #endif // NDEBUG |
847 | 878 | } |
848 | 879 |
|
| 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 | + |
849 | 886 | // Start the pattern driver. |
850 | 887 | llvm::SmallDenseSet<Operation *, 4> surviving; |
851 | 888 | MultiOpPatternRewriteDriver driver(ops.front()->getContext(), patterns, |
|
0 commit comments