[Mlir-commits] [mlir] [mlir] [core]: in -mlir-print-ir-*, dump the pass options as well (PR #195198)

Jeremy Kun llvmlistbot at llvm.org
Thu Apr 30 16:51:29 PDT 2026


https://github.com/j2kun updated https://github.com/llvm/llvm-project/pull/195198

>From 9a618de1cc56b3efbd92c813ce56f320bab70754 Mon Sep 17 00:00:00 2001
From: Jeremy Kun <jkun at google.com>
Date: Thu, 30 Apr 2026 16:42:00 -0700
Subject: [PATCH 1/2] [mlir] [core]: in -mlir-print-ir-*, dump the pass options
 as well

---
 mlir/lib/Pass/IRPrinting.cpp    | 32 +++++++++++++++++++++-----------
 mlir/test/Pass/ir-printing.mlir |  4 ++++
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/mlir/lib/Pass/IRPrinting.cpp b/mlir/lib/Pass/IRPrinting.cpp
index c11dbc627c0be..6eab721045176 100644
--- a/mlir/lib/Pass/IRPrinting.cpp
+++ b/mlir/lib/Pass/IRPrinting.cpp
@@ -46,12 +46,18 @@ class IRPrinterInstrumentation : public PassInstrumentation {
 };
 } // namespace
 
-static void printIR(Operation *op, bool printModuleScope, raw_ostream &out,
-                    OpPrintingFlags flags) {
+static void printIR(Operation* op, bool printModuleScope, raw_ostream& out,
+                    OpPrintingFlags flags, Pass *pass = nullptr) {
   // Otherwise, check to see if we are not printing at module scope.
-  if (!printModuleScope)
-    return op->print(out << " //----- //\n",
-                     op->getBlock() ? flags.useLocalScope() : flags);
+  if (!printModuleScope) {
+    out << " //----- //\n";
+    if (pass) {
+      out << "// Pass options: ";
+      pass->printAsTextualPipeline(out);
+      out << "\n";
+    }
+    return op->print(out, op->getBlock() ? flags.useLocalScope() : flags);
+  }
 
   // Otherwise, we are printing at module scope.
   out << " ('" << op->getName() << "' operation";
@@ -59,11 +65,15 @@ static void printIR(Operation *op, bool printModuleScope, raw_ostream &out,
           op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName()))
     out << ": @" << symbolName.getValue();
   out << ") //----- //\n";
+  if (pass) {
+    out << "// Pass options: ";
+    pass->printAsTextualPipeline(out);
+    out << "\n";
+  }
 
   // Find the top-level operation.
-  auto *topLevelOp = op;
-  while (auto *parentOp = topLevelOp->getParentOp())
-    topLevelOp = parentOp;
+  auto* topLevelOp = op;
+  while (auto* parentOp = topLevelOp->getParentOp()) topLevelOp = parentOp;
   topLevelOp->print(out, flags);
 }
 
@@ -79,7 +89,7 @@ void IRPrinterInstrumentation::runBeforePass(Pass *pass, Operation *op) {
     out << "// -----// IR Dump Before " << pass->getName() << " ("
         << pass->getArgument() << ")";
     printIR(op, config->shouldPrintAtModuleScope(), out,
-            config->getOpPrintingFlags());
+            config->getOpPrintingFlags(), pass);
     out << "\n\n";
   });
 }
@@ -110,7 +120,7 @@ void IRPrinterInstrumentation::runAfterPass(Pass *pass, Operation *op) {
     out << "// -----// IR Dump After " << pass->getName() << " ("
         << pass->getArgument() << ")";
     printIR(op, config->shouldPrintAtModuleScope(), out,
-            config->getOpPrintingFlags());
+            config->getOpPrintingFlags(), pass);
     out << "\n\n";
   });
 }
@@ -125,7 +135,7 @@ void IRPrinterInstrumentation::runAfterPassFailed(Pass *pass, Operation *op) {
     out << formatv("// -----// IR Dump After {0} Failed ({1})", pass->getName(),
                    pass->getArgument());
     printIR(op, config->shouldPrintAtModuleScope(), out,
-            config->getOpPrintingFlags());
+            config->getOpPrintingFlags(), pass);
     out << "\n\n";
   });
 }
diff --git a/mlir/test/Pass/ir-printing.mlir b/mlir/test/Pass/ir-printing.mlir
index 467d76fdaa7f6..2da605f808d7d 100644
--- a/mlir/test/Pass/ir-printing.mlir
+++ b/mlir/test/Pass/ir-printing.mlir
@@ -5,6 +5,7 @@
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-before=cse -mlir-print-ir-module-scope -o /dev/null 2>&1 | FileCheck -check-prefix=BEFORE_MODULE %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,cse))' -mlir-print-ir-after-all -mlir-print-ir-after-change -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_ALL_CHANGE %s
 // RUN: not mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,test-pass-failure))' -mlir-print-ir-after-failure -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_FAILURE %s
+// RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(canonicalize{max-iterations=5}))' -mlir-print-ir-before=canonicalize -o /dev/null 2>&1 | FileCheck -check-prefix=OPTIONS %s
 
 func.func @foo() {
   %0 = arith.constant 0 : i32
@@ -65,3 +66,6 @@ func.func @bar() {
 // AFTER_FAILURE-NOT: // -----// IR Dump After{{.*}}CSE
 // AFTER_FAILURE: // -----// IR Dump After{{.*}}TestFailurePass Failed (test-pass-failure) //----- //
 // AFTER_FAILURE: func @foo()
+
+// OPTIONS: // -----// IR Dump Before{{.*}}CanonicalizerPass (canonicalize) //----- //
+// OPTIONS-NEXT: // Pass options: canonicalize{cse-between-iterations=false{{[[:space:]]*}}max-iterations=5 max-num-rewrites=-1 region-simplify=normal test-convergence=false top-down=true}

>From 9a36bebc9e0b9d6bd7466597c12f86489339d5e8 Mon Sep 17 00:00:00 2001
From: Jeremy Kun <jkun at google.com>
Date: Thu, 30 Apr 2026 16:51:17 -0700
Subject: [PATCH 2/2] format

---
 mlir/lib/Pass/IRPrinting.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Pass/IRPrinting.cpp b/mlir/lib/Pass/IRPrinting.cpp
index 6eab721045176..41c50625f0edc 100644
--- a/mlir/lib/Pass/IRPrinting.cpp
+++ b/mlir/lib/Pass/IRPrinting.cpp
@@ -46,7 +46,7 @@ class IRPrinterInstrumentation : public PassInstrumentation {
 };
 } // namespace
 
-static void printIR(Operation* op, bool printModuleScope, raw_ostream& out,
+static void printIR(Operation *op, bool printModuleScope, raw_ostream &out,
                     OpPrintingFlags flags, Pass *pass = nullptr) {
   // Otherwise, check to see if we are not printing at module scope.
   if (!printModuleScope) {
@@ -72,8 +72,9 @@ static void printIR(Operation* op, bool printModuleScope, raw_ostream& out,
   }
 
   // Find the top-level operation.
-  auto* topLevelOp = op;
-  while (auto* parentOp = topLevelOp->getParentOp()) topLevelOp = parentOp;
+  auto *topLevelOp = op;
+  while (auto *parentOp = topLevelOp->getParentOp())
+    topLevelOp = parentOp;
   topLevelOp->print(out, flags);
 }
 



More information about the Mlir-commits mailing list