How is TableGen used?

Hi all!

I’m sorry if this has already been asked. I understood (but correct me if I’m wrong) that .td files are written in a DSL, that provides info to the compiler about some parameters. For instance, the structure of all the machine instructions of a ISA. This info can should end up being hardcoded into the compiler(s), but to avoid filling everything by hand, developers only need to mantain the .td files. Then, a backend reads the .td file and generate some code. Only to debug purposes, the output of the backend can be inspected with the tool llvm-tblgen --<backend_name>. However, generally you don’t use the CLI tool, but have CMake run it, save the generated .cpp files, and use them to then build the compiler/library. Is this correct up to now?

I’m struggling to grasp however where can I find the generated files. Are they useful, say, to expand the compiler, or should I completely ignore them as they are not meant to be treated/read/included directly…?

Thanks and sorry if this is noob-ish!

Most of the ones used for ISA descriptions live in build/lib/Target/XYZ/XYZGen*.inc and get #included into source files under llvm/lib/Target/XYZ.

You’d never modify them directly; if they need to change you’d either modify the .td file input or the generator .cpp under llvm/utils/TableGen.

It can occasionally be useful to read them though; in particular has comments that can be very useful when working out why some CodeGen pattern you wrote isn’t matching as expected (the -debug output refers to failures on particular parts of that table).

1 Like

In general if you don’t find a .inc of the same name find the cmake rule that takes the .td as an input. Sometimes the extension or name of the output will change slightly.

1 Like

And yes this is correct, I think we both skipped that bit :slight_smile:

1 Like

To emphasise what has been said, on where to find them, they are called .inc, not .cpp, and are in the build directory/tree with the object files, not next to the .cpp sources.

Many thanks to both!
Last question: is it then safe to assume that every .inc file is a C++ file generated by a .td, and vice versa? Or are there any exceptions? If yes, what is the meaning of the suffix .inc?

I think .inc means included by a .cpp

uhm, ok, but then how is it different from .h files?

Technically it’s not different. But by convention we use .h for files that contain declarations (types, function prototypes, etc), usually to describe an interface to some set of APIs. We use .inc for files that contain other kinds of code that are not complete compilation units (that are not, or even cannot, be compiled by themselves).

1 Like