[llvm-dev] Supporting sub commands in LLVM command line tools

Zachary Turner via llvm-dev llvm-dev at lists.llvm.org
Fri Jun 17 22:46:48 PDT 2016

Ahh.  I don't think you would necessarily need this change in order to make
that work.  There's nothing that says that cl options *have* to be global,
and indeed in this patch I have some examples (mostly in unit tests) of
making some options on the stack and then wiping the slate clean later to
create some new options.  So you could do something like this:

struct OptOptions : public LLVMOptions {
    cl::opt<bool> PrintCallgraph("print-callgraph");

class LLCOptions : public LLVMOptions {
   cl::opt<bool> NoFixup("mno-fixup");

std::unique_ptr<LLVMOptions> Options;

int main(int argc, char **argv) {
    if (argv[0] == "opt")
        Options = llvm::make_unique<OptOptions>();
   else if (argv[0] == "llc")
        Options = llvm::make_unique<LLCOptions>();
   // etc

You could, however, use this to make it so that if you just ran the llvm
binary by itself, you could invoke any of the subtools as a subcommand,
perhaps even combined with the above, so you could do something like:

$ opt -print-callgraph <etc>
$ llc -mno-fixup <etc>
$ llvm opt -print-callgraph <etc>
$ llvm llc -mno-fixup <etc>

On Fri, Jun 17, 2016 at 10:37 PM Mehdi Amini <mehdi.amini at apple.com> wrote:

> On Jun 17, 2016, at 10:29 PM, Zachary Turner <zturner at google.com> wrote:
> Not sure I follow how this would work.  "llvm" is an executable, and "opt"
> is a symlink to "llvm"?  How does llvm then detect that it needs to use the
> opt set of commands?
> Busybox-like: If (argv[0] == “opt”)
>> Mehdi
> That said, in principle sure you could have "llvm opt <opt-specific
> command syntax>" or "llvm llc <llc options>".  At some point you'd probably
> need to extend this to support nested subcommands, which I didn't attempt
> here.
> On Fri, Jun 17, 2016 at 6:58 PM Mehdi Amini <mehdi.amini at apple.com> wrote:
>> Hi,
>> I haven't looked at the implementation, but conceptually this looks nice!
>> We talked internally about an option to build something like a single
>> "llvm" binary that would be symlinked by opt/llc/etc. So that when you
>> invoke `opt`, it would run the same binary but internally the right
>> subcommand set of options would be used. The downside is that running
>> `ninja llvm-mc` would depends on every LLVM libraries though.
>> This is a bit orthogonal to what you’re doing, but I assume your patch
>> would help to build such an option right?
>>>> Mehdi
>> On Jun 17, 2016, at 5:00 PM, Zachary Turner via llvm-dev <
>> llvm-dev at lists.llvm.org> wrote:
>> Hi all,
>> I've written a patch <http://reviews.llvm.org/D21485> to support
>> subcommands in llvm command line tools.  This potentially has broad
>> interest (either positive or negative), so I figured I'd give a heads up
>> here instead of just on llvm-commits.
>> The motivation for this is that we frequently have tools with
>> incompatible sets of command line options.  I ran into this on llvm-pdbdump
>> and was debating breaking off some of the functionality into a separate
>> tool to make the command line interface more sensible, and to prevent
>> people getting confused about which options could be used with which other
>> options.
>> A better approach to this, in my opinion, is the use of sub commands.
>> This way all the options that can be used together are grouped together,
>> and you simply can't specify incompatible options at the same time.
>> There is more information in the patch, including some examples of what
>> it looks like, so if you're interested in this or have a strong opinion one
>> way or the other, let me know.
>> Note that this is an **opt in** feature, and if you continue using
>> cl::opt as you always have, this change should be invisible to you.
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160618/3e733c74/attachment.html>

More information about the llvm-dev mailing list