diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -295,6 +295,11 @@ public: PyRegionList(PyOperationRef operation) : operation(std::move(operation)) {} + PyRegionIterator dunderIter() { + operation->checkValid(); + return PyRegionIterator(operation); + } + intptr_t dunderLen() { operation->checkValid(); return mlirOperationGetNumRegions(operation->get()); @@ -312,6 +317,7 @@ static void bind(py::module &m) { py::class_(m, "RegionSequence", py::module_local()) .def("__len__", &PyRegionList::dunderLen) + .def("__iter__", &PyRegionList::dunderIter) .def("__getitem__", &PyRegionList::dunderGetItem); } diff --git a/mlir/test/python/ir/operation.py b/mlir/test/python/ir/operation.py --- a/mlir/test/python/ir/operation.py +++ b/mlir/test/python/ir/operation.py @@ -48,11 +48,9 @@ # CHECK: .verify = True print(f".verify = {module.operation.verify()}") - # Get the regions and blocks from the default collections. - default_regions = list(op.regions) - default_blocks = list(default_regions[0]) + # Get the blocks from the default collection. + default_blocks = list(regions[0]) # They should compare equal regardless of how obtained. - assert default_regions == regions assert default_blocks == blocks # Should be able to get the operations from either the named collection @@ -79,6 +77,13 @@ # CHECK: OP 1: func.return walk_operations("", op) + # CHECK: Region iter: