Book Summary: Learn LLVM 17

Amr Hesham
3 min readFeb 20, 2024

--

Hello everyone, I am Amr and i am a Software engineer who is interested in Compiler and Tools development and in this article i will try to write a summary for Learn LLVM 17 book so you can get an idea about what is the content and what to expect from it.

The book contains four parts which are

  • The Basics of Compiler Construction with LLVM
  • From Source to Machine Code Generation
  • Taking LLVM to the Next Level
  • Roll Your Own Backend

Part 1: The Basics of Compiler Construction with LLVM

The authors starts this part with step by step on how to install the LLVM on your system with the required dependencies then start to give you an overview about common the structure of the compiler and what is the goal of every pass.

After finishing the overview, the authors start with the first project which is a simple arithmetic expression language, this project it teach you how to define and read grammar, build a handwritten lexer, parser and convert from source code to Abstract Syntax Tree (AST) data resources, then going to Semantic analysis pass to validate this tree and make sure the program is valid for example checking if types and identifiers are valid and declared before used, then start the code generation pass which is generating LLVM IR from the AST and support the language with simple runtime library written in C and create call for it inside the code generation

Part 2: From Source to Machine Code Generation

In this part the authors start the second project which is a compiler for a subset of Modula-2 Programming language, this language supports types, generics and object-orientated programming here is an example from the book.

MODULE Gcd;

PROCEDURE GCD(a, b: INTEGER) : INTEGER;
VAR t: INTEGER;
BEGIN
IF b = 0 THEN
RETURN a;
END;
WHILE b # 0 DO
t := a MOD b;
a := b;
b := t;
END;
RETURN a;
END GCD;

END Gcd.m

The authors starts to define the grammar for the language which is now similar to real language not only arithmetic expression and show you the structure for the project then moving to preparing how to manage files and diagnostics and starting the lexer and the parser and AST which similar to the arithmetic expression language but for sure a lot bigger.

Then moving to performing semantic analysis which is now contains more analysis such as checking that variables or objects names are unique in the current scope and not declared twice, resolve types for constants without declaring it, and in assignment expression make sure that the value match the type for this variable

On the next chapters in this part the authors start to teach you about LLVM IR and SSA by generating IR from C code using Clang Compiler (Also i recommend to use the Compiler explore website too) and then starting to generate LLVM IR form the program AST with and implement optimization passes.

Part 3: Taking LLVM to the Next Level

In this part the authors start to teach you about the LLVM DSL TableGen language, The JIT (Just in time) Compiler and use LLVM tools in deteching issues, debugging, static analyzer and profiling then how to write your own tool using LibClang, In my opinion learning how to use and create tools with LibClang is very important so you can create a lot of good tools or using it to parse C code into AST allow your language to call C code and vice versa.

Part 4: Roll Your Own Backend

In this part the authors starts to teach you about how to add a new backend target for a CPU architecture not supported by LLVM

Summary

The book is very good. i liked that the authors stars with simple language first then moving to subset of real world language (Modula-2) with features that you see in most languages, also with well structure for code and compiler components not dummy.

In my opinion the book is good in both cases if you are want to start learning about compiler or have read some books before but interested to start learning how to create compiler using the LLVM infrastructure.

I hope you enjoyed my article and you can find me on

You can find me on: GitHub, LinkedIn, and Twitter.

Enjoy Programming 😋.

--

--

Amr Hesham
Amr Hesham

Written by Amr Hesham

Software Engineer | interested in Compilers, Language design

No responses yet