我如何将可变数量的参数传递给LLVM OPT PASS

How do I pass a variable number of arguments to a LLVM opt pass?

本文关键字:参数传递 LLVM PASS OPT      更新时间:2023-10-16

我想将可变数量的参数传递给我的llvm opt pass。

为此,我做了类似的事情:

static cl::list<std::string> Files(cl::Positional, cl::OneOrMore);
static cl::list<std::string> Libraries("l", cl::ZeroOrMore);

但是,如果我现在调用类似的选择:

foo@foo-Ubuntu:~/llvm-ir-obfuscation$ opt -load cmake-build-debug/water/libMapInstWMPass.so -mapiWM programs/ll/sum100.ll -S 2 3 4  -o foo.ll
opt: Too many positional arguments specified!
Can specify at most 2 positional arguments: See: opt -help

,然后我会得到OPT最多接受2个位置参数的错误。

我在做什么错?

我认为问题是opt已经在解析其自己的参数,并且已经具有作为位置参数处理的比特码文件,因此拥有一个以上的位置参数会产生歧义。p>该文档将API解释,就好像它在独立应用程序中使用一样。因此,例如,如果您做这样的事情:

int main(int argc, char *argv[]) {
  cl::list<std::string> Files(cl::Positional, cl::OneOrMore);
  cl::list<std::string> Files2(cl::Positional, cl::OneOrMore);
  cl::list<std::string> Libraries("l", cl::ZeroOrMore);
  cl::ParseCommandLineOptions(argc, argv);
  for(auto &e : Libraries) outs() << e << "n";
  outs() << "....n";
  for(auto &e : Files) outs() << e << "n";
  outs() << "....n";
  for(auto &e : Files2) outs() << e << "n";
  outs() << "....n";
}

您得到这样的东西:

$ foo -l one two three four five six
one
....
two
three
four
five
....
six
....

现在,如果您围绕两个位置参数定义进行交换,或者甚至将Files2cl::OneOrMore选项更改为cl::ZeroOrMore,则将获得错误

$ option: error - option can never match, because another positional argument will match an unbounded number of values, and this option does not require a value!

个人,当我使用 opt i放置位置参数选项时,请执行类似的操作:

cl::list<std::string> Lists("lists", cl::desc("Specify names"), cl::OneOrMore);

可以做到这一点:

opt -load ./fooPass.so -foo -o out.bc -lists one ./in.bc -lists two

std::string上的迭代列表以我获得的方式相同:

one
two

@compor建议,这可能与opt和您自己的通过的交织论点有关。命令行库主要是为LLVM框架中的独立应用程序编写的。

但是,您可以做类似:

的事情
static cl::list<std::string> Args1("args1", cl::Positional, cl::CommaSeparated);
static cl::list<std::string> Args2("args2", cl::ZeroOrMore);

这具有一个优势,您可以使用逗号,例如arg1,arg2,...或使用标识符-args1 arg1 arg2 ...在命令行上输入多个参数;这些被插入到Args1列表中。如果您仅在命令行上提供单个位置参数arg,则Args1仅包含此参数。

另外,您可以在命令行上指定-args2 arg(命名为非置位选项(。这些将进入Args2列表。