Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add arguments for user defined passes in llvm

Now we are implementing a analysis pass for llvm, following this tutorial. and need to pass an additional argument to the plugin such as below:

opt -load /path/to/myplugin.so -mypass -mypass_option input.bc

However I did not find any manual telling me how to do. So I'm wondering whether it is possible in practice.

Thanks in advance.

like image 334
Hongxu Chen Avatar asked Nov 29 '12 13:11

Hongxu Chen


People also ask

What is pass in LLVM?

The “Hello” pass is designed to simply print out the name of non-external functions that exist in the program being compiled. It does not modify the program at all, it just inspects it. The source code and files for this pass are available in the LLVM source tree in the lib/Transforms/Hello directory.

How do you find the argument of a function in LLVM?

You can use Function::getArgumentList() method to get a list of function's arguments.

What is mem2reg?

-mem2reg : Promote Memory to Register This file promotes memory references to be register references. It promotes alloca instructions which only have loads and stores as uses.


1 Answers

You should use the CommandLine library which comes built-in with LLVM. Basically, you just put at the top of the .cpp file of the pass:

#include "llvm/Support/CommandLine.h"

static cl::opt<string> InputFilename("mypass_option", cl::desc("Specify input filename for mypass"), cl::value_desc("filename"));

But I recommend you check the above link, it has full reference + convenient quickstart section.

For an example, take a look at the built-in loop unrolling pass - it defines two unsigned and two boolean options, right at the top of the source file, by using cl::opt<unsigned> and cl::opt<bool>.

like image 81
Oak Avatar answered Oct 15 '22 04:10

Oak