16

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.

Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85
  • That particular example is not optimal because the idiom is to read from `stdin`. Presumably you have another use case in mind? – Brian Cain Nov 29 '12 at 13:36
  • What kind of argument do you need? As far as I know a pass should take everything it needs from the IR and output IR again. – Tobias Langner Nov 29 '12 at 13:40
  • @TobiasLangner It's some argument like the analysis level or analysis location of the `-mypass` pass. Maybe it is like the gcc optimization level `-O1`,`-O2`,etc. – Hongxu Chen Nov 29 '12 at 13:46
  • @BrianCain I was considering to read the `mypass_option` from a file, however it seems that I have to modify the specified file every time I use the pass. – Hongxu Chen Nov 29 '12 at 13:48

1 Answers1

16

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

Oak
  • 26,231
  • 8
  • 93
  • 152