Trouble with using PassManager to execute custom passes that have dependencies.


I wrote my own FunctionPass that depends on DominatorTree and LoopInfo passes. Then my own pass is executed outside the LLVM optimizer infrastructure (I.e., in a main file). However, I got the following error:

LLVM/llvm-3.3.src/lib/IR/PassManager.cpp:637: void llvm::PMTopLevelManager::schedulePass(llvm::Pass*): Assertion `PI && “Expected required passes to be initialized”’ failed.

Since the pass was not executed by opt, I took it that I did not need to register the pass or modify the LLVM code base to accommodate it … Then I try to fix the problem by calling INITIALIZE_PASS_DEPENDENCY right after char XXXPass::ID=0. Then the compiler complains something about it has to be declared inside the llvm namespace. Any hints on how to fix that?

Best Regards,

--------------------------- Code -----------------------------------

using namespace llvm;

class XXXPass : public FunctionPass {
XXXPass() : FunctionPass(ID) {}

void getAnalysisUsage(AnalysisUsage &AU) const {
virtual bool runOnFunction(Function &F) {
DominatorTree *DT = &getAnalysis();
LoopInfo *li = &getAnalysis();
return false;
static char ID;

char XXXPass::ID = 0;

int main(int argc, char **argv) {
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[2], Err, getGlobalContext());
if (!Mod) {
Err.print(argv[0], errs());
return 1;
Function *Func = Mod->getFunction(“test”);

PassManager PM;
PM.add(new XXXPass());*Mod);
return 0;

This just isn't supported by the existing pass management machinery. There
is no viable way to run them outside of the LLVM pass managers.

There is work to replace this part of LLVM's infrastructure and it will
make some of these kinds of things more obvious, but there will still be
requirements for passes to operate on dependencies.