[Mlir-commits] [mlir] [MLIR][Python] Register Containers as Sequences for `match` compatibility (PR #174091)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jan 1 02:38:57 PST 2026


MaPePeR wrote:

> > So maybe also turning Region itself into a Sequence would be a good addition as well? Would extend the Scope of this PR, though.
> 
> I mean it works long-hand right? No need if there's _some_ way to write the match clause.

Yes, it does work the long way. Though there are no errors or warnings if you do it "wrong", so it can take a while to figure out why it isn't working. `Region` is already `Iteratable`, so it isn't so far off of being a `Sequence`.

And, if `Region` would be a Sequence, the `match` statement could be more concise:
```
# Matching scf.while with scf.condition as last operation in before block and scf.yield as last operation in after block
match some_scf_while:
    case scf.WhileOp(
        before=[Block(operations=[*_, scf.ConditionOp() as condition_op])],
        after=[Block(operations=[*_, scf.YieldOp() as yield_op])],
    ):
        print("Matched while with Region implementing Sequence") # Does not work currently.
    case scf.WhileOp(
        before=Region(blocks=[Block(operations=[*_, scf.ConditionOp() as condition_op])]),
        after=Region(blocks=[Block(operations=[*_, scf.YieldOp() as yield_op])]),
    ):
        print("Matched while with current Region")
```

Though it would arguably also allow `operation.regions[0][0]` instead of `operation.regions[0].blocks[0]`, which isn't great.

I think `match` statemants are a kind of superpower for handling this kind of data structure so they should be as ergonomically as possible.

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


More information about the Mlir-commits mailing list