15
15
#include " CompilerUtils.hpp"
16
16
17
17
#include < fstream>
18
+ #include < memory>
18
19
#include < regex>
19
20
20
21
#include " mlir/Dialect/Func/IR/FuncOps.h"
21
22
#include " mlir/Dialect/LLVMIR/LLVMDialect.h"
22
23
#include " mlir/Parser/Parser.h"
24
+ #include " mlir/Pass/Pass.h"
23
25
#include " mlir/Pass/PassManager.h"
24
26
#include " mlir/Support/FileUtilities.h"
27
+ #include " mlir/Support/Timing.h"
25
28
#include " mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
26
29
#include " mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
27
30
#include " mlir/Target/LLVMIR/Export.h"
35
38
#include " llvm/Support/SourceMgr.h"
36
39
#include " llvm/Support/TargetSelect.h"
37
40
#include " llvm/Support/ToolOutputFile.h"
41
+ #include " llvm/Support/raw_ostream.h"
38
42
#include " llvm/Target/TargetMachine.h"
39
43
40
44
#include " src/Accelerators/Accelerator.hpp"
49
53
using namespace mlir ;
50
54
using namespace onnx_mlir ;
51
55
56
+ mlir::DefaultTimingManager timingManager;
57
+ mlir::TimingScope rootTimingScope;
52
58
namespace onnx_mlir {
53
59
54
60
// Make a function that forces preserving all files using the runtime arguments
@@ -327,6 +333,8 @@ std::string getTargetFilename(
327
333
// Returns 0 on success, error code on failure.
328
334
static int genLLVMBitcode (const mlir::OwningOpRef<ModuleOp> &module,
329
335
std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt) {
336
+ auto llvmTiming = rootTimingScope.nest (
337
+ " [onnx-mlir] Compiling MLIR module to LLVM Optimized Bitcode" );
330
338
std::error_code error;
331
339
332
340
// Write bitcode to a file.
@@ -397,7 +405,8 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
397
405
// Return 0 on success, error code on failure.
398
406
static int genModelObject (
399
407
std::string bitcodeNameWithExt, std::string &modelObjNameWithExt) {
400
-
408
+ auto objectTiming =
409
+ rootTimingScope.nest (" [onnx-mlir] Compiling LLVM Bitcode to Object File" );
401
410
std::string llcPath = getToolPath (" llc" );
402
411
Command llvmToObj (/* exePath=*/ llcPath);
403
412
setXllcOption ({" --code-model" , modelSizeStr[modelSize]});
@@ -418,6 +427,8 @@ static int genModelObject(
418
427
// Return 0 on success, error code on failure.
419
428
static int genJniObject (const mlir::OwningOpRef<ModuleOp> &module,
420
429
std::string jniSharedLibPath, std::string jniObjPath) {
430
+ auto jniTiming =
431
+ rootTimingScope.nest (" [onnx-mlir] Compiling JNI Object File" );
421
432
Command ar (/* exePath=*/ getToolPath (" ar" , true ));
422
433
int rc = ar.appendStr (" x" )
423
434
// old version of ar does not support --output so comment out
@@ -436,7 +447,8 @@ static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
436
447
static int genSharedLib (std::string sharedLibNameWithExt,
437
448
std::vector<std::string> opts, std::vector<std::string> objs,
438
449
std::vector<std::string> libs, std::vector<std::string> libDirs) {
439
-
450
+ auto sharedLibTiming =
451
+ rootTimingScope.nest (" [onnx-mlir] Linking Shared Library" );
440
452
#ifdef _WIN32
441
453
std::vector<std::string> outputOpt = {" /Fe:" + sharedLibNameWithExt};
442
454
// link has to be before libpath since they need to be passed through to the
@@ -486,6 +498,7 @@ static int genSharedLib(std::string sharedLibNameWithExt,
486
498
// Return 0 on success, error code on failure.
487
499
static int genJniJar (const mlir::OwningOpRef<ModuleOp> &module,
488
500
std::string modelSharedLibPath, std::string modelJniJarPath) {
501
+ auto jniJarTiming = rootTimingScope.nest (" [onnx-mlir] Creating JNI Jar" );
489
502
llvm::SmallString<8 > libraryPath (getLibraryPath ());
490
503
llvm::sys::path::append (libraryPath, " javaruntime.jar" );
491
504
std::string javaRuntimeJarPath = llvm::StringRef (libraryPath).str ();
@@ -880,6 +893,9 @@ static int emitOutput(mlir::OwningOpRef<ModuleOp> &module,
880
893
int compileModule (mlir::OwningOpRef<ModuleOp> &module,
881
894
mlir::MLIRContext &context, std::string outputNameNoExt,
882
895
EmissionTargetType emissionTarget) {
896
+ auto compileModuleTiming =
897
+ rootTimingScope.nest (" [onnx-mlir] Compiling Module using MLIR" );
898
+
883
899
int rc = setupModule (module, context, outputNameNoExt);
884
900
if (rc != CompilerSuccess)
885
901
return rc;
@@ -905,10 +921,14 @@ int compileModule(mlir::OwningOpRef<ModuleOp> &module,
905
921
heapLogFileame, reportHeapBefore, reportHeapAfter));
906
922
}
907
923
(void )mlir::applyPassManagerCLOptions (pm);
908
- mlir::applyDefaultTimingPassManagerCLOptions (pm);
924
+
925
+ if (enableTiming) {
926
+ pm.enableTiming (compileModuleTiming);
927
+ }
909
928
910
929
if (mlir::failed (pm.run (*module)))
911
930
return CompilerFailure;
931
+ compileModuleTiming.stop ();
912
932
return emitOutput (module, context, outputNameNoExt, pm, emissionTarget);
913
933
}
914
934
0 commit comments