Skip to content

Commit d0d26ee

Browse files
committed
[mlir][python] Hook up PyRegionList.__iter__ to PyRegionIterator
This fixes a -Wunused-member-function warning, at the moment `PyRegionIterator` is never constructed by anything (the only use was removed in D111697), and iterating over region lists is just falling back to a generic python iterator object. Reviewed By: stellaraccident Differential Revision: https://reviews.llvm.org/D150244
1 parent 693a1b7 commit d0d26ee

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ class PyRegionList {
295295
public:
296296
PyRegionList(PyOperationRef operation) : operation(std::move(operation)) {}
297297

298+
PyRegionIterator dunderIter() {
299+
operation->checkValid();
300+
return PyRegionIterator(operation);
301+
}
302+
298303
intptr_t dunderLen() {
299304
operation->checkValid();
300305
return mlirOperationGetNumRegions(operation->get());
@@ -312,6 +317,7 @@ class PyRegionList {
312317
static void bind(py::module &m) {
313318
py::class_<PyRegionList>(m, "RegionSequence", py::module_local())
314319
.def("__len__", &PyRegionList::dunderLen)
320+
.def("__iter__", &PyRegionList::dunderIter)
315321
.def("__getitem__", &PyRegionList::dunderGetItem);
316322
}
317323

mlir/test/python/ir/operation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ def testTraverseOpRegionBlockIterators():
4848
# CHECK: .verify = True
4949
print(f".verify = {module.operation.verify()}")
5050

51-
# Get the regions and blocks from the default collections.
52-
default_regions = list(op.regions)
53-
default_blocks = list(default_regions[0])
51+
# Get the blocks from the default collection.
52+
default_blocks = list(regions[0])
5453
# They should compare equal regardless of how obtained.
55-
assert default_regions == regions
5654
assert default_blocks == blocks
5755

5856
# Should be able to get the operations from either the named collection
@@ -79,6 +77,13 @@ def walk_operations(indent, op):
7977
# CHECK: OP 1: func.return
8078
walk_operations("", op)
8179

80+
# CHECK: Region iter: <mlir.{{.+}}.RegionIterator
81+
# CHECK: Block iter: <mlir.{{.+}}.BlockIterator
82+
# CHECK: Operation iter: <mlir.{{.+}}.OperationIterator
83+
print(" Region iter:", iter(op.regions))
84+
print(" Block iter:", iter(op.regions[0]))
85+
print("Operation iter:", iter(op.regions[0].blocks[0]))
86+
8287

8388
# Verify index based traversal of the op/region/block hierarchy.
8489
# CHECK-LABEL: TEST: testTraverseOpRegionBlockIndices

0 commit comments

Comments
 (0)