How to include a opt pass in clang driver

Hello,

I have written a pass for the IR and I can run it with opt -load lib/LLVMMyPass.so -mypass -myarguments -S -o output.ll < output.bc

I have registered my pass with the following code:

static RegisterPass
X(“mypass”, “MyPass Pass (with getAnalysisUsage implemented)”);

How do I include the same pass in the clang driver. I tried running the pass:

clang output.c -o output -Xclang -load -Xclang lib/LLVMMyPass.so -mypass -myarguments

However, the pass is not being run as I cannot see output from my pass. What is the standard way to do it?

I have also tried approach described here: https://www.cs.cornell.edu/~asampson/blog/clangpass.html . It didn’t work.

See RegisterStandardPasses in llvm/Transforms/IPO/PassManagerBuilder.h .

-Eli

Hi Eli,

I have tried that:

static void registerMyPass(const PassManagerBuilder &,
llvm::legacy::PassManagerBase &PM) {
PM.add(new MyPass());
}

static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_OptimizerLast,
registerMyPass);

It still couldn’t find my pass.

OptimizerLast doesn't run unless you turn on optimizations (-O2). Maybe that's the issue?

-Eli

Yeah it worked. Thank you so much! Correct command

clang output.c -o output -Xclang -load -Xclang lib/LLVMMyPass.so -mllvm -myarguments