This is an archive of the discontinued LLVM Phabricator instance.

[mlir][python] Create all missing attribute builders.
ClosedPublic

Authored by ingomueller-net on Sep 2 2023, 5:37 AM.

Details

Summary

This patch adds attribute builders for all buildable attributes from the
builtin dialect that did not previously have any. These builders can be
used to construct attributes of a particular type identified by a string
from a Python argument without knowing the details of how to pass that
Python argument to the attribute constructor. This is used, for example,
in the generated code of the Python bindings of ops.

The list of "all" attributes was produced with:

(

grep -h "ods_ir.AttrBuilder.get" $(find ../build/ -name "*_ops_gen.py") \
  | cut -f2 -d"'"
git grep -ho "^def [a-zA-Z0-9_]*" -- include/mlir/IR/CommonAttrConstraints.td \
  | cut -f2 -d" "

) | sort -u

Then, I only retained those that had an occurence in
mlir/include/mlir/IR. In particular, this drops many dialect-specific
attributes; registering those builders is something that those dialects
should do. Finally, I removed those attrbiutes that had a match in
mlir/python/mlir/ir.py already and implemented the remaining ones. The
only ones that still miss a builder now are the following:

  • Represent more than one possible attribute type:
    • Any.*Attr (9x)
    • IntNonNegative
    • IntPositive
    • IsNullAttr
    • ElementsAttr
  • I am not sure what "constant attributes" are:
    • ConstBoolAttrFalse
    • ConstBoolAttrTrue
    • ConstUnitAttr
  • Location not exposed by Python bindings:
    • LocationArrayAttr
    • LocationAttr
  • get function not implemented in Python bindings:
    • StringElementsAttr

This patch also fixes a compilation problem with
I64SmallVectorArrayAttr.

Diff Detail

Event Timeline

ingomueller-net created this revision.Sep 2 2023, 5:37 AM
Herald added a project: Restricted Project. · View Herald Transcript
ingomueller-net requested review of this revision.Sep 2 2023, 5:38 AM
ingomueller-net retitled this revision from WIP: [mlir][python] Create all missing attribute builders. to [mlir][python] Create all missing attribute builders..Sep 2 2023, 5:38 AM
makslevental accepted this revision.Sep 2 2023, 8:18 AM

LGTM! Thanks for all the typing!

This revision is now accepted and ready to land.Sep 2 2023, 8:18 AM
rkayaith requested changes to this revision.Sep 3 2023, 9:01 AM
rkayaith added inline comments.
mlir/python/mlir/ir.py
26

I don't think this one is right, APIntAttr doesn't specify a bit width.

mlir/test/python/dialects/python_test.py
145–209

there's a local_scope option you can use when printing to avoid the attribute aliases for these

211–212

could you assert op.verify() as a sanity check

This revision now requires changes to proceed.Sep 3 2023, 9:01 AM
ingomueller-net marked 2 inline comments as done.Sep 4 2023, 12:47 AM
ingomueller-net added inline comments.
mlir/python/mlir/ir.py
26

Yeah, you're right. I guess this means we can't add a builder. Removed in the next version.

mlir/test/python/dialects/python_test.py
145–209

Good idea! Implemented in next version.

211–212

Good idea! Implemented in next version.

ingomueller-net marked 2 inline comments as done.

Implement changes suggested by @rkayaith:

  • Remove constructor for APIntAttr because that constraint doesn't specify the bitwidth so we don't know which one to specify when building the attribute type.
  • op.verify in the tests.
  • Use use_local_scope=True in tests.
rkayaith accepted this revision.Sep 4 2023, 7:57 AM
This revision is now accepted and ready to land.Sep 4 2023, 7:57 AM

@ftynse: should I wait for you to have a look or go ahead and land?