diff --git a/src/Compiler/CompilerOptions.cpp b/src/Compiler/CompilerOptions.cpp index 8a5bd9d657..139cbd1847 100644 --- a/src/Compiler/CompilerOptions.cpp +++ b/src/Compiler/CompilerOptions.cpp @@ -407,13 +407,19 @@ static llvm::cl::opt VerboseOutputOpt("v", llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions)); static llvm::cl::list> XoptOpt("Xopt", - llvm::cl::desc("Arguments to forward to LLVM's 'opt' option processing"), + llvm::cl::desc( + "Arguments to forward to LLVM's 'opt' option processing" + "Multiple arguments to 'opt' need to be pass with seperate 'Xopt'" + "For example, '-Xopt opt1 -Xopt opt2 ...'"), llvm::cl::value_desc("A valid LLVM's 'opt' option"), llvm::cl::location(Xopt), llvm::cl::cat(OnnxMlirOptions), llvm::cl::Hidden, llvm::cl::ValueRequired, llvm::cl::ZeroOrMore, llvm::cl::CommaSeparated); static llvm::cl::list> XllcOpt("Xllc", - llvm::cl::desc("Arguments to forward to LLVM's 'llc' option processing"), + llvm::cl::desc( + "Arguments to forward to LLVM's 'llc' option processing" + "Multiple arguments to 'llc' need to be pass with seperate 'Xllc'" + "For example, '-Xllc opt1 -Xllc opt2 ...'"), llvm::cl::value_desc("A valid LLVM's 'llc' option"), llvm::cl::location(Xllc), llvm::cl::cat(OnnxMlirOptions), llvm::cl::Hidden, llvm::cl::ValueRequired, llvm::cl::ZeroOrMore, llvm::cl::CommaSeparated); @@ -914,6 +920,21 @@ void setLLVMOption(const std::string &flag) { mllvm = flag; } void clearLLVMOption() { mllvm.clear(); } std::string getLLVMOption() { return (mllvm != "") ? mllvm : std::string(); } +static std::vector split(std::string &input) { + std::stringstream ss(input); + std::istream_iterator begin(ss); + std::istream_iterator end; + std::vector vstrings(begin, end); + return vstrings; +} + +std::vector getLLVMOptions() { + if (mllvm == "") + return std::vector(); + + return split(mllvm); +} + // Support for model tag void setModelTag(const std::string &str) { modelTag = str; } void clearModelTag() { modelTag = ""; } diff --git a/src/Compiler/CompilerOptions.hpp b/src/Compiler/CompilerOptions.hpp index 3e0940d70b..449202198a 100644 --- a/src/Compiler/CompilerOptions.hpp +++ b/src/Compiler/CompilerOptions.hpp @@ -183,6 +183,10 @@ std::vector getXllcOption(); void setLLVMOption(const std::string &flag); void clearLLVMOption(); std::string getLLVMOption(); +// Break down the result of getLLVMOption into substrings +std::vector getLLVMOptions(); +std::vector getLLVMOPTOptions(); +std::vector getLLVMLLCOptions(); // Options support for OMCompilerOptions. using CompilerOptionList = diff --git a/src/Compiler/CompilerUtils.cpp b/src/Compiler/CompilerUtils.cpp index f4ec2d6d45..260e56a534 100644 --- a/src/Compiler/CompilerUtils.cpp +++ b/src/Compiler/CompilerUtils.cpp @@ -72,6 +72,26 @@ enum class KeepFilesOfType { All, MLIR, LLVMIR, Bitcode, Object, None }; // flags. static constexpr KeepFilesOfType overridePreserveFiles = KeepFilesOfType::None; +// Get optimization level from onnx-mlir only when it is not specified +std::string getOptimizationLevelUniqueOption( + std::vector> specialOptionsList) { + if (std::any_of(specialOptionsList.begin(), specialOptionsList.end(), + [](std::vector specialOptions) { + if (std::any_of(specialOptions.begin(), specialOptions.end(), + [](std::string str) { + std::regex optimization("^-O[0-9]"); + std::smatch m; + return std::regex_match(str, m, optimization); + })) // End of one options + return true; + else + return false; + })) + return std::string(); + else + return getOptimizationLevelOption(); +} + static bool keepFiles(KeepFilesOfType preserve) { // When wanting to preserve all files, do it regardless of isBitcode. if (overridePreserveFiles == KeepFilesOfType::All) @@ -437,12 +457,14 @@ static int genLLVMBitcode(const mlir::OwningOpRef &module, std::string optPath = getToolPath("opt"); Command optBitcode(/*exePath=*/optPath); setXoptOption({"--code-model", modelSizeStr[modelSize]}); - int rc = optBitcode.appendStr(getOptimizationLevelOption()) + int rc = optBitcode + .appendStr(getOptimizationLevelUniqueOption( + {getLLVMOptions(), getXoptOption()})) .appendStr(getTargetTripleOption()) .appendStr(getTargetArchOption()) .appendStr(getTargetCPUOption()) .appendList(getXoptOption()) - .appendStr(getLLVMOption()) + .appendList(getLLVMOptions()) .appendList({"-o", optimizedBitcodeNameWithExt}) .appendStr(unoptimizedBitcodeNameWithExt) .exec(); @@ -459,12 +481,14 @@ static int genModelObject( std::string llcPath = getToolPath("llc"); Command llvmToObj(/*exePath=*/llcPath); setXllcOption({"--code-model", modelSizeStr[modelSize]}); - int rc = llvmToObj.appendStr(getOptimizationLevelOption()) + int rc = llvmToObj + .appendStr(getOptimizationLevelUniqueOption( + {getLLVMOptions(), getXllcOption()})) .appendStr(getTargetTripleOption()) .appendStr(getTargetArchOption()) .appendStr(getTargetCPUOption()) .appendList(getXllcOption()) - .appendStr(getLLVMOption()) + .appendList(getLLVMOptions()) .appendStr("-filetype=obj") .appendStr("-relocation-model=pic") .appendList({"-o", modelObjNameWithExt})