<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/93337>93337</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Python bidings don't know that modules are operations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:python
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            stellaraccident,
            ftynse,
            makslevental
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ftynse
      </td>
    </tr>
</table>

<pre>
    The following trivial code crashes:

```
from mlir.ir import *

m = ir.Module.create()
m.operation.erase()
```

because `erase` erases the module operation, and then the destructor of `PyModule` attempts to erase it the second time (transitively through the module object).

We should connect `PyModule` to the corresponding `PyOperation` in the bindings and avoid calling `mlirModuleDestroy` if the operation was already destroyed, using the `valid` flag on the operation.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxcU0GPozwM_TXhYg1KQ6H0wGG-qXr7tHNYac8hMSU7gVSx6Yh_v0rodqcjoUDIe89-tqOJ3GVG7ET9n1CKGL3XURvjLM4slBLqTSg18DoTPraT_iCPN5xZ-_SzPhV64THEbgMWfbBr93NEGIL34dPNF-Dobk57MMEimKhpRBLVq5AnIf-ujbw_eTvEMMHkXSxdBDddQ2QQ6okxgahO4GL5f7CLx9JE1IxCtUId74gyXDFqdmEuMWp6OvwWcFt7NHohBNHIjdBIyB8EPCJMORI8VIV6Az3bdDZngEXiuBgOEcKQVN7XLbskpJlxujIBh00UHGcWoQlJxU0IQrUc9UyO3Q39CjzGsFzGp_D9bzQs1LH8mvovBBrD4i2YMM9o-Ht4DlnEhBiRrmG2qTMZ8-Php5HgNie9ywDK_vQtOAtGe3_npM5syqfkOKyZOWTmozrwqQm0j6jtulUmrGhTzRbKUzHmQt-0dzbxB68vEOZnkbKwXWWP1VEX2O0Ou6ZupZJNMXZNPwx7e6jb1tTYm53qmwM2Sg7WNG0rsXCdkmova7XfHWspm7Lfydqao6qNta1SrdhLnLTzpfe3qQzxUjiiBbtjVVWHwusePd0vRzIsqtfrymNqex772CXeS79cSOyld8T0T4kde-zeMx56t9XSJu6B4WMOn8Cj5ntLCXT84pmKJfpuZL7mW6LOQp0vjselL02YhDqnKPfXyzWGbRzOOXkS6rzlf-vUnwAAAP__h1BEUA">