Skip to content

Commit 99a5e7b

Browse files
[mlir][NFC] Split registerAndParseCLIOptions() in mlir-opt (llvm#166538)
mlir-opt's registerAndParseCLIOptions() forces users to both register default MLIR options and parse the command line string. Custom mlir-opt implementations, however, may need to provide own options or own parsing. It seems that separating the two functions makes it easier to achieve necessary customizations. For example, one can register "default" options, then register custom options (not available in standard mlir-opt), then parse all of them. Other cases include two-stage parsing where some additional options become available based on parsed information (e.g. compilation target can allow additional options to be present).
1 parent c17a839 commit 99a5e7b

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ class MlirOptMainConfig {
359359
/// the loaded IR.
360360
using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
361361

362+
/// Register basic command line options.
363+
/// - toolName is used for the header displayed by `--help`.
364+
/// - registry should contain all the dialects that can be parsed in the source.
365+
/// - return std::string for help header.
366+
std::string registerCLIOptions(llvm::StringRef toolName,
367+
DialectRegistry &registry);
368+
369+
/// Parse command line options.
370+
/// - helpHeader is used for the header displayed by `--help`.
371+
/// - return std::pair<std::string, std::string> for
372+
/// inputFilename and outputFilename command line option values.
373+
std::pair<std::string, std::string> parseCLIOptions(int argc, char **argv,
374+
llvm::StringRef helpHeader);
375+
362376
/// Register and parse command line options.
363377
/// - toolName is used for the header displayed by `--help`.
364378
/// - registry should contain all the dialects that can be parsed in the source.

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,8 @@ processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
681681
return success();
682682
}
683683

684-
std::pair<std::string, std::string>
685-
mlir::registerAndParseCLIOptions(int argc, char **argv,
686-
llvm::StringRef toolName,
687-
DialectRegistry &registry) {
688-
static cl::opt<std::string> inputFilename(
689-
cl::Positional, cl::desc("<input file>"), cl::init("-"));
690-
691-
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
692-
cl::value_desc("filename"),
693-
cl::init("-"));
694-
// Register any command line options.
684+
std::string mlir::registerCLIOptions(llvm::StringRef toolName,
685+
DialectRegistry &registry) {
695686
MlirOptMainConfig::registerCLOptions(registry);
696687
registerAsmPrinterCLOptions();
697688
registerMLIRContextCLOptions();
@@ -706,11 +697,29 @@ mlir::registerAndParseCLIOptions(int argc, char **argv,
706697
interleaveComma(registry.getDialectNames(), os,
707698
[&](auto name) { os << name; });
708699
}
709-
// Parse pass names in main to ensure static initialization completed.
700+
return helpHeader;
701+
}
702+
703+
std::pair<std::string, std::string>
704+
mlir::parseCLIOptions(int argc, char **argv, llvm::StringRef helpHeader) {
705+
static cl::opt<std::string> inputFilename(
706+
cl::Positional, cl::desc("<input file>"), cl::init("-"));
707+
708+
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
709+
cl::value_desc("filename"),
710+
cl::init("-"));
710711
cl::ParseCommandLineOptions(argc, argv, helpHeader);
711712
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
712713
}
713714

715+
std::pair<std::string, std::string>
716+
mlir::registerAndParseCLIOptions(int argc, char **argv,
717+
llvm::StringRef toolName,
718+
DialectRegistry &registry) {
719+
auto helpHeader = registerCLIOptions(toolName, registry);
720+
return parseCLIOptions(argc, argv, helpHeader);
721+
}
722+
714723
static LogicalResult printRegisteredDialects(DialectRegistry &registry) {
715724
llvm::outs() << "Available Dialects: ";
716725
interleave(registry.getDialectNames(), llvm::outs(), ",");

0 commit comments

Comments
 (0)