Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ class MlirOptMainConfig {
/// the loaded IR.
using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;

/// Register basic command line options.
/// - toolName is used for the header displayed by `--help`.
/// - registry should contain all the dialects that can be parsed in the source.
/// - return std::string for help header.
std::string registerCLIOptions(llvm::StringRef toolName,
DialectRegistry &registry);

/// Parse command line options.
/// - helpHeader is used for the header displayed by `--help`.
/// - return std::pair<std::string, std::string> for
/// inputFilename and outputFilename command line option values.
std::pair<std::string, std::string> parseCLIOptions(int argc, char **argv,
llvm::StringRef helpHeader);

/// Register and parse command line options.
/// - toolName is used for the header displayed by `--help`.
/// - registry should contain all the dialects that can be parsed in the source.
Expand Down
33 changes: 21 additions & 12 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,8 @@ processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
return success();
}

std::pair<std::string, std::string>
mlir::registerAndParseCLIOptions(int argc, char **argv,
llvm::StringRef toolName,
DialectRegistry &registry) {
static cl::opt<std::string> inputFilename(
cl::Positional, cl::desc("<input file>"), cl::init("-"));

static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
cl::init("-"));
// Register any command line options.
std::string mlir::registerCLIOptions(llvm::StringRef toolName,
DialectRegistry &registry) {
MlirOptMainConfig::registerCLOptions(registry);
registerAsmPrinterCLOptions();
registerMLIRContextCLOptions();
Expand All @@ -706,11 +697,29 @@ mlir::registerAndParseCLIOptions(int argc, char **argv,
interleaveComma(registry.getDialectNames(), os,
[&](auto name) { os << name; });
}
// Parse pass names in main to ensure static initialization completed.
return helpHeader;
}

std::pair<std::string, std::string>
mlir::parseCLIOptions(int argc, char **argv, llvm::StringRef helpHeader) {
static cl::opt<std::string> inputFilename(
cl::Positional, cl::desc("<input file>"), cl::init("-"));

static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
cl::init("-"));
Comment on lines +705 to +710
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these two stay in mlir::registerCLIOptions since they are both registering the options rather than parsing?

cl::ParseCommandLineOptions(argc, argv, helpHeader);
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
Copy link
Contributor Author

@andrey-golubev andrey-golubev Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weiweichen i need the values here when returning from the "parse" method. if I move them into registration "as is", i'll lose the local variable access.

potentially, these could be moved to MlirOptMainConfigCLOptions though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentle ping. if this is fine, i'd merge it "as is". worst case I can make a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, this looks fine then.

}

std::pair<std::string, std::string>
mlir::registerAndParseCLIOptions(int argc, char **argv,
llvm::StringRef toolName,
DialectRegistry &registry) {
auto helpHeader = registerCLIOptions(toolName, registry);
return parseCLIOptions(argc, argv, helpHeader);
}

static LogicalResult printRegisteredDialects(DialectRegistry &registry) {
llvm::outs() << "Available Dialects: ";
interleave(registry.getDialectNames(), llvm::outs(), ",");
Expand Down