This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Python] Add a simple PyOpOperand iterator for PyValue uses.
ClosedPublic

Authored by mikeurbach on Dec 7 2022, 5:58 PM.

Details

Summary

This adds a simple PyOpOperand based on MlirOpOperand, which can has
properties for the owner op and operation number.

This also adds a PyOpOperandIterator that defines methods for iter
and next so PyOpOperands can be iterated over using the the
MlirOpOperand C API.

Finally, a uses psuedo-container is added to PyValue so the uses can
generically be iterated.

Depends on D139596

Diff Detail

Event Timeline

mikeurbach created this revision.Dec 7 2022, 5:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2022, 5:58 PM
mikeurbach requested review of this revision.Dec 7 2022, 5:58 PM
mikeurbach updated this revision to Diff 481130.Dec 7 2022, 6:00 PM

Fixup unrelated whitespace change.

mikeurbach updated this revision to Diff 481309.Dec 8 2022, 8:51 AM

Switch to the new C API for OpOperand.

Rebase main after C API landed.

I'm not sure who to tag on Python API additions these days, maybe @mehdi_amini, @jpienaar, or @jdd could take a look?

jdd accepted this revision.Dec 13 2022, 4:38 PM

Looks correct to me!

mlir/lib/Bindings/Python/IRCore.cpp
452

It should be possible to get the context from the operand via the forContext method on PyMlirContextRef.

486–487

Why reassign? It'd be clearer to collapse 486 through 488 into one line IMO.

This revision is now accepted and ready to land.Dec 13 2022, 4:38 PM
stellaraccident accepted this revision.Dec 13 2022, 5:01 PM

Lgtm modulo trivial comments

mlir/test/python/ir/value.py
94

Can you move these checks down to the for loop for locality?

mikeurbach added inline comments.Dec 13 2022, 5:18 PM
mlir/lib/Bindings/Python/IRCore.cpp
486–487

I'm not sure I follow how to avoid the reassignment. We need to update some state so the next call to dunderNext either stops the iteration or returns the next op operand. This is the same pattern used for blocks, for example: https://github.com/llvm/llvm-project/blob/3c9f479a5e47d5f3de07ccac365589dd928bfd20/mlir/lib/Bindings/Python/IRCore.cpp#L268-L277

jdd added inline comments.Dec 13 2022, 5:24 PM
mlir/lib/Bindings/Python/IRCore.cpp
486–487

Sorry... I misread. You're right. I don't read so good.

Some clean ups suggested in review.

mikeurbach marked 4 inline comments as done.Dec 13 2022, 6:00 PM

Thanks for the reviews!