diff options
Diffstat (limited to 'mlir/lib/Bindings/Python')
-rw-r--r-- | mlir/lib/Bindings/Python/IRAttributes.cpp | 13 | ||||
-rw-r--r-- | mlir/lib/Bindings/Python/IRCore.cpp | 15 | ||||
-rw-r--r-- | mlir/lib/Bindings/Python/IRModule.h | 7 |
3 files changed, 32 insertions, 3 deletions
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp index 8f79caf..db84ee1 100644 --- a/mlir/lib/Bindings/Python/IRAttributes.cpp +++ b/mlir/lib/Bindings/Python/IRAttributes.cpp @@ -16,8 +16,8 @@ #include "NanobindUtils.h" #include "mlir-c/BuiltinAttributes.h" #include "mlir-c/BuiltinTypes.h" -#include "mlir/Bindings/Python/NanobindAdaptors.h" #include "mlir/Bindings/Python/Nanobind.h" +#include "mlir/Bindings/Python/NanobindAdaptors.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/Support/raw_ostream.h" @@ -1428,6 +1428,12 @@ public: } }; +// Check if the python version is less than 3.13. Py_IsFinalizing is a part +// of stable ABI since 3.13 and before it was available as _Py_IsFinalizing. +#if PY_VERSION_HEX < 0x030d0000 +#define Py_IsFinalizing _Py_IsFinalizing +#endif + class PyDenseResourceElementsAttribute : public PyConcreteAttribute<PyDenseResourceElementsAttribute> { public: @@ -1474,8 +1480,9 @@ public: // The userData is a Py_buffer* that the deleter owns. auto deleter = [](void *userData, const void *data, size_t size, size_t align) { - if (!Py_IsInitialized()) - Py_Initialize(); + if (Py_IsFinalizing()) + return; + assert(Py_IsInitialized() && "expected interpreter to be initialized"); Py_buffer *ownedView = static_cast<Py_buffer *>(userData); nb::gil_scoped_acquire gil; PyBuffer_Release(ownedView); diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp index 7b790e9..5feed95 100644 --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -1454,6 +1454,14 @@ void PyOperationBase::moveBefore(PyOperationBase &other) { operation.parentKeepAlive = otherOp.parentKeepAlive; } +bool PyOperationBase::isBeforeInBlock(PyOperationBase &other) { + PyOperation &operation = getOperation(); + PyOperation &otherOp = other.getOperation(); + operation.checkValid(); + otherOp.checkValid(); + return mlirOperationIsBeforeInBlock(operation, otherOp); +} + bool PyOperationBase::verify() { PyOperation &op = getOperation(); PyMlirContext::ErrorCapture errors(op.getContext()); @@ -3409,6 +3417,13 @@ void mlir::python::populateIRCore(nb::module_ &m) { .def("move_before", &PyOperationBase::moveBefore, nb::arg("other"), "Puts self immediately before the other operation in its parent " "block.") + .def("is_before_in_block", &PyOperationBase::isBeforeInBlock, + nb::arg("other"), + "Given an operation 'other' that is within the same parent block, " + "return" + "whether the current operation is before 'other' in the operation " + "list" + "of the parent block.") .def( "clone", [](PyOperationBase &self, nb::object ip) { diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index 0fdd2d1..9c22dea 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -624,6 +624,13 @@ public: void moveAfter(PyOperationBase &other); void moveBefore(PyOperationBase &other); + /// Given an operation 'other' that is within the same parent block, return + /// whether the current operation is before 'other' in the operation list + /// of the parent block. + /// Note: This function has an average complexity of O(1), but worst case may + /// take O(N) where N is the number of operations within the parent block. + bool isBeforeInBlock(PyOperationBase &other); + /// Verify the operation. Throws `MLIRError` if verification fails, and /// returns `true` otherwise. bool verify(); |