How compilers work: introduction to LLVM passes
LLVM is a compiler infrastructure project most known for Clang - the C and C++ compiler frontend. In this presentation, I will show how to develop an LLVM pass - the key component in any code transformation, optimisation or analysis used by LLVM. The goal of this talk is to present the full workflow - C++ implementation, CMake set-up, execution and testing.
LLVM provides a remarkably powerful and expressive modern C++ API for code analysis and transformation. It is a great environment for learning how compilers work and for experimenting with various ideas around code generation and programming languages. In a few relatively simple steps, LLVM allows one to build a simple code analysis tool. In this talk, I would like to showcase this and provide the required building blocks so that further experimenting becomes easy. I will walk through the infrastructure available in LLVM, present a few interesting example passes and demonstrate how to build and maintain them out-of-tree (i.e. without requiring or making any changes to the LLVM codebase). I will also demonstrate how to build a standalone LLVM-based analysis tool as well as how to set up LLVM compiler tests to maintain correctness. All examples will be demonstrated live.
This is a tutorial for aspiring LLVM developers as well as those interested in learning more about how modern compilers work. It is a self-contained crash-course that does not require any prior knowledge about compilers or LLVM. Some familiarity with CMake and LLVM IR (or any other assembly language) will be helpful, but is not essential. The presented examples don't require building LLVM from sources and can be tested with the pre-build packages available online. All examples are available on GitHub: https://github.com/banach-space/llvm-tutor
- Background information and development set-up
- Implementing and registering a pass (including CMake set-up)
- Examples of analysis and transformation passes
- Running and debugging an out-of-tree pass
- Integrating a pass into an out-of-tree LLVM-based tool
- Integration with clang/opt/bugpoint
- Setting up and running LIT tests for out-of-tree passes
Contributor to LLVM, FOSS enthusiast, author and maintainer of popular LLVM and Clang tutorials. Compiler engineer at Arm. Aspiring highlander.