Skip to content

[MLIR][mlir-opt] Add registerationAndParseCLIOptions for MlirOptMain. #70581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2023
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
19 changes: 19 additions & 0 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ class MlirOptMainConfig {
/// the loaded IR.
using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;

/// 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.
/// - return std::pair<std::string, std::string> for
/// inputFilename and outputFilename command line option values.
std::pair<std::string, std::string>
registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
DialectRegistry &registry);

/// Perform the core processing behind `mlir-opt`.
/// - outputStream is the stream where the resulting IR is printed.
/// - buffer is the in-memory file to parser and process.
Expand All @@ -245,6 +254,16 @@ LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
DialectRegistry &registry);

/// Implementation for tools like `mlir-opt`.
/// This function can be used with registrationAndParseCLIOptions so that
/// CLI options can be accessed before running MlirOptMain.
/// - inputFilename is the name of the input mlir file.
/// - outputFilename is the name of the output file.
/// - registry should contain all the dialects that can be parsed in the source.
LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
llvm::StringRef outputFilename,
DialectRegistry &registry);

/// Helper wrapper to return the result of MlirOptMain directly from main.
///
/// Example:
Expand Down
77 changes: 50 additions & 27 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
llvm::raw_string_ostream ostream(buffer);
if (useBytecode) {
if (failed(writeBytecodeToFile(op, ostream))) {
op->emitOpError() << "failed to write bytecode, cannot verify round-trip.\n";
op->emitOpError()
<< "failed to write bytecode, cannot verify round-trip.\n";
return failure();
}
} else {
Expand All @@ -281,7 +282,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
roundtripModule =
parseSourceString<Operation *>(ostream.str(), parseConfig);
if (!roundtripModule) {
op->emitOpError() << "failed to parse bytecode back, cannot verify round-trip.\n";
op->emitOpError()
Copy link
Contributor

Choose a reason for hiding this comment

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

whitespace changes? Did the file need reformatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whitespace changes? Did the file need reformatting?

Weird, I think my clang-format is acting out somehow here. Wait, the reformatting looks like needed since the original code has 88 characters in the line where llvm coding style says it should be "within 80 columns" 🤔.

<< "failed to parse bytecode back, cannot verify round-trip.\n";
return failure();
}
}
Expand All @@ -300,7 +302,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
}
if (reference != roundtrip) {
// TODO implement a diff.
return op->emitOpError() << "roundTrip testing roundtripped module differs from reference:\n<<<<<<Reference\n"
return op->emitOpError() << "roundTrip testing roundtripped module differs "
"from reference:\n<<<<<<Reference\n"
<< reference << "\n=====\n"
<< roundtrip << "\n>>>>>roundtripped\n";
}
Expand Down Expand Up @@ -443,6 +446,36 @@ static LogicalResult processBuffer(raw_ostream &os,
return sourceMgrHandler.verify();
}

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.
MlirOptMainConfig::registerCLOptions(registry);
registerAsmPrinterCLOptions();
registerMLIRContextCLOptions();
registerPassManagerCLOptions();
registerDefaultTimingManagerCLOptions();
tracing::DebugCounter::registerCLOptions();

// Build the list of dialects as a header for the --help message.
std::string helpHeader = (toolName + "\nAvailable Dialects: ").str();
{
llvm::raw_string_ostream os(helpHeader);
interleaveComma(registry.getDialectNames(), os,
[&](auto name) { os << name; });
}
// Parse pass names in main to ensure static initialization completed.
cl::ParseCommandLineOptions(argc, argv, helpHeader);
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
}

LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
std::unique_ptr<llvm::MemoryBuffer> buffer,
DialectRegistry &registry,
Expand Down Expand Up @@ -477,34 +510,13 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
/*insertMarkerInOutput=*/true);
}

LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
LogicalResult mlir::MlirOptMain(int argc, char **argv,
llvm::StringRef inputFilename,
llvm::StringRef outputFilename,
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("-"));

InitLLVM y(argc, argv);

// Register any command line options.
MlirOptMainConfig::registerCLOptions(registry);
registerAsmPrinterCLOptions();
registerMLIRContextCLOptions();
registerPassManagerCLOptions();
registerDefaultTimingManagerCLOptions();
tracing::DebugCounter::registerCLOptions();

// Build the list of dialects as a header for the --help message.
std::string helpHeader = (toolName + "\nAvailable Dialects: ").str();
{
llvm::raw_string_ostream os(helpHeader);
interleaveComma(registry.getDialectNames(), os,
[&](auto name) { os << name; });
}
// Parse pass names in main to ensure static initialization completed.
cl::ParseCommandLineOptions(argc, argv, helpHeader);
MlirOptMainConfig config = MlirOptMainConfig::createFromCLOptions();

// When reading from stdin and the input is a tty, it is often a user mistake
Expand Down Expand Up @@ -535,3 +547,14 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
output->keep();
return success();
}

LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
DialectRegistry &registry) {

// Register and parse command line options.
std::string inputFilename, outputFilename;
std::tie(inputFilename, outputFilename) =
registerAndParseCLIOptions(argc, argv, toolName, registry);

return MlirOptMain(argc, argv, inputFilename, outputFilename, registry);
}