Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static code generation using the LLVM API

Tags:

c++

llvm

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).

like image 783
ritter Avatar asked May 09 '13 13:05

ritter


People also ask

Does LLVM compile to machine code?

LLVM can also generate relocatable machine code at compile-time or link-time or even binary machine code at run-time. LLVM supports a language-independent instruction set and type system.

Is LLVM better than GCC?

While LLVM and GCC both support a wide variety languages and libraries, they are licensed and developed differently. LLVM libraries are licensed more liberally and GCC has more restrictions for its reuse. When it comes to performance differences, GCC has been considered superior in the past.

What is LLVM used for?

LLVM is a library that is used to construct, optimize and produce intermediate and/or binary machine code. LLVM can be used as a compiler framework, where you provide the "front end" (parser and lexer) and the "back end" (code that converts LLVM's representation to actual machine code).


1 Answers

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.

like image 146
Eli Bendersky Avatar answered Sep 21 '22 04:09

Eli Bendersky