ENOSUCHBLOG

Programming, philosophy, pedaling.


Introducing llvm-passgen

Sep 26, 2019     Tags: devblog, programming, rust    

This post is at least a year old.

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:

1
$ cargo install llvm-passgen

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

1
2
3
4
llvm-passgen Foo
cd Foo/build
cmake ..
make

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

1
2
3
4
llvm-passgen Bar --kind module
cd Bar/build
cmake ..
make

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

1
2
3
4
llvm-passgen Baz --dest /tmp --kind block
cd /tmp/Baz/build
cmake ..
make

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:

1
2
3
cd build
LLVM_DIR=/path/to/llvm/cmake cmake ..
make

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.