[RISCV][llvm-tblgen] Support conditional definitions using !exists clauses
authorPhilip Reames <preames@rivosinc.com>
Fri, 3 Mar 2023 19:10:26 +0000 (11:10 -0800)
committerPhilip Reames <listmail@philipreames.com>
Fri, 3 Mar 2023 19:29:47 +0000 (11:29 -0800)
commit04ed64e42fb4079d6d406018733936a2fbf9db8f
tree6bdf19c321eabf436a2b718c01815e3e5270816f
parentbd80dbf284c685db3427a169386d150398b5151d
[RISCV][llvm-tblgen] Support conditional definitions using !exists clauses

The core part of this change is an extension to the tablegen language to allow conditional definition of records using if-statements based on !exists conditions.

The RISCV td file change is mostly to illustrate the potential use of conditional definitions. I am deliberately not maximally simplifying in this change to make merging with downstream code (or simply rebasing while this on review) easier.

Some background to make the change understandable.

TableGen does not have an if statement internally. It has if expressions - in the form of TernInitOp with IF opcode - and foreach statements. It implements an if-statement as a foreach which iterates either 0 or 1 times.

Foreach nodes are then evaluated via unrolling inside the parser. Specifically, they are evaluated, at latest, when the outermost multiclass or loop containing them reaches end of scope. The unrolled statements remain (potentially) unresolved after unrolling, but the number of iterations must be known at this point.

An !exists clause can only evaluate at final evaluation. (Specifically, forward references to definitions are allowed - up to the end of the containing scope at least.) The existing code did not set the final flag on the resolver, and thus would leave the !exists clause in an unresolved state. This would then cause an error since we don't know how many iterations on which to unroll the (synthetic) foreach loop.

I chose to only finally-evaluate the condition of the if-expression. This allows us to pick an arm at scope exit without inhibiting definitions in the arm from having self references.

Differential Revision: https://reviews.llvm.org/D145108
llvm/lib/TableGen/Record.cpp
llvm/lib/TableGen/TGParser.cpp
llvm/lib/Target/RISCV/RISCVScheduleV.td
llvm/test/TableGen/exists.td