Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 8411f12

Browse files
authored
[MLIR][Python] bind InsertionPointAfter (#157156)
1 parent 7974cad commit 8411f12

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ PyOpView::PyOpView(const nb::object &operationObject)
20192019
// PyInsertionPoint.
20202020
//------------------------------------------------------------------------------
20212021

2022-
PyInsertionPoint::PyInsertionPoint(PyBlock &block) : block(block) {}
2022+
PyInsertionPoint::PyInsertionPoint(const PyBlock &block) : block(block) {}
20232023

20242024
PyInsertionPoint::PyInsertionPoint(PyOperationBase &beforeOperationBase)
20252025
: refOperation(beforeOperationBase.getOperation().getRef()),
@@ -2073,6 +2073,17 @@ PyInsertionPoint PyInsertionPoint::atBlockTerminator(PyBlock &block) {
20732073
return PyInsertionPoint{block, std::move(terminatorOpRef)};
20742074
}
20752075

2076+
PyInsertionPoint PyInsertionPoint::after(PyOperationBase &op) {
2077+
PyOperation &operation = op.getOperation();
2078+
PyBlock block = operation.getBlock();
2079+
MlirOperation nextOperation = mlirOperationGetNextInBlock(operation);
2080+
if (mlirOperationIsNull(nextOperation))
2081+
return PyInsertionPoint(block);
2082+
PyOperationRef nextOpRef = PyOperation::forOperation(
2083+
block.getParentOperation()->getContext(), nextOperation);
2084+
return PyInsertionPoint{block, std::move(nextOpRef)};
2085+
}
2086+
20762087
nb::object PyInsertionPoint::contextEnter(nb::object insertPoint) {
20772088
return PyThreadContextEntry::pushInsertionPoint(insertPoint);
20782089
}
@@ -3861,6 +3872,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
38613872
nb::arg("block"), "Inserts at the beginning of the block.")
38623873
.def_static("at_block_terminator", &PyInsertionPoint::atBlockTerminator,
38633874
nb::arg("block"), "Inserts before the block terminator.")
3875+
.def_static("after", &PyInsertionPoint::after, nb::arg("operation"),
3876+
"Inserts after the operation.")
38643877
.def("insert", &PyInsertionPoint::insert, nb::arg("operation"),
38653878
"Inserts an operation.")
38663879
.def_prop_ro(

mlir/lib/Bindings/Python/IRModule.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,14 +821,17 @@ class PyInsertionPoint {
821821
public:
822822
/// Creates an insertion point positioned after the last operation in the
823823
/// block, but still inside the block.
824-
PyInsertionPoint(PyBlock &block);
824+
PyInsertionPoint(const PyBlock &block);
825825
/// Creates an insertion point positioned before a reference operation.
826826
PyInsertionPoint(PyOperationBase &beforeOperationBase);
827827

828828
/// Shortcut to create an insertion point at the beginning of the block.
829829
static PyInsertionPoint atBlockBegin(PyBlock &block);
830830
/// Shortcut to create an insertion point before the block terminator.
831831
static PyInsertionPoint atBlockTerminator(PyBlock &block);
832+
/// Shortcut to create an insertion point to the node after the specified
833+
/// operation.
834+
static PyInsertionPoint after(PyOperationBase &op);
832835

833836
/// Inserts an operation.
834837
void insert(PyOperationBase &operationBase);

0 commit comments

Comments
 (0)