diff --git a/mlir/lib/Rewrite/PatternApplicator.cpp b/mlir/lib/Rewrite/PatternApplicator.cpp --- a/mlir/lib/Rewrite/PatternApplicator.cpp +++ b/mlir/lib/Rewrite/PatternApplicator.cpp @@ -15,7 +15,7 @@ #include "ByteCode.h" #include "llvm/Support/Debug.h" -#define DEBUG_TYPE "pattern-match" +#define DEBUG_TYPE "pattern-application" using namespace mlir; using namespace mlir::detail; @@ -39,6 +39,29 @@ }); } +// Log IR after pattern application. +static Optional logAttemptPatternApplication(Operation *op, + const Pattern *pattern) { + LLVM_DEBUG({ + if (!op->getContext()->isMultithreadingEnabled()) + return op->getParentOfType(); + }); + return llvm::None; +} +static void logSucessfulPatternApplication(Optional op, + const Pattern *pattern) { + LLVM_DEBUG({ + llvm::dbgs() << "// *** IR Dump After Pattern Application ***\n"; + if (op) { + op.getValue().dump(); + } else { + llvm::dbgs() + << "Failed to dump module IR. Multithreading must be disabled."; + } + llvm::dbgs() << "\n\n"; + }); +} + void PatternApplicator::applyCostModel(CostModel model) { // Apply the cost model to the bytecode patterns first, and then the native // patterns. @@ -174,18 +197,20 @@ // benefit, so if we match we can immediately rewrite. For PDL patterns, the // match has already been performed, we just need to rewrite. rewriter.setInsertionPoint(op); + Optional moduleOp = logAttemptPatternApplication(op, bestPattern); if (pdlMatch) { bytecode->rewrite(rewriter, *pdlMatch, *mutableByteCodeState); result = success(!onSuccess || succeeded(onSuccess(*bestPattern))); - } else { const auto *pattern = static_cast(bestPattern); result = pattern->matchAndRewrite(op, rewriter); if (succeeded(result) && onSuccess && failed(onSuccess(*pattern))) result = failure(); } - if (succeeded(result)) + if (succeeded(result)) { + logSucessfulPatternApplication(moduleOp, bestPattern); break; + } // Perform any necessary cleanups. if (onFailure)