Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit 1b16481

Browse files
[mlir][NFC] Split registerAndParseCLIOptions() in mlir-opt (#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 3f4ba0b commit 1b16481

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
@@ -299,6 +299,20 @@ class MlirOptMainConfig {
299299
/// the loaded IR.
300300
using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
301301

302+
/// Register basic command line options.
303+
/// - toolName is used for the header displayed by `--help`.
304+
/// - registry should contain all the dialects that can be parsed in the source.
305+
/// - return std::string for help header.
306+
std::string registerCLIOptions(llvm::StringRef toolName,
307+
DialectRegistry &registry);
308+
309+
/// Parse command line options.
310+
/// - helpHeader is used for the header displayed by `--help`.
311+
/// - return std::pair<std::string, std::string> for
312+
/// inputFilename and outputFilename command line option values.
313+
std::pair<std::string, std::string> parseCLIOptions(int argc, char **argv,
314+
llvm::StringRef helpHeader);
315+
302316
/// Register and parse command line options.
303317
/// - toolName is used for the header displayed by `--help`.
304318
/// - 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
@@ -554,17 +554,8 @@ static LogicalResult processBuffer(raw_ostream &os,
554554
return sourceMgrHandler.verify();
555555
}
556556

557-
std::pair<std::string, std::string>
558-
mlir::registerAndParseCLIOptions(int argc, char **argv,
559-
llvm::StringRef toolName,
560-
DialectRegistry &registry) {
561-
static cl::opt<std::string> inputFilename(
562-
cl::Positional, cl::desc("<input file>"), cl::init("-"));
563-
564-
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
565-
cl::value_desc("filename"),
566-
cl::init("-"));
567-
// Register any command line options.
557+
std::string mlir::registerCLIOptions(llvm::StringRef toolName,
558+
DialectRegistry &registry) {
568559
MlirOptMainConfig::registerCLOptions(registry);
569560
registerAsmPrinterCLOptions();
570561
registerMLIRContextCLOptions();
@@ -579,11 +570,29 @@ mlir::registerAndParseCLIOptions(int argc, char **argv,
579570
interleaveComma(registry.getDialectNames(), os,
580571
[&](auto name) { os << name; });
581572
}
582-
// Parse pass names in main to ensure static initialization completed.
573+
return helpHeader;
574+
}
575+
576+
std::pair<std::string, std::string>
577+
mlir::parseCLIOptions(int argc, char **argv, llvm::StringRef helpHeader) {
578+
static cl::opt<std::string> inputFilename(
579+
cl::Positional, cl::desc("<input file>"), cl::init("-"));
580+
581+
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
582+
cl::value_desc("filename"),
583+
cl::init("-"));
583584
cl::ParseCommandLineOptions(argc, argv, helpHeader);
584585
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
585586
}
586587

588+
std::pair<std::string, std::string>
589+
mlir::registerAndParseCLIOptions(int argc, char **argv,
590+
llvm::StringRef toolName,
591+
DialectRegistry &registry) {
592+
auto helpHeader = registerCLIOptions(toolName, registry);
593+
return parseCLIOptions(argc, argv, helpHeader);
594+
}
595+
587596
static LogicalResult printRegisteredDialects(DialectRegistry &registry) {
588597
llvm::outs() << "Available Dialects: ";
589598
interleave(registry.getDialectNames(), llvm::outs(), ",");

0 commit comments

Comments
 (0)