llvm-capstone/mlir/lib/Bindings/Python
Alex Zinenko 78bd124649 Revert "[mlir][python] Make the Context/Operation capsule creation methods work as documented. (#76010)"
This reverts commit bbc2976868.

This change seems to be at odds with the non-owning part semantics of
MlirOperation in C API. Since downstream clients can only take and
return MlirOperation, it does not sound correct to force all returns of
MlirOperation transfer ownership. Specifically, this makes it impossible
for downstreams to implement IR-traversing functions that, e.g., look at
neighbors of an operation.

The following patch triggers the exception, and there does not seem to
be an alternative way for a downstream binding writer to express this:

```
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 39757dfad5be..2ce640674245 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -3071,6 +3071,11 @@ void mlir::python::populateIRCore(py::module &m) {
                   py::arg("successors") = py::none(), py::arg("regions") = 0,
                   py::arg("loc") = py::none(), py::arg("ip") = py::none(),
                   py::arg("infer_type") = false, kOperationCreateDocstring)
+      .def("_get_first_in_block", [](PyOperation &self) -> MlirOperation {
+        MlirBlock block = mlirOperationGetBlock(self.get());
+        MlirOperation first = mlirBlockGetFirstOperation(block);
+        return first;
+      })
       .def_static(
           "parse",
           [](const std::string &sourceStr, const std::string &sourceName,
diff --git a/mlir/test/python/ir/operation.py b/mlir/test/python/ir/operation.py
index f59b1a26ba48..6b12b8da5c24 100644
--- a/mlir/test/python/ir/operation.py
+++ b/mlir/test/python/ir/operation.py
@@ -24,6 +24,25 @@ def expect_index_error(callback):
     except IndexError:
         pass

+@run
+def testCustomBind():
+    ctx = Context()
+    ctx.allow_unregistered_dialects = True
+    module = Module.parse(
+        r"""
+    func.func @f1(%arg0: i32) -> i32 {
+      %1 = "custom.addi"(%arg0, %arg0) : (i32, i32) -> i32
+      return %1 : i32
+    }
+  """,
+        ctx,
+    )
+    add = module.body.operations[0].regions[0].blocks[0].operations[0]
+    op = add.operation
+    # This will get a reference to itself.
+    f1 = op._get_first_in_block()
+
+

 # Verify iterator based traversal of the op/region/block hierarchy.
 # CHECK-LABEL: TEST: testTraverseOpRegionBlockIterators
```
2023-12-21 10:06:44 +00:00
..
AsyncPasses.cpp
DialectLinalg.cpp
DialectPDL.cpp
DialectQuant.cpp
DialectSparseTensor.cpp [mlir][sparse] rename DimLevelType to LevelType (#73561) 2023-11-27 14:27:52 -08:00
DialectTransform.cpp [MLIR][transform][python] add sugared python abstractions for transform dialect (#75073) 2023-12-15 13:04:43 +01:00
ExecutionEngineModule.cpp [mlir][Python] Apply ClangTidy findings. 2023-12-11 09:43:08 +00:00
Globals.h
GPUPasses.cpp
IRAffine.cpp
IRAttributes.cpp Apply clang-tidy fixes for performance-unnecessary-value-param in IRAttributes.cpp (NFC) 2023-11-18 15:38:21 -08:00
IRCore.cpp Revert "[mlir][python] Make the Context/Operation capsule creation methods work as documented. (#76010)" 2023-12-21 10:06:44 +00:00
IRInterfaces.cpp [mlir][Python] Apply ClangTidy findings. 2023-12-11 09:43:08 +00:00
IRModule.cpp [mlir][python] remove eager loading of dialect module (for type and value casting) (#72338) 2023-11-20 19:54:55 -06:00
IRModule.h Revert "[mlir][python] Make the Context/Operation capsule creation methods work as documented. (#76010)" 2023-12-21 10:06:44 +00:00
IRTypes.cpp [mlir][python] add type wrappers (#71218) 2023-11-27 15:58:00 -06:00
LinalgPasses.cpp
MainModule.cpp
Pass.cpp [mlir][Python] Apply ClangTidy findings. 2023-12-11 09:43:08 +00:00
Pass.h
PybindUtils.h
RegisterEverything.cpp
SparseTensorPasses.cpp