Lesson 6: LLVM
What is LLVM?
- LLVM is a very popular compiler infrastructure for C and other languages, including Rust.
- It has a nice SSA-form IR and a nice API for manipulating it.
- In one sense, it is like a supercharged version of Bril. (Not coincidental because I modeled Bril after it.)
- It is also, in a sense, the opposite of Bril because it is real infrastructure that the world really runs on for lots of software out there. It’s likely that some of the software you’re using to watch this video, somewhere along the chain, was compiled with LLVM.
- So it is 10,000 times more realistic. Which is great, but it means that some ways Bril is “idealized” will be ruthlessly not-idealized in LLVM.
- Did I mention that LLVM is written in C++? So we’re going to write in C++ (a sharp contrast from Bril, where any language will do).
- We will follow an old tutorial I wrote.
- You will need to install LLVM. (And also CMake.)
- Not covered in detail here. It’s probably in your package manager. There are also tips in that blog post.
- For example, try
brew install cmake llvmusing Homebrew on macOS. (On macOS, please install from Homebrew instead of using the version that comes with Xcode.)
- Make sure you can do
- Compile C programs to LLVM IR:
clang -emit-llvm -S -o - something.c
- Show off the language reference. (RTFM often!)
- Show off the Doxygen pages (and how to Google for them).
Writing a Pass
Please follow the “LLVM for Grad Students” tutorial. (The second video walks through the same steps.)
- Follow the LLVM tutorial blog post far enough to implement a pass that changes program execution.
- This is intentionally open-ended. You can be as ambitious or as unambitious as you want.
- An example of an unambitious but acceptable task would be to print out a message every time the program uses floating-point division.
- An example of an ambitious task would be to implement a fancy optimization on LLVM IR and make sure it speeds things up in actual wall-clock time execution.
- Find a real-ish C/C++ program somewhere and run your pass on it to observe the results.