Skip to content

Commit 7223e0f

Browse files
committed
I finished a timing tool that allows the flag “--enable-timing” to start a detailed timing report for the entire onnx-mlir execution process, including setup in main() in onnx-mlir.cpp, input file processing before compilation, the passes run in the pass manager, and finally, the lower level compilation/generation stages (LLVM, Object, Shared Library, JNI). This tool will print the detailed execution report as a tree of all the timed stages to the terminal with the (wall time) and (relative percentage of program execution) for each timed stage and the total execution time of the onnx-mlir compiler. I used the DefaultTimingManager from llvm-project “timing.cpp” and timing scopes to time and name essential code sections where work is being done.
Signed-off-by: Paramvir Sran <[email protected]>
1 parent 5b1d90b commit 7223e0f

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ project(onnx-mlir)
77

88
option(ONNX_MLIR_BUILD_TESTS "Build ONNX-MLIR test executables. If OFF, just generate build targets." ON)
99
option(ONNX_MLIR_CCACHE_BUILD "Set to ON for a ccache enabled build." OFF)
10-
option(ONNX_MLIR_ENABLE_STABLEHLO "Enable StableHLO support." ON)
10+
option(ONNX_MLIR_ENABLE_STABLEHLO "Enable StableHLO support." OFF)
1111
option(ONNX_MLIR_DECOMP_ONNX_CONVTRANSPOSE "Enable ONNXConvTransposeOp decomposition." ON)
1212
option(ONNX_MLIR_ENABLE_WERROR "Enable warnings as errors." OFF)
1313
option(ONNX_MLIR_SUPPRESS_THIRD_PARTY_WARNINGS "Suppress warning in third_party code." ON)

docs/devcontainer-example/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"customizations": {
44
"vscode": {
55
"settings": {
6+
"extensions.verifySignature": false,
67
"terminal.integrated.defaultProfile.linux": "bash",
78
"cmake.mergedCompileCommands": "${workspaceFolder}/compile_commands.json",
89
"cmakeExplorer.suiteDelimiter": ".",
910
"cmakeExplorer.debugConfig": "(gdb) Launch",
1011
"editor.formatOnSave": true,
1112
"[cpp]": {
1213
"editor.defaultFormatter": "xaver.clang-format",
13-
"editor.tabSize": 2
14+
"editor.tabSize":2
1415
},
1516
"clangd.path": "/usr/bin/clangd-15",
1617
"clangd.arguments": [

src/Compiler/CompilerOptions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ std::vector<std::string> extraLibs; // onnx-mlir only
8282
ProfileIRs profileIR; // onnx-mlir only
8383
OptReport optReport; // onnx-mlir only
8484
bool useOldBufferization; // onnx-mlir only
85+
bool enableTiming; // onnx-mlir only
8586
bool split_input_file; // onnx-mlir-opt only
8687
bool verify_diagnostics; // onnx-mlir-opt only
8788
bool verify_passes; // onnx-mlir-opt only
@@ -546,6 +547,12 @@ static llvm::cl::opt<OptReport, true> optReportOpt("opt-report",
546547
clEnumVal(Simd, "Provide report on how SIMD is applied to ONNX ops.")),
547548
llvm::cl::init(OptReport::NoReport), llvm::cl::cat(OnnxMlirOptions));
548549

550+
static llvm::cl::opt<bool, true> enable_timing("enable-timing",
551+
llvm::cl::desc("Enable pass timing (default is false)\n"
552+
"Set to 'true' if you want to enable pass timings."),
553+
llvm::cl::location(enableTiming), llvm::cl::init(false),
554+
llvm::cl::cat(OnnxMlirOptions));
555+
549556
// Options for onnx-mlir-opt only
550557
static llvm::cl::opt<bool, true> split_input_file_opt("split-input-file",
551558
llvm::cl::desc("Split the input file into pieces and process each "
@@ -579,6 +586,7 @@ static llvm::cl::opt<bool, true> useOldBufferizationOpt("use-old-bufferization",
579586
llvm::cl::location(useOldBufferization), llvm::cl::init(true),
580587
llvm::cl::cat(OnnxMlirOptions));
581588

589+
582590
// Configuration states associated with certain options.
583591
// For example, when maccel is specified, NNPA can register
584592
// dependent libdnn.

src/Compiler/CompilerOptions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ extern std::vector<std::string> extraLibs; // onnx-mlir only
125125
extern ProfileIRs profileIR; // onnx-mlir only
126126
extern OptReport optReport; // onnx-mlir only
127127
extern bool useOldBufferization; // onnx-mlir only
128+
extern bool enableTiming; // onnx-mlir only
128129
extern bool split_input_file; // onnx-mlir-opt only
129130
extern bool verify_diagnostics; // onnx-mlir-opt only
130131
extern bool verify_passes; // onnx-mlir-opt only

src/Compiler/CompilerUtils.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
#include "CompilerUtils.hpp"
1616

17-
#include <fstream>
18-
#include <regex>
19-
2017
#include "mlir/Dialect/Func/IR/FuncOps.h"
2118
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2219
#include "mlir/Parser/Parser.h"
20+
#include "mlir/Pass/Pass.h"
2321
#include "mlir/Pass/PassManager.h"
2422
#include "mlir/Support/FileUtilities.h"
23+
#include "mlir/Support/Timing.h"
2524
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
2625
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
2726
#include "mlir/Target/LLVMIR/Export.h"
@@ -35,7 +34,11 @@
3534
#include "llvm/Support/SourceMgr.h"
3635
#include "llvm/Support/TargetSelect.h"
3736
#include "llvm/Support/ToolOutputFile.h"
37+
#include "llvm/Support/raw_ostream.h"
3838
#include "llvm/Target/TargetMachine.h"
39+
#include <fstream>
40+
#include <memory>
41+
#include <regex>
3942

4043
#include "src/Accelerators/Accelerator.hpp"
4144
#include "src/Builder/FrontendDialectTransformer.hpp"
@@ -49,6 +52,8 @@
4952
using namespace mlir;
5053
using namespace onnx_mlir;
5154

55+
extern mlir::DefaultTimingManager timingManager;
56+
5257
namespace onnx_mlir {
5358

5459
// Make a function that forces preserving all files using the runtime arguments
@@ -146,8 +151,7 @@ int Command::exec(std::string wdir) const {
146151
llvm::errs() << llvm::join(argsRef, " ") << "\n"
147152
<< "Error message: " << errMsg << "\n"
148153
<< "Program path: " << _path << "\n"
149-
<< "Command execution failed."
150-
<< "\n";
154+
<< "Command execution failed." << "\n";
151155
return rc;
152156
}
153157

@@ -327,6 +331,10 @@ std::string getTargetFilename(
327331
// Returns 0 on success, error code on failure.
328332
static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
329333
std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt) {
334+
auto rootScope = timingManager.getRootScope();
335+
auto llvmScope =
336+
rootScope.nest("[onnx-mlir] Compiling to LLVM Optimized Bitcode");
337+
330338
std::error_code error;
331339

332340
// Write bitcode to a file.
@@ -397,7 +405,9 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
397405
// Return 0 on success, error code on failure.
398406
static int genModelObject(
399407
std::string bitcodeNameWithExt, std::string &modelObjNameWithExt) {
400-
408+
auto rootScope = timingManager.getRootScope();
409+
auto objectScope =
410+
rootScope.nest("[onnx-mlir] Compiling LLVM Bitcode to Object File");
401411
std::string llcPath = getToolPath("llc");
402412
Command llvmToObj(/*exePath=*/llcPath);
403413
setXllcOption({"--code-model", modelSizeStr[modelSize]});
@@ -418,6 +428,9 @@ static int genModelObject(
418428
// Return 0 on success, error code on failure.
419429
static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
420430
std::string jniSharedLibPath, std::string jniObjPath) {
431+
auto rootScope = timingManager.getRootScope();
432+
auto jniScope = rootScope.nest("[onnx-mlir] Compiling JNI Object File");
433+
421434
Command ar(/*exePath=*/getToolPath("ar", true));
422435
int rc = ar.appendStr("x")
423436
// old version of ar does not support --output so comment out
@@ -436,7 +449,8 @@ static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
436449
static int genSharedLib(std::string sharedLibNameWithExt,
437450
std::vector<std::string> opts, std::vector<std::string> objs,
438451
std::vector<std::string> libs, std::vector<std::string> libDirs) {
439-
452+
auto rootScope = timingManager.getRootScope();
453+
auto libraryScope = rootScope.nest("[onnx-mlir] Linking Shared Library");
440454
#ifdef _WIN32
441455
std::vector<std::string> outputOpt = {"/Fe:" + sharedLibNameWithExt};
442456
// link has to be before libpath since they need to be passed through to the
@@ -486,6 +500,9 @@ static int genSharedLib(std::string sharedLibNameWithExt,
486500
// Return 0 on success, error code on failure.
487501
static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module,
488502
std::string modelSharedLibPath, std::string modelJniJarPath) {
503+
auto rootScope = timingManager.getRootScope();
504+
auto jniJarScope = rootScope.nest("[onnx-mlir] Creating JNI Jar");
505+
489506
llvm::SmallString<8> libraryPath(getLibraryPath());
490507
llvm::sys::path::append(libraryPath, "javaruntime.jar");
491508
std::string javaRuntimeJarPath = llvm::StringRef(libraryPath).str();
@@ -508,6 +525,7 @@ static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module,
508525
// Return 0 on success, error code on failure
509526
static int compileModuleToObject(const mlir::OwningOpRef<ModuleOp> &module,
510527
std::string outputNameWithoutExt, std::string &objectNameWithExt) {
528+
511529
std::string bitcodeNameWithExt = outputNameWithoutExt + ".bc";
512530
int rc = genLLVMBitcode(module, outputNameWithoutExt, bitcodeNameWithExt);
513531
if (rc != CompilerSuccess)
@@ -522,6 +540,7 @@ static int compileModuleToObject(const mlir::OwningOpRef<ModuleOp> &module,
522540
static int compileModuleToSharedLibrary(
523541
const mlir::OwningOpRef<ModuleOp> &module, std::string outputNameNoExt,
524542
std::string &libNameWithExt) {
543+
525544
std::string modelObjNameWithExt;
526545
int rc = compileModuleToObject(module, outputNameNoExt, modelObjNameWithExt);
527546
if (rc != CompilerSuccess)
@@ -879,6 +898,9 @@ static int emitOutput(mlir::OwningOpRef<ModuleOp> &module,
879898
int compileModule(mlir::OwningOpRef<ModuleOp> &module,
880899
mlir::MLIRContext &context, std::string outputNameNoExt,
881900
EmissionTargetType emissionTarget) {
901+
auto rootScope = timingManager.getRootScope();
902+
auto compileModuleScope = rootScope.nest("[onnx-mlir] Compile Module Setup");
903+
882904
int rc = setupModule(module, context, outputNameNoExt);
883905
if (rc != CompilerSuccess)
884906
return rc;
@@ -904,7 +926,11 @@ int compileModule(mlir::OwningOpRef<ModuleOp> &module,
904926
heapLogFileame, reportHeapBefore, reportHeapAfter));
905927
}
906928
(void)mlir::applyPassManagerCLOptions(pm);
907-
mlir::applyDefaultTimingPassManagerCLOptions(pm);
929+
930+
if (enableTiming) {
931+
pm.enableTiming(rootScope);
932+
}
933+
compileModuleScope.stop();
908934

909935
if (mlir::failed(pm.run(*module)))
910936
return CompilerFailure;

src/Dialect/ONNX/ElementsAttr/ElementsAttrBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <algorithm>
2424
#include <numeric>
25-
2625
using namespace mlir;
2726

2827
namespace onnx_mlir {
@@ -217,6 +216,8 @@ ElementsAttr ElementsAttrBuilder::combine(ElementsAttr lhs, ElementsAttr rhs,
217216
WideNum *dst = dstNums.data();
218217
const WideNum *lhsSrc = lhsNums.get().data();
219218
const WideNum *rhsSrc = rhsNums.get().data();
219+
220+
220221
for (auto &idxoffs :
221222
StridesRange<2>(combinedShape, {xpLhsStrides, xpRhsStrides})) {
222223
dst[idxoffs.flattenedIndex] =

src/onnx-mlir.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// Implements main for onnx-mlir driver.
1212
//===----------------------------------------------------------------------===//
1313

14-
#include <iostream>
1514
#include <regex>
1615

16+
#include "mlir/Support/Timing.h"
1717
#include "src/Compiler/CompilerOptions.hpp"
1818
#include "src/Compiler/CompilerUtils.hpp"
1919
#include "src/Version/Version.hpp"
@@ -23,8 +23,9 @@
2323

2424
using namespace onnx_mlir;
2525

26-
int main(int argc, char *argv[]) {
26+
mlir::DefaultTimingManager timingManager;
2727

28+
int main(int argc, char *argv[]) {
2829
// Register MLIR command line options.
2930
mlir::registerAsmPrinterCLOptions();
3031
mlir::registerMLIRContextCLOptions();
@@ -44,9 +45,14 @@ int main(int argc, char *argv[]) {
4445
llvm::errs() << "Failed to parse options\n";
4546
return 1;
4647
}
47-
4848
initCompilerConfig();
4949

50+
// timing manager reporting enabled via "--enable-timing" compiler flag
51+
mlir::applyDefaultTimingManagerCLOptions(timingManager);
52+
timingManager.setEnabled(enableTiming);
53+
auto rootScope = timingManager.getRootScope();
54+
auto setupScope = rootScope.nest("[onnx-mlir] Preparing for Compilation");
55+
5056
// Special handling of outputBaseName to derive output filename.
5157
// outputBaseName must specify a file, so ignore invalid values
5258
// such as ".", "..", "./", "/.", etc.
@@ -71,7 +77,8 @@ int main(int argc, char *argv[]) {
7177
LLVM_DEBUG(llvm::dbgs() << "multithreading is disabled\n");
7278
}
7379
loadDialects(context);
74-
80+
setupScope.stop();
81+
auto inputFileScope = rootScope.nest("[onnx-mlir] Processing of Input File");
7582
mlir::OwningOpRef<mlir::ModuleOp> module;
7683
std::string errorMessage;
7784
int rc = processInputFile(inputFilename, context, module, &errorMessage);
@@ -80,6 +87,6 @@ int main(int argc, char *argv[]) {
8087
llvm::errs() << errorMessage << "\n";
8188
return 1;
8289
}
83-
90+
inputFileScope.stop();
8491
return compileModule(module, context, outputBaseName, emissionTarget);
8592
}

0 commit comments

Comments
 (0)