[Mlir-commits] [mlir] [mlir][python] Expose `PyInsertionPoint`'s reference operation (PR #69082)

Tomás Longeri llvmlistbot at llvm.org
Sat Oct 14 16:50:57 PDT 2023


https://github.com/tlongeri created https://github.com/llvm/llvm-project/pull/69082

 The reason I want this is that I am writing my own Python bindings and would like to use the insertion point from `PyThreadContextEntry::getDefaultInsertionPoint()`  to call C++ functions that take an `OpBuilder` (I don't need to expose it in Python but it also seems appropriate). AFAICT, there is currently no way to translate a `PyInsertionPoint` into an `OpBuilder` because the operation is inaccessible.

>From c274afbe10a1c5d4e63ceef1df2f7b105b44885a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Longeri?= <tlongeri at google.com>
Date: Fri, 13 Oct 2023 23:15:43 +0000
Subject: [PATCH] [mlir][python] Expose PyInsertionPoint's reference operation

---
 mlir/lib/Bindings/Python/IRCore.cpp | 8 +++++++-
 mlir/lib/Bindings/Python/IRModule.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index c8373e06f0db776..4d86ab2d9583834 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -3207,7 +3207,13 @@ void mlir::python::populateIRCore(py::module &m) {
            "Inserts an operation.")
       .def_property_readonly(
           "block", [](PyInsertionPoint &self) { return self.getBlock(); },
-          "Returns the block that this InsertionPoint points to.");
+          "Returns the block that this InsertionPoint points to.")
+      .def_property_readonly(
+          "ref_operation",
+          [](PyInsertionPoint &self) { return self.getRefOperation(); },
+          "The reference operation before which new operations are "
+          "inserted, or None if the insertion point is at the end of "
+          "the block");
 
   //----------------------------------------------------------------------------
   // Mapping of PyAttribute.
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 3ca7dd851961a46..c5412e735dddcb5 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -833,6 +833,7 @@ class PyInsertionPoint {
                    const pybind11::object &excTb);
 
   PyBlock &getBlock() { return block; }
+  std::optional<PyOperationRef> &getRefOperation() { return refOperation; }
 
 private:
   // Trampoline constructor that avoids null initializing members while



More information about the Mlir-commits mailing list