forked from OSchip/llvm-project
[MLIR] [Python] Allow 'operation.parent' to return 'None'
This is more Pythonic and better matches the C++ and C APIs. Reviewed By: stellaraccident Differential Revision: https://reviews.llvm.org/D108183
This commit is contained in:
parent
198e6771e2
commit
1689dade42
|
@ -868,22 +868,23 @@ py::object PyOperationBase::getAsm(bool binary,
|
|||
return fileObject.attr("getvalue")();
|
||||
}
|
||||
|
||||
PyOperationRef PyOperation::getParentOperation() {
|
||||
llvm::Optional<PyOperationRef> PyOperation::getParentOperation() {
|
||||
checkValid();
|
||||
if (!isAttached())
|
||||
throw SetPyError(PyExc_ValueError, "Detached operations have no parent");
|
||||
MlirOperation operation = mlirOperationGetParentOperation(get());
|
||||
if (mlirOperationIsNull(operation))
|
||||
throw SetPyError(PyExc_ValueError, "Operation has no parent.");
|
||||
return {};
|
||||
return PyOperation::forOperation(getContext(), operation);
|
||||
}
|
||||
|
||||
PyBlock PyOperation::getBlock() {
|
||||
checkValid();
|
||||
PyOperationRef parentOperation = getParentOperation();
|
||||
llvm::Optional<PyOperationRef> parentOperation = getParentOperation();
|
||||
MlirBlock block = mlirOperationGetBlock(get());
|
||||
assert(!mlirBlockIsNull(block) && "Attached operation has null parent");
|
||||
return PyBlock{std::move(parentOperation), block};
|
||||
assert(parentOperation && "Operation has no parent");
|
||||
return PyBlock{std::move(*parentOperation), block};
|
||||
}
|
||||
|
||||
py::object PyOperation::getCapsule() {
|
||||
|
@ -2121,8 +2122,11 @@ void mlir::python::populateIRCore(py::module &m) {
|
|||
py::arg("loc") = py::none(), py::arg("ip") = py::none(),
|
||||
kOperationCreateDocstring)
|
||||
.def_property_readonly("parent",
|
||||
[](PyOperation &self) {
|
||||
return self.getParentOperation().getObject();
|
||||
[](PyOperation &self) -> py::object {
|
||||
auto parent = self.getParentOperation();
|
||||
if (parent)
|
||||
return parent->getObject();
|
||||
return py::none();
|
||||
})
|
||||
.def("erase", &PyOperation::erase)
|
||||
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "mlir-c/IR.h"
|
||||
#include "mlir-c/IntegerSet.h"
|
||||
#include "llvm/ADT/DenseMap.h"
|
||||
#include "llvm/ADT/Optional.h"
|
||||
|
||||
namespace mlir {
|
||||
namespace python {
|
||||
|
@ -452,7 +453,7 @@ public:
|
|||
|
||||
/// Gets the parent operation or raises an exception if the operation has
|
||||
/// no parent.
|
||||
PyOperationRef getParentOperation();
|
||||
llvm::Optional<PyOperationRef> getParentOperation();
|
||||
|
||||
/// Gets a capsule wrapping the void* within the MlirOperation.
|
||||
pybind11::object getCapsule();
|
||||
|
|
Loading…
Reference in New Issue