@@ -2730,14 +2730,59 @@ class PyOpAttributeMap {
27302730 operation->get (), toMlirStringRef (name)));
27312731 }
27322732
2733+ template <typename F>
2734+ auto forEachAttr (F fn) {
2735+ intptr_t n = mlirOperationGetNumAttributes (operation->get ());
2736+ for (intptr_t i = 0 ; i < n; ++i) {
2737+ MlirNamedAttribute na = mlirOperationGetAttribute (operation->get (), i);
2738+ MlirStringRef name = mlirIdentifierStr (na.name );
2739+ fn (name, na.attribute );
2740+ }
2741+ }
2742+
27332743 static void bind (nb::module_ &m) {
27342744 nb::class_<PyOpAttributeMap>(m, " OpAttributeMap" )
27352745 .def (" __contains__" , &PyOpAttributeMap::dunderContains)
27362746 .def (" __len__" , &PyOpAttributeMap::dunderLen)
27372747 .def (" __getitem__" , &PyOpAttributeMap::dunderGetItemNamed)
27382748 .def (" __getitem__" , &PyOpAttributeMap::dunderGetItemIndexed)
27392749 .def (" __setitem__" , &PyOpAttributeMap::dunderSetItem)
2740- .def (" __delitem__" , &PyOpAttributeMap::dunderDelItem);
2750+ .def (" __delitem__" , &PyOpAttributeMap::dunderDelItem)
2751+ .def (" __iter__" ,
2752+ [](PyOpAttributeMap &self) {
2753+ nb::list keys;
2754+ self.forEachAttr ([&](MlirStringRef name, MlirAttribute) {
2755+ keys.append (nb::str (name.data , name.length ));
2756+ });
2757+ return nb::iter (keys);
2758+ })
2759+ .def (" keys" ,
2760+ [](PyOpAttributeMap &self) {
2761+ nb::list out;
2762+ self.forEachAttr ([&](MlirStringRef name, MlirAttribute) {
2763+ out.append (nb::str (name.data , name.length ));
2764+ });
2765+ return out;
2766+ })
2767+ .def (" values" ,
2768+ [](PyOpAttributeMap &self) {
2769+ nb::list out;
2770+ self.forEachAttr ([&](MlirStringRef, MlirAttribute attr) {
2771+ out.append (PyAttribute (self.operation ->getContext (), attr)
2772+ .maybeDownCast ());
2773+ });
2774+ return out;
2775+ })
2776+ .def (" items" , [](PyOpAttributeMap &self) {
2777+ nb::list out;
2778+ self.forEachAttr ([&](MlirStringRef name, MlirAttribute attr) {
2779+ out.append (
2780+ nb::make_tuple (nb::str (name.data , name.length ),
2781+ PyAttribute (self.operation ->getContext (), attr)
2782+ .maybeDownCast ()));
2783+ });
2784+ return out;
2785+ });
27412786 }
27422787
27432788private:
0 commit comments