5
votes

Starting from an LLVM module *Mod containing some IR: How do I use a specific back-end to generate target-specific assembler from it? Unfortunately the Kaleidoscope tutorial does not detail on this. It only uses the execution engine to JIT compile the program (on the build, target architecture), but does not actually select a specific back-end to generate target-specific assembler. Neither does any LLVM tutorials, as they are not detailing on building a static compiler, I assume. Thus, this question can provide this missing piece of information.

Since we want to build a static compiler, we don't want to use the command line tools like llc, etc. to accomplish the job. We want to use the LLVM API.

To make this more concrete, let's start with this code:

LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, Context);

The filename (given by the 1st argument) was successfully parsed to IR. Now let's skip the optimization passes and create some assembler with a specific back-end, e.g. the PTX backend (available since version 3.2).

1

1 Answers

8
votes

Just look in the source code of tools/llc/llc.cpp. It does all that, and it's pretty short and not hard to understand. If you have specific questions about some things it does you don't understand, feel free to ask.