diff --git a/mlir/include/mlir/ExecutionEngine/JitRunner.h b/mlir/include/mlir/ExecutionEngine/JitRunner.h --- a/mlir/include/mlir/ExecutionEngine/JitRunner.h +++ b/mlir/include/mlir/ExecutionEngine/JitRunner.h @@ -20,6 +20,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/Orc/Core.h" +#include "llvm/Support/CommandLine.h" namespace llvm { class Module; @@ -36,11 +37,68 @@ class Operation; struct LogicalResult; +/// This options struct prevents the need for global static initializers, and +/// is only initialized if the JITRunner is invoked. +struct JitRunnerOptions { + llvm::cl::opt inputFilename{llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::init("-")}; + llvm::cl::opt mainFuncName{ + "e", llvm::cl::desc("The function to be called"), + llvm::cl::value_desc(""), llvm::cl::init("main")}; + llvm::cl::opt mainFuncType{ + "entry-point-result", + llvm::cl::desc("Textual description of the function type to be called"), + llvm::cl::value_desc("f32 | i32 | i64 | void"), llvm::cl::init("f32")}; + + llvm::cl::OptionCategory optFlags{"opt-like flags"}; + + // CLI variables for -On options. + llvm::cl::opt optO0{"O0", + llvm::cl::desc("Run opt passes and codegen at O0"), + llvm::cl::cat(optFlags)}; + llvm::cl::opt optO1{"O1", + llvm::cl::desc("Run opt passes and codegen at O1"), + llvm::cl::cat(optFlags)}; + llvm::cl::opt optO2{"O2", + llvm::cl::desc("Run opt passes and codegen at O2"), + llvm::cl::cat(optFlags)}; + llvm::cl::opt optO3{"O3", + llvm::cl::desc("Run opt passes and codegen at O3"), + llvm::cl::cat(optFlags)}; + + llvm::cl::OptionCategory clOptionsCategory{"linking options"}; + llvm::cl::list clSharedLibs{ + "shared-libs", llvm::cl::desc("Libraries to link dynamically"), + llvm::cl::MiscFlags::CommaSeparated, llvm::cl::cat(clOptionsCategory)}; + + /// CLI variables for debugging. + llvm::cl::opt dumpObjectFile{ + "dump-object-file", + llvm::cl::desc("Dump JITted-compiled object to file specified with " + "-object-filename (.o by default).")}; + + llvm::cl::opt objectFilename{ + "object-filename", + llvm::cl::desc("Dump JITted-compiled object to file .o")}; + + llvm::cl::opt hostSupportsJit{"host-supports-jit", + llvm::cl::desc("Report host JIT support"), + llvm::cl::Hidden}; + + llvm::cl::opt noImplicitModule{ + "no-implicit-module", + llvm::cl::desc( + "Disable implicit addition of a top-level module op during parsing"), + llvm::cl::init(false)}; +}; + struct JitRunnerConfig { /// MLIR transformer applied after parsing the input into MLIR IR and before /// passing the MLIR IR to the ExecutionEngine. - llvm::function_ref mlirTransformer = - nullptr; + llvm::function_ref + mlirTransformer = nullptr; /// A custom function that is passed to ExecutionEngine. It processes MLIR and /// creates an LLVM IR module. diff --git a/mlir/lib/ExecutionEngine/JitRunner.cpp b/mlir/lib/ExecutionEngine/JitRunner.cpp --- a/mlir/lib/ExecutionEngine/JitRunner.cpp +++ b/mlir/lib/ExecutionEngine/JitRunner.cpp @@ -31,7 +31,6 @@ #include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassNameParser.h" -#include "llvm/Support/CommandLine.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/StringSaver.h" @@ -44,62 +43,6 @@ using llvm::Error; namespace { -/// This options struct prevents the need for global static initializers, and -/// is only initialized if the JITRunner is invoked. -struct Options { - llvm::cl::opt inputFilename{llvm::cl::Positional, - llvm::cl::desc(""), - llvm::cl::init("-")}; - llvm::cl::opt mainFuncName{ - "e", llvm::cl::desc("The function to be called"), - llvm::cl::value_desc(""), llvm::cl::init("main")}; - llvm::cl::opt mainFuncType{ - "entry-point-result", - llvm::cl::desc("Textual description of the function type to be called"), - llvm::cl::value_desc("f32 | i32 | i64 | void"), llvm::cl::init("f32")}; - - llvm::cl::OptionCategory optFlags{"opt-like flags"}; - - // CLI variables for -On options. - llvm::cl::opt optO0{"O0", - llvm::cl::desc("Run opt passes and codegen at O0"), - llvm::cl::cat(optFlags)}; - llvm::cl::opt optO1{"O1", - llvm::cl::desc("Run opt passes and codegen at O1"), - llvm::cl::cat(optFlags)}; - llvm::cl::opt optO2{"O2", - llvm::cl::desc("Run opt passes and codegen at O2"), - llvm::cl::cat(optFlags)}; - llvm::cl::opt optO3{"O3", - llvm::cl::desc("Run opt passes and codegen at O3"), - llvm::cl::cat(optFlags)}; - - llvm::cl::OptionCategory clOptionsCategory{"linking options"}; - llvm::cl::list clSharedLibs{ - "shared-libs", llvm::cl::desc("Libraries to link dynamically"), - llvm::cl::MiscFlags::CommaSeparated, llvm::cl::cat(clOptionsCategory)}; - - /// CLI variables for debugging. - llvm::cl::opt dumpObjectFile{ - "dump-object-file", - llvm::cl::desc("Dump JITted-compiled object to file specified with " - "-object-filename (.o by default).")}; - - llvm::cl::opt objectFilename{ - "object-filename", - llvm::cl::desc("Dump JITted-compiled object to file .o")}; - - llvm::cl::opt hostSupportsJit{"host-supports-jit", - llvm::cl::desc("Report host JIT support"), - llvm::cl::Hidden}; - - llvm::cl::opt noImplicitModule{ - "no-implicit-module", - llvm::cl::desc( - "Disable implicit addition of a top-level module op during parsing"), - llvm::cl::init(false)}; -}; - struct CompileAndExecuteConfig { /// LLVM module transformer that is passed to ExecutionEngine. std::function transformer; @@ -147,7 +90,7 @@ llvm::inconvertibleErrorCode()); } -static Optional getCommandLineOptLevel(Options &options) { +static Optional getCommandLineOptLevel(JitRunnerOptions &options) { Optional optLevel; SmallVector>, 4> optFlags{ options.optO0, options.optO1, options.optO2, options.optO3}; @@ -164,7 +107,7 @@ } // JIT-compile the given module and run "entryPoint" with "args" as arguments. -static Error compileAndExecute(Options &options, Operation *module, +static Error compileAndExecute(JitRunnerOptions &options, Operation *module, StringRef entryPoint, CompileAndExecuteConfig config, void **args) { Optional jitCodeGenOptLevel; @@ -256,7 +199,7 @@ return Error::success(); } -static Error compileAndExecuteVoidFunction(Options &options, Operation *module, +static Error compileAndExecuteVoidFunction(JitRunnerOptions &options, Operation *module, StringRef entryPoint, CompileAndExecuteConfig config) { auto mainFunction = dyn_cast_or_null( @@ -300,7 +243,7 @@ return Error::success(); } template -Error compileAndExecuteSingleReturnFunction(Options &options, Operation *module, +Error compileAndExecuteSingleReturnFunction(JitRunnerOptions &options, Operation *module, StringRef entryPoint, CompileAndExecuteConfig config) { auto mainFunction = dyn_cast_or_null( @@ -337,7 +280,7 @@ JitRunnerConfig config) { // Create the options struct containing the command line options for the // runner. This must come before the command line options are parsed. - Options options; + JitRunnerOptions options; llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR CPU execution driver\n"); if (options.hostSupportsJit) { @@ -365,7 +308,7 @@ } if (config.mlirTransformer) - if (failed(config.mlirTransformer(m.get()))) + if (failed(config.mlirTransformer(m.get(), options))) return EXIT_FAILURE; auto tmBuilderOrError = llvm::orc::JITTargetMachineBuilder::detectHost(); @@ -389,7 +332,7 @@ // Get the function used to compile and execute the module. using CompileAndExecuteFnT = - Error (*)(Options &, Operation *, StringRef, CompileAndExecuteConfig); + Error (*)(JitRunnerOptions &, Operation *, StringRef, CompileAndExecuteConfig); auto compileAndExecuteFn = StringSwitch(options.mainFuncType.getValue()) .Case("i32", compileAndExecuteSingleReturnFunction) diff --git a/mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp b/mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp --- a/mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp +++ b/mlir/tools/mlir-spirv-cpu-runner/mlir-spirv-cpu-runner.cpp @@ -74,7 +74,8 @@ return mainModule; } -static LogicalResult runMLIRPasses(Operation *module) { +static LogicalResult runMLIRPasses(Operation *module, + JitRunnerOptions &options) { PassManager passManager(module->getContext(), module->getName().getStringRef()); applyPassManagerCLOptions(passManager); diff --git a/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp b/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp --- a/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp +++ b/mlir/tools/mlir-vulkan-runner/mlir-vulkan-runner.cpp @@ -41,7 +41,7 @@ using namespace mlir; -static LogicalResult runMLIRPasses(Operation *op) { +static LogicalResult runMLIRPasses(Operation *op, JitRunnerOptions& options) { auto module = dyn_cast(op); if (!module) return op->emitOpError("expected a 'builtin.module' op");