Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit db9c9aa

Browse files
authored
[mlir] Expose skipRegions option for Op printing in the C and Python bindings (#96150)
The MLIR C and Python Bindings expose various methods from `mlir::OpPrintingFlags` . This PR adds a binding for the `skipRegions` method, which allows to skip the printing of Regions when printing Ops. It also exposes this option as parameter in the python `get_asm` and `print` methods
1 parent 28a845c commit db9c9aa

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

IRCore.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static const char kOperationPrintDocstring[] =
108108
and report failures in a more robust fashion. Set this to True if doing this
109109
in order to avoid running a redundant verification. If the IR is actually
110110
invalid, behavior is undefined.
111+
skip_regions: Whether to skip printing regions. Defaults to False.
111112
)";
112113

113114
static const char kOperationPrintStateDocstring[] =
@@ -1221,7 +1222,7 @@ void PyOperationBase::print(std::optional<int64_t> largeElementsLimit,
12211222
bool enableDebugInfo, bool prettyDebugInfo,
12221223
bool printGenericOpForm, bool useLocalScope,
12231224
bool assumeVerified, py::object fileObject,
1224-
bool binary) {
1225+
bool binary, bool skipRegions) {
12251226
PyOperation &operation = getOperation();
12261227
operation.checkValid();
12271228
if (fileObject.is_none())
@@ -1239,6 +1240,8 @@ void PyOperationBase::print(std::optional<int64_t> largeElementsLimit,
12391240
mlirOpPrintingFlagsUseLocalScope(flags);
12401241
if (assumeVerified)
12411242
mlirOpPrintingFlagsAssumeVerified(flags);
1243+
if (skipRegions)
1244+
mlirOpPrintingFlagsSkipRegions(flags);
12421245

12431246
PyFileAccumulator accum(fileObject, binary);
12441247
mlirOperationPrintWithFlags(operation, flags, accum.getCallback(),
@@ -1314,7 +1317,7 @@ py::object PyOperationBase::getAsm(bool binary,
13141317
std::optional<int64_t> largeElementsLimit,
13151318
bool enableDebugInfo, bool prettyDebugInfo,
13161319
bool printGenericOpForm, bool useLocalScope,
1317-
bool assumeVerified) {
1320+
bool assumeVerified, bool skipRegions) {
13181321
py::object fileObject;
13191322
if (binary) {
13201323
fileObject = py::module::import("io").attr("BytesIO")();
@@ -1328,7 +1331,8 @@ py::object PyOperationBase::getAsm(bool binary,
13281331
/*useLocalScope=*/useLocalScope,
13291332
/*assumeVerified=*/assumeVerified,
13301333
/*fileObject=*/fileObject,
1331-
/*binary=*/binary);
1334+
/*binary=*/binary,
1335+
/*skipRegions=*/skipRegions);
13321336

13331337
return fileObject.attr("getvalue")();
13341338
}
@@ -3043,7 +3047,8 @@ void mlir::python::populateIRCore(py::module &m) {
30433047
/*prettyDebugInfo=*/false,
30443048
/*printGenericOpForm=*/false,
30453049
/*useLocalScope=*/false,
3046-
/*assumeVerified=*/false);
3050+
/*assumeVerified=*/false,
3051+
/*skipRegions=*/false);
30473052
},
30483053
"Returns the assembly form of the operation.")
30493054
.def("print",
@@ -3053,15 +3058,17 @@ void mlir::python::populateIRCore(py::module &m) {
30533058
py::arg("binary") = false, kOperationPrintStateDocstring)
30543059
.def("print",
30553060
py::overload_cast<std::optional<int64_t>, bool, bool, bool, bool,
3056-
bool, py::object, bool>(&PyOperationBase::print),
3061+
bool, py::object, bool, bool>(
3062+
&PyOperationBase::print),
30573063
// Careful: Lots of arguments must match up with print method.
30583064
py::arg("large_elements_limit") = py::none(),
30593065
py::arg("enable_debug_info") = false,
30603066
py::arg("pretty_debug_info") = false,
30613067
py::arg("print_generic_op_form") = false,
30623068
py::arg("use_local_scope") = false,
30633069
py::arg("assume_verified") = false, py::arg("file") = py::none(),
3064-
py::arg("binary") = false, kOperationPrintDocstring)
3070+
py::arg("binary") = false, py::arg("skip_regions") = false,
3071+
kOperationPrintDocstring)
30653072
.def("write_bytecode", &PyOperationBase::writeBytecode, py::arg("file"),
30663073
py::arg("desired_version") = py::none(),
30673074
kOperationPrintBytecodeDocstring)
@@ -3073,7 +3080,8 @@ void mlir::python::populateIRCore(py::module &m) {
30733080
py::arg("pretty_debug_info") = false,
30743081
py::arg("print_generic_op_form") = false,
30753082
py::arg("use_local_scope") = false,
3076-
py::arg("assume_verified") = false, kOperationGetAsmDocstring)
3083+
py::arg("assume_verified") = false, py::arg("skip_regions") = false,
3084+
kOperationGetAsmDocstring)
30773085
.def("verify", &PyOperationBase::verify,
30783086
"Verify the operation. Raises MLIRError if verification fails, and "
30793087
"returns true otherwise.")

IRModule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,15 @@ class PyOperationBase {
574574
/// Implements the bound 'print' method and helps with others.
575575
void print(std::optional<int64_t> largeElementsLimit, bool enableDebugInfo,
576576
bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope,
577-
bool assumeVerified, py::object fileObject, bool binary);
577+
bool assumeVerified, py::object fileObject, bool binary,
578+
bool skipRegions);
578579
void print(PyAsmState &state, py::object fileObject, bool binary);
579580

580581
pybind11::object getAsm(bool binary,
581582
std::optional<int64_t> largeElementsLimit,
582583
bool enableDebugInfo, bool prettyDebugInfo,
583584
bool printGenericOpForm, bool useLocalScope,
584-
bool assumeVerified);
585+
bool assumeVerified, bool skipRegions);
585586

586587
// Implement the bound 'writeBytecode' method.
587588
void writeBytecode(const pybind11::object &fileObject,

0 commit comments

Comments
 (0)