Skip to content

Commit 70abe9c

Browse files
committed
Merge branch 'main' into mem_reduction_stickified
2 parents e9c6805 + e5901e2 commit 70abe9c

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

src/Compiler/CompilerOptions.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,19 @@ static llvm::cl::opt<bool, true> VerboseOutputOpt("v",
407407
llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions));
408408

409409
static llvm::cl::list<std::string, std::vector<std::string>> XoptOpt("Xopt",
410-
llvm::cl::desc("Arguments to forward to LLVM's 'opt' option processing"),
410+
llvm::cl::desc(
411+
"Arguments to forward to LLVM's 'opt' option processing"
412+
"Multiple arguments to 'opt' need to be pass with seperate 'Xopt'"
413+
"For example, '-Xopt opt1 -Xopt opt2 ...'"),
411414
llvm::cl::value_desc("A valid LLVM's 'opt' option"),
412415
llvm::cl::location(Xopt), llvm::cl::cat(OnnxMlirOptions), llvm::cl::Hidden,
413416
llvm::cl::ValueRequired, llvm::cl::ZeroOrMore, llvm::cl::CommaSeparated);
414417

415418
static llvm::cl::list<std::string, std::vector<std::string>> XllcOpt("Xllc",
416-
llvm::cl::desc("Arguments to forward to LLVM's 'llc' option processing"),
419+
llvm::cl::desc(
420+
"Arguments to forward to LLVM's 'llc' option processing"
421+
"Multiple arguments to 'llc' need to be pass with seperate 'Xllc'"
422+
"For example, '-Xllc opt1 -Xllc opt2 ...'"),
417423
llvm::cl::value_desc("A valid LLVM's 'llc' option"),
418424
llvm::cl::location(Xllc), llvm::cl::cat(OnnxMlirOptions), llvm::cl::Hidden,
419425
llvm::cl::ValueRequired, llvm::cl::ZeroOrMore, llvm::cl::CommaSeparated);
@@ -914,6 +920,21 @@ void setLLVMOption(const std::string &flag) { mllvm = flag; }
914920
void clearLLVMOption() { mllvm.clear(); }
915921
std::string getLLVMOption() { return (mllvm != "") ? mllvm : std::string(); }
916922

923+
static std::vector<std::string> split(std::string &input) {
924+
std::stringstream ss(input);
925+
std::istream_iterator<std::string> begin(ss);
926+
std::istream_iterator<std::string> end;
927+
std::vector<std::string> vstrings(begin, end);
928+
return vstrings;
929+
}
930+
931+
std::vector<std::string> getLLVMOptions() {
932+
if (mllvm == "")
933+
return std::vector<std::string>();
934+
935+
return split(mllvm);
936+
}
937+
917938
// Support for model tag
918939
void setModelTag(const std::string &str) { modelTag = str; }
919940
void clearModelTag() { modelTag = ""; }

src/Compiler/CompilerOptions.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ std::vector<std::string> getXllcOption();
183183
void setLLVMOption(const std::string &flag);
184184
void clearLLVMOption();
185185
std::string getLLVMOption();
186+
// Break down the result of getLLVMOption into substrings
187+
std::vector<std::string> getLLVMOptions();
188+
std::vector<std::string> getLLVMOPTOptions();
189+
std::vector<std::string> getLLVMLLCOptions();
186190

187191
// Options support for OMCompilerOptions.
188192
using CompilerOptionList =

src/Compiler/CompilerUtils.cpp

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ enum class KeepFilesOfType { All, MLIR, LLVMIR, Bitcode, Object, None };
7272
// flags.
7373
static constexpr KeepFilesOfType overridePreserveFiles = KeepFilesOfType::None;
7474

75+
// Get optimization level from onnx-mlir only when it is not specified
76+
std::string getOptimizationLevelUniqueOption(
77+
std::vector<std::vector<std::string>> specialOptionsList) {
78+
if (std::any_of(specialOptionsList.begin(), specialOptionsList.end(),
79+
[](std::vector<std::string> specialOptions) {
80+
if (std::any_of(specialOptions.begin(), specialOptions.end(),
81+
[](std::string str) {
82+
std::regex optimization("^-O[0-9]");
83+
std::smatch m;
84+
return std::regex_match(str, m, optimization);
85+
})) // End of one options
86+
return true;
87+
else
88+
return false;
89+
}))
90+
return std::string();
91+
else
92+
return getOptimizationLevelOption();
93+
}
94+
7595
static bool keepFiles(KeepFilesOfType preserve) {
7696
// When wanting to preserve all files, do it regardless of isBitcode.
7797
if (overridePreserveFiles == KeepFilesOfType::All)
@@ -437,12 +457,14 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
437457
std::string optPath = getToolPath("opt");
438458
Command optBitcode(/*exePath=*/optPath);
439459
setXoptOption({"--code-model", modelSizeStr[modelSize]});
440-
int rc = optBitcode.appendStr(getOptimizationLevelOption())
460+
int rc = optBitcode
461+
.appendStr(getOptimizationLevelUniqueOption(
462+
{getLLVMOptions(), getXoptOption()}))
441463
.appendStr(getTargetTripleOption())
442464
.appendStr(getTargetArchOption())
443465
.appendStr(getTargetCPUOption())
444466
.appendList(getXoptOption())
445-
.appendStr(getLLVMOption())
467+
.appendList(getLLVMOptions())
446468
.appendList({"-o", optimizedBitcodeNameWithExt})
447469
.appendStr(unoptimizedBitcodeNameWithExt)
448470
.exec();
@@ -459,12 +481,14 @@ static int genModelObject(
459481
std::string llcPath = getToolPath("llc");
460482
Command llvmToObj(/*exePath=*/llcPath);
461483
setXllcOption({"--code-model", modelSizeStr[modelSize]});
462-
int rc = llvmToObj.appendStr(getOptimizationLevelOption())
484+
int rc = llvmToObj
485+
.appendStr(getOptimizationLevelUniqueOption(
486+
{getLLVMOptions(), getXllcOption()}))
463487
.appendStr(getTargetTripleOption())
464488
.appendStr(getTargetArchOption())
465489
.appendStr(getTargetCPUOption())
466490
.appendList(getXllcOption())
467-
.appendStr(getLLVMOption())
491+
.appendList(getLLVMOptions())
468492
.appendStr("-filetype=obj")
469493
.appendStr("-relocation-model=pic")
470494
.appendList({"-o", modelObjNameWithExt})
@@ -558,6 +582,9 @@ static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module,
558582

559583
// Copy javaruntime.jar to model jar.
560584
llvm::sys::fs::copy_file(javaRuntimeJarPath, modelJniJarPath);
585+
if (VerboseOutput)
586+
llvm::outs() << "cp " << javaRuntimeJarPath << " " << modelJniJarPath
587+
<< "\n";
561588

562589
// Add shared library to model jar.
563590
Command jar(getToolPath("jar", true));
@@ -774,8 +801,8 @@ static int emitOutputFiles(std::string outputNameNoExt,
774801
return rc;
775802
}
776803
if (VerboseOutput)
777-
printf(
778-
"Object file '%s' has been compiled.\n", modelObjNameWithExt.c_str());
804+
llvm::outs() << "Object file '" << modelObjNameWithExt
805+
<< "' has been compiled.\n";
779806
} break;
780807
case EmitLib: {
781808
std::string sharedLibNameWithExt;
@@ -789,8 +816,8 @@ static int emitOutputFiles(std::string outputNameNoExt,
789816
return rc;
790817
}
791818
if (VerboseOutput)
792-
printf("Shared library '%s' has been compiled.\n",
793-
sharedLibNameWithExt.c_str());
819+
llvm::outs() << "Shared library '" << sharedLibNameWithExt
820+
<< "' has been compiled.\n";
794821
} break;
795822
case EmitJNI: {
796823
int rc = compileModuleToJniJar(module, outputNameNoExt);
@@ -802,16 +829,17 @@ static int emitOutputFiles(std::string outputNameNoExt,
802829
return rc;
803830
}
804831
if (VerboseOutput)
805-
printf(
806-
"JNI archive '%s.jar' has been compiled.\n", outputNameNoExt.c_str());
832+
llvm::outs() << "JNI archive '" << outputNameNoExt
833+
<< ".jar' has been compiled.\n";
807834
} break;
808835
default: {
809836
// Emit the version with all constants included.
810-
std::string ouputNameWithExt =
837+
std::string outputNameWithExt =
811838
getTargetFilename(outputNameNoExt, emissionTarget);
812-
int rc = outputCode(module, ouputNameWithExt);
839+
int rc = outputCode(module, outputNameWithExt);
813840
if (VerboseOutput)
814-
printf("Full MLIR code written to: \n\t%s\n\n", ouputNameWithExt.c_str());
841+
llvm::outs() << "Full MLIR code written to:\n"
842+
<< "\t" << outputNameWithExt << "\n\n";
815843
if (rc != CompilerSuccess)
816844
return rc;
817845

@@ -821,10 +849,11 @@ static int emitOutputFiles(std::string outputNameNoExt,
821849
std::string tempNameWithExt = outputNameNoExt + ".tmp";
822850
int rc = outputCode(module, tempNameWithExt, /*largeElementLimit=*/100);
823851
if (VerboseOutput) {
824-
printf("Constant-free MLIR Code written to: \n\t%s\n\n",
825-
tempNameWithExt.c_str());
826-
printf("Use:\n\t%s\nto continue lowering the code to other dialects.\n",
827-
ouputNameWithExt.c_str());
852+
llvm::outs() << "Constant-free MLIR Code written to:\n"
853+
<< "\t" << tempNameWithExt << "\n\n";
854+
llvm::outs() << "Use:\n"
855+
<< "\t" << outputNameWithExt
856+
<< "\nto continue lowering the code to other dialects.\n";
828857
}
829858
if (rc != CompilerSuccess)
830859
return rc;

0 commit comments

Comments
 (0)