Sep 26, 2019 Tags: devblog, programming, rust
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:
I consistently forget which LLVM CMake files I want to include
within my CMakeLists
.
llvm-passgen provides a sensible set of defaults. In particular, LLVMConfig
is always
included, preventing many of the annoying compiler/linker flag issues that show up with custom
builds.
Similarly, llvm-passgen uses LLVM’s add_llvm_library
helper to specify your pass.
No more fiddling with add_library
to approximate what LLVM expects from your build.
Setting up an LLVM pass is an exercise in tedium — there are a few basic lines and variables that every pass’s source code needs, and debugging their absence can be annoying. llvm-passgen’s pass templates ensure that your boilerplate is never forgotten.
Where appropriate, llvm-passgen’s templates provide doInitialization
and doFinalization
stubs. These are easy to forget and accidentally replace with constructor usage, which
LLVM explicitly disallows for basic block and function passes!
Using llvm-passgen is really easy. You can install it from cargo
:
1
$ cargo install llvm-passgen
By default, llvm-passgen will generate llvm::FunctionPass
es:
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
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.