Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][python] Expose PyInsertionPoint's reference operation #69082

Merged
merged 1 commit into from
Oct 18, 2023

Conversation

tlongeri
Copy link
Contributor

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.

@llvmbot llvmbot added the mlir label Oct 14, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2023

@llvm/pr-subscribers-mlir

Author: Tomás Longeri (tlongeri)

Changes

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

Copy link
Member

@ftynse ftynse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlongeri tlongeri force-pushed the expose-pyinsertionpoint-operation branch 2 times, most recently from 5a69438 to 1955ff4 Compare October 16, 2023 05:29
@llvmbot llvmbot added the mlir:python MLIR Python bindings label Oct 16, 2023
@tlongeri
Copy link
Contributor Author

Consider updating https://github.com/llvm/llvm-project/blob/main/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi#L743 as well to provide type hints.

Done!

Also fixed the lambda in def_property_readonly (it was broken and I hadn't tested it properly)

@tlongeri tlongeri force-pushed the expose-pyinsertionpoint-operation branch 2 times, most recently from 02e9985 to 43820c4 Compare October 18, 2023 10:15
Updates the insertion_point.py test with asserts for existing testcases
that check the reference operation and the block.
@tlongeri tlongeri force-pushed the expose-pyinsertionpoint-operation branch from 43820c4 to f3b7c75 Compare October 18, 2023 10:16
@tlongeri
Copy link
Contributor Author

tlongeri commented Oct 18, 2023

Updated the insertion_point.py test as well. It now has asserts checking block and ref_operation on the previously existing checks. I was going to add separate testcases for it but I realized the MLIR would've just been identical to the already existing ones.

@ftynse ftynse merged commit 5a600c2 into llvm:main Oct 18, 2023
2 checks passed
@tlongeri tlongeri deleted the expose-pyinsertionpoint-operation branch October 23, 2023 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:python MLIR Python bindings mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants