Skip to content
Closed
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
35 changes: 25 additions & 10 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,25 @@ class MlirOptMainConfig {
std::string generateReproducerFileFlag = "";
};

/// VPUX-specific method to get value for arch kind from command line
/// and register HW-specific passes and pipelines
using AdditionalRegistrationFn =
std::function<void(llvm::StringRef helpHeader)>;

/// This defines the function type used to setup the pass manager. This can be
/// used to pass in a callback to setup a default pass pipeline to be applied on
/// 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 All @@ -326,12 +335,18 @@ LogicalResult MlirOptMain(llvm::raw_ostream &outputStream,
/// Implementation for tools like `mlir-opt`.
/// - toolName is used for the header displayed by `--help`.
/// - registry should contain all the dialects that can be parsed in the source.
/// - additionalRegistration will be called before the main command line parsing
/// to perform additional registrations.
LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
DialectRegistry &registry,
const AdditionalRegistrationFn &additionalRegistration
= [](llvm::StringRef){});
DialectRegistry &registry);

/// Implementation for tools like `mlir-opt`.
/// This function can be used with registerAndParseCLIOptions 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.
///
Expand Down
80 changes: 36 additions & 44 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,8 @@ 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.
std::string mlir::registerCLIOptions(llvm::StringRef toolName,
DialectRegistry &registry) {
MlirOptMainConfig::registerCLOptions(registry);
registerAsmPrinterCLOptions();
registerMLIRContextCLOptions();
Expand All @@ -579,11 +570,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("-"));
cl::ParseCommandLineOptions(argc, argv, helpHeader);
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
}

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 Expand Up @@ -630,41 +639,13 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
config.outputSplitMarker());
}

LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
DialectRegistry &registry,
const AdditionalRegistrationFn &additionalRegistration) {
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("-"));
LogicalResult mlir::MlirOptMain(int argc, char **argv,
llvm::StringRef inputFilename,
llvm::StringRef outputFilename,
DialectRegistry &registry) {

InitLLVM y(argc, argv);

// Register any command line options.
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; });
}

// It is not possible to place a call after command line parser
// since not all options are registered at the moment
additionalRegistration(helpHeader);

MlirOptMainConfig::registerCLOptions(registry);

// Parse pass names in main to ensure static initialization completed.
cl::ParseCommandLineOptions(argc, argv, helpHeader);
MlirOptMainConfig config = MlirOptMainConfig::createFromCLOptions();

if (config.shouldShowDialects())
Expand Down Expand Up @@ -701,3 +682,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);
}
Loading