-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
[mlir] Quality of life improvements to python API types. #66723
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Moves several orphaned methods from Operation/OpView -> _OperationBase so that both hierarchies share them (whether unknown or known to ODS). * Adds typing information for missing `MLIRError` exception. * Adds `DiagnosticInfo` typing. * Adds `DenseResourceElementsAttr` typing that was missing.
@llvm/pr-subscribers-mlir Changes
Full diff: https://github.com/llvm/llvm-project/pull/66723.diff 3 Files Affected:
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index b06937bc285e206..48a65278cc3e6f2 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -2768,6 +2768,24 @@ void mlir::python::populateIRCore(py::module &m) {
return PyOpAttributeMap(
self.getOperation().getRef());
})
+ .def_property_readonly(
+ "context",
+ [](PyOperationBase &self) {
+ PyOperation &concreteOperation = self.getOperation();
+ concreteOperation.checkValid();
+ return concreteOperation.getContext().getObject();
+ },
+ "Context that owns the Operation")
+ .def_property_readonly("name",
+ [](PyOperationBase &self) {
+ auto &concreteOperation = self.getOperation();
+ concreteOperation.checkValid();
+ MlirOperation operation =
+ concreteOperation.get();
+ MlirStringRef name = mlirIdentifierStr(
+ mlirOperationGetName(operation));
+ return py::str(name.data, name.length);
+ })
.def_property_readonly("operands",
[](PyOperationBase &self) {
return PyOpOperandList(
@@ -2813,6 +2831,14 @@ void mlir::python::populateIRCore(py::module &m) {
},
"Returns the source location the operation was defined or derived "
"from.")
+ .def_property_readonly("parent",
+ [](PyOperationBase &self) -> py::object {
+ auto parent =
+ self.getOperation().getParentOperation();
+ if (parent)
+ return parent->getObject();
+ return py::none();
+ })
.def(
"__str__",
[](PyOperationBase &self) {
@@ -2855,6 +2881,12 @@ void mlir::python::populateIRCore(py::module &m) {
.def("move_before", &PyOperationBase::moveBefore, py::arg("other"),
"Puts self immediately before the other operation in its parent "
"block.")
+ .def(
+ "clone",
+ [](PyOperationBase &self, py::object ip) {
+ return self.getOperation().clone(ip);
+ },
+ py::arg("ip") = py::none())
.def(
"detach_from_parent",
[](PyOperationBase &self) {
@@ -2866,7 +2898,8 @@ void mlir::python::populateIRCore(py::module &m) {
operation.detachFromParent();
return operation.createOpView();
},
- "Detaches the operation from its parent block.");
+ "Detaches the operation from its parent block.")
+ .def("erase", [](PyOperationBase &self) { self.getOperation().erase(); });
py::class_<PyOperation, PyOperationBase>(m, "Operation", py::module_local())
.def_static("create", &PyOperation::create, py::arg("name"),
@@ -2887,45 +2920,17 @@ void mlir::python::populateIRCore(py::module &m) {
py::arg("context") = py::none(),
"Parses an operation. Supports both text assembly format and binary "
"bytecode format.")
- .def_property_readonly("parent",
- [](PyOperation &self) -> py::object {
- auto parent = self.getParentOperation();
- if (parent)
- return parent->getObject();
- return py::none();
- })
- .def("erase", &PyOperation::erase)
- .def("clone", &PyOperation::clone, py::arg("ip") = py::none())
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
&PyOperation::getCapsule)
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyOperation::createFromCapsule)
- .def_property_readonly("name",
- [](PyOperation &self) {
- self.checkValid();
- MlirOperation operation = self.get();
- MlirStringRef name = mlirIdentifierStr(
- mlirOperationGetName(operation));
- return py::str(name.data, name.length);
- })
- .def_property_readonly(
- "context",
- [](PyOperation &self) {
- self.checkValid();
- return self.getContext().getObject();
- },
- "Context that owns the Operation")
+ .def_property_readonly("operation", [](py::object self) { return self; })
.def_property_readonly("opview", &PyOperation::createOpView);
auto opViewClass =
py::class_<PyOpView, PyOperationBase>(m, "OpView", py::module_local())
.def(py::init<py::object>(), py::arg("operation"))
.def_property_readonly("operation", &PyOpView::getOperationObject)
- .def_property_readonly(
- "context",
- [](PyOpView &self) {
- return self.getOperation().getContext().getObject();
- },
- "Context that owns the Operation")
+ .def_property_readonly("opview", [](py::object self) { return self; })
.def("__str__", [](PyOpView &self) {
return py::str(self.getOperationObject());
});
diff --git a/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi b/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
index 23f4687d0394d9e..e8f4440d216eeb4 100644
--- a/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
+++ b/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
@@ -43,11 +43,13 @@ __all__ = [
"DenseElementsAttr",
"DenseFPElementsAttr",
"DenseIntElementsAttr",
+ "DenseResourceElementsAttr",
"Dialect",
"DialectDescriptor",
"Dialects",
"Diagnostic",
"DiagnosticHandler",
+ "DiagnosticInfo",
"DiagnosticSeverity",
"DictAttr",
"Float8E4M3FNType",
@@ -74,6 +76,7 @@ __all__ = [
"Location",
"MemRefType",
"Module",
+ "MLIRError",
"NamedAttribute",
"NoneType",
"OpaqueType",
@@ -123,10 +126,16 @@ class _OperationBase:
@property
def attributes(self) -> OpAttributeMap: ...
@property
+ def context(self) -> Context: ...
+ @property
def location(self) -> Location: ...
@property
+ def name(self) -> str: ...
+ @property
def operands(self) -> OpOperandList: ...
@property
+ @property
+ def parent(self) -> Optional[_OperationBase]: ...
def regions(self) -> RegionSequence: ...
@property
def result(self) -> OpResult: ...
@@ -530,6 +539,10 @@ class DenseIntElementsAttr(DenseElementsAttr):
@property
def type(self) -> Type: ...
+class DenseResourceElementsAttr(Attribute):
+ @staticmethod
+ def get_from_buffer(array: Any, name: str, type: Type, alignment: Optional[int] = None, is_mutable: bool = False, context: Optional[Context] = None) -> None: ...
+
class Dialect:
def __init__(self, descriptor: DialectDescriptor) -> None: ...
@property
@@ -563,6 +576,17 @@ class DiagnosticHandler:
def __enter__(self) -> DiagnosticHandler: ...
def __exit__(self, arg0: object, arg1: object, arg2: object) -> None: ...
+class DiagnosticInfo:
+ def __init__(self, diag: Diagnostic) -> None: ...
+ @property
+ def severity(self) -> "DiagnosticSeverity": ...
+ @property
+ def location(self) -> "Location": ...
+ @property
+ def message(self) -> str: ...
+ @property
+ def notes(self) -> Sequence["DiagnosticInfo"]: ...
+
class DiagnosticSeverity:
ERROR: DiagnosticSeverity
WARNING: DiagnosticSeverity
@@ -871,6 +895,9 @@ class Module:
@property
def operation(self) -> Operation: ...
+class MLIRError(Exception):
+ def __init__(self, message: str, error_diagnostics: List[DiagnosticInfo]) -> None: ...
+
class NamedAttribute:
@property
def attr(self) -> Attribute: ...
@@ -950,9 +977,9 @@ class OpView(_OperationBase):
loc: Optional[Location] = None,
ip: Optional[InsertionPoint] = None) -> _TOperation: ...
@property
- def context(self) -> Context: ...
- @property
def operation(self) -> Operation: ...
+ @property
+ def opview(self) -> "OpView": ...
class Operation(_OperationBase):
def _CAPICreate(self) -> object: ...
@@ -968,13 +995,9 @@ class Operation(_OperationBase):
@property
def _CAPIPtr(self) -> object: ...
@property
- def context(self) -> Context: ...
- @property
- def name(self) -> str: ...
+ def operation(self) -> "Operation": ...
@property
def opview(self) -> OpView: ...
- @property
- def parent(self) -> Optional[_OperationBase]: ...
class OperationIterator:
def __iter__(self) -> OperationIterator: ...
diff --git a/mlir/python/mlir/_mlir_libs/_mlir/passmanager.pyi b/mlir/python/mlir/_mlir_libs/_mlir/passmanager.pyi
index 44d22255e3110dc..c072d5e0fb86f32 100644
--- a/mlir/python/mlir/_mlir_libs/_mlir/passmanager.pyi
+++ b/mlir/python/mlir/_mlir_libs/_mlir/passmanager.pyi
@@ -20,6 +20,6 @@ class PassManager:
def enable_verifier(self, enable: bool) -> None: ...
@staticmethod
def parse(pipeline: str, context: Optional[_ir.Context] = None) -> PassManager: ...
- def run(self, module: _ir.Module) -> None: ...
+ def run(self, module: _ir._OperationBase) -> None: ...
@property
def _CAPIPtr(self) -> object: ...
|
stellaraccident
added a commit
to nod-ai/SHARK-ModelDev
that referenced
this pull request
Sep 19, 2023
It will be some time before we can enforce this. I triaged some initial issues and sent llvm/llvm-project#66723 upstream to cover some mis-aligned things. We should triage again once that lands.
mikeurbach
approved these changes
Sep 19, 2023
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.
The different parts make sense to me, thanks!
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
* Moves several orphaned methods from Operation/OpView -> _OperationBase so that both hierarchies share them (whether unknown or known to ODS). * Adds typing information for missing `MLIRError` exception. * Adds `DiagnosticInfo` typing. * Adds `DenseResourceElementsAttr` typing that was missing.
This was referenced Sep 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
MLIRError
exception.DiagnosticInfo
typing.DenseResourceElementsAttr
typing that was missing.