Integrating llvm pass with pass manager

Hello,

I have followed steps given in - https://stackoverflow.com/questions/29910051/integrating-llvm-passes/48142693#48142693, 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.

Regards,
Sangeeta

-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,

INITIALIZE_PASS_BEGIN

INITIALIZE_PASS_END
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.”),
cl::init(false));

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:

https://github.com/CompilerTeaching/SimplePass/blob/ba5248a9ea0bd9e1fab3b1f8a5c85d6e0db57acd/SimplePass.cc#L116

David

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 ./SimplePass.so to your clang command line.

David

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.