Skip to content

Commit

Permalink
fix broken ONNX_MLIR_FLAGS (onnx#2669)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Eichenberger <[email protected]>
  • Loading branch information
AlexandreEichenberger authored Jan 5, 2024
1 parent 7656086 commit d70cb7a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/doc_example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main() {
}
printf("\n");

// Destory the list and the tensors inside of it.
// Destroy the list and the tensors inside of it.
// Use omTensorListDestroyShallow if only want to destroy the list themselves.
omTensorListDestroy(input_list);
omTensorListDestroy(output_list);
Expand Down
41 changes: 24 additions & 17 deletions src/Compiler/CompilerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#define DEBUG_TYPE "compiler_options"

// Default env var where to find default options, when defined.
const std::string OnnxMlirEnvOptionName = "ONNX_MLIR_FLAGS";

namespace onnx_mlir {

// Use external storage for the options so that they are globally accessible
Expand Down Expand Up @@ -277,11 +280,16 @@ static llvm::cl::opt<std::string, true> shapeInformationOpt("shapeInformation",
llvm::cl::value_desc("value"), llvm::cl::location(shapeInformation),
llvm::cl::cat(OnnxMlirOptions));

// Default value is defined by the OnnxMlirEnvOptionName constant string
// variable, but the default setting mechanism here cannot be used here as we
// need to evaluate this value prior to the compiler options being set. Proper
// handling of the value of this compiler option is set by the calling the
// parseCustomEnvFlagsCommandLineOption(...) function.
static llvm::cl::opt<std::string, true> customEnvFlagsOpt("customEnvFlags",
llvm::cl::desc("Override default option env var OnnxMlirEnvOptionName: "
"ONNX_MLIR_FLAGS"),
llvm::cl::value_desc("option env var"), llvm::cl::location(customEnvFlags),
llvm::cl::init("ONNX_MLIR_FLAGS"), llvm::cl::cat(OnnxMlirOptions));
llvm::cl::cat(OnnxMlirOptions));

static llvm::cl::opt<ModelSize, true> modelSizeOpt("modelSize",
llvm::cl::desc("Model to generate code"),
Expand Down Expand Up @@ -579,8 +587,10 @@ std::string customEnvFlags;
// The customEnvFlags must be scanned before the normal options.
bool parseCustomEnvFlagsCommandLineOption(
int argc, const char *const *argv, llvm::raw_ostream *errs) {
// Find -customEnvFlags=val and save its value.
std::string envVar;
// Use the default ONNX MLIR Environment variable, unless specified otherwise
// by an argument, see below.
std::string envVar = OnnxMlirEnvOptionName;
// Customized version? -customEnvFlags=val and save its value.
for (int i = argc - 1; i > 1; --i) {
std::string arg(argv[i]);
if (arg.find("--customEnvFlags") == 0) {
Expand All @@ -592,22 +602,19 @@ bool parseCustomEnvFlagsCommandLineOption(
break;
}
}
if (!envVar.empty()) {
// We have a custom env var, verify that it does not recursively hold
// another -customEnvFlags.
const char *envValCstr;
if ((envValCstr = std::getenv(envVar.c_str()))) {
std::string envVal(envValCstr);
if (envVal.find("-customEnvFlags") != std::string::npos) {
if (errs)
*errs << "Warning: recursive use of --customEnvFlags in custom "
"environment flag not permited\n";
return false;
}
// Check that the env var does not recursively hold another -customEnvFlags.
const char *envValCstr;
if ((envValCstr = std::getenv(envVar.c_str()))) {
std::string envVal(envValCstr);
if (envVal.find("-customEnvFlags") != std::string::npos) {
if (errs)
*errs << "Warning: recursive use of --customEnvFlags in "
"environment flag not permited\n";
return false;
}
// The envVar is verified, use it.
setCustomEnvVar(envVar);
}
// The envVar is verified, use it.
setCustomEnvVar(envVar);
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Compiler/CompilerOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#define DEFAULT_INSTRUMENTSTAGE_CL_ENUM clEnumVal(Onnx, "Profile for onnx ops.")

// Variable contains the name of the default environment variable that is used
// to find the default onnx-mlir options.
// Its default value is ONNX_MLIR_FLAGS, as defined in CompilerOptions.cpp.
// TODO: may want to do this constant set by a variable in CMakeFiles.
extern const std::string OnnxMlirEnvOptionName;

namespace onnx_mlir {
Expand Down
2 changes: 0 additions & 2 deletions src/Compiler/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
using namespace mlir;
using namespace onnx_mlir;

const std::string OnnxMlirEnvOptionName = "ONNX_MLIR_FLAGS";

namespace onnx_mlir {

// Make a function that forces preserving all files using the runtime arguments
Expand Down

0 comments on commit d70cb7a

Please sign in to comment.