This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][python bindings] Add `PyValue.get_name`
ClosedPublic

Authored by makslevental on May 4 2023, 2:00 PM.

Details

Summary

Useful for easier debugging (no need to regex out all of the stuff around the id).

Diff Detail

Event Timeline

makslevental created this revision.May 4 2023, 2:00 PM
Herald added a project: Restricted Project. · View Herald Transcript
makslevental published this revision for review.May 4 2023, 3:21 PM
makslevental edited the summary of this revision. (Show Details)
makslevental added inline comments.May 4 2023, 3:24 PM
mlir/lib/Bindings/Python/IRCore.cpp
3344

Probably this should be named something other than print but the conventional name escapes me (only thing that comes to mind is show vs print in Haskell).

mehdi_amini added inline comments.May 4 2023, 3:37 PM
mlir/include/mlir-c/IR.h
780

Can you document the arguments? In particular the parentOp here and the contract with "value"

move parent into C function

makslevental marked an inline comment as done.May 4 2023, 6:53 PM
makslevental added inline comments.
mlir/include/mlir-c/IR.h
780

Prior I had the "get parent" stuff inside the C function and moved it out because it seemed like most of the functions in IR.cpp are "small"; your comment made me realize it's much safer/better to have them there in order to enforce the relationship.

mlir/lib/CAPI/IR/IR.cpp
781

Block::printAsOperand similarly prints <<UNLINKED BLOCK>> in such a case (see here).

rkayaith added inline comments.May 4 2023, 7:06 PM
mlir/lib/Bindings/Python/IRCore.cpp
3344

imo this should be the behaviour of __str__, and the current __str__ should be __repr__

mlir/lib/CAPI/IR/IR.cpp
775–779

To get the expected result for values in nested regions I think you'll need to continue traversing up to find the top-level op, similar to here: https://github.com/llvm/llvm-project/blob/2e3cabe172c6f2eaf1f097ffeff1664b3768223a/mlir/lib/IR/AsmPrinter.cpp#L3680-L3691

780–783

In this case I think printing with AsmState(ctx) should work fine?

rkayaith added inline comments.May 4 2023, 7:10 PM
mlir/lib/Bindings/Python/IRCore.cpp
3344

on second thought, if the current __str__ prints the full operation, using it for __repr__ is probably not a good idea

rkayaith added inline comments.May 4 2023, 7:28 PM
mlir/lib/CAPI/IR/IR.cpp
780–783

nevermind, after looking at the implementation of that, it would just end up printing something similar to what you did already

makslevental marked an inline comment as done.

handle vals in nested regions correctly

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

I agree! I can make the change and then patch the failing lit tests if there's a consensus.

makslevental added inline comments.May 4 2023, 7:49 PM
mlir/lib/Bindings/Python/IRCore.cpp
3344

f the interpretations of __repr__ and __str__ are

  • __repr__ is to be unambiguous
  • __str__ is to be readable

then I think printing only the valueid for __str__ and printing the whole op for __repr__ would be the right thing to do?

I mean it doesn't matter to me as long as there's someway to print the valueid (i.e., this dunder).

mlir/lib/CAPI/IR/IR.cpp
780–783

With AsmState(ctx) you get %0 always I believe, which either is or isn't counterintutive.

add one more test case

rkayaith added inline comments.May 5 2023, 9:33 PM
mlir/lib/Bindings/Python/IRCore.cpp
162–164

I'm not sure this should be documented here, it seems more like a bug in the C++ API I think?

3344

If you keep this as a method, how about get_name or get_id?

mlir/lib/CAPI/IR/IR.cpp
776–777

This traversal should happen for block args as well

795

This is never freed, I'd just go back to printing the << ... >> string if that simplifies things

traverse for block args, free asmstate, handle corner case

makslevental marked 3 inline comments as done.May 5 2023, 10:14 PM

clang-format

rkayaith added inline comments.May 6 2023, 9:50 PM
mlir/include/mlir-c/IR.h
779–780

The "this isn't canonical" comment doesn't seem like it should be here (documenting a bug?)

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

imo get_name by itself is unambiguous and the _as_operand doesn't add anything here

3352

to be consistent with ir.Operation.print

mlir/lib/CAPI/IR/IR.cpp
770

There's additional printing options that can affect the value id, like printGenericOpForm. If you want to support different options it seems better to just take a MlirOpPrintingFlags here (probably not worth exposing all the options python-side though, that seems easy to extend later).

778–779
780–785

Values can only be OpResults or BlockArguments, so this else can't be reached. I think the check should look something like this instead

788

I would add a Value::printAsOperand(raw_ostream &os, const OpPrintingFlags &flags={}) overload that does this. That would also let you share the logic here with Operation::print.

800–802
makslevental marked 3 inline comments as done.

refactor

rkayaith added inline comments.May 7 2023, 5:32 PM
mlir/lib/Bindings/Python/IRCore.cpp
3347

You'll need to call mlirOpPrintingFlagsDestroy at the end to avoid this leaking

3348–3350

llvm style is to not use braces here

mlir/lib/IR/AsmPrinter.cpp
3676–3677 ↗(On Diff #520219)

the llvm style is to use static for functions instead of anonymous namespaces

3699–3700 ↗(On Diff #520219)

cast() will do this assert for you

fix mistakes

makslevental marked 11 inline comments as done.May 7 2023, 5:44 PM
rkayaith accepted this revision.May 7 2023, 5:50 PM

LGTM, can you also update the revision title

This revision is now accepted and ready to land.May 7 2023, 5:50 PM
makslevental retitled this revision from [MLIR][python bindings] Add `PyValue.print_as_operand` (`Value::printAsOperand`) to [MLIR][python bindings] Add `PyValue.get_name`.May 7 2023, 5:54 PM