<div dir="ltr">Is anyone currently working on overhauling the command-line processing code?<div><br></div><div><br></div><div>We're currently having some design issues with this component, and I'd like to start a larger conversation on it.  For context, I am talking from an "LLVM as a library" perspective instead of an "LLVM as a set of tools" perspective.  In a nut-shell, the problems we are facing are as follows:</div>

<div><br></div><div>1. As per bug 11944, the command-line processing logic accounts for a non-trivial amount of global constructors and leads to memory that is allocated when the containing library is loaded, and not freed until the containing library is unloaded.  Granted, this is not a lot of data...</div>

<div><br></div><div>2. Command-line options are currently used for codegen options in inherently non-thread-safe ways.</div><div><br></div><div><br></div><div>(1) is mostly self-explanatory and has a bug about it, but (2) requires a bit more explanation.  Take for example the "-unroll-threshold" option in LoopUnroll.  If I'm setting up a pass pipeline programmatically, I add the LoopUnroll pass to the PassManager, but I cannot set the unroll threshold without calling cl::ParseCommandLineOptions() [or perhaps some ugly hacks that involve getting at and modifying the global state].  In addition to being awkward, this is not thread safe!  I cannot run two PassManager pipelines concurrently with different unroll threshold values.  In this case, I am singling out the LoopUnroll pass, but this design is very prevalent within the LLVM analysis/transformation/codegen infrastructure.  This has no effect on users of opt/llc as tools, but library users can be greatly affected by this.</div>

<div><br></div><div><br></div><div>Ideally, what I would like to see is a separation between the definition of analysis/transformation/codegen options and their value storage.  To get the conversation started, I would like to propose the following:</div>

<div><br><br></div><div><br>Requirements<br></div><div><br></div><div>- Make it easy for passes to declare arbitrary options, just like they do now<br></div><div>- Let different pass pipelines have different sets of option values<br>
</div><div>- Keep the option registry dynamic, so plugins loaded with "-load" can register new options<br></div><div>- Let option values be parseable from the command-line (for opt, llc, ...)<br><br><br></div><div>
Implementation<br><br></div><div>As a first design draft, I propose that cl::opt and friends be extended to support argument storage in an LLVMContext.  Instead of storing the value directly in a cl::opt instance or specifying a global variable, this new storage would utilize a StringMap stored in an LLVMContext.  As a consequence, parsing would be delayed until the option is read.<br>
<br></div><div><br>* Command Line Parsing<br><br>The cl::ParseCommandLineOptions() call would take an additional parameter, an LLVMContext reference.  Global options would be written directly to their cl::opt instances as they are now, but per-context options will be copied in the LLVMContext instance.  Tools would use something like:<br>
<br></div><div>LLVMContext &Ctx = getGlobalContext();<br></div><div>cl::ParseCommandLineOptions(argc, argv, "my tool", Ctx);<br><br></div><div>In addition, library users can use a new API:<br><br></div><div>
LLVMContext MyCtx;<br></div><div>cl::ParseContextOptions(ArgList, MyCtx);<br><br></div><div>which will only parse per-context options (and not overwrite any global state).<br><br></div><div>* Reading Option Values<br><br>
</div><div>For per-context options, an LLVMContext would be required when reading back the option values.  For example, in the LoopUnroll example, you could write:<br><br></div><div>unsigned Threshold = UnrollThreshold(Mod.getContext());<br>
<br></div><div>This would get the "-unroll-threshold" option value for the context for the current module.  Parsing of the value would be delayed until the value is read, since the value would need to be stored as a string in the context.  Global options can be used as they currently are.  It would be a run-time error to read a per-context option without a context parameter.<br>
</div><div><br></div><div><br></div><div>Open Questions<br><br></div><div>- Can we just make all options per-context, and assign tool options to the global context?  May require special handling for "-debug", "-time-passes", and friends.<br>
<br></div><div>- Alternatively, we could try to just eliminate the codegen options altogether and rely on per-module data, like the new attributes functionality.  But this seems like it may be less flexible and would require the Module to know what options to use.  Supporting command-line options would then require changes to the Module instance.<br>
</div><div><br><br></div>This is just a first-pass idea for making options more scalable to library users.  Please let me know if you have any other ideas, or if someone is already working on this.<br><div><div><div><br></div>
-- <br>
<br><div>Thanks,</div><div><br></div><div>Justin Holewinski</div>
</div></div></div>