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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Oct 14 16:51:55 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Tomás Longeri (tlongeri)

<details>
<summary>Changes</summary>

 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.

---
Full diff: https://github.com/llvm/llvm-project/pull/69082.diff


2 Files Affected:

- (modified) mlir/lib/Bindings/Python/IRCore.cpp (+7-1) 
- (modified) mlir/lib/Bindings/Python/IRModule.h (+1) 


``````````diff
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

``````````

</details>


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


More information about the Mlir-commits mailing list