Search code examples
c++llvmllvm-c++-apillvm-ir

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.


Solution

  • 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>.