-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Conversation
@llvm/pr-subscribers-mlir Author: Tomás Longeri (tlongeri) ChangesThe reason I want this is that I am writing my own Python bindings and would like to use the insertion point from Full diff: https://github.com/llvm/llvm-project/pull/69082.diff 2 Files Affected:
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
5a69438
to
1955ff4
Compare
Done! Also fixed the lambda in |
02e9985
to
43820c4
Compare
Updates the insertion_point.py test with asserts for existing testcases that check the reference operation and the block.
43820c4
to
f3b7c75
Compare
Updated the insertion_point.py test as well. It now has asserts checking |
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 anOpBuilder
(I don't need to expose it in Python but it also seems appropriate). AFAICT, there is currently no way to translate aPyInsertionPoint
into anOpBuilder
because the operation is inaccessible.