[Mlir-commits] [mlir] [mlir] Find the pattern dump root only when IR debugging is enabled (PR #233771)

Ivan Butygin llvmlistbot at llvm.org
Tue Oct 20 02:52:28 PDT 2026


https://github.com/Hardcode84 created https://github.com/llvm/llvm-project/pull/233771

Extracted from https://github.com/llvm/llvm-project/pull/231423

`PatternApplicator` debug printing was unconditionally calling `getDumpRootOp` which walks nested ops chains. This was taking a noticeable time in synthetic 1000-nested test. Call `getDumpRootOp` only if printing is actually enabled.

>From 9f2eb6540b5410adfd113d0d540808818f0515f4 Mon Sep 17 00:00:00 2001
From: Ivan Butygin <ivan.butygin at gmail.com>
Date: Tue, 20 Oct 2026 11:21:34 +0200
Subject: [PATCH] [mlir] Find the pattern dump root only when IR debugging is
 enabled

---
 mlir/lib/Rewrite/PatternApplicator.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Rewrite/PatternApplicator.cpp b/mlir/lib/Rewrite/PatternApplicator.cpp
index e1b56fd6efda00..4b6ca9bac0a0bd 100644
--- a/mlir/lib/Rewrite/PatternApplicator.cpp
+++ b/mlir/lib/Rewrite/PatternApplicator.cpp
@@ -200,7 +200,11 @@ LogicalResult PatternApplicator::matchAndRewrite(
 #ifndef NDEBUG
           // Operation `op` may be invalidated after applying the rewrite
           // pattern.
-          Operation *dumpRootOp = getDumpRootOp(op);
+          Operation *dumpRootOp = nullptr;
+          // LLVM_DEBUG enables level 1, but IR dumps require level 2. Avoid
+          // walking ancestors to find the dump root when no IR will be printed.
+          LLVM_DEBUG(if (llvm::isCurrentDebugType(DEBUG_TYPE, 2)) dumpRootOp =
+                         getDumpRootOp(op));
 #endif
           if (pdlMatch) {
             result =



More information about the Mlir-commits mailing list