
In this course you are going to learn about converting high level programming constructs into LLVM IR by using LLVM APIs. This is a practical class about LLVM programming. After learning this class, you will be able to write your own compiler by using LLVM infrastructure.
This course has two prerequisites. Firstly, basic C/C++ programming skills is required. You must be familiar with the follwing knowledge, such as variables, functions, pointers, etc. Secondly, basic compiler principle concepts is recommended. It will be helpull if you know the follwing concepts, such as parser, basic blocks, SSA, etc.
All the source code will be discussed and developed in this course is presented in this github url. Before we dive into llvm programming, let's review the core compoents of compiler architecture.
In general, compiler will translate source files into an executable file in three phases.
On phase one, the compiler frontend reads the soure file and produce ir file.
On phase two, the compiler middleend reads the ir and emit the optimized ir.
On phase three, the compile reads the ir produced by the middleend and produce an executable file.
From this compiler architecture diagram we can see that there are lots of stuff to do to implement a compiler. By using LLVM libraries, things become simplified.
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. By using LLVM, compiler developer only need to modify their frontend to emit llvm ir. We can reuse llvm middleend and llvm backend. Finally we can get a high quality executable file produced by LLVM tools.
In next section we will setup develop environment to program with LLVM. Thanks for watching.
In this lecture we are goning to setup llvm develop environment.
Firstly you need to install linux operating system on your machine. I use ubuntu 22, other linux distribution will go. Secondly, install clang and clang++ by using command sudo apt-get install clang-14. Lastly, install llvm by using command sudo apt-get install llvm-14.
This slide lists the most important llvm tools we will use in this course. The usage of each tool is well documented in this url.
Let's try these tools. We will going to mkdir llvm-ir-builder to create our project directory. Then cd llvm-ir-builder, mkdir examples and cd examples. Type touch hello.c; vim hello.c.
Let's write the classical hello world code. Let's compile and run the code.Type clang hello.c -o hello.out; ./hello.out . 'hello world!' is printed, nice and simple.
We can use clang with emit-llvm flag to produce llvm assmbly. type clang -emit-llvm hello.c -S -o hello.ll. This is the contents of llvm assmbly. We use lli to run hello.ll.
We can use llvm-as to produce on-disk bitcode representation. llvm-as hello.ll -o hello.bc. We can use lli hello.bc to run the code.
We can use llvm-dis to generate human readable form. llvm-dis hello.bc -o hello.ll.
We can use llc to generate assembly. type llc hello.bc -o hello.s.
We can use gcc to compile hello.s into an executable file. type gcc hello.s -o hello.native; ./hello.native.
Now we are farmiliar with some llvm tools, in next lecture we will create our first module by using llvm apis.
In this course you are going to learn about converting high level programming constructs into LLVM IR by using LLVM APIs. This is a practical class about LLVM programming. After learning this class, you will be able to write your own compiler by using LLVM infrastructure.
This course has two prerequisites. Firstly, basic C/C++ programming skills is required. You must be familiar with the follwing knowledge, such as variables, functions, pointers, etc. Secondly, basic compiler principle concepts is recommended. It will be helpull if you know the follwing concepts, such as parser, basic blocks, SSA, etc.
All the source code will be discussed and developed in this course is presented in this github url. Before we dive into llvm programming, let's review the core compoents of compiler architecture.
In general, compiler will translate source files into an executable file in three phases.
On phase one, the compiler frontend reads the soure file and produce ir file.
On phase two, the compiler middleend reads the ir and emit the optimized ir.
On phase three, the compile reads the ir produced by the middleend and produce an executable file.
From this compiler architecture diagram we can see that there are lots of stuff to do to implement a compiler. By using LLVM libraries, things become simplified.
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. By using LLVM, compiler developer only need to modify their frontend to emit llvm ir. We can reuse llvm middleend and llvm backend. Finally we can get a high quality executable file produced by LLVM tools.