This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add Python binding for MLIR Dict Attribute
ClosedPublic

Authored by kweisamx on Dec 9 2020, 11:37 PM.

Diff Detail

Event Timeline

kweisamx created this revision.Dec 9 2020, 11:37 PM
kweisamx requested review of this revision.Dec 9 2020, 11:37 PM
kweisamx updated this revision to Diff 310777.Dec 10 2020, 12:09 AM

[mlir] Add Python binding for MLIR Dict Attribute

ftynse added a subscriber: ftynse.Dec 10 2020, 3:32 AM

Thanks!

mlir/lib/Bindings/Python/IRModules.cpp
1904

This should be getting a context of _this_ attribute instead of accepting any context as argument. PyConcreteAttribute has a field containing the current attribute IIRC.

1910–1935

Can this be defined as a member function rather than a long lambda?

1911

Do we really need to make this optional? Creating an empty dictionary attribute from an empty dictionary makes more sense to me.

1938–1939

Ideally, we would have the same interface querying for DictAttr as we do for the attrs field on ops, i.e. also have an indexed getitem.

mlir/test/Bindings/Python/ir_attributes.py
281

Please also add a check that the exception is thrown for non-existent attribute.

mehdi_amini added inline comments.Dec 10 2020, 9:05 AM
mlir/lib/Bindings/Python/IRModules.cpp
1913

Instead of creating a vector of pair<string,MlirAttribute> and then use it to build a vector of MlirNamedAttribute, can you just build a vector of MlirNamedAttribute in the first place?
(also use SmallVector<MlirNamedAttribute>)

1920

Nit: remove the c_str(), it is only pessimizing here.

kweisamx updated this revision to Diff 311143.Dec 11 2020, 1:31 AM

Thanks for advice. I refine it.

kweisamx marked 6 inline comments as done.Dec 11 2020, 1:39 AM
kweisamx added inline comments.
mlir/lib/Bindings/Python/IRModules.cpp
1913

In mlirNamedAttributeGet, it use MlirStringRef.
If we build vector with MlirNamedAttribute and pass name directly. It will be change after next iterator(Because we use reference)?
That I build MlirAttribute with two step.

Are there other ways better?

mehdi_amini added inline comments.Dec 11 2020, 11:46 AM
mlir/lib/Bindings/Python/IRModules.cpp
1913

Oh, it seems that MlirNamedAttribute are non-owning for the key... This is specific to the C API, the C++ API creates a context-owned string instead. I think we should change the C API.

I'm doing it here: https://reviews.llvm.org/D93133

ftynse added inline comments.Dec 11 2020, 2:29 PM
mlir/lib/Bindings/Python/IRModules.cpp
1913

Yes, we must have a todo somewhere to fix that, legacy from early iterations of C API.

mehdi_amini added inline comments.Dec 11 2020, 2:40 PM
mlir/lib/Bindings/Python/IRModules.cpp
1913

Alright this is landed, if you rebase you can eliminate this extra vector now :)

kweisamx updated this revision to Diff 311392.EditedDec 12 2020, 7:07 AM

Great!
I fixed it with eliminating extra vector .
Please help me review this commit.
Thanks.

mehdi_amini accepted this revision.Dec 12 2020, 9:43 AM

LGTM!

Do you need me to land this for you? (if so please let me know the authorship information: name/email).

mlir/lib/Bindings/Python/IRModules.cpp
1933

Please run git clang-format (arc does it for you I believe)

This revision is now accepted and ready to land.Dec 12 2020, 9:43 AM
kweisamx updated this revision to Diff 311421.Dec 12 2020, 6:02 PM

Fix clang-format

kweisamx marked an inline comment as done.Dec 12 2020, 6:02 PM

Yes, i would contribute more for this project :)

name: kweisamx
email: kweisamx0322@gmail.com

Thank!

This revision was landed with ongoing or failed builds.Dec 12 2020, 9:09 PM
This revision was automatically updated to reflect the committed changes.