clang -O3 -emit-llvm -o source.ll source.cIt's not that complicated, but let's say that you're trying to instrument an existing project that's been built with autotools. Do you rewrite the `.in` files? Do you try to hack together a wrapper script that pretends like it's gcc but actually does all of the stuff above? What I'd really like to do is just put my custom pass into clang and use it instead:
opt -S -load custom_pass.so -custompass -o source_opt.ll source.ll
llvm-link -o out.ll source_opt.ll
llc -filetype=obj -o out.o out.ll
clang -O3 -"magic" source.cWell, as it so happens, LLVM has magic built-in, but it's not where you might think. There's no option to tell clang to load and run a custom pass in a certain phase. There is, however, an option to tell clang to load a custom extension, and there's a mechanism to allow that extension to register itself in a particular phase. This is just as good, so long as we're okay with the specific insertion points that LLVM provides (there are about half a dozen).
So what's the magic?
clang -Xclang -load -Xclang your_custom_pass.so ...And a couple of lines inserted at the end of your pass code. I've written some demo code to show how it works, and it's available on github: