LLVM Pass Template simplifies the process of creating your own LLVM passes. This project provides a ready-to-use template that allows immediate bootstrapping of out-of-tree passes without the hassle of building LLVM from scratch. With clear instructions and example code, developers can dive straight into LLVM pass development.
The LLVM Pass Template repository serves as a robust starting point for developing custom LLVM passes using LLVM 21. Built with the foundational knowledge from llvm-tutor and the official guide on Writing an LLVM Pass, it streamlines the process of creating out-of-tree LLVM passes, which eliminates the need to build LLVM from source.
Key Features
- Quick Bootstrap: Instantly initiate the creation of your own LLVM passes with minimal overhead.
- Example Included: The repository includes an example pass called FunctionCounter, which counts the number of functions in an input LLVM IR file and outputs the result to the console.
Core Usage
To create an LLVM IR file from C/C++, utilize the clang compiler for a more human-readable output:
${LLVM_DIR}/bin/clang -fno-discard-value-names -S -emit-llvm <file>.c -o <file>.ll
To proceed with building the pass, execute the following commands:
export LLVM_DIR=<location_of_LLVM_21>
mkdir build
cd build
cmake -DLT_LLVM_INSTALL_DIR=${LLVM_DIR} .. # -G Ninja
make # ninja
A dedicated build folder is employed to enhance IDE support via the .clangd file. Adjust the path as necessary if using a different build directory.
To run the pass on an LLVM IR file via the opt tool:
${LLVM_DIR}/bin/opt \
--load-pass-plugin build/lib/libFunctionCounter.{dylib|so} \
--passes="print<function-counter>" \
-disable-output \
<file>.ll
For those who prefer to run the analysis pass through an executable, the func-count tool offers a user-friendly command line access. This tool is defined in FuncCountMain.cpp and allows execution of FunctionCounter without involving opt:
<build_dir>/bin/func-count <file>.ll
Testing Capabilities
To execute tests, llvm-lit must be installed, which can be done via pip or brew (on macOS):
{pip | brew} install lit
Run the tests with the following command:
lit ./build/test
This repository is designed for both beginners and experienced developers who seek to explore and extend their capabilities with LLVM, providing essential tools and examples to facilitate the creation of sophisticated LLVM passes.
No comments yet.
Sign in to be the first to comment.