-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][python] Add walk
method to PyOperationBase
#87962
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -674,6 +674,7 @@ void PyMlirContext::clearOperationsInside(PyOperationBase &op) { | |
data->rootOp.getOperation().getContext()->clearOperation(op); | ||
else | ||
data->rootSeen = true; | ||
return MlirWalkResult::MlirWalkResultAdvance; | ||
}; | ||
mlirOperationWalk(op.getOperation(), invalidatingCallback, | ||
static_cast<void *>(&data), MlirWalkPreOrder); | ||
|
@@ -1249,6 +1250,21 @@ void PyOperationBase::writeBytecode(const py::object &fileObject, | |
.str()); | ||
} | ||
|
||
void PyOperationBase::walk( | ||
std::function<MlirWalkResult(MlirOperation)> callback, | ||
MlirWalkOrder walkOrder) { | ||
PyOperation &operation = getOperation(); | ||
operation.checkValid(); | ||
MlirOperationWalkCallback walkCallback = [](MlirOperation op, | ||
void *userData) { | ||
auto *fn = | ||
static_cast<std::function<MlirWalkResult(MlirOperation)> *>(userData); | ||
return (*fn)(op); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: can you actually check the result here to see if it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it works since the function is type casted to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. variant is overkill for sure but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My two cents is if we can do this in a way that doesn't add much overhead it would be nice. But I'm also happy to see this go in with the user callback required to explicitly return a WalkResult... "explicit is better than implicit". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm too traumatized by the C++ version of omitting the return from a function and getting a random segfault, so I'm on the side of always having an explicit return. But not objecting strongly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I am similarly traumatized but in Python-land I think it's fairly well-known that a function with no return statement actually returns |
||
}; | ||
|
||
mlirOperationWalk(operation, walkCallback, &callback, walkOrder); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like the c++ walk to evolve to support two callbacks to have both pre/post order visitation in the same walk. This isn't a blocker for your change, but I'll need to break the C API, so just a heads up :) |
||
py::object PyOperationBase::getAsm(bool binary, | ||
std::optional<int64_t> largeElementsLimit, | ||
bool enableDebugInfo, bool prettyDebugInfo, | ||
|
@@ -2511,6 +2527,15 @@ void mlir::python::populateIRCore(py::module &m) { | |
.value("NOTE", MlirDiagnosticNote) | ||
.value("REMARK", MlirDiagnosticRemark); | ||
|
||
py::enum_<MlirWalkOrder>(m, "WalkOrder", py::module_local()) | ||
.value("PRE_ORDER", MlirWalkPreOrder) | ||
.value("POST_ORDER", MlirWalkPostOrder); | ||
|
||
py::enum_<MlirWalkResult>(m, "WalkResult", py::module_local()) | ||
.value("ADVANCE", MlirWalkResultAdvance) | ||
.value("INTERRUPT", MlirWalkResultInterrupt) | ||
.value("SKIP", MlirWalkResultSkip); | ||
|
||
//---------------------------------------------------------------------------- | ||
// Mapping of Diagnostics. | ||
//---------------------------------------------------------------------------- | ||
|
@@ -2989,8 +3014,7 @@ void mlir::python::populateIRCore(py::module &m) { | |
py::arg("binary") = false, kOperationPrintStateDocstring) | ||
.def("print", | ||
py::overload_cast<std::optional<int64_t>, bool, bool, bool, bool, | ||
bool, py::object, bool>( | ||
&PyOperationBase::print), | ||
bool, py::object, bool>(&PyOperationBase::print), | ||
mikeurbach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Careful: Lots of arguments must match up with print method. | ||
py::arg("large_elements_limit") = py::none(), | ||
py::arg("enable_debug_info") = false, | ||
|
@@ -3038,7 +3062,9 @@ void mlir::python::populateIRCore(py::module &m) { | |
return operation.createOpView(); | ||
}, | ||
"Detaches the operation from its parent block.") | ||
.def("erase", [](PyOperationBase &self) { self.getOperation().erase(); }); | ||
.def("erase", [](PyOperationBase &self) { self.getOperation().erase(); }) | ||
.def("walk", &PyOperationBase::walk, py::arg("callback"), | ||
py::arg("walk_order") = MlirWalkPostOrder); | ||
|
||
py::class_<PyOperation, PyOperationBase>(m, "Operation", py::module_local()) | ||
.def_static("create", &PyOperation::create, py::arg("name"), | ||
|
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.
This was needed to implicitly convert python object to std::function.