langston-barrrett.github.io
Learning LLVM
This is something I wrote to someone about starting out with LLVM.
Here are a scattered collection of thoughts and resources that might be helpful to you as you start learning about LLVM:
-
The official Language Reference is the most authoritative discussion of the semantics of LLVM IR, though it's pretty informal.
-
Compiler Explorer is a tool you can use to view the output of many versions of many compilers. The
-emit-llvm
flag for Clang shows you LLVM bitcode. -
GLLVM is a tool to compile multi-file programs to a single LLVM bitcode module.
-
getelementptr
is hard to understand. Here are some resources: -
When compiling to bitcode that you intend to read, the
-fno-discard-value-names
flag to Clang causes it to not rename everything to something like%5
, but rather keep the names present in the program. For C++ code,-fno-rtti
can also simplify things. -
I don't know if this will be relevant for you, but if you ever find yourself writing an LLVM pass, there are two "pass managers" and the documentation for the new one is... nonexistant? Here's the only resource I've found that explains it at all, and it has some nice example passes: https://github.com/banach-space/llvm-tutor
-
For a very general overview of what LLVM is and how it works, see the LLVM chapter of AOSA
-
Helpful blogs and posts:
-
Gotchas:
- Structs that are passed by-value at the C level can be "unpacked" at the LLVM level:
struct s { int x; double y; }; void f(struct s ss) {}
define dso_local void @f(i32 %0, double %1) local_unnamed_addr #0 { ret void }
struct s { int x; int y; }; struct s f(struct s s) { ++s.y; return s; }
define dso_local i64 @f(i64 %0) local_unnamed_addr #0 { %2 = add i64 %0, 4294967296 ret i64 %2 }