aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/IRModule.h
diff options
context:
space:
mode:
authorAlex Zinenko <zinenko@google.com>2023-12-21 10:01:44 +0000
committerAlex Zinenko <zinenko@google.com>2023-12-21 10:06:44 +0000
commit78bd124649ece163d3a26b33608bdbe518d8ff76 (patch)
treeedb119370273c3819d16e8069e58f7b7d0ffd1cf /mlir/lib/Bindings/Python/IRModule.h
parentcb3a8934365c11ab23c918b44985f5a2f287acb1 (diff)
downloadllvm-78bd124649ece163d3a26b33608bdbe518d8ff76.zip
llvm-78bd124649ece163d3a26b33608bdbe518d8ff76.tar.gz
llvm-78bd124649ece163d3a26b33608bdbe518d8ff76.tar.bz2
Revert "[mlir][python] Make the Context/Operation capsule creation methods work as documented. (#76010)"
This reverts commit bbc29768683b394b34600347f46be2b8245ddb30. 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 ```
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r--mlir/lib/Bindings/Python/IRModule.h19
1 files changed, 1 insertions, 18 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 04164b7..79b7e0c 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -176,19 +176,8 @@ public:
static PyMlirContext *createNewContextForInit();
/// Returns a context reference for the singleton PyMlirContext wrapper for
- /// the given context. It is only valid to call this on an MlirContext that
- /// is already owned by the Python bindings. Typically this will be because
- /// it came in some fashion from createNewContextForInit(). However, it
- /// is also possible to explicitly transfer ownership of an existing
- /// MlirContext to the Python bindings via stealExternalContext().
+ /// the given context.
static PyMlirContextRef forContext(MlirContext context);
-
- /// Explicitly takes ownership of an MlirContext that must not already be
- /// known to the Python bindings. Once done, the life-cycle of the context
- /// will be controlled by the Python bindings, and it will be destroyed
- /// when the reference count goes to zero.
- static PyMlirContextRef stealExternalContext(MlirContext context);
-
~PyMlirContext();
/// Accesses the underlying MlirContext.
@@ -617,12 +606,6 @@ public:
forOperation(PyMlirContextRef contextRef, MlirOperation operation,
pybind11::object parentKeepAlive = pybind11::object());
- /// Explicitly takes ownership of an operation that must not already be known
- /// to the Python bindings. Once done, the life-cycle of the operation
- /// will be controlled by the Python bindings.
- static PyOperationRef stealExternalOperation(PyMlirContextRef contextRef,
- MlirOperation operation);
-
/// Creates a detached operation. The operation must not be associated with
/// any existing live operation.
static PyOperationRef