Skip to content

Commit dec908a

Browse files
[mlir][Transforms] GreedyPatternRewriteDriver: log successful folding (#77796)
Similar to successful pattern applications, dump the rewritten IR after each successful folding when running with `-debug`.
1 parent 7700ea1 commit dec908a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ struct DebugFingerPrints : public RewriterBase::ForwardingListener {
136136
};
137137
#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
138138

139+
#ifndef NDEBUG
140+
static Operation *getDumpRootOp(Operation *op) {
141+
// Dump the parent op so that materialized constants are visible. If the op
142+
// is a top-level op, dump it directly.
143+
if (Operation *parentOp = op->getParentOp())
144+
return parentOp;
145+
return op;
146+
}
147+
static void logSuccessfulFolding(Operation *op) {
148+
llvm::dbgs() << "// *** IR Dump After Successful Folding ***\n";
149+
op->dump();
150+
llvm::dbgs() << "\n\n";
151+
}
152+
#endif // NDEBUG
153+
139154
//===----------------------------------------------------------------------===//
140155
// Worklist
141156
//===----------------------------------------------------------------------===//
@@ -434,10 +449,14 @@ bool GreedyPatternRewriteDriver::processWorklist() {
434449
SmallVector<OpFoldResult> foldResults;
435450
if (succeeded(op->fold(foldResults))) {
436451
LLVM_DEBUG(logResultWithLine("success", "operation was folded"));
452+
#ifndef NDEBUG
453+
Operation *dumpRootOp = getDumpRootOp(op);
454+
#endif // NDEBUG
437455
if (foldResults.empty()) {
438456
// Op was modified in-place.
439457
notifyOperationModified(op);
440458
changed = true;
459+
LLVM_DEBUG(logSuccessfulFolding(dumpRootOp));
441460
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
442461
if (config.scope && failed(verify(config.scope->getParentOp())))
443462
llvm::report_fatal_error("IR failed to verify after folding");
@@ -492,6 +511,7 @@ bool GreedyPatternRewriteDriver::processWorklist() {
492511
if (materializationSucceeded) {
493512
replaceOp(op, replacements);
494513
changed = true;
514+
LLVM_DEBUG(logSuccessfulFolding(dumpRootOp));
495515
#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
496516
if (config.scope && failed(verify(config.scope->getParentOp())))
497517
llvm::report_fatal_error("IR failed to verify after folding");

0 commit comments

Comments
 (0)