[RFC][TableGen] Auto-numbering variable

For example in ValueTypes.td (and Intrinsics.td), there are many numbered defs. I think they could be autonumbered.

TableGen’s defvar defines immutable variable by design. I wonder whether we could introduce another variable type or just enhance semantics.

Just my idea

defvar index = 1;

def OtherVT : ValueType<0, index++>;
def i1 : VTInt<1, index++>;
def i2 : VTInt<2, index++>;
def i4 : VTInt<4, index++>;

// ...
assert index <= 248, "Unexpected number of defs";

defvar meta_idx = 248; // May be the expression for alignment(index)
def token : ValueType<0, meta_idx++>;
// ...
  • defvar is designed as immutable. Could we enhance it mutable or introduce a new type?
  • The example introduces the new token ++. I guess it would make the Lexer more weird. Could we introduce yet another token or operator here?
  • We have to simplify semantics of post increment, or It would cause confusion.
    • My suggestion: Vars will be incremented at the stmt delimiter ;.
  • Could we introduce leap incrementor (like index += 2)?
  • Would defvar pollute the namespace? OTOH, such a incremental index var should have simple and short name.

Other random ideas

  • Could we introduce the special variable to acquire the line number?
    • It would make harder readability and reformatting to rely on line numbers.
  • Could defset provide the current number of elements, or the special counter something, for its elements?

How close could you get with existing features?

I don’t 100% understand the motivation but Compiler Explorer looks similar.

At least in C, I’ve seen __LINE__ used to get a constantly increasing number but where you aren’t bothered about the exact value.

So I agree with your assessment there.

How close could you get with existing features?
I don’t 100% understand the motivation but Compiler Explorer looks similar.

I think it would not be applicable since it doesn’t give unique names nor unique body.

But I have noticed ValueTypes stuff can be rewritten as unaware of number(Value). Some emitters are using Value but I guess they could be rewritten.

OTOH IIT_Info (I wrote it, in Intrinsics.td) relies on Number. I guess it could get rid of Number.

At the moment, I will try getting rid of given numbers for both types.

I guess “Auto-numbering” feature would be wanted in the future but I won’t start experimenting it soon. Thank you.

I think the evaluation of tablegen can be confusing enough without introducing mutable variables into this. What other contexts would this be useful? I can see that it solves a problem specific to these generated enum type of contexts, but there’s only a couple of them