Skip to content

LLVM Basics

Fabian Schiebel edited this page Oct 3, 2023 · 11 revisions

What is LLVM?

LLVM is an umbrella project that hosts and develops a set of close-knit low-level tool chain components (e.g. assemblers, compilers, debuggers, etc.).

LLVM is mostly used as a common infrastructure to implement a broad variety of statically and run-time compiled languages (e.g. the family of languages supported by GCC, Java, .NET, Python, Ruby, Scheme, Haskell, D). It is also replaced a broad variety of special purpose compilers, such as the run-time specialization engine in Apple's OpenGL stack and the image processing library in Adobe's After Effects product. Finally LLVM has also been used to create a wide variety of new products, perhaps the best known of which is the OpenCL GPU programming language.

Intermediate Representation (IR) is the most important aspect of LLVM framework, which is the basis for all LLVM optimization passes. LLVM IR is designed to host mid-level analyses and transformations that you find in the optimizer section of the compiler. IR exists in three equivalent representations, in-memory C++ data structures, binary files or LLVM bitcode (file extension: .bc) and human readable assembly like form (file extension: .ll).

Here is a simple example of a human readable representation of IR (file with .ll extension):

; ModuleID = 'main.cpp'
source_filename = "main.cpp"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline norecurse nounwind optnone uwtable
define dso_local i32 @main() #0 {
entry:
  %i = alloca i32, align 4
  %j = alloca i32, align 4
  store i32 42, i32* %i, align 4
  %0 = load i32, i32* %i, align 4
  %add = add nsw i32 %0, 1
  store i32 %add, i32* %j, align 4
  ret i32 0
}

which corresponds to this simple code example:

int main() {
    int i = 42;
    int j = i + 1;
}

A shell script of LLVM installation can be found here => LLVM installation.

Some more useful resources on LLVM can be found in the following:

https://llvm.org/docs/LangRef.html

http://llvm.org/docs/GettingStarted.html

http://llvm.org/docs/ProgrammersManual.html

http://llvm.org/docs/Lexicon.html