[Mlir-commits] [mlir] [MLIR][Python] Add type filter to walk() binding and add get_ops_of_type() utility (PR #186131)

Maksim Levental llvmlistbot at llvm.org
Thu Mar 12 10:52:13 PDT 2026


================
@@ -35,18 +35,41 @@ def get_parent_of_type(op: OpView | Operation, op_class: type[OpView]) -> OpView
     if not (isinstance(op_class, type) and issubclass(op_class, OpView)):
         raise TypeError(f"op_class must be an OpView subclass, got {op_class!r}")
     try:
-        parent = op.parent
+        op = op.parent
     except ValueError:
         return None  # No parent chain.
-    while parent is not None:
-        if isinstance(parent.opview, op_class):
-            return parent.opview
-        parent = parent.parent
+    while op is not None:
+        if isinstance(op.opview, op_class):
+            return op.opview
+        op = op.parent
     return None
 
 
+def get_ops_of_type(
+    root: OpView | Operation | Module, op_class: type[OpView]
----------------
makslevental wrote:

nit:
```suggestion
    root: OpView | Operation | Module, op_class: type[OpView] | None
```
to indicate optionality

https://github.com/llvm/llvm-project/pull/186131


More information about the Mlir-commits mailing list