This repository contains simple code demonstrations of writing an LLVM pass, based on the blog post LLVM for Grad Students.
This project is licensed under the MIT License.
LLVM is a powerful compiler infrastructure that can be leveraged for various program analysis and transformation tasks, beyond just implementing new compiler optimizations. This repository provides a starting point for graduate students and researchers to explore using LLVM in their own work.
The main implementation code is located in skeleton/Skeleton.cpp. Configuring the build system can be a common pain point, but this repository aims to streamline that process so you can focus on the core LLVM pass development.
Use the following commands to build the project:
$ mkdir build
$ cd build
$ cmake ..
$ make
The commands above will produce a shared library (.so file) in the build directory, which contains the compiled LLVM pass. In this example, the output file will be named SkeletonPass.so.
To use the compiled pass when compiling a target C/C++ file, use the following command:
$ clang -fpass-plugin=`echo build/skeleton/SkeletonPass.*` target.c
Replace target.c with the path to your target source file.
If your pass needs to interact with a runtime library, you can compile the runtime code to an object file (.o) and link it with the instrumented target code:
$ cc -c rtlib.c # The runtime functions are in rtlib.c
$ clang -fpass-plugin=`echo build/skeleton/SkeletonPass.*` -c target.c
$ cc target.o rtlib.o # Link the object files to produce the executable
$ ./a.out
This approach allows you to implement more complex runtime behaviors in C and integrate them with your LLVM pass.
The blog post provides valuable information on understanding LLVM IR, writing passes, and exploring advanced topics like annotations. We recommend referring to the original post for a more comprehensive introduction to working with LLVM.
Happy hacking!