Programming, philosophy, pedaling.

Introducing llvm-passgen

Sep 26, 2019

Tags: programming, devblog

It's a Rust tool announcement this time: llvm-passgen!1

llvm-passgen is a very small tool: all it does is generate valid build and source skeletons for LLVM passes.

This is a trivial task (it's only two or three files, depending on how you count), but one that I frequently forget how to do correctly:

Using llvm-passgen is really easy. You can install it from cargo:

$ cargo install llvm-passgen

By default, llvm-passgen will generate llvm::FunctionPasses:

llvm-passgen Foo
cd Foo/build
cmake ..

You can make it generate other kinds of passes with the --kind flag. module, function, and block are currently understood:

llvm-passgen Bar --kind module
cd Bar/build
cmake ..

You can also ask llvm-passgen to construct the pass in a different parent directory with --dest:

llvm-passgen Baz --dest /tmp --kind block
cd /tmp/Baz/build
cmake ..

Depending on your setup, cmake .. may not discover LLVM's CMake configuration by default. If you see errors stemming from the include directives, you can specify LLVM_DIR explicitly:

cd build
LLVM_DIR=/path/to/llvm/cmake cmake ..

Future work

LLVM has several other useful passes beyond the ones currently supported by llvm-passgen — adding support for e.g. LoopPass and CallGraphSCCPass (among others) is an obvious next step.

Parameterizing the CMake template might also make sense — the generated CMakeLists.txt currently makes assumptions about LLVM's build that appear to hold true for LLVM 6 and newer, but may not be the case for older versions.

  1. This is actually my second Rust tool. I didn't bother writing an introduction post for my first because it was my first ever Rust project.