Running a pass from within a another pass

I wrote my own in-tree LLVM pass using the new pass manager. I am now trying to run a sequence of other passes from within my pass before my pass goes on to do other things. My code that tries to run other passes looks something like this:

static void runPasses(Function &F) {
  FunctionPassManager FPM;
  FunctionAnalysisManager FAM;
  FPM.addPass(InstCombinePass());, FAM);

Building llvm leads to these linker errors:

/usr/git/llvm-fork/llvm/include/llvm/IR/PassManagerInternal.h:89: error: undefined reference to 'llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)'
/usr/git/llvm-fork/llvm/lib/Transforms/Utils/MyOwnPass.cpp:667: error: undefined reference to 'llvm::InstCombinePass::InstCombinePass()'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Are there any existing examples or documentation that I can take a look at to understand how to run other passes from within my own pass?

In this particular example, you’ll have to link against LLVMInstCombine. If you’re using CMake, you need to either add InstCombine to your LINK_COMPONENTS argument of add_llvm_library, or you need to use set(LLVM_LINK_COMPONENTS InstCombine) before your add_llvm_library line. This is, of course, assuming you have your own pass library defined via your own CMakeLists.txt file. If you’re modifying an existing one, you’ll have to make sure it links against InstCombine’s library.

That said, if you’re attempting to run the pass from inside another pass, creating an brand new FunctionAnalysisManager and trying to use it that way may result in some “analysis pass not registered” errors when you call run(). It’s better to pass the same manager your own pass uses since cross-registration has already occurred on that one.

You’re not really supposed to run passes within other passes. If you want other passes run first, just run them earlier in the pass pipeline