[PATCH] D71407: [TableGen] Introduce a `defvar` statement.

Simon Tatham via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 12 03:23:01 PST 2019


simon_tatham created this revision.
simon_tatham added reviewers: nhaehnle, hfinkel.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71407

Files:
  llvm/docs/TableGen/LangRef.rst
  llvm/lib/TableGen/TGLexer.cpp
  llvm/lib/TableGen/TGLexer.h
  llvm/lib/TableGen/TGParser.cpp
  llvm/lib/TableGen/TGParser.h
  llvm/test/TableGen/defvar.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71407.233562.patch
Type: text/x-patch
Size: 8952 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191212/8db47e95/attachment.bin>


More information about the llvm-commits mailing list