Skip to content

Commit 1081a5e

Browse files
manbeariantungld
andauthored
move passes options to their own thing (#1363)
Signed-off-by: Ian Bearman <[email protected]> Co-authored-by: Tung D. Le <[email protected]>
1 parent 88f7d97 commit 1081a5e

13 files changed

+128
-130
lines changed

docs/Options.md

+7-17
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,16 @@
55
Command-line options can be used to alter the default behavior of onnx-mlir, or onnx-mlir-opt, and help user experimenting, debugging or performance tuning. We implemented command-line in ONNX-MLIR based on the command-line utility provided by LLVM. We did not define `Option` or `ListOption` with MLIR pass classes(see discussion).
66

77
## Organize Options
8-
Refer [llvm document](https://llvm.org/docs/CommandLine.html) for basic idea of how to define an option. In ONNX-MLIR, options are put into groups (`llvm::cl::OptionCategory`).
9-
One group of options are only used by onnx-mlir to configure its input or output. These options are defined in src/main.cpp and src/Compiler/CompilerUtils.cpp within OnnxMlirOptions category.
10-
The rest of options may be used by both onnx-mlir and onnx-mlir-opt to control the behavior of a pass or passes. So far, only one group is defined as an example.
8+
Refer [llvm document](https://llvm.org/docs/CommandLine.html) for basic idea of how to define an option. In ONNX-MLIR, options are put into groups (`llvm::cl::OptionCategory`). All command-line options for onnx-mlir are in the `OnnxMlirOptions` group.
119

1210
## Code structure
13-
The head file for options is `src/Support/OMOptions.hpp`. This file should contain the declaration of groups used by both onnx-mlir and onnx-mlir-opt, and options that may be shared by different passes.
14-
The definition of group and shared options are in `src/Support/OMOptions.cpp`.
11+
Command-line options should be placed in `src/Compiler/CompilerOptions.cpp` and declared in `src/Compiler/CompilerOptions.hpp`.
1512

1613
## Define an option
17-
* Add a declaration of the option in 'src/Support/OMOptions.hpp`
18-
* In `src/SupportOMOptions.cpp`, define the option with the desirable llvm::cl class and option group
19-
* For the file to use this option, make sure `src/Support/OMOptions.hpp` is included.
20-
* Add OMSupport to the list of libraries in CMakefile.txt
14+
- Add a declaration of the option in `src/Compiler/CompilerOptions.hpp`
15+
- In `src/Compiler/CompilerOptions.cpp`, define the option
16+
- Do **not** include `src/Compiler/CompilerOptions.hpp` in new source files; it should only be used in the onnx-mlir and onnn-mlir-opt command-line tools.
17+
- Do create 'Pass Options' to pass information to specific passses and transformations
2118

2219
## Define an option local to a transformation
23-
If an option is only used in one transformation, it can be defined locally in the file for the transformation. In the file for transformation:
24-
* Include `src/Support/OMOtions.hpp`
25-
* Define the option with the desirable llvm::cl class and option group
26-
* Add OMSupport to the list of libraries in CMakefile.txt
27-
28-
## Discussion
29-
### MLIR Option support
30-
MLIR allows to define options within the pass class definition. However, the document said you have to define the same options for PassPipeline and pass a lambda function to assign the options. I thought that this requirement complicated code. I am open to using MLIR if someone know how to easily implement it and know its advantages.
20+
Use MLIR's Pass Options to configure passes.

include/onnx-mlir/Compiler/OMCompilerTypes.h

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ typedef enum {
4141
LLVMFlag, /* Kind for -mllvm string. */
4242
} OptionKind;
4343

44+
/* Compiler options to describe instrumentation options */
45+
typedef enum {
46+
InstrumentBeforeOp,
47+
InstrumentAfterOp,
48+
InstrumentReportTime,
49+
InstrumentReportMemory
50+
} InstrumentActions;
51+
4452
#ifdef __cplusplus
4553
} // namespace onnx_mlir
4654
#endif

src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ void addONNXToZHighPasses(
7373
pm.addNestedPass<FuncOp>(onnx_mlir::createConstPropONNXToONNXPass());
7474
// Add instrumentation for Onnx Ops in the same way as onnx-mlir.
7575
if (instrumentZHighOps == "" || instrumentZHighOps == "NONE")
76-
pm.addNestedPass<FuncOp>(onnx_mlir::createInstrumentONNXPass());
76+
pm.addNestedPass<FuncOp>(onnx_mlir::createInstrumentONNXPass(
77+
instrumentONNXOps, instrumentControlBits.getBits()));
7778
pm.addPass(onnx_mlir::createONNXToZHighPass(execNodesOnCpu));
7879
pm.addPass(onnx_mlir::createShapeInferencePass());
7980
// There are more opportunities for const propagation once all zhigh ops were

src/Accelerators/NNPA/Support/OMNNPAOptions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*/
44

5-
//===--------------------------- OMOptions.cpp ----------------------------===//
5+
//===--------------------------- OMNNPAOptions.cpp ------------------------===//
66
//
77
// Copyright 2019-2021 The IBM Research Authors.
88
//

src/Compiler/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ add_onnx_mlir_library(CompilerOptions
3535

3636
INCLUDE_DIRS PRIVATE
3737
${FILE_GENERATE_DIR}
38-
38+
3939
INCLUDE_DIRS PUBLIC
4040
${ONNX_MLIR_SRC_ROOT}/include
4141

@@ -53,7 +53,7 @@ add_onnx_mlir_library(CompilerPasses
5353

5454
INCLUDE_DIRS PRIVATE
5555
${FILE_GENERATE_DIR}
56-
56+
5757
INCLUDE_DIRS PUBLIC
5858
${ONNX_MLIR_SRC_ROOT}/include
5959

@@ -73,6 +73,8 @@ endif()
7373
add_onnx_mlir_library(CompilerUtils
7474
CompilerUtils.cpp
7575

76+
EXCLUDE_FROM_OM_LIBS
77+
7678
DEPENDS
7779
ExternalUtil
7880
MLIRIR

src/Compiler/CompilerOptions.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,40 @@ llvm::cl::opt<OptLevel> OptimizationLevel(
123123
clEnumVal(O3, "Optimization level 3.")),
124124
llvm::cl::init(O0), llvm::cl::cat(OnnxMlirOptions));
125125

126-
llvm::cl::OptionCategory OMPassOptions("ONNX-MLIR Pass Options",
127-
"These are options to provide fine control on passes");
128-
129126
llvm::cl::opt<std::string> instrumentONNXOps("instrument-onnx-ops",
130127
llvm::cl::desc("Specify onnx ops to be instrumented\n"
131128
"\"NONE\" or \"\" for no instrument\n"
132129
"\"ALL\" for all ops. \n"
133130
"\"op1 op2 ...\" for the specified ops."),
134-
llvm::cl::init(""), llvm::cl::cat(OMPassOptions));
131+
llvm::cl::init(""), llvm::cl::cat(OnnxMlirOptions));
132+
133+
llvm::cl::bits<InstrumentActions> instrumentControlBits(
134+
llvm::cl::desc("Specify what instrumentation actions at runtime:"),
135+
llvm::cl::values(
136+
clEnumVal(InstrumentBeforeOp, "insert instrument before op"),
137+
clEnumVal(InstrumentAfterOp, "insert instrument after op"),
138+
clEnumVal(
139+
InstrumentReportTime, "instrument runtime reports time usage"),
140+
clEnumVal(
141+
InstrumentReportMemory, "instrument runtime reports memory usage")),
142+
llvm::cl::cat(OnnxMlirOptions));
135143

136144
llvm::cl::opt<bool> enableMemoryBundling("enable-memory-bundling",
137145
llvm::cl::desc(
138146
"Enable memory bundling related optimizations (default=false)\n"
139147
"Set to 'false' if you experience significant compile time."),
140-
llvm::cl::init(false), llvm::cl::cat(OMPassOptions));
148+
llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions));
141149

142150
llvm::cl::opt<int> onnxOpTransformThreshold("onnx-op-transform-threshold",
143151
llvm::cl::desc(
144152
"Max iteration for dynamic op transform passes (default=3).\n"
145153
"If set to 0, onnxOpTransformPass will be disabled, and\n"
146154
"static iteration will be used"),
147-
llvm::cl::init(3), llvm::cl::cat(OMPassOptions));
155+
llvm::cl::init(3), llvm::cl::cat(OnnxMlirOptions));
148156

149157
llvm::cl::opt<bool> onnxOpTransformReport("onnx-op-transform-report",
150158
llvm::cl::desc("Report diagnostic info for op transform passes."),
151-
llvm::cl::init(false), llvm::cl::cat(OMPassOptions));
159+
llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions));
152160

153161
// Configuration states associated with certain options.
154162
// For example, when maccel is specified, NNPA can register

src/Compiler/CompilerOptions.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extern const std::string OnnxMlirEnvOptionName;
2525
namespace onnx_mlir {
2626
extern llvm::cl::OptionCategory OnnxMlirOptions;
2727

28-
extern llvm::cl::opt<std::string> instrumentONNXOps;
2928
extern llvm::cl::opt<bool> invokeOnnxVersionConverter;
3029
extern llvm::cl::opt<bool> preserveLocations;
3130
extern llvm::cl::opt<bool> printIR;
@@ -45,8 +44,8 @@ extern llvm::cl::opt<std::string> Xopt;
4544
extern llvm::cl::opt<std::string> Xllc;
4645
extern llvm::cl::opt<std::string> mllvm;
4746

48-
extern llvm::cl::OptionCategory OMPassOptions;
4947
extern llvm::cl::opt<std::string> instrumentONNXOps;
48+
extern llvm::cl::bits<InstrumentActions> instrumentControlBits;
5049
extern llvm::cl::opt<bool> enableMemoryBundling;
5150
extern llvm::cl::opt<int> onnxOpTransformThreshold;
5251
extern llvm::cl::opt<bool> onnxOpTransformReport;

src/Compiler/CompilerPasses.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void addONNXToMLIRPasses(mlir::PassManager &pm) {
5353

5454
if (onnxOpTransformThreshold > 0) {
5555
// Dynamic iterate in ONNXOpTransformPass
56-
pm.addPass(onnx_mlir::createONNXOpTransformPass(onnxOpTransformThreshold));
56+
pm.addPass(onnx_mlir::createONNXOpTransformPass(
57+
onnxOpTransformThreshold, onnxOpTransformReport));
5758
} else {
5859
// Statically add extra passes
5960
for (int i = 0; i < repeatOnnxTransform; i++) {
@@ -75,7 +76,8 @@ void addONNXToKrnlPasses(mlir::PassManager &pm, int optLevel, bool enableCSE) {
7576
// Verify ONNX ops before lowering to Krnl.
7677
pm.addNestedPass<FuncOp>(onnx_mlir::createONNXPreKrnlVerifyPass());
7778
// Add instrumentation for Onnx Ops
78-
pm.addNestedPass<FuncOp>(onnx_mlir::createInstrumentONNXPass());
79+
pm.addNestedPass<FuncOp>(onnx_mlir::createInstrumentONNXPass(
80+
instrumentONNXOps, instrumentControlBits.getBits()));
7981
pm.addPass(onnx_mlir::createLowerToKrnlPass(optLevel));
8082
// An additional pass of canonicalization is helpful because lowering
8183
// from ONNX dialect to Standard dialect exposes additional canonicalization

src/Pass/Passes.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace onnx_mlir {
2424

2525
/// Pass for ONNX graph level optimization
2626
std::unique_ptr<mlir::Pass> createONNXOpTransformPass();
27-
std::unique_ptr<mlir::Pass> createONNXOpTransformPass(int threshold);
27+
std::unique_ptr<mlir::Pass> createONNXOpTransformPass(
28+
int threshold, bool report);
2829

2930
/// Pass for rewriting inside frontend dialect.
3031
std::unique_ptr<mlir::Pass> createDecomposeONNXToONNXPass();
@@ -39,6 +40,8 @@ std::unique_ptr<mlir::Pass> createElideConstantValuePass();
3940

4041
/// Pass for instrument the Onnx ops
4142
std::unique_ptr<mlir::Pass> createInstrumentONNXPass();
43+
std::unique_ptr<mlir::Pass> createInstrumentONNXPass(
44+
llvm::StringRef ops, int actions);
4245

4346
/// Pass for verifying Onnx ops before lowering to Krnl
4447
std::unique_ptr<mlir::Pass> createONNXPreKrnlVerifyPass();

src/Support/OMOptions.cpp

-52
This file was deleted.

src/Transform/ONNX/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ add_onnx_mlir_library(OMShapeInference
4343
add_onnx_mlir_library(OMInstrumentONNX
4444
InstrumentONNXPass.cpp
4545

46+
INCLUDE_DIRS PUBLIC
47+
${ONNX_MLIR_SRC_ROOT}/include
48+
4649
LINK_LIBS PUBLIC
47-
CompilerOptions
4850
OMONNXOps
4951
OMKrnlOps
5052
MLIRPass
@@ -54,7 +56,6 @@ add_onnx_mlir_library(OMOpTransform
5456
ONNXOpTransformPass.cpp
5557

5658
LINK_LIBS PUBLIC
57-
CompilerOptions
5859
OMONNXOps
5960
MLIRPass
6061
OMONNXRewrite

0 commit comments

Comments
 (0)