This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][llvm-tblgen] Support conditional definitions using !exists clauses
ClosedPublic

Authored by reames on Mar 1 2023, 12:35 PM.

Details

Summary

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

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.

Diff Detail

Event Timeline

reames created this revision.Mar 1 2023, 12:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2023, 12:35 PM
reames requested review of this revision.Mar 1 2023, 12:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2023, 12:35 PM
pcwang-thead edited reviewers, added: pcwang-thead; removed: wangpc.Mar 1 2023, 7:34 PM
pcwang-thead accepted this revision.Mar 1 2023, 10:43 PM

Thanks for improving this and it's really a helpful extension I think.
The code is LGTM and the detailed description is explanatory, but please wait a few days for others' opinions.

llvm/lib/TableGen/TGParser.cpp
387

Remove this blank please.

This revision is now accepted and ready to land.Mar 1 2023, 10:43 PM
This revision was landed with ongoing or failed builds.Mar 3 2023, 11:30 AM
This revision was automatically updated to reflect the committed changes.
foad added a subscriber: foad.Mar 7 2023, 5:08 AM

Hi, this patch makes tablegen crash instead of error on cases like this:

if !cast<A>("").x then
  def x;

(Based on the test case for https://github.com/llvm/llvm-project/issues/49830)

Could you take a look please?

llvm/lib/TableGen/TGParser.cpp
405

Is it possible to add a lit test for this error case?