[Mlir-commits] [mlir] caa159f - [mlir][python] Add simple debugging and printing helpers

Nicolas Vasilache llvmlistbot at llvm.org
Fri Apr 16 06:49:13 PDT 2021


Author: Nicolas Vasilache
Date: 2021-04-16T13:47:46Z
New Revision: caa159f044a05f782701a525d8b0e8f346abbd64

URL: https://github.com/llvm/llvm-project/commit/caa159f044a05f782701a525d8b0e8f346abbd64
DIFF: https://github.com/llvm/llvm-project/commit/caa159f044a05f782701a525d8b0e8f346abbd64.diff

LOG: [mlir][python] Add simple debugging and printing helpers

Differential Revision: https://reviews.llvm.org/D100643

Added: 
    

Modified: 
    mlir/include/mlir-c/IR.h
    mlir/include/mlir-c/Pass.h
    mlir/lib/Bindings/Python/IRCore.cpp
    mlir/lib/Bindings/Python/Pass.cpp
    mlir/lib/Bindings/Python/mlir/ir.py
    mlir/lib/CAPI/IR/IR.cpp
    mlir/lib/CAPI/IR/Pass.cpp
    mlir/test/Bindings/Python/context_managers.py

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 048bd46679db1..c64ec174d0b89 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -75,6 +75,13 @@ struct MlirNamedAttribute {
 };
 typedef struct MlirNamedAttribute MlirNamedAttribute;
 
+//===----------------------------------------------------------------------===//
+// Global API.
+//===----------------------------------------------------------------------===//
+
+/// Set the global debugging flag.
+MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable);
+
 //===----------------------------------------------------------------------===//
 // Context API.
 //===----------------------------------------------------------------------===//
@@ -119,6 +126,10 @@ mlirContextGetNumLoadedDialects(MlirContext context);
 MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
                                                            MlirStringRef name);
 
+/// Set threading mode (must be set to false to print-ir-after-all).
+MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context,
+                                                        bool enable);
+
 /// Returns whether the given fully-qualified operation (i.e.
 /// 'dialect.operation') is registered with the context. This will return true
 /// if the dialect is loaded and the operation is registered within the

diff  --git a/mlir/include/mlir-c/Pass.h b/mlir/include/mlir-c/Pass.h
index 9669a53cd1397..d8b2168127f90 100644
--- a/mlir/include/mlir-c/Pass.h
+++ b/mlir/include/mlir-c/Pass.h
@@ -65,6 +65,14 @@ mlirPassManagerGetAsOpPassManager(MlirPassManager passManager);
 MLIR_CAPI_EXPORTED MlirLogicalResult
 mlirPassManagerRun(MlirPassManager passManager, MlirModule module);
 
+/// Enable print-ir-after-all.
+MLIR_CAPI_EXPORTED void
+mlirPassManagerEnableIRPrinting(MlirPassManager passManager);
+
+/// Enable / disable verify-each.
+MLIR_CAPI_EXPORTED void
+mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable);
+
 /// Nest an OpPassManager under the top-level PassManager, the nested
 /// passmanager will only run on operations matching the provided name.
 /// The returned OpPassManager will be destroyed when the parent is destroyed.

diff  --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 7a7bae92cf87a..0f3a1c0dc1570 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1712,6 +1712,11 @@ class PyOpAttributeMap {
 //------------------------------------------------------------------------------
 
 void mlir::python::populateIRCore(py::module &m) {
+  //----------------------------------------------------------------------------
+  // Mapping of Global functions
+  //----------------------------------------------------------------------------
+  m.def("_enable_debug", [](bool enable) { mlirEnableGlobalDebug(enable); });
+
   //----------------------------------------------------------------------------
   // Mapping of MlirContext
   //----------------------------------------------------------------------------
@@ -1766,6 +1771,10 @@ void mlir::python::populateIRCore(py::module &m) {
           [](PyMlirContext &self, bool value) {
             mlirContextSetAllowUnregisteredDialects(self.get(), value);
           })
+      .def("enable_multithreading",
+           [](PyMlirContext &self, bool enable) {
+             mlirContextEnableMultithreading(self.get(), enable);
+           })
       .def("is_registered_operation",
            [](PyMlirContext &self, std::string &name) {
              return mlirContextIsRegisteredOperation(

diff  --git a/mlir/lib/Bindings/Python/Pass.cpp b/mlir/lib/Bindings/Python/Pass.cpp
index 0e2f5bafb4655..f2433573bd0c2 100644
--- a/mlir/lib/Bindings/Python/Pass.cpp
+++ b/mlir/lib/Bindings/Python/Pass.cpp
@@ -68,6 +68,18 @@ void mlir::python::populatePassManagerSubmodule(py::module &m) {
       .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyPassManager::createFromCapsule)
       .def("_testing_release", &PyPassManager::release,
            "Releases (leaks) the backing pass manager (testing)")
+      .def(
+          "enable_ir_printing",
+          [](PyPassManager &passManager) {
+            mlirPassManagerEnableIRPrinting(passManager.get());
+          },
+          "Enable print-ir-after-all.")
+      .def(
+          "enable_verifier",
+          [](PyPassManager &passManager, bool enable) {
+            mlirPassManagerEnableVerifier(passManager.get(), enable);
+          },
+          "Enable / disable verify-each.")
       .def_static(
           "parse",
           [](const std::string pipeline, DefaultingPyMlirContext context) {

diff  --git a/mlir/lib/Bindings/Python/mlir/ir.py b/mlir/lib/Bindings/Python/mlir/ir.py
index e5ba1bdb0270f..e2c785c504dfd 100644
--- a/mlir/lib/Bindings/Python/mlir/ir.py
+++ b/mlir/lib/Bindings/Python/mlir/ir.py
@@ -6,3 +6,8 @@
 from ._cext_loader import _reexport_cext
 _reexport_cext("ir", __name__)
 del _reexport_cext
+
+# Extra functions that are not visible to _reexport_cext.
+# TODO: is this really necessary?
+from _mlir.ir import _enable_debug
+_enable_debug = _enable_debug
\ No newline at end of file

diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 14cde9633f525..616caae1ef69d 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -21,8 +21,16 @@
 #include "mlir/Interfaces/InferTypeOpInterface.h"
 #include "mlir/Parser.h"
 
+#include "llvm/Support/Debug.h"
+
 using namespace mlir;
 
+//===----------------------------------------------------------------------===//
+// Global API.
+//===----------------------------------------------------------------------===//
+
+void mlirEnableGlobalDebug(bool enable) { ::llvm::DebugFlag = true; }
+
 //===----------------------------------------------------------------------===//
 // Context API.
 //===----------------------------------------------------------------------===//
@@ -64,6 +72,10 @@ bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
   return unwrap(context)->isOperationRegistered(unwrap(name));
 }
 
+void mlirContextEnableMultithreading(MlirContext context, bool enable) {
+  return unwrap(context)->enableMultithreading(enable);
+}
+
 //===----------------------------------------------------------------------===//
 // Dialect API.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/CAPI/IR/Pass.cpp b/mlir/lib/CAPI/IR/Pass.cpp
index b3685ddf4db4f..4bfc9d0132b37 100644
--- a/mlir/lib/CAPI/IR/Pass.cpp
+++ b/mlir/lib/CAPI/IR/Pass.cpp
@@ -38,6 +38,14 @@ MlirLogicalResult mlirPassManagerRun(MlirPassManager passManager,
   return wrap(unwrap(passManager)->run(unwrap(module)));
 }
 
+void mlirPassManagerEnableIRPrinting(MlirPassManager passManager) {
+  return unwrap(passManager)->enableIRPrinting();
+}
+
+void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable) {
+  unwrap(passManager)->enableVerifier(enable);
+}
+
 MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager,
                                                 MlirStringRef operationName) {
   return wrap(&unwrap(passManager)->nest(unwrap(operationName)));

diff  --git a/mlir/test/Bindings/Python/context_managers.py b/mlir/test/Bindings/Python/context_managers.py
index b93fcf70ac482..9fde95af0be82 100644
--- a/mlir/test/Bindings/Python/context_managers.py
+++ b/mlir/test/Bindings/Python/context_managers.py
@@ -10,6 +10,13 @@ def run(f):
   assert Context._get_live_count() == 0
 
 
+# CHECK-LABEL: TEST: testExports
+def testExports():
+  from mlir.ir import _enable_debug
+
+run(testExports)
+
+
 # CHECK-LABEL: TEST: testContextEnterExit
 def testContextEnterExit():
   with Context() as ctx:


        


More information about the Mlir-commits mailing list