Skip to content

Commit b856fc5

Browse files
authored
Option to not emit the full MLIR (only emit .tmp file) (#2997)
When emitting MLIR, there are two versions of IR, <name>.onnx.mlir and <name>.tmp . Since the constant values are embedded in <name>.onnx.mlir, we got memory and disk pressure especially in large models. This option specify not emit full MLIR(<name>.onnx.mlir). This option works with emitting MLIR options such as --EmitONNXIR and --EmitMLIR. Signed-off-by: Haruki Imai <[email protected]>
1 parent 33b466e commit b856fc5

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/Compiler/CompilerOptions.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//===------------------------ CompilerOptions.cpp -------------------------===//
66
//
7-
// Copyright 2022, 2023 The IBM Research Authors.
7+
// Copyright 2022, 2024 The IBM Research Authors.
88
//
99
// =============================================================================
1010
//
@@ -49,6 +49,7 @@ EmissionTargetType emissionTarget; // onnx-mlir only
4949
bool invokeOnnxVersionConverter; // onnx-mlir only
5050
bool preserveLocations; // onnx-mlir only
5151
bool printIR; // onnx-mlir only
52+
bool doNotEmitFullMLIRCode; // onnx-mlir only
5253
bool preserveBitcode; // onnx-mlir only
5354
bool preserveLLVMIR; // onnx-mlir only
5455
bool preserveMLIR; // onnx-mlir only
@@ -281,6 +282,16 @@ static llvm::cl::opt<bool, true> printIROpt("printIR",
281282
llvm::cl::desc("Print the IR to stdout:."), llvm::cl::location(printIR),
282283
llvm::cl::init(false), llvm::cl::cat(OnnxMlirOptions));
283284

285+
static llvm::cl::opt<bool, true> doNotEmitFullMLIRCodeOpt(
286+
"do-not-emit-full-mlir-code",
287+
llvm::cl::desc(
288+
"Do not emit the MLIR the constant values are embeded "
289+
"(<name>onnx.mlir). Emit only the MLIR without the constants "
290+
"(<name>.tmp). Need to be used with emitting MLIR options such as "
291+
"--EmitONNXIR and --EmitMLIR."),
292+
llvm::cl::location(doNotEmitFullMLIRCode), llvm::cl::init(false),
293+
llvm::cl::cat(OnnxMlirOptions));
294+
284295
static llvm::cl::opt<bool, true> preserveBitcodeOpt("preserveBitcode",
285296
llvm::cl::desc("Preserve the bitcode files (optimized and unoptimized)."),
286297
llvm::cl::location(preserveBitcode), llvm::cl::init(false),

src/Compiler/CompilerOptions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern bool printIR; // onnx-mlir only
9797
extern bool preserveBitcode; // onnx-mlir only
9898
extern bool preserveLLVMIR; // onnx-mlir only
9999
extern bool preserveMLIR; // onnx-mlir only
100+
extern bool doNotEmitFullMLIRCode; // onnx-mlir only
100101
extern bool useOnnxModelTypes; // onnx-mlir only
101102
extern int repeatOnnxTransform; // onnx-mlir only
102103
extern std::string shapeInformation; // onnx-mlir only

src/Compiler/CompilerUtils.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,13 +835,14 @@ static int emitOutputFiles(std::string outputNameNoExt,
835835
// Emit the version with all constants included.
836836
std::string outputNameWithExt =
837837
getTargetFilename(outputNameNoExt, emissionTarget);
838-
int rc = outputCode(module, outputNameWithExt);
839-
if (VerboseOutput)
840-
llvm::outs() << "Full MLIR code written to:\n"
841-
<< "\t" << outputNameWithExt << "\n\n";
842-
if (rc != CompilerSuccess)
843-
return rc;
844-
838+
if (!doNotEmitFullMLIRCode) {
839+
int rc = outputCode(module, outputNameWithExt);
840+
if (VerboseOutput)
841+
llvm::outs() << "Full MLIR code written to:\n"
842+
<< "\t" << outputNameWithExt << "\n\n";
843+
if (rc != CompilerSuccess)
844+
return rc;
845+
}
845846
// Elide element attributes if larger than 100.
846847
if (emissionTarget == EmitONNXBasic || emissionTarget == EmitONNXIR ||
847848
emissionTarget == EmitMLIR) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: onnx-mlir --EmitMLIR --do-not-emit-full-mlir-code %s -o %t && test ! -f %t.onnx.mlir && rm %t.tmp
2+
3+
module {
4+
func.func @main_graph(%arg0: tensor<?xf32>) -> tensor<?xf32> {
5+
onnx.Return %arg0 : tensor<?xf32>
6+
}
7+
"onnx.EntryPoint"() {func = @main_graph} : () -> ()
8+
}
9+

0 commit comments

Comments
 (0)