Skip to content

Commit

Permalink
Handle the options passed to llvm opt and llvm llc (#2955)
Browse files Browse the repository at this point in the history
* implementation

Signed-off-by: chentong319 <chentong@us.ibm.com>

* remove redundant

Signed-off-by: chentong319 <chentong@us.ibm.com>

---------

Signed-off-by: chentong319 <chentong@us.ibm.com>
Co-authored-by: Tung D. Le <tung@jp.ibm.com>
  • Loading branch information
chentong319 and tungld authored Sep 27, 2024
1 parent f7d5895 commit 2570159
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/Compiler/CompilerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,19 @@ static llvm::cl::opt<bool, true> VerboseOutputOpt("v",
llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions));

static llvm::cl::list<std::string, std::vector<std::string>> 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<std::string, std::vector<std::string>> 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);
Expand Down Expand Up @@ -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<std::string> split(std::string &input) {
std::stringstream ss(input);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
return vstrings;
}

std::vector<std::string> getLLVMOptions() {
if (mllvm == "")
return std::vector<std::string>();

return split(mllvm);
}

// Support for model tag
void setModelTag(const std::string &str) { modelTag = str; }
void clearModelTag() { modelTag = ""; }
Expand Down
4 changes: 4 additions & 0 deletions src/Compiler/CompilerOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ std::vector<std::string> getXllcOption();
void setLLVMOption(const std::string &flag);
void clearLLVMOption();
std::string getLLVMOption();
// Break down the result of getLLVMOption into substrings
std::vector<std::string> getLLVMOptions();
std::vector<std::string> getLLVMOPTOptions();
std::vector<std::string> getLLVMLLCOptions();

// Options support for OMCompilerOptions.
using CompilerOptionList =
Expand Down
32 changes: 28 additions & 4 deletions src/Compiler/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>> specialOptionsList) {
if (std::any_of(specialOptionsList.begin(), specialOptionsList.end(),
[](std::vector<std::string> 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)
Expand Down Expand Up @@ -437,12 +457,14 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &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();
Expand All @@ -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})
Expand Down

0 comments on commit 2570159

Please sign in to comment.