irprinter is a command-line tool for exploring LLVM Intermediate Representation (IR) code. It allows users to print IR code for specific functions, which is particularly useful when dumping the entire translation unit would result in excessive output.
- Print LLVM IR code for a translation unit (C/C++) to the console.
- Modify and add compiler flags (e.g., replace -g with -O2) and regenerate the (modified) IR.
- Regex matching of (demangled) function names, with options to print:
- Function signatures only.
- Functions including their bodies.
- Dump the entire IR code of the translation unit.
See main.cpp for all possible command-line arguments.
Assume test.c contains the code:
int foo () {
return 2;
}
int main() {
int val;
val = foo();
return 0;
}
In this example we
- load
test.c
with standard Clang flags, - list all functions in
test.c
, - print the body of main,
- optimize the code with
-O3
, and finally, - print the body of main again.
ahueck@sys:~/irprint/install$ ./bin/irprinter ../test.c --
ir-printer> l
Match 1 [foo]:
; Function Attrs: noinline nounwind optnone uwtable
define i32 @foo() #0
Match 2 [main]:
; Function Attrs: noinline nounwind optnone uwtable
define i32 @main() #0
ir-printer> p main
Match 1 [main]:
; Function Attrs: noinline nounwind optnone uwtable
define i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
%3 = call i32 @foo()
store i32 %3, i32* %2, align 4
ret i32 0
}
ir-printer> f -O3
Set flag to -O3. Re-generating module...
ir-printer> p main
Match 1 [main]:
; Function Attrs: norecurse nounwind readnone uwtable
define i32 @main() local_unnamed_addr #1 {
ret i32 0
}
- CMake >= 3.20
- Clang/LLVM 12, 14, 18 (CMake needs to find the installation, see the LLVM CMake documentation or the CI workflow)
- C++17 compiler
In the root project folder, execute the following commands (see also CI workflow)
cmake -B build -DCMAKE_INSTALL_PREFIX=*your path*
cmake --build build --target install --parallel