Skip to content

Conversation

@qingyunqu
Copy link
Contributor

No description provided.

@qingyunqu qingyunqu requested review from a team as code owners September 9, 2023 17:50
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Sep 9, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 9, 2023

@llvm/pr-subscribers-mlir

Changes

None

Full diff: https://github.com/llvm/llvm-project/pull/65854.diff

3 Files Affected:

  • (modified) mlir/include/mlir-c/Pass.h (+4-2)
  • (modified) mlir/lib/Bindings/Python/Pass.cpp (+27-2)
  • (modified) mlir/lib/CAPI/IR/Pass.cpp (+18-2)
diff --git a/mlir/include/mlir-c/Pass.h b/mlir/include/mlir-c/Pass.h
index 35db138305d1e22..6a59b1642d30367 100644
--- a/mlir/include/mlir-c/Pass.h
+++ b/mlir/include/mlir-c/Pass.h
@@ -75,8 +75,10 @@ MLIR_CAPI_EXPORTED MlirLogicalResult
 mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op);
 
 /// Enable mlir-print-ir-after-all.
-MLIR_CAPI_EXPORTED void
-mlirPassManagerEnableIRPrinting(MlirPassManager passManager);
+MLIR_CAPI_EXPORTED void mlirPassManagerEnableIRPrinting(
+    MlirPassManager passManager, bool printBeforePass, bool printAfterPass,
+    bool printModuleScope, bool printAfterOnlyOnChange,
+    bool printAfterOnlyOnFailure, MlirOpPrintingFlags flags);
 
 /// Enable / disable verify-each.
 MLIR_CAPI_EXPORTED void
diff --git a/mlir/lib/Bindings/Python/Pass.cpp b/mlir/lib/Bindings/Python/Pass.cpp
index cdbfcfbc22957a6..e9a3780eb772e28 100644
--- a/mlir/lib/Bindings/Python/Pass.cpp
+++ b/mlir/lib/Bindings/Python/Pass.cpp
@@ -73,9 +73,34 @@ void mlir::python::populatePassManagerSubmodule(py::module &m) {
            "Releases (leaks) the backing pass manager (testing)")
       .def(
           "enable_ir_printing",
-          [](PyPassManager &passManager) {
-            mlirPassManagerEnableIRPrinting(passManager.get());
+          [](PyPassManager &passManager, bool printBeforePass,
+             bool printAfterPass, bool printModuleScope,
+             bool printAfterOnlyOnChange, bool printAfterOnlyOnFailure,
+             std::optional largeElementsLimit, bool enableDebugInfo,
+             bool printGenericOpForm) {
+            MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
+            if (largeElementsLimit)
+              mlirOpPrintingFlagsElideLargeElementsAttrs(flags,
+                                                         *largeElementsLimit);
+            if (enableDebugInfo)
+              mlirOpPrintingFlagsEnableDebugInfo(flags, /*enable=*/true,
+                                                 /*prettyForm=*/false);
+            if (printGenericOpForm)
+              mlirOpPrintingFlagsPrintGenericOpForm(flags);
+            mlirPassManagerEnableIRPrinting(passManager.get(), printBeforePass,
+                                            printAfterPass, printModuleScope,
+                                            printAfterOnlyOnChange,
+                                            printAfterOnlyOnFailure, flags);
+            mlirOpPrintingFlagsDestroy(flags);
           },
+          py::arg("print_before_pass") = true,
+          py::arg("print_after_pass") = true,
+          py::arg("print_module_scope") = true,
+          py::arg("print_after_only_on_change") = true,
+          py::arg("print_after_only_on_failure") = false,
+          py::arg("large_elements_limit") = py::none(),
+          py::arg("enable_debug_info") = false,
+          py::arg("print_generic_op_form") = false,
           "Enable mlir-print-ir-after-all.")
       .def(
           "enable_verifier",
diff --git a/mlir/lib/CAPI/IR/Pass.cpp b/mlir/lib/CAPI/IR/Pass.cpp
index d242baae99c0862..d13a71bb19cf794 100644
--- a/mlir/lib/CAPI/IR/Pass.cpp
+++ b/mlir/lib/CAPI/IR/Pass.cpp
@@ -13,6 +13,7 @@
 #include "mlir/CAPI/Support.h"
 #include "mlir/CAPI/Utils.h"
 #include "mlir/Pass/PassManager.h"
+#include 
 #include 
 
 using namespace mlir;
@@ -44,8 +45,23 @@ MlirLogicalResult mlirPassManagerRunOnOp(MlirPassManager passManager,
   return wrap(unwrap(passManager)->run(unwrap(op)));
 }
 
-void mlirPassManagerEnableIRPrinting(MlirPassManager passManager) {
-  return unwrap(passManager)->enableIRPrinting();
+void mlirPassManagerEnableIRPrinting(MlirPassManager passManager,
+                                     bool printBeforePass, bool printAfterPass,
+                                     bool printModuleScope,
+                                     bool printAfterOnlyOnChange,
+                                     bool printAfterOnlyOnFailure,
+                                     MlirOpPrintingFlags flags) {
+  std::function shouldPrintBeforePass = nullptr;
+  std::function shouldPrintAfterPass = nullptr;
+  if (printBeforePass)
+    shouldPrintBeforePass = [](Pass *, Operation *) { return true; };
+  if (printAfterPass)
+    shouldPrintAfterPass = [](Pass *, Operation *) { return true; };
+  return unwrap(passManager)
+      ->enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
+                         printModuleScope, printAfterOnlyOnChange,
+                         printAfterOnlyOnFailure, /*out=*/llvm::errs(),
+                         *unwrap(flags));
 }
 
 void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable) {

@qingyunqu qingyunqu marked this pull request as draft April 14, 2024 16:03
@qingyunqu qingyunqu force-pushed the enhance-ir-printing branch from 45c6dd1 to 835ad09 Compare April 14, 2024 16:11
bool printModuleScope = false, bool printAfterOnlyOnChange = false,
bool printAfterOnlyOnFailure = false,
bool printAfterOnlyOnFailure = false, bool printBeforePass = false,
bool printAfterPass = false,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joker-eph I try to create C struct of PassManager::IRPrinterConfig, but this will change the member variable. I don't know whether it is a good practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this PR is only a draft now, don't care about the detail code.

@ftynse ftynse closed this in 2e51e15 Dec 5, 2024
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 13, 2025
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
makslevental pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
bump-llvm bot pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants