[all-commits] [llvm/llvm-project] 3388b0: [TableGen] Introduce a `defvar` statement.

Simon Tatham via All-commits all-commits at lists.llvm.org
Tue Jan 14 02:20:14 PST 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 3388b0f59dcc7813278c753f96b66229f290cc59
      https://github.com/llvm/llvm-project/commit/3388b0f59dcc7813278c753f96b66229f290cc59
  Author: Simon Tatham <simon.tatham at arm.com>
  Date:   2020-01-14 (Tue, 14 Jan 2020)

  Changed paths:
    M llvm/docs/TableGen/LangRef.rst
    M llvm/lib/TableGen/TGLexer.cpp
    M llvm/lib/TableGen/TGLexer.h
    M llvm/lib/TableGen/TGParser.cpp
    M llvm/lib/TableGen/TGParser.h
    A llvm/test/TableGen/defvar.td

  Log Message:
  -----------
  [TableGen] Introduce a `defvar` statement.

Summary:
This allows you to define a global or local variable to an arbitrary
value, and refer to it in subsequent definitions.

The main use I anticipate for this is if you have to compute some
difficult function of the parameters of a multiclass, and then use it
many times. For example:

  multiclass Foo<int i, string s> {
    defvar op = !cast<BaseClass>("whatnot_" # s # "_" # i);
    def myRecord {
      dag a = (op this, (op that, the other), (op x, y, z));
      int b = op.subfield;
    }
    def myOtherRecord<"template params including", op>;
  }

There are a couple of ways to do this already, but they're not really
satisfactory. You can replace `defvar x = y` with a loop over a
singleton list, `foreach x = [y] in { ... }` - but that's unintuitive
to someone who hasn't seen that workaround idiom before, and requires
an extra pair of braces that you often didn't really want. Or you can
define a nested pair of multiclasses, with the inner one taking `x` as
a template parameter, and the outer one instantiating it just once
with the desired value of `x` computed from its other parameters - but
that makes it awkward to sequentially compute each value based on the
previous ones. I think `defvar` makes things considerably easier.

You can also use `defvar` at the top level, where it inserts globals
into the same map used by `defset`. That allows you to define global
constants without having to make a dummy record for them to live in:

  defvar MAX_BUFSIZE = 512;

  // previously:
  // def Dummy { int MAX_BUFSIZE = 512; }
  // and then refer to Dummy.MAX_BUFSIZE everywhere

Reviewers: nhaehnle, hfinkel

Reviewed By: hfinkel

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D71407


  Commit: ddbc0b1e516407a24d986a1998026f1ac5864270
      https://github.com/llvm/llvm-project/commit/ddbc0b1e516407a24d986a1998026f1ac5864270
  Author: Simon Tatham <simon.tatham at arm.com>
  Date:   2020-01-14 (Tue, 14 Jan 2020)

  Changed paths:
    M llvm/docs/TableGen/LangRef.rst
    M llvm/lib/TableGen/TGLexer.cpp
    M llvm/lib/TableGen/TGLexer.h
    M llvm/lib/TableGen/TGParser.cpp
    M llvm/lib/TableGen/TGParser.h
    M llvm/test/TableGen/defvar.td
    A llvm/test/TableGen/ifstmt.td

  Log Message:
  -----------
  [TableGen] Introduce an if/then/else statement.

Summary:
This allows you to make some of the defs in a multiclass or `foreach`
conditional on an expression computed from the parameters or iteration
variables.

It was already possible to simulate an if statement using a `foreach`
with a dummy iteration variable and a list constructed using `!if` so
that it had length 0 or 1 depending on the condition, e.g.

  foreach unusedIterationVar = !if(condition, [1], []<int>) in { ... }

But this syntax is nicer to read, and also more convenient because it
allows an else clause.

To avoid upheaval in the implementation, I've implemented `if` as pure
syntactic sugar on the `foreach` implementation: internally, `ParseIf`
actually does construct exactly the kind of foreach shown above (and
another reversed one for the else clause if present).

Reviewers: nhaehnle, hfinkel

Reviewed By: hfinkel

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D71474


Compare: https://github.com/llvm/llvm-project/compare/45924eb46716...ddbc0b1e5164


More information about the All-commits mailing list