Description
I am running into a problem with the Python bindings of MLIR related to how PyOperation
s track the underlying Operation
s.
If I understand things right, when a PyOperation
is created, the pointer to the underlying Operation
is tracked in PyMlirContext::liveOperations
(e.g., here). This is used (potentially among other things) to return the same PyOperation
for existing Operation
s, e.g., here.
However, it is possible to obtain a PyOperation
to an Operation
that is then destroyed without the Python binding noticing, for example, by running a pass that deletes that Operation
. At that point, PyMlirContext::liveOperations
has an entry for an Operation
that doesn't exist anymore. It is then possible that that memory location is used for a new Operation
that is created through PyOperation::createDetached
(for example, through PyOperation::clone
, which is called by the Python-bound method _OperationBase.clone
). PyMlirContext::liveOperations
thus contains the pointer of the previous operation that has been destroyed in the meantime and PyOperation::createDetached
wants to add that same pointer again for a (supposedly not-yet-existing) "new" Operation
. Accordingly, the assertion in PyOperation::createDetached
fails.
I am unsure what the best way to fix this is. After a day of thinking about the problem, my best guess is to say that such a tracking is, in fact, not possible. My second best guess is to say that it is illegal to hold PyOperation
to ops that could be modified by some other functions but (1) that seems quite error-prone and (2) I haven't been able to find out how to release the PyOperation
that is causing the problem in my case. Before I spend more time to think about this topic, let me ask for input from the people that are more familiar with that part of the code: @stellaraccident, @teqdruid, @mikeurbach, @ftynse.