@@ -110,6 +110,15 @@ static const char kOperationPrintDocstring[] =
110
110
invalid, behavior is undefined.
111
111
)" ;
112
112
113
+ static const char kOperationPrintStateDocstring [] =
114
+ R"( Prints the assembly form of the operation to a file like object.
115
+
116
+ Args:
117
+ file: The file like object to write to. Defaults to sys.stdout.
118
+ binary: Whether to write bytes (True) or str (False). Defaults to False.
119
+ state: AsmState capturing the operation numbering and flags.
120
+ )" ;
121
+
113
122
static const char kOperationGetAsmDocstring [] =
114
123
R"( Gets the assembly form of the operation with all options available.
115
124
@@ -1169,11 +1178,11 @@ void PyOperation::checkValid() const {
1169
1178
}
1170
1179
}
1171
1180
1172
- void PyOperationBase::print (py::object fileObject, bool binary,
1173
- std::optional<int64_t > largeElementsLimit,
1181
+ void PyOperationBase::print (std::optional<int64_t > largeElementsLimit,
1174
1182
bool enableDebugInfo, bool prettyDebugInfo,
1175
1183
bool printGenericOpForm, bool useLocalScope,
1176
- bool assumeVerified) {
1184
+ bool assumeVerified, py::object fileObject,
1185
+ bool binary) {
1177
1186
PyOperation &operation = getOperation ();
1178
1187
operation.checkValid ();
1179
1188
if (fileObject.is_none ())
@@ -1198,6 +1207,17 @@ void PyOperationBase::print(py::object fileObject, bool binary,
1198
1207
mlirOpPrintingFlagsDestroy (flags);
1199
1208
}
1200
1209
1210
+ void PyOperationBase::print (PyAsmState &state, py::object fileObject,
1211
+ bool binary) {
1212
+ PyOperation &operation = getOperation ();
1213
+ operation.checkValid ();
1214
+ if (fileObject.is_none ())
1215
+ fileObject = py::module::import (" sys" ).attr (" stdout" );
1216
+ PyFileAccumulator accum (fileObject, binary);
1217
+ mlirOperationPrintWithState (operation, state.get (), accum.getCallback (),
1218
+ accum.getUserData ());
1219
+ }
1220
+
1201
1221
void PyOperationBase::writeBytecode (const py::object &fileObject,
1202
1222
std::optional<int64_t > bytecodeVersion) {
1203
1223
PyOperation &operation = getOperation ();
@@ -1230,13 +1250,14 @@ py::object PyOperationBase::getAsm(bool binary,
1230
1250
} else {
1231
1251
fileObject = py::module::import (" io" ).attr (" StringIO" )();
1232
1252
}
1233
- print (fileObject, /* binary=*/ binary,
1234
- /* largeElementsLimit=*/ largeElementsLimit,
1253
+ print (/* largeElementsLimit=*/ largeElementsLimit,
1235
1254
/* enableDebugInfo=*/ enableDebugInfo,
1236
1255
/* prettyDebugInfo=*/ prettyDebugInfo,
1237
1256
/* printGenericOpForm=*/ printGenericOpForm,
1238
1257
/* useLocalScope=*/ useLocalScope,
1239
- /* assumeVerified=*/ assumeVerified);
1258
+ /* assumeVerified=*/ assumeVerified,
1259
+ /* fileObject=*/ fileObject,
1260
+ /* binary=*/ binary);
1240
1261
1241
1262
return fileObject.attr (" getvalue" )();
1242
1263
}
@@ -2946,15 +2967,23 @@ void mlir::python::populateIRCore(py::module &m) {
2946
2967
/* assumeVerified=*/ false );
2947
2968
},
2948
2969
" Returns the assembly form of the operation." )
2949
- .def (" print" , &PyOperationBase::print,
2970
+ .def (" print" ,
2971
+ py::overload_cast<PyAsmState &, pybind11::object, bool >(
2972
+ &PyOperationBase::print),
2973
+ py::arg (" state" ), py::arg (" file" ) = py::none (),
2974
+ py::arg (" binary" ) = false , kOperationPrintStateDocstring )
2975
+ .def (" print" ,
2976
+ py::overload_cast<std::optional<int64_t >, bool , bool , bool , bool ,
2977
+ bool , py::object, bool >(
2978
+ &PyOperationBase::print),
2950
2979
// Careful: Lots of arguments must match up with print method.
2951
- py::arg (" file" ) = py::none (), py::arg (" binary" ) = false ,
2952
2980
py::arg (" large_elements_limit" ) = py::none (),
2953
2981
py::arg (" enable_debug_info" ) = false ,
2954
2982
py::arg (" pretty_debug_info" ) = false ,
2955
2983
py::arg (" print_generic_op_form" ) = false ,
2956
2984
py::arg (" use_local_scope" ) = false ,
2957
- py::arg (" assume_verified" ) = false , kOperationPrintDocstring )
2985
+ py::arg (" assume_verified" ) = false , py::arg (" file" ) = py::none (),
2986
+ py::arg (" binary" ) = false , kOperationPrintDocstring )
2958
2987
.def (" write_bytecode" , &PyOperationBase::writeBytecode, py::arg (" file" ),
2959
2988
py::arg (" desired_version" ) = py::none (),
2960
2989
kOperationPrintBytecodeDocstring )
0 commit comments