Skip to content

Commit 77c6339

Browse files
authored
[MLIR][mlir-opt] Add registerationAndParseCLIOptions for MlirOptMain. (#70581)
Seprate registeration and CLI parsing from `MlirOptMain` to `mlir::registrationAndParseCLIOptions` and a new `MlirOptMain` so that external tools to drive `mlir-opt` can call these two new APIs to get CLI option values before running `MlirOptMain`.
1 parent 0a29879 commit 77c6339

File tree

2 files changed

+69
-27
lines changed

2 files changed

+69
-27
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ class MlirOptMainConfig {
229229
/// the loaded IR.
230230
using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
231231

232+
/// Register and parse command line options.
233+
/// - toolName is used for the header displayed by `--help`.
234+
/// - registry should contain all the dialects that can be parsed in the source.
235+
/// - return std::pair<std::string, std::string> for
236+
/// inputFilename and outputFilename command line option values.
237+
std::pair<std::string, std::string>
238+
registerAndParseCLIOptions(int argc, char **argv, llvm::StringRef toolName,
239+
DialectRegistry &registry);
240+
232241
/// Perform the core processing behind `mlir-opt`.
233242
/// - outputStream is the stream where the resulting IR is printed.
234243
/// - buffer is the in-memory file to parser and process.
@@ -245,6 +254,16 @@ LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
245254
LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
246255
DialectRegistry &registry);
247256

257+
/// Implementation for tools like `mlir-opt`.
258+
/// This function can be used with registrationAndParseCLIOptions so that
259+
/// CLI options can be accessed before running MlirOptMain.
260+
/// - inputFilename is the name of the input mlir file.
261+
/// - outputFilename is the name of the output file.
262+
/// - registry should contain all the dialects that can be parsed in the source.
263+
LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef inputFilename,
264+
llvm::StringRef outputFilename,
265+
DialectRegistry &registry);
266+
248267
/// Helper wrapper to return the result of MlirOptMain directly from main.
249268
///
250269
/// Example:

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

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
268268
llvm::raw_string_ostream ostream(buffer);
269269
if (useBytecode) {
270270
if (failed(writeBytecodeToFile(op, ostream))) {
271-
op->emitOpError() << "failed to write bytecode, cannot verify round-trip.\n";
271+
op->emitOpError()
272+
<< "failed to write bytecode, cannot verify round-trip.\n";
272273
return failure();
273274
}
274275
} else {
@@ -281,7 +282,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
281282
roundtripModule =
282283
parseSourceString<Operation *>(ostream.str(), parseConfig);
283284
if (!roundtripModule) {
284-
op->emitOpError() << "failed to parse bytecode back, cannot verify round-trip.\n";
285+
op->emitOpError()
286+
<< "failed to parse bytecode back, cannot verify round-trip.\n";
285287
return failure();
286288
}
287289
}
@@ -300,7 +302,8 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
300302
}
301303
if (reference != roundtrip) {
302304
// TODO implement a diff.
303-
return op->emitOpError() << "roundTrip testing roundtripped module differs from reference:\n<<<<<<Reference\n"
305+
return op->emitOpError() << "roundTrip testing roundtripped module differs "
306+
"from reference:\n<<<<<<Reference\n"
304307
<< reference << "\n=====\n"
305308
<< roundtrip << "\n>>>>>roundtripped\n";
306309
}
@@ -443,6 +446,36 @@ static LogicalResult processBuffer(raw_ostream &os,
443446
return sourceMgrHandler.verify();
444447
}
445448

449+
std::pair<std::string, std::string>
450+
mlir::registerAndParseCLIOptions(int argc, char **argv,
451+
llvm::StringRef toolName,
452+
DialectRegistry &registry) {
453+
static cl::opt<std::string> inputFilename(
454+
cl::Positional, cl::desc("<input file>"), cl::init("-"));
455+
456+
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
457+
cl::value_desc("filename"),
458+
cl::init("-"));
459+
// Register any command line options.
460+
MlirOptMainConfig::registerCLOptions(registry);
461+
registerAsmPrinterCLOptions();
462+
registerMLIRContextCLOptions();
463+
registerPassManagerCLOptions();
464+
registerDefaultTimingManagerCLOptions();
465+
tracing::DebugCounter::registerCLOptions();
466+
467+
// Build the list of dialects as a header for the --help message.
468+
std::string helpHeader = (toolName + "\nAvailable Dialects: ").str();
469+
{
470+
llvm::raw_string_ostream os(helpHeader);
471+
interleaveComma(registry.getDialectNames(), os,
472+
[&](auto name) { os << name; });
473+
}
474+
// Parse pass names in main to ensure static initialization completed.
475+
cl::ParseCommandLineOptions(argc, argv, helpHeader);
476+
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
477+
}
478+
446479
LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
447480
std::unique_ptr<llvm::MemoryBuffer> buffer,
448481
DialectRegistry &registry,
@@ -477,34 +510,13 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
477510
/*insertMarkerInOutput=*/true);
478511
}
479512

480-
LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
513+
LogicalResult mlir::MlirOptMain(int argc, char **argv,
514+
llvm::StringRef inputFilename,
515+
llvm::StringRef outputFilename,
481516
DialectRegistry &registry) {
482-
static cl::opt<std::string> inputFilename(
483-
cl::Positional, cl::desc("<input file>"), cl::init("-"));
484-
485-
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
486-
cl::value_desc("filename"),
487-
cl::init("-"));
488517

489518
InitLLVM y(argc, argv);
490519

491-
// Register any command line options.
492-
MlirOptMainConfig::registerCLOptions(registry);
493-
registerAsmPrinterCLOptions();
494-
registerMLIRContextCLOptions();
495-
registerPassManagerCLOptions();
496-
registerDefaultTimingManagerCLOptions();
497-
tracing::DebugCounter::registerCLOptions();
498-
499-
// Build the list of dialects as a header for the --help message.
500-
std::string helpHeader = (toolName + "\nAvailable Dialects: ").str();
501-
{
502-
llvm::raw_string_ostream os(helpHeader);
503-
interleaveComma(registry.getDialectNames(), os,
504-
[&](auto name) { os << name; });
505-
}
506-
// Parse pass names in main to ensure static initialization completed.
507-
cl::ParseCommandLineOptions(argc, argv, helpHeader);
508520
MlirOptMainConfig config = MlirOptMainConfig::createFromCLOptions();
509521

510522
// When reading from stdin and the input is a tty, it is often a user mistake
@@ -535,3 +547,14 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
535547
output->keep();
536548
return success();
537549
}
550+
551+
LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
552+
DialectRegistry &registry) {
553+
554+
// Register and parse command line options.
555+
std::string inputFilename, outputFilename;
556+
std::tie(inputFilename, outputFilename) =
557+
registerAndParseCLIOptions(argc, argv, toolName, registry);
558+
559+
return MlirOptMain(argc, argv, inputFilename, outputFilename, registry);
560+
}

0 commit comments

Comments
 (0)