Assembly format of an array of EnumAttr

I try to understand how the assembly format works with EnumAttr. I have the following piece of code for TestDialect that defines an EnumAttr and a few operations that use the enum.

def TestEnum
    : I32EnumAttr<"TestEnum", "a test enum", [
        I32EnumAttrCase<"First", 0, "first">,
        I32EnumAttrCase<"Second", 1, "second">,
        I32EnumAttrCase<"Third", 2, "third">,
      ]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "test";

def TestEnumAttr : EnumAttr<Test_Dialect, TestEnum, "enum">;

def TestEnumArrayAttr : TypedArrayAttrBase<TestEnumAttr, "">;

def OpWithSingleEnum : TEST_Op<"op_with_single_enum"> {
  let arguments = (ins TestEnumAttr:$value);
  let assemblyFormat = "$value attr-dict";

def OpWithArrayEnum : TEST_Op<"op_with_array_enum"> {
  let arguments = (ins TestEnumArrayAttr:$values);
  let assemblyFormat = "$values attr-dict";

When EnumAttr is a simple attribute in “op_with_single_enum”, we get a nice print format:

  test.op_with_single_enum first

But once wrapped into an array, the only option is

  test.op_with_array_enum [#test<enum first>, #test<enum second>]

It’s a bit sad and counterintuitive that information about the type from TypedArrayAttrBase is not used to have a nicer printing/parsing of arrays.

What would be really nice to have is:

  test.op_with_array_enum [first, second]

And even:

  test.op_with_array_enum { array_of_enums = [first, second] }

Are there any plans to do that? Is that even feasible?

@mogball @pifon2a @nicolasvasilache

1 Like

The problem here is that TypedArrayAttrBase is only an ODS thing, some sugar to add a verifier constraint over what is an ArrayAttr. The latter does not carry any more information about the elements: it can contain a heterogeneous list of attributes. We would need a better C++ type for constrained arrays with uniform element types.

In the meantime you can get some nice API and printer by using a custom attribute for representing an array with a particular element type attribute.

The first one is possible with a custom array attribute like Mehdi said. The second is unfortunately not possible because the generic syntax cannot be that pretty.