Skip to content

LLVM Basics

Bhuvan Sharma edited this page Jun 24, 2020 · 1 revision

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-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

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

which corresponds to this simple code example:

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

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

In order to gain more information about LLVM you can follow the bellow links:

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

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

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

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

https://phasar.org/download/