[llvm-dev] TableGen: spring cleaning, new features for "functional programming"

Artem Belevich via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 20 11:50:08 PST 2018

On Mon, Feb 19, 2018 at 11:26 AM Nicolai Hähnle <nhaehnle at gmail.com> wrote:

> Hi all,
> While working on some features for the AMDGPU backend -- specifically,
> explicit address components for image intrinsics, which involves
> generating both
> (a) a lot of intrinsics with different but somewhat regular parameter
> types, and
> (b) the patterns to select instructions for those intrinsics
> -- I got fed up with a lot of the TableGen bugs and limitations and set
> out to fix things.
​It must be the season. :-) I've been tactically cleaning up the most
annoying (for me) tablegen issues I've ran into, but your patch set makes
things way more interesting. ​

> I now have an already rather long list of ~30 patches which do a bunch
> of things in lib/TableGen/ such as:
> - better and earlier error messages


> - cleanup type checking
> - cleanup variable resolving
> - cleanup record instantiation

​That can mean whole lot of different things. ​Do you have an overarching
plan how tablegen is supposed to do all of the above in a consistent
manner? Just making it different may not improve things much.

> - late generation of anonymous records that appear in expressions

​That would help, somewhat, though the biggest problem (IMO) with the
anonymous records is that when they are instantiated using multiclass
template parameters, the inner records are not folded because the values
are not known yet and they may never be known because instantiated
anonymous record has no access to template arguments that may be used by
its fields. If you moved instantiation of anonymous records to the point
where the instances of the class/multiclass that uses them are generated,
that would help a lot.​

- cleanup !foreach

​+1 for having scoped temporary variable. ATM tablegen can use a class
field for that, but using it in the global scope still needs a record for
it. Things also go wrong when one accidentally uses a record/field with a
known value.​

- add !foldl
- add !isa<type>(...) and make !cast more useful


> - add !dag builtin to generate DAG nodes from lists

​Yes, please! Constructing dags using !foreach and !con()​ is a pain.

> - some other minor new built-ins like !size, !le, !lt, !ge, !gt
> - add a defset mechanism for collecting records which can then later be
> looped over in a foreach statement
> - make foreach statements in multiclass work
> - probably more...

​I'll list few things that I have on my list of tablegen annoyances to fix:​

- ​!subst() needs some minor tweaks when it's applied to dag from !foreach
-- it wants specific types, but I sometimes want to replace a dag argument
(a record) with another dag​.
​- I want some sort of !macro() operator that will never be folded by
itself, but will instead literally substitute its value when it's used, so
it will have access to the multiclass template arguments, class fields,
etc.​ We're currently using anonymous records for similar purposes, but
those are instantiated at the wrong time, so it's often impossible to use
them as sort of 'subroutines'. Your late generation change you've mentioned
above may help, but I'm not sure if that's the use pattern you had in mind.

> An earlier version of the patches is here if you already want to take a
> look: https://cgit.freedesktop.org/~nh/llvm/log/?h=mimg
> My plan is to clean those up over the next days and weeks and submit
> them to Phabricator.

​I'd be happy to help reviewing them.​

> With the exception of !foreach, which doesn't actually seem to be used,

I do have few outstanding changes for NVPTX where I rely on !foreach, but I
think it should not be too hard to adapt them.​

> all the changes should be backward compatible. In fact, most of the
> larger changes are simply refactoring TableGen in a way that makes
> things work that look like they should already work but don't.
> I would appreciate feedback and help in reviewing the patches when they
> arrive!

​Count me in. And thank you for doing this work!


> Thanks,
> Nicolai
> P.S.: I'm going to document some of my findings and changes on my blog,
> the first entry is here:
> https://nhaehnle.blogspot.de/2018/02/tablegen-1-what-has-tablegen-ever-done.html
> --
> Lerne, wie die Welt wirklich ist,
> Aber vergiss niemals, wie sie sein sollte.

--Artem Belevich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180220/460c9114/attachment-0001.html>

More information about the llvm-dev mailing list