-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][Python] make Sliceable inherit from Sequence #170551
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 | ||
|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |||
| #include "llvm/Support/raw_ostream.h" | ||||
|
|
||||
| #include <string> | ||||
| #include <typeinfo> | ||||
| #include <variant> | ||||
|
|
||||
| template <> | ||||
|
|
@@ -344,7 +345,16 @@ class Sliceable { | |||
|
|
||||
| /// Binds the indexing and length methods in the Python class. | ||||
| static void bind(nanobind::module_ &m) { | ||||
| auto clazz = nanobind::class_<Derived>(m, Derived::pyClassName) | ||||
| const std::type_info &elemTy = typeid(ElementTy); | ||||
| PyObject *elemTyInfo = nanobind::detail::nb_type_lookup(&elemTy); | ||||
| assert(elemTyInfo && | ||||
| "expected nb_type_lookup to succeed for Sliceable elemTy"); | ||||
| nanobind::handle elemTyName = nanobind::detail::nb_type_name(elemTyInfo); | ||||
| std::string sig = std::string("class ") + Derived::pyClassName + | ||||
| "(collections.abc.Sequence[" + | ||||
| nanobind::cast<std::string>(elemTyName) + "])"; | ||||
|
Member
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. Don't we have
Contributor
Author
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.
No:
You realize that all of nanobind/pybind hinges on https://github.com/search?q=repo%3Awjakob%2Fnanobind%20typeid&type=code Specifically the core impl (the cpp to python mapping) uses Ie that's why RTTI is on for the bindings. So we can be sure it works in all the places the bindings work 🙂. |
||||
| auto clazz = nanobind::class_<Derived>(m, Derived::pyClassName, | ||||
| nanobind::sig(sig.c_str())) | ||||
| .def("__add__", &Sliceable::dunderAdd); | ||||
| Derived::bindDerived(clazz); | ||||
|
|
||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,10 @@ def testTraverseOpRegionBlockIterators(): | |
| ) | ||
| op = module.operation | ||
| assert op.context is ctx | ||
| # Note, __nb_signature__ stores the fully-qualified signature - the actual type stub emitted is | ||
| # class RegionSequence(Sequence[Region]) | ||
| # CHECK: class RegionSequence(collections.abc.Sequence[mlir._mlir_libs._mlir.ir.Region]) | ||
| print(RegionSequence.__nb_signature__) | ||
|
Contributor
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. Oh, that's a nice way to test this! |
||
| # Get the block using iterators off of the named collections. | ||
| regions = list(op.regions[:]) | ||
| blocks = list(regions[0].blocks) | ||
|
|
||
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.
There's no way for this to fail - the
ElementTyneeds to be registered beforeSliceable[ElementTy]can be registered...