@@ -198,7 +198,9 @@ py::object classmethod(Func f, Args... args) {
198198static py::object
199199createCustomDialectWrapper (const std::string &dialectNamespace,
200200 py::object dialectDescriptor) {
201- auto dialectClass = PyGlobals::get ().lookupDialectClass (dialectNamespace);
201+ auto dialectClass = PyGlobals::withInstance ([&](PyGlobals& instance) {
202+ return instance.lookupDialectClass (dialectNamespace);
203+ });
202204 if (!dialectClass) {
203205 // Use the base class.
204206 return py::cast (PyDialect (std::move (dialectDescriptor)));
@@ -601,16 +603,23 @@ class PyOpOperandIterator {
601603
602604PyMlirContext::PyMlirContext (MlirContext context) : context(context) {
603605 py::gil_scoped_acquire acquire;
604- auto &liveContexts = getLiveContexts ();
605- liveContexts[context.ptr ] = this ;
606+ withLiveContexts ([&](LiveContextMap& liveContexts) {
607+ liveContexts[context.ptr ] = this ;
608+ return this ;
609+ });
606610}
607611
608612PyMlirContext::~PyMlirContext () {
609613 // Note that the only public way to construct an instance is via the
610614 // forContext method, which always puts the associated handle into
611615 // liveContexts.
612616 py::gil_scoped_acquire acquire;
613- getLiveContexts ().erase (context.ptr );
617+
618+ withLiveContexts ([&](LiveContextMap& liveContexts) {
619+ liveContexts.erase (context.ptr );
620+ return this ;
621+ });
622+
614623 mlirContextDestroy (context);
615624}
616625
@@ -632,27 +641,32 @@ PyMlirContext *PyMlirContext::createNewContextForInit() {
632641
633642PyMlirContextRef PyMlirContext::forContext (MlirContext context) {
634643 py::gil_scoped_acquire acquire;
635- auto &liveContexts = getLiveContexts ();
636- auto it = liveContexts.find (context.ptr );
637- if (it == liveContexts.end ()) {
638- // Create.
639- PyMlirContext *unownedContextWrapper = new PyMlirContext (context);
640- py::object pyRef = py::cast (unownedContextWrapper);
641- assert (pyRef && " cast to py::object failed" );
642- liveContexts[context.ptr ] = unownedContextWrapper;
643- return PyMlirContextRef (unownedContextWrapper, std::move (pyRef));
644- }
645- // Use existing.
646- py::object pyRef = py::cast (it->second );
647- return PyMlirContextRef (it->second , std::move (pyRef));
644+ return withLiveContexts ([&](LiveContextMap& liveContexts) {
645+ auto it = liveContexts.find (context.ptr );
646+ if (it == liveContexts.end ()) {
647+ // Create.
648+ PyMlirContext *unownedContextWrapper = new PyMlirContext (context);
649+ py::object pyRef = py::cast (unownedContextWrapper);
650+ assert (pyRef && " cast to py::object failed" );
651+ liveContexts[context.ptr ] = unownedContextWrapper;
652+ return PyMlirContextRef (unownedContextWrapper, std::move (pyRef));
653+ }
654+ // Use existing.
655+ py::object pyRef = py::cast (it->second );
656+ return PyMlirContextRef (it->second , std::move (pyRef));
657+ });
648658}
649659
650660PyMlirContext::LiveContextMap &PyMlirContext::getLiveContexts () {
651661 static LiveContextMap liveContexts;
652662 return liveContexts;
653663}
654664
655- size_t PyMlirContext::getLiveCount () { return getLiveContexts ().size (); }
665+ size_t PyMlirContext::getLiveCount () {
666+ return withLiveContexts ([&](LiveContextMap& liveContexts) {
667+ return liveContexts.size ();
668+ });
669+ }
656670
657671size_t PyMlirContext::getLiveOperationCount () { return liveOperations.size (); }
658672
@@ -1556,8 +1570,10 @@ py::object PyOperation::createOpView() {
15561570 checkValid ();
15571571 MlirIdentifier ident = mlirOperationGetName (get ());
15581572 MlirStringRef identStr = mlirIdentifierStr (ident);
1559- auto operationCls = PyGlobals::get ().lookupOperationClass (
1560- StringRef (identStr.data , identStr.length ));
1573+ auto operationCls = PyGlobals::withInstance ([&](PyGlobals& instance){
1574+ return instance.lookupOperationClass (
1575+ StringRef (identStr.data , identStr.length ));
1576+ });
15611577 if (operationCls)
15621578 return PyOpView::constructDerived (*operationCls, *getRef ().get ());
15631579 return py::cast (PyOpView (getRef ().getObject ()));
@@ -2008,7 +2024,9 @@ pybind11::object PyValue::maybeDownCast() {
20082024 assert (!mlirTypeIDIsNull (mlirTypeID) &&
20092025 " mlirTypeID was expected to be non-null." );
20102026 std::optional<pybind11::function> valueCaster =
2011- PyGlobals::get ().lookupValueCaster (mlirTypeID, mlirTypeGetDialect (type));
2027+ PyGlobals::withInstance ([&](PyGlobals& instance) {
2028+ return instance.lookupValueCaster (mlirTypeID, mlirTypeGetDialect (type));
2029+ });
20122030 // py::return_value_policy::move means use std::move to move the return value
20132031 // contents into a new instance that will be owned by Python.
20142032 py::object thisObj = py::cast (this , py::return_value_policy::move);
@@ -3487,8 +3505,10 @@ void mlir::python::populateIRCore(py::module &m) {
34873505 assert (!mlirTypeIDIsNull (mlirTypeID) &&
34883506 " mlirTypeID was expected to be non-null." );
34893507 std::optional<pybind11::function> typeCaster =
3490- PyGlobals::get ().lookupTypeCaster (mlirTypeID,
3491- mlirAttributeGetDialect (self));
3508+ PyGlobals::withInstance ([&](PyGlobals& instance){
3509+ return instance.lookupTypeCaster (mlirTypeID,
3510+ mlirAttributeGetDialect (self));
3511+ });
34923512 if (!typeCaster)
34933513 return py::cast (self);
34943514 return typeCaster.value ()(self);
@@ -3585,9 +3605,11 @@ void mlir::python::populateIRCore(py::module &m) {
35853605 MlirTypeID mlirTypeID = mlirTypeGetTypeID (self);
35863606 assert (!mlirTypeIDIsNull (mlirTypeID) &&
35873607 " mlirTypeID was expected to be non-null." );
3588- std::optional<pybind11::function> typeCaster =
3589- PyGlobals::get ().lookupTypeCaster (mlirTypeID,
3608+ std::optional<pybind11::function> typeCaster =
3609+ PyGlobals::withInstance ([&](PyGlobals& instance){
3610+ return instance.lookupTypeCaster (mlirTypeID,
35903611 mlirTypeGetDialect (self));
3612+ });
35913613 if (!typeCaster)
35923614 return py::cast (self);
35933615 return typeCaster.value ()(self);
0 commit comments