4

I created my own LLVM optimization pass in LLVM 3.7.0 . I want to use this pass within a cmake project. I need to run the pass as last, after all optimization passes of -O2 (or -O3) are executed by clang (or clang++).

Unfortunately, I did not find a mechanism to invoke the pass by passing the flag directly from the clang (if you point me out a way to do that, this would already be helpful).

Assuming there is no way to run the pass by giving a flag to clang, I need an extra optimization pass in my toolchain to be placed between the compilation and the linking phase. I need it throughout the whole cmake project.

The commands I would need to generate a binary from a two source files are:

clang -c -g -emit-llvm -O3 mySource0.c -o mySource0.bc
clang -c -g -emit-llvm -O3 mySource1.c -o mySource.bc
llvm-link mySource0.bc mySource1.bc -o main.bc
opt -load myAnalysis.so -myAnalysis main.bc -o main.analysis.bc
clang <libraryRelatedFlags> main.analysis.bc -o myExecutable

My pass is registered as:

static RegisterPass<myAnalysis> X("myAnalysis", "Implement my analysis", false, false);

as in: http://llvm.org/docs/WritingAnLLVMPass.html#basic-code-required

2
  • I'm not at all familiar with custom llvm passes, but could adriansampson.net/blog/clangpass.html help? Commented Oct 23, 2015 at 9:53
  • I tried that... But I did not succeed to generate the possibility to enable/disable my pass pass via clang. Following that procedure, thought, I arrived to integrate my pass in one of the optimization level (e.g. my pass was always executed when -O3 was given). This unfortunately generates other issues... Commented Oct 23, 2015 at 11:34

1 Answer 1

1

If I understand your question correctly, you are simply aiming to add your pass so that it is run under the -O3.

You'll need to edit $(llvm-dir)/tools/opt/opt.cpp to get your pass to run -O3. You'll need to find where the OptLevelO3 bool is used to add passes and make sure to add your pass there as well.

If instead you just want your pass to be run on it's own flag you'll need to initialize your pass properly on top of registering it. We can look at DependenceAnalysis.cpp as a good example of how to do this:

INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da", "Dependence Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_PASS_END(DependenceAnalysis, "da", "Dependence Analysis", true, true)

You also mentioned that you want your pass to run after some other passes. Simply mark them as DA did it with:

INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)

to make sure that your pass is run AFTER the pass that you want to depend on.

3
  • If the answer is correct, would you mind marking it as such? :)
    – TheBat
    Commented Nov 6, 2015 at 22:30
  • @TheBat I saw a different answer (stackoverflow.com/a/29921436/577165), which needs to do something more than what you described here. Can you clarify it?
    – Infinite
    Commented Feb 18, 2018 at 22:16
  • Do you run this pass using opt -load /path/to/your/shared/lib -flag /path/to/your/bcfile ?
    – lava_07
    Commented Nov 10, 2020 at 8:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.