The order of attribute classes in tablegen-generated file

I’m using .td to define my dialect attribute classes. I have two attr A and B. Attr A has a member function (extra declaration) that returns SmallVector<B>. The compilation of this header file fails because the definition of B is after A and therefore throws an “incomplete type” error. Swapping A and B in .td file does not change the order of generated A and B in file.

class A;
class B;

class A : public AttrBase {
  ArrayRef<B> foo();  // ArrayRef works because it's only a pointer of B

  SmallVector<B> bar();  // Compile error: SmallVector needs to know sizeof(B)

class B : public AttrBase {

Any suggestion how I should fix this? Thanks in advance!


I don’t know the fix. The workaround I’ve employed to work around this is to split my .td in two.

Can we not order the classes alphabetically but rather keep the order of appearance in ODS? This seems to come from tablegen…

We made a change with operation emission to change ordering to be in file order, a similar change could be made for attributes. A new sorting function was added locally to enable that.

1 Like

[mlir][attrtypedef] Sort by def order · llvm/llvm-project@e1e3baa · GitHub should do it if you want to give it a shot.

Merged [mlir][attrtypedef] Sort by def order in file. (#71083) · llvm/llvm-project@308f58c · GitHub so should be fixed now.