Integrating llvm pass with pass manager


I have followed steps given in -, to integrate my pass with pass manager and run it with clang. I am able to run my pass with opt - opt -mypass but when I try to run it with clang, I always get an error - unknown argument: '-mypass'. Please help me for the same. I would be really grateful.


-mypass should be only recognize by the backend part (i.e. llvm). You should add -mllvm to tell clang the following argument have to be passed to the backend part.

Can you please tell me when I need to give this option and how. I am sorry I am new to this. Thank you so much for responding.

Do you pull clang under llvm/tools and compile it as well? In theory, if opt recognize the option, so does clang -mllvm.

Look back to the SO link [1] you posted, I assume you register your pass by writing something like below already,


ModulePass *llvm::createYourPass() { return new YourPass(); }

The only suggestion I can give is looking at other existing pass to see what you might miss. For example, take a look on X86OptimizeLEAs.cpp. It works like the following ways.

$ clang -mllvm -disable-x86-lea-opt test.c
$ opt -disable-x86-lea-opt test.ll

On the other hand, Mem2Reg.cpp fail to be used on clang.

$ clang -mllvm -mem2reg test.c
$ clang (LLVM option parsing): Unknown command line argument ‘-mem2reg’. Try: ‘clang (LLVM option parsing) -help’

I guess you need to implement the code below for your pass just like X86OptimizeLEAS.cpp does.

static cl::opt
DisableX86LEAOpt(“disable-x86-lea-opt”, cl::Hidden,
cl::desc(“X86: Disable LEA optimizations.”),

Clang doesn’t support adding passes from the command line the way opt does. Opt has special parsing in opt.cpp for this that clang doesn’t have.

I’m not sure what the correct way to do this is. I think your plugin needs to do something to tell clang/llvm when to run the pass. I’ll try to look later when I’m back at a computer.

You need to use RegisterStandardPasses to add it to the default pipeline automatically. You can find an example here:


I have taken SimplePass and added in Transform directory, “libLLVMSimplePass.a” is built but I can not see this pass in opt —help. I don’t even see the name of pass while registering it in the example.

The SimplePass example is intended to be built out of tree, so I’ve no idea what happens if you try building it in tree. I wouldn’t expect to see it in the opt --help output, because it doesn’t define its own command-line options, it just adds itself to the default optimiser pipeline (at the end, and even if you’re running at -O0). You can see that it works by building it with its own CMake build system and then passing -Xclang -load -Xclang ./ to your clang command line.


Thats not what I am looking for. I want to run it with clang by just giving name of my pass. I have expalined it in my previous mail.