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

Commit 201984b

Browse files
authored
[mlir][python,CAPI] expose Op::isBeforeInBlock (#150271)
1 parent 8457ce3 commit 201984b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

IRCore.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,14 @@ void PyOperationBase::moveBefore(PyOperationBase &other) {
14541454
operation.parentKeepAlive = otherOp.parentKeepAlive;
14551455
}
14561456

1457+
bool PyOperationBase::isBeforeInBlock(PyOperationBase &other) {
1458+
PyOperation &operation = getOperation();
1459+
PyOperation &otherOp = other.getOperation();
1460+
operation.checkValid();
1461+
otherOp.checkValid();
1462+
return mlirOperationIsBeforeInBlock(operation, otherOp);
1463+
}
1464+
14571465
bool PyOperationBase::verify() {
14581466
PyOperation &op = getOperation();
14591467
PyMlirContext::ErrorCapture errors(op.getContext());
@@ -3409,6 +3417,13 @@ void mlir::python::populateIRCore(nb::module_ &m) {
34093417
.def("move_before", &PyOperationBase::moveBefore, nb::arg("other"),
34103418
"Puts self immediately before the other operation in its parent "
34113419
"block.")
3420+
.def("is_before_in_block", &PyOperationBase::isBeforeInBlock,
3421+
nb::arg("other"),
3422+
"Given an operation 'other' that is within the same parent block, "
3423+
"return"
3424+
"whether the current operation is before 'other' in the operation "
3425+
"list"
3426+
"of the parent block.")
34123427
.def(
34133428
"clone",
34143429
[](PyOperationBase &self, nb::object ip) {

IRModule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,13 @@ class PyOperationBase {
624624
void moveAfter(PyOperationBase &other);
625625
void moveBefore(PyOperationBase &other);
626626

627+
/// Given an operation 'other' that is within the same parent block, return
628+
/// whether the current operation is before 'other' in the operation list
629+
/// of the parent block.
630+
/// Note: This function has an average complexity of O(1), but worst case may
631+
/// take O(N) where N is the number of operations within the parent block.
632+
bool isBeforeInBlock(PyOperationBase &other);
633+
627634
/// Verify the operation. Throws `MLIRError` if verification fails, and
628635
/// returns `true` otherwise.
629636
bool verify();

0 commit comments

Comments
 (0)