@@ -634,58 +634,6 @@ PyMlirContext::LiveContextMap &PyMlirContext::getLiveContexts() {
634
634
635
635
size_t PyMlirContext::getLiveCount () { return getLiveContexts ().size (); }
636
636
637
- size_t PyMlirContext::getLiveOperationCount () { return liveOperations.size (); }
638
-
639
- std::vector<PyOperation *> PyMlirContext::getLiveOperationObjects () {
640
- std::vector<PyOperation *> liveObjects;
641
- for (auto &entry : liveOperations)
642
- liveObjects.push_back (entry.second .second );
643
- return liveObjects;
644
- }
645
-
646
- size_t PyMlirContext::clearLiveOperations () {
647
- for (auto &op : liveOperations)
648
- op.second .second ->setInvalid ();
649
- size_t numInvalidated = liveOperations.size ();
650
- liveOperations.clear ();
651
- return numInvalidated;
652
- }
653
-
654
- void PyMlirContext::clearOperation (MlirOperation op) {
655
- auto it = liveOperations.find (op.ptr );
656
- if (it != liveOperations.end ()) {
657
- it->second .second ->setInvalid ();
658
- liveOperations.erase (it);
659
- }
660
- }
661
-
662
- void PyMlirContext::clearOperationsInside (PyOperationBase &op) {
663
- typedef struct {
664
- PyOperation &rootOp;
665
- bool rootSeen;
666
- } callBackData;
667
- callBackData data{op.getOperation (), false };
668
- // Mark all ops below the op that the passmanager will be rooted
669
- // at (but not op itself - note the preorder) as invalid.
670
- MlirOperationWalkCallback invalidatingCallback = [](MlirOperation op,
671
- void *userData) {
672
- callBackData *data = static_cast <callBackData *>(userData);
673
- if (LLVM_LIKELY (data->rootSeen ))
674
- data->rootOp .getOperation ().getContext ()->clearOperation (op);
675
- else
676
- data->rootSeen = true ;
677
- return MlirWalkResult::MlirWalkResultAdvance;
678
- };
679
- mlirOperationWalk (op.getOperation (), invalidatingCallback,
680
- static_cast <void *>(&data), MlirWalkPreOrder);
681
- }
682
- void PyMlirContext::clearOperationsInside (MlirOperation op) {
683
- PyOperationRef opRef = PyOperation::forOperation (getRef (), op);
684
- clearOperationsInside (opRef->getOperation ());
685
- }
686
-
687
- size_t PyMlirContext::getLiveModuleCount () { return liveModules.size (); }
688
-
689
637
pybind11::object PyMlirContext::contextEnter () {
690
638
return PyThreadContextEntry::pushContext (*this );
691
639
}
@@ -1055,39 +1003,21 @@ PyLocation &DefaultingPyLocation::resolve() {
1055
1003
PyModule::PyModule (PyMlirContextRef contextRef, MlirModule module)
1056
1004
: BaseContextObject(std::move(contextRef)), module(module) {}
1057
1005
1058
- PyModule::~PyModule () {
1059
- py::gil_scoped_acquire acquire;
1060
- auto &liveModules = getContext ()->liveModules ;
1061
- assert (liveModules.count (module.ptr ) == 1 &&
1062
- " destroying module not in live map" );
1063
- liveModules.erase (module.ptr );
1064
- mlirModuleDestroy (module);
1065
- }
1006
+ PyModule::~PyModule () { mlirModuleDestroy (module); }
1066
1007
1067
1008
PyModuleRef PyModule::forModule (MlirModule module) {
1068
1009
MlirContext context = mlirModuleGetContext (module);
1069
1010
PyMlirContextRef contextRef = PyMlirContext::forContext (context);
1070
1011
1071
- py::gil_scoped_acquire acquire;
1072
- auto &liveModules = contextRef->liveModules ;
1073
- auto it = liveModules.find (module.ptr );
1074
- if (it == liveModules.end ()) {
1075
- // Create.
1076
- PyModule *unownedModule = new PyModule (std::move (contextRef), module);
1077
- // Note that the default return value policy on cast is automatic_reference,
1078
- // which does not take ownership (delete will not be called).
1079
- // Just be explicit.
1080
- py::object pyRef =
1081
- py::cast (unownedModule, py::return_value_policy::take_ownership);
1082
- unownedModule->handle = pyRef;
1083
- liveModules[module.ptr ] =
1084
- std::make_pair (unownedModule->handle , unownedModule);
1085
- return PyModuleRef (unownedModule, std::move (pyRef));
1086
- }
1087
- // Use existing.
1088
- PyModule *existing = it->second .second ;
1089
- py::object pyRef = py::reinterpret_borrow<py::object>(it->second .first );
1090
- return PyModuleRef (existing, std::move (pyRef));
1012
+ // Create.
1013
+ PyModule *unownedModule = new PyModule (std::move (contextRef), module);
1014
+ // Note that the default return value policy on cast is automatic_reference,
1015
+ // which does not take ownership (delete will not be called).
1016
+ // Just be explicit.
1017
+ py::object pyRef =
1018
+ py::cast (unownedModule, py::return_value_policy::take_ownership);
1019
+ unownedModule->handle = pyRef;
1020
+ return PyModuleRef (unownedModule, std::move (pyRef));
1091
1021
}
1092
1022
1093
1023
py::object PyModule::createFromCapsule (py::object capsule) {
@@ -1112,10 +1042,6 @@ PyOperation::~PyOperation() {
1112
1042
// If the operation has already been invalidated there is nothing to do.
1113
1043
if (!valid)
1114
1044
return ;
1115
- auto &liveOperations = getContext ()->liveOperations ;
1116
- assert (liveOperations.count (operation.ptr ) == 1 &&
1117
- " destroying operation not in live map" );
1118
- liveOperations.erase (operation.ptr );
1119
1045
if (!isAttached ()) {
1120
1046
mlirOperationDestroy (operation);
1121
1047
}
@@ -1124,7 +1050,6 @@ PyOperation::~PyOperation() {
1124
1050
PyOperationRef PyOperation::createInstance (PyMlirContextRef contextRef,
1125
1051
MlirOperation operation,
1126
1052
py::object parentKeepAlive) {
1127
- auto &liveOperations = contextRef->liveOperations ;
1128
1053
// Create.
1129
1054
PyOperation *unownedOperation =
1130
1055
new PyOperation (std::move (contextRef), operation);
@@ -1137,34 +1062,20 @@ PyOperationRef PyOperation::createInstance(PyMlirContextRef contextRef,
1137
1062
if (parentKeepAlive) {
1138
1063
unownedOperation->parentKeepAlive = std::move (parentKeepAlive);
1139
1064
}
1140
- liveOperations[operation.ptr ] = std::make_pair (pyRef, unownedOperation);
1141
1065
return PyOperationRef (unownedOperation, std::move (pyRef));
1142
1066
}
1143
1067
1144
1068
PyOperationRef PyOperation::forOperation (PyMlirContextRef contextRef,
1145
1069
MlirOperation operation,
1146
1070
py::object parentKeepAlive) {
1147
- auto &liveOperations = contextRef->liveOperations ;
1148
- auto it = liveOperations.find (operation.ptr );
1149
- if (it == liveOperations.end ()) {
1150
- // Create.
1151
- return createInstance (std::move (contextRef), operation,
1152
- std::move (parentKeepAlive));
1153
- }
1154
- // Use existing.
1155
- PyOperation *existing = it->second .second ;
1156
- py::object pyRef = py::reinterpret_borrow<py::object>(it->second .first );
1157
- return PyOperationRef (existing, std::move (pyRef));
1071
+ // Create.
1072
+ return createInstance (std::move (contextRef), operation,
1073
+ std::move (parentKeepAlive));
1158
1074
}
1159
1075
1160
1076
PyOperationRef PyOperation::createDetached (PyMlirContextRef contextRef,
1161
1077
MlirOperation operation,
1162
1078
py::object parentKeepAlive) {
1163
- auto &liveOperations = contextRef->liveOperations ;
1164
- assert (liveOperations.count (operation.ptr ) == 0 &&
1165
- " cannot create detached operation that already exists" );
1166
- (void )liveOperations;
1167
-
1168
1079
PyOperationRef created = createInstance (std::move (contextRef), operation,
1169
1080
std::move (parentKeepAlive));
1170
1081
created->attached = false ;
@@ -1530,9 +1441,6 @@ void PyOperation::erase() {
1530
1441
// TODO: Fix memory hazards when erasing a tree of operations for which a deep
1531
1442
// Python reference to a child operation is live. All children should also
1532
1443
// have their `valid` bit set to false.
1533
- auto &liveOperations = getContext ()->liveOperations ;
1534
- if (liveOperations.count (operation.ptr ))
1535
- liveOperations.erase (operation.ptr );
1536
1444
mlirOperationDestroy (operation);
1537
1445
valid = false ;
1538
1446
}
@@ -2274,7 +2182,6 @@ class PyBlockArgumentList
2274
2182
: public Sliceable<PyBlockArgumentList, PyBlockArgument> {
2275
2183
public:
2276
2184
static constexpr const char *pyClassName = " BlockArgumentList" ;
2277
- using SliceableT = Sliceable<PyBlockArgumentList, PyBlockArgument>;
2278
2185
2279
2186
PyBlockArgumentList (PyOperationRef operation, MlirBlock block,
2280
2187
intptr_t startIndex = 0 , intptr_t length = -1 ,
@@ -2598,14 +2505,6 @@ void mlir::python::populateIRCore(py::module &m) {
2598
2505
PyMlirContextRef ref = PyMlirContext::forContext (self.get ());
2599
2506
return ref.releaseObject ();
2600
2507
})
2601
- .def (" _get_live_operation_count" , &PyMlirContext::getLiveOperationCount)
2602
- .def (" _get_live_operation_objects" ,
2603
- &PyMlirContext::getLiveOperationObjects)
2604
- .def (" _clear_live_operations" , &PyMlirContext::clearLiveOperations)
2605
- .def (" _clear_live_operations_inside" ,
2606
- py::overload_cast<MlirOperation>(
2607
- &PyMlirContext::clearOperationsInside))
2608
- .def (" _get_live_module_count" , &PyMlirContext::getLiveModuleCount)
2609
2508
.def_property_readonly (MLIR_PYTHON_CAPI_PTR_ATTR,
2610
2509
&PyMlirContext::getCapsule)
2611
2510
.def (MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyMlirContext::createFromCapsule)
@@ -2915,7 +2814,13 @@ void mlir::python::populateIRCore(py::module &m) {
2915
2814
// Defer to the operation's __str__.
2916
2815
return self.attr (" operation" ).attr (" __str__" )();
2917
2816
},
2918
- kOperationStrDunderDocstring );
2817
+ kOperationStrDunderDocstring )
2818
+ .def (
2819
+ " __eq__" ,
2820
+ [](PyModule &self, PyModule &other) {
2821
+ return mlirModuleEqual (self.get (), other.get ());
2822
+ },
2823
+ " other" _a);
2919
2824
2920
2825
// ----------------------------------------------------------------------------
2921
2826
// Mapping of Operation.
@@ -2927,7 +2832,8 @@ void mlir::python::populateIRCore(py::module &m) {
2927
2832
})
2928
2833
.def (" __eq__" ,
2929
2834
[](PyOperationBase &self, PyOperationBase &other) {
2930
- return &self.getOperation () == &other.getOperation ();
2835
+ return mlirOperationEqual (self.getOperation ().get (),
2836
+ other.getOperation ().get ());
2931
2837
})
2932
2838
.def (" __eq__" ,
2933
2839
[](PyOperationBase &self, py::object other) { return false ; })
0 commit comments