Skip to content

Commit caa159f

Browse files
[mlir][python] Add simple debugging and printing helpers
Differential Revision: https://reviews.llvm.org/D100643
1 parent 437fb42 commit caa159f

File tree

8 files changed

+72
-0
lines changed

8 files changed

+72
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ struct MlirNamedAttribute {
7575
};
7676
typedef struct MlirNamedAttribute MlirNamedAttribute;
7777

78+
//===----------------------------------------------------------------------===//
79+
// Global API.
80+
//===----------------------------------------------------------------------===//
81+
82+
/// Set the global debugging flag.
83+
MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable);
84+
7885
//===----------------------------------------------------------------------===//
7986
// Context API.
8087
//===----------------------------------------------------------------------===//
@@ -119,6 +126,10 @@ mlirContextGetNumLoadedDialects(MlirContext context);
119126
MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
120127
MlirStringRef name);
121128

129+
/// Set threading mode (must be set to false to print-ir-after-all).
130+
MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context,
131+
bool enable);
132+
122133
/// Returns whether the given fully-qualified operation (i.e.
123134
/// 'dialect.operation') is registered with the context. This will return true
124135
/// if the dialect is loaded and the operation is registered within the

mlir/include/mlir-c/Pass.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ mlirPassManagerGetAsOpPassManager(MlirPassManager passManager);
6565
MLIR_CAPI_EXPORTED MlirLogicalResult
6666
mlirPassManagerRun(MlirPassManager passManager, MlirModule module);
6767

68+
/// Enable print-ir-after-all.
69+
MLIR_CAPI_EXPORTED void
70+
mlirPassManagerEnableIRPrinting(MlirPassManager passManager);
71+
72+
/// Enable / disable verify-each.
73+
MLIR_CAPI_EXPORTED void
74+
mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable);
75+
6876
/// Nest an OpPassManager under the top-level PassManager, the nested
6977
/// passmanager will only run on operations matching the provided name.
7078
/// The returned OpPassManager will be destroyed when the parent is destroyed.

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,11 @@ class PyOpAttributeMap {
17121712
//------------------------------------------------------------------------------
17131713

17141714
void mlir::python::populateIRCore(py::module &m) {
1715+
//----------------------------------------------------------------------------
1716+
// Mapping of Global functions
1717+
//----------------------------------------------------------------------------
1718+
m.def("_enable_debug", [](bool enable) { mlirEnableGlobalDebug(enable); });
1719+
17151720
//----------------------------------------------------------------------------
17161721
// Mapping of MlirContext
17171722
//----------------------------------------------------------------------------
@@ -1766,6 +1771,10 @@ void mlir::python::populateIRCore(py::module &m) {
17661771
[](PyMlirContext &self, bool value) {
17671772
mlirContextSetAllowUnregisteredDialects(self.get(), value);
17681773
})
1774+
.def("enable_multithreading",
1775+
[](PyMlirContext &self, bool enable) {
1776+
mlirContextEnableMultithreading(self.get(), enable);
1777+
})
17691778
.def("is_registered_operation",
17701779
[](PyMlirContext &self, std::string &name) {
17711780
return mlirContextIsRegisteredOperation(

mlir/lib/Bindings/Python/Pass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ void mlir::python::populatePassManagerSubmodule(py::module &m) {
6868
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyPassManager::createFromCapsule)
6969
.def("_testing_release", &PyPassManager::release,
7070
"Releases (leaks) the backing pass manager (testing)")
71+
.def(
72+
"enable_ir_printing",
73+
[](PyPassManager &passManager) {
74+
mlirPassManagerEnableIRPrinting(passManager.get());
75+
},
76+
"Enable print-ir-after-all.")
77+
.def(
78+
"enable_verifier",
79+
[](PyPassManager &passManager, bool enable) {
80+
mlirPassManagerEnableVerifier(passManager.get(), enable);
81+
},
82+
"Enable / disable verify-each.")
7183
.def_static(
7284
"parse",
7385
[](const std::string pipeline, DefaultingPyMlirContext context) {

mlir/lib/Bindings/Python/mlir/ir.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
from ._cext_loader import _reexport_cext
77
_reexport_cext("ir", __name__)
88
del _reexport_cext
9+
10+
# Extra functions that are not visible to _reexport_cext.
11+
# TODO: is this really necessary?
12+
from _mlir.ir import _enable_debug
13+
_enable_debug = _enable_debug

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
#include "mlir/Interfaces/InferTypeOpInterface.h"
2222
#include "mlir/Parser.h"
2323

24+
#include "llvm/Support/Debug.h"
25+
2426
using namespace mlir;
2527

28+
//===----------------------------------------------------------------------===//
29+
// Global API.
30+
//===----------------------------------------------------------------------===//
31+
32+
void mlirEnableGlobalDebug(bool enable) { ::llvm::DebugFlag = true; }
33+
2634
//===----------------------------------------------------------------------===//
2735
// Context API.
2836
//===----------------------------------------------------------------------===//
@@ -64,6 +72,10 @@ bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
6472
return unwrap(context)->isOperationRegistered(unwrap(name));
6573
}
6674

75+
void mlirContextEnableMultithreading(MlirContext context, bool enable) {
76+
return unwrap(context)->enableMultithreading(enable);
77+
}
78+
6779
//===----------------------------------------------------------------------===//
6880
// Dialect API.
6981
//===----------------------------------------------------------------------===//

mlir/lib/CAPI/IR/Pass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ MlirLogicalResult mlirPassManagerRun(MlirPassManager passManager,
3838
return wrap(unwrap(passManager)->run(unwrap(module)));
3939
}
4040

41+
void mlirPassManagerEnableIRPrinting(MlirPassManager passManager) {
42+
return unwrap(passManager)->enableIRPrinting();
43+
}
44+
45+
void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable) {
46+
unwrap(passManager)->enableVerifier(enable);
47+
}
48+
4149
MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager,
4250
MlirStringRef operationName) {
4351
return wrap(&unwrap(passManager)->nest(unwrap(operationName)));

mlir/test/Bindings/Python/context_managers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ def run(f):
1010
assert Context._get_live_count() == 0
1111

1212

13+
# CHECK-LABEL: TEST: testExports
14+
def testExports():
15+
from mlir.ir import _enable_debug
16+
17+
run(testExports)
18+
19+
1320
# CHECK-LABEL: TEST: testContextEnterExit
1421
def testContextEnterExit():
1522
with Context() as ctx:

0 commit comments

Comments
 (0)