This is an archive of the discontinued LLVM Phabricator instance.

[mlir][ods] Added EnumAttr, an AttrDef implementation of enum attributes
ClosedPublic

Authored by Mogball on Dec 6 2021, 1:22 PM.

Details

Summary

EnumAttr is a pure TableGen implementation of enum attributes using AttrDef. This is meant as a drop-in replacement for StrEnumAttr, which is soon to be deprecated. StrEnumAttr is often used over IntEnumAttr because its more readable in MLIR assembly formats. However, storing and manipulating strings is not efficient. Defining StrEnumAttr can also be awkward and relies on a lot of special logic in EnumsGen, and has some hidden sharp edges.

Also, EnumAttr stores the enum directly, removing the need to convert to/from integers when calling attribute getters on ops.

Diff Detail

Event Timeline

Mogball created this revision.Dec 6 2021, 1:22 PM
Mogball requested review of this revision.Dec 6 2021, 1:22 PM

Nice!

mlir/include/mlir/IR/EnumAttr.td
39

Not using the surrounding < and > forces the opaque/generic syntax for the attribute, that's a bit unfortunate though.

Is there a way to get a better printing? (in particular in the declarative assembly used by operations)

mlir/test/lib/Dialect/Test/TestOps.td
311

Can you use a declarative assembly here?

Mogball added inline comments.Dec 6 2021, 2:01 PM
mlir/include/mlir/IR/EnumAttr.td
39

I can add the braces for now, but the reason why I wanted to wait on your strip dialect prefix patch is so that this won't be a concern (when attributes are used in assembly formats). I.e. #test<"enum first"> will be first when parsed as part of an assembly format.

Mogball updated this revision to Diff 392226.Dec 6 2021, 4:24 PM

Update test op with assemblyFormat

Mogball marked 2 inline comments as done.Dec 7 2021, 9:24 AM
Mogball added inline comments.
mlir/include/mlir/IR/EnumAttr.td
39

Users can also override the assemblyFormat themselves.

Mogball updated this revision to Diff 393190.Dec 9 2021, 9:09 AM

Updated assembly format with stripped dialect prefix

mlir/test/IR/enum-attr-roundtrip.mlir
18

@mehdi_amini check it out!

LGTM, but will let Mehdi sign off (as he had some comments).

mlir/include/mlir/IR/EnumAttr.td
37–38
mehdi_amini added inline comments.Dec 16 2021, 6:30 PM
mlir/include/mlir/IR/EnumAttr.td
39

I think we should just document this better.
To begin with, indicate that the default printer/parser is optimized for uses in the op assembly format.
Provide example of how to use this and example of how it looks like in IR.

mlir/test/IR/enum-attr-roundtrip.mlir
18

Nice! ;)

Mogball updated this revision to Diff 395042.Dec 16 2021, 6:45 PM
Mogball marked 3 inline comments as done.

Add more documentation

mehdi_amini accepted this revision.Dec 16 2021, 6:54 PM
This revision is now accepted and ready to land.Dec 16 2021, 6:54 PM
This revision was landed with ongoing or failed builds.Dec 16 2021, 6:55 PM
This revision was automatically updated to reflect the committed changes.