Skip to content

Commit a43f9b4

Browse files
Compiler interface for compiler options (llvm#1167)
Add runtime support to set options: command line options can also be given by env variable ONNX_MLIR_FLAG S and ONNX_MLIR_OPT_FLAGS. In addition, the compiler interface was augmented to enable the setting of options using either command line and/or an environment variable. Signed-off-by: Alexandre Eichenberger <[email protected]>
1 parent 0f427cf commit a43f9b4

24 files changed

+337
-220
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ These are frontend options.
100100
The full list of options is given by the `--help` option. Note that just as most compilers, the default optimization level is `-O0`.
101101
We recommend using `-O3` for most applications.
102102

103+
Options are also read from the `ONNX_MLIR_FLAGS` environment variable. For example, `ONNX_MLIR_FLAGS="-O3"` will ensure `-O3` for all compilations.
104+
103105
### Simple Example
104106

105107
For example, use the following command to lower an ONNX model (e.g., add.onnx) to ONNX dialect:

include/OnnxMlirCompiler.h

+81-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#define ONNX_MLIR_ONNXMLIRCOMPILER_H
1414

1515
#include <onnx-mlir/Compiler/OMCompilerTypes.h>
16+
#ifdef __cplusplus
17+
#include <cstdint>
18+
#else
19+
#include <stdint.h>
20+
#endif // #ifdef __cplusplus
21+
1622

1723
#ifdef ONNX_MLIR_BUILT_AS_STATIC
1824
#define ONNX_MLIR_EXPORT
@@ -33,47 +39,104 @@
3339

3440
#ifdef __cplusplus
3541
extern "C" {
42+
namespace onnx_mlir {
3643
#endif
3744

38-
namespace onnx_mlir {
45+
/*!
46+
* Define ONNX-MLIR compiler options with options defined by
47+
* the envVarName (default ONNX_MLIR_FLAGS) environment variable.
48+
* Values not recognized as compiler options result in an error.
49+
* Only a single call to omSetCompilerOptionsFromEnv,
50+
* omSetCompilerOptionsFromArgs, or omSetCompilerOptionsFromEnvAndArgs
51+
* is allowed.
52+
* @param envVarName Environment variable name, use default when null.
53+
* @return 0 on success or non-zero error code on failure.
54+
*/
55+
ONNX_MLIR_EXPORT int64_t omSetCompilerOptionsFromEnv(
56+
const char *envVarName);
57+
58+
/*!
59+
* Define ONNX-MLIR compiler options with options defined by
60+
* the argc/argv parameters. Call expects argv[0] to contain the program
61+
* name. Values not recognized as compiler options result in an error.
62+
* Only a single call to omSetCompilerOptionsFromEnv,
63+
* omSetCompilerOptionsFromArgs, or omSetCompilerOptionsFromEnvAndArgs
64+
* is allowed.
65+
* @param argc Number of input parameters in argv.
66+
* @param argv Array of strings, some of which may be compiler options.
67+
* First argv is ignored as it contains the name of the program.
68+
* @return 0 on success or non-zero error code on failure.
69+
*/
70+
ONNX_MLIR_EXPORT int64_t omSetCompilerOptionsFromArgs(
71+
int64_t argc, char *argv[]);
72+
73+
/*!
74+
* Define ONNX-MLIR compiler options with options defined by
75+
* the envVarName (default ONNX_MLIR_FLAGS) environment variable
76+
* and the argc/argv parameters. Call expects argv[0] to contain the program
77+
* name. Values not recognized as compiler options result in an error.
78+
* Only a single call to omSetCompilerOptionsFromEnv,
79+
* omSetCompilerOptionsFromArgs, or omSetCompilerOptionsFromEnvAndArgs
80+
* is allowed.
81+
* @param argc Number of input parameters in argv.
82+
* @param argv Array of strings, some of which may be compiler options.
83+
* First argv is ignored as it contains the name of the program.
84+
* @param envVarName Environment variable name, use default when null.
85+
* @return 0 on success or non-zero error code on failure.
86+
*/
87+
ONNX_MLIR_EXPORT int64_t omSetCompilerOptionsFromArgsAndEnv(
88+
int64_t argc, char *argv[], const char *envVarName);
89+
90+
/*!
91+
* Overwrite the compiler option defined by input parameters.
92+
* Set default value by calling this function before a call to
93+
* omSetCompilerOptionsFromEnv, omSetCompilerOptionsFromArgs, or
94+
* omSetCompilerOptionsFromEnvAndArgs. Or overwrite the current value
95+
* by calling this function after one of the above 3 setter functions.
96+
* @param kind Describe which option kind is being set.
97+
* @param val Value of the option being set. Null pointer reset the
98+
* option.
99+
* @return 0 on success or non-zero error code on failure.
100+
*/
101+
ONNX_MLIR_EXPORT int64_t omSetCompilerOption(
102+
const OptionKind kind, const char *val);
103+
104+
/*!
105+
* Get the compiler options.
106+
* @param kind Describe which option kind is being set.
107+
* @return Value of compiler option.
108+
*/
109+
ONNX_MLIR_EXPORT const char *omGetCompilerOption(
110+
const OptionKind kind);
39111

40112
/*!
41113
* Compile an onnx model from a file containing MLIR or ONNX protobuf.
42114
* @param inputFilename File name pointing onnx model protobuf or MLIR.
43115
* @param outputBaseName File name without extension to write output.
44116
* @param emissionTarget Target format to compile to.
45-
* @param optionKey List of keys specified for the compiler options.
46-
* @param optionVal List of string values for corresponding keys.
47-
* @param optionNum Number of keys and strings.
48117
* @param errorMessage Error message.
49118
* @return 0 on success or non-zero error code on failure.
50119
*/
51-
ONNX_MLIR_EXPORT int omCompileFromFile(const char *inputFilename,
120+
ONNX_MLIR_EXPORT int64_t omCompileFromFile(const char *inputFilename,
52121
const char *outputBaseName, EmissionTargetType emissionTarget,
53-
const onnx_mlir::OptionKind *optionKey, const char **optionVal,
54-
const int optionNum, const char **errorMessage);
122+
const char **errorMessage);
55123

56124
/*!
57125
* Compile an onnx model from an ONNX protobuf array.
58126
* @param inputBuffer ONNX protobuf array.
59127
* @param bufferSize Size of ONNX protobuf array.
60128
* @param outputBaseName File name without extension to write output.
61129
* @param emissionTarget Target format to compile to.
62-
* @param optionKey List of keys specified for the compiler options.
63-
* @param optionVal List of string values for corresponding keys.
64-
* @param optionNum Number of keys and strings.
130+
* @param errorMessage Error message.
65131
* @return 0 on success or non-zero error code on failure
66132
*/
67-
ONNX_MLIR_EXPORT int omCompileFromArray(const void *inputBuffer, int bufferSize,
68-
const char *outputBaseName, EmissionTargetType emissionTarget,
69-
const onnx_mlir::OptionKind *optionKey, const char **optionVal,
70-
const int optionNum
71-
);
72-
73-
} // namespace onnx_mlir
133+
ONNX_MLIR_EXPORT int64_t omCompileFromArray(const void *inputBuffer,
134+
int bufferSize, const char *outputBaseName,
135+
EmissionTargetType emissionTarget, const char **errorMessage);
74136

75137
#ifdef __cplusplus
76-
}
138+
} // namespace onnx_mlir
139+
} // extern C
77140
#endif
78141

79142
#endif

include/onnx-mlir/Compiler/OMCompilerTypes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef enum {
3838
CompilerOptLevel, /* Kind for '0'...'3' string describing OptLevel. */
3939
OPTFlag, /* Kind for -Xopt string. */
4040
LLCFlag, /* Kind for -Xllc string. */
41-
LLVMFlag /* Kind for -mllvm string. */
41+
LLVMFlag, /* Kind for -mllvm string. */
4242
} OptionKind;
4343

4444
#ifdef __cplusplus

0 commit comments

Comments
 (0)