diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -754,9 +754,8 @@ PyOperation::~PyOperation() { auto &liveOperations = getContext()->liveOperations; - assert(liveOperations.count(operation.ptr) == 1 && - "destroying operation not in live map"); - liveOperations.erase(operation.ptr); + if (liveOperations.count(operation.ptr)) + liveOperations.erase(operation.ptr); if (!isAttached()) { mlirOperationDestroy(operation); } @@ -2094,6 +2093,7 @@ py::arg("successors") = py::none(), py::arg("regions") = 0, py::arg("loc") = py::none(), py::arg("ip") = py::none(), kOperationCreateDocstring) + .def("destroy", [](PyOperation &self) { self.~PyOperation(); }) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyOperation::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyOperation::createFromCapsule) diff --git a/mlir/test/Bindings/Python/ir_operation.py b/mlir/test/Bindings/Python/ir_operation.py --- a/mlir/test/Bindings/Python/ir_operation.py +++ b/mlir/test/Bindings/Python/ir_operation.py @@ -646,3 +646,17 @@ assert m2 is m run(testCapsuleConversions) + +# CHECK-LABEL: TEST: testOperationDestroy +def testOperationDestroy(): + ctx = Context() + ctx.allow_unregistered_dialects = True + with Location.unknown(ctx): + m = Module.create() + with InsertionPoint(m.body): + op = Operation.create("custom.op1") + assert ctx._get_live_operation_count() == 2 + op.operation.destroy() + assert ctx._get_live_operation_count() == 1 + +run(testOperationDestroy)