@@ -3430,19 +3430,35 @@ void mlir::python::populateIRCore(py::module &m) {
3430
3430
kValueDunderStrDocstring )
3431
3431
.def (
3432
3432
" get_name" ,
3433
- [](PyValue &self, bool useLocalScope) {
3433
+ [](PyValue &self, std::optional<bool > useLocalScope,
3434
+ std::optional<std::reference_wrapper<PyAsmState>> state) {
3434
3435
PyPrintAccumulator printAccum;
3435
- MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate ();
3436
- if (useLocalScope)
3437
- mlirOpPrintingFlagsUseLocalScope (flags);
3438
- MlirAsmState state = mlirAsmStateCreateForValue (self.get (), flags);
3439
- mlirValuePrintAsOperand (self.get (), state, printAccum.getCallback (),
3436
+ MlirOpPrintingFlags flags;
3437
+ MlirAsmState valueState;
3438
+ // Use state if provided, else create a new state.
3439
+ if (state) {
3440
+ valueState = state.value ().get ().get ();
3441
+ // Don't allow setting using local scope and state at same time.
3442
+ if (useLocalScope.has_value ())
3443
+ throw py::value_error (
3444
+ " setting AsmState and local scope together not supported" );
3445
+ } else {
3446
+ flags = mlirOpPrintingFlagsCreate ();
3447
+ if (useLocalScope.value_or (false ))
3448
+ mlirOpPrintingFlagsUseLocalScope (flags);
3449
+ valueState = mlirAsmStateCreateForValue (self.get (), flags);
3450
+ }
3451
+ mlirValuePrintAsOperand (self.get (), valueState, printAccum.getCallback (),
3440
3452
printAccum.getUserData ());
3441
- mlirOpPrintingFlagsDestroy (flags);
3442
- mlirAsmStateDestroy (state);
3453
+ // Release state if allocated locally.
3454
+ if (!state) {
3455
+ mlirOpPrintingFlagsDestroy (flags);
3456
+ mlirAsmStateDestroy (valueState);
3457
+ }
3443
3458
return printAccum.join ();
3444
3459
},
3445
- py::arg (" use_local_scope" ) = false , kGetNameAsOperand )
3460
+ py::arg (" use_local_scope" ) = std::nullopt ,
3461
+ py::arg (" state" ) = std::nullopt , kGetNameAsOperand )
3446
3462
.def_property_readonly (
3447
3463
" type" , [](PyValue &self) { return mlirValueGetType (self.get ()); })
3448
3464
.def (
@@ -3461,6 +3477,10 @@ void mlir::python::populateIRCore(py::module &m) {
3461
3477
PyOpResult::bind (m);
3462
3478
PyOpOperand::bind (m);
3463
3479
3480
+ py::class_<PyAsmState>(m, " AsmState" , py::module_local ())
3481
+ .def (py::init<PyValue &, bool >(), py::arg (" value" ),
3482
+ py::arg (" use_local_scope" ) = false );
3483
+
3464
3484
// ----------------------------------------------------------------------------
3465
3485
// Mapping of SymbolTable.
3466
3486
// ----------------------------------------------------------------------------
0 commit comments