14
14
15
15
#include " CompilerUtils.hpp"
16
16
17
- #include < fstream>
18
- #include < regex>
19
-
20
17
#include " mlir/Dialect/Func/IR/FuncOps.h"
21
18
#include " mlir/Dialect/LLVMIR/LLVMDialect.h"
22
19
#include " mlir/Parser/Parser.h"
20
+ #include " mlir/Pass/Pass.h"
23
21
#include " mlir/Pass/PassManager.h"
24
22
#include " mlir/Support/FileUtilities.h"
23
+ #include " mlir/Support/Timing.h"
25
24
#include " mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
26
25
#include " mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
27
26
#include " mlir/Target/LLVMIR/Export.h"
35
34
#include " llvm/Support/SourceMgr.h"
36
35
#include " llvm/Support/TargetSelect.h"
37
36
#include " llvm/Support/ToolOutputFile.h"
37
+ #include " llvm/Support/raw_ostream.h"
38
38
#include " llvm/Target/TargetMachine.h"
39
+ #include < fstream>
40
+ #include < memory>
41
+ #include < regex>
39
42
40
43
#include " src/Accelerators/Accelerator.hpp"
41
44
#include " src/Builder/FrontendDialectTransformer.hpp"
49
52
using namespace mlir ;
50
53
using namespace onnx_mlir ;
51
54
55
+ extern mlir::DefaultTimingManager timingManager;
56
+
52
57
namespace onnx_mlir {
53
58
54
59
// Make a function that forces preserving all files using the runtime arguments
@@ -146,8 +151,7 @@ int Command::exec(std::string wdir) const {
146
151
llvm::errs () << llvm::join (argsRef, " " ) << " \n "
147
152
<< " Error message: " << errMsg << " \n "
148
153
<< " Program path: " << _path << " \n "
149
- << " Command execution failed."
150
- << " \n " ;
154
+ << " Command execution failed." << " \n " ;
151
155
return rc;
152
156
}
153
157
@@ -327,6 +331,10 @@ std::string getTargetFilename(
327
331
// Returns 0 on success, error code on failure.
328
332
static int genLLVMBitcode (const mlir::OwningOpRef<ModuleOp> &module,
329
333
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
+
330
338
std::error_code error;
331
339
332
340
// Write bitcode to a file.
@@ -397,7 +405,9 @@ 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 rootScope = timingManager.getRootScope ();
409
+ auto objectScope =
410
+ rootScope.nest (" [onnx-mlir] Compiling LLVM Bitcode to Object File" );
401
411
std::string llcPath = getToolPath (" llc" );
402
412
Command llvmToObj (/* exePath=*/ llcPath);
403
413
setXllcOption ({" --code-model" , modelSizeStr[modelSize]});
@@ -418,6 +428,9 @@ static int genModelObject(
418
428
// Return 0 on success, error code on failure.
419
429
static int genJniObject (const mlir::OwningOpRef<ModuleOp> &module,
420
430
std::string jniSharedLibPath, std::string jniObjPath) {
431
+ auto rootScope = timingManager.getRootScope ();
432
+ auto jniScope = rootScope.nest (" [onnx-mlir] Compiling JNI Object File" );
433
+
421
434
Command ar (/* exePath=*/ getToolPath (" ar" , true ));
422
435
int rc = ar.appendStr (" x" )
423
436
// old version of ar does not support --output so comment out
@@ -436,7 +449,8 @@ static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
436
449
static int genSharedLib (std::string sharedLibNameWithExt,
437
450
std::vector<std::string> opts, std::vector<std::string> objs,
438
451
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" );
440
454
#ifdef _WIN32
441
455
std::vector<std::string> outputOpt = {" /Fe:" + sharedLibNameWithExt};
442
456
// 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,
486
500
// Return 0 on success, error code on failure.
487
501
static int genJniJar (const mlir::OwningOpRef<ModuleOp> &module,
488
502
std::string modelSharedLibPath, std::string modelJniJarPath) {
503
+ auto rootScope = timingManager.getRootScope ();
504
+ auto jniJarScope = rootScope.nest (" [onnx-mlir] Creating JNI Jar" );
505
+
489
506
llvm::SmallString<8 > libraryPath (getLibraryPath ());
490
507
llvm::sys::path::append (libraryPath, " javaruntime.jar" );
491
508
std::string javaRuntimeJarPath = llvm::StringRef (libraryPath).str ();
@@ -508,6 +525,7 @@ static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module,
508
525
// Return 0 on success, error code on failure
509
526
static int compileModuleToObject (const mlir::OwningOpRef<ModuleOp> &module,
510
527
std::string outputNameWithoutExt, std::string &objectNameWithExt) {
528
+
511
529
std::string bitcodeNameWithExt = outputNameWithoutExt + " .bc" ;
512
530
int rc = genLLVMBitcode (module, outputNameWithoutExt, bitcodeNameWithExt);
513
531
if (rc != CompilerSuccess)
@@ -522,6 +540,7 @@ static int compileModuleToObject(const mlir::OwningOpRef<ModuleOp> &module,
522
540
static int compileModuleToSharedLibrary (
523
541
const mlir::OwningOpRef<ModuleOp> &module, std::string outputNameNoExt,
524
542
std::string &libNameWithExt) {
543
+
525
544
std::string modelObjNameWithExt;
526
545
int rc = compileModuleToObject (module, outputNameNoExt, modelObjNameWithExt);
527
546
if (rc != CompilerSuccess)
@@ -879,6 +898,9 @@ static int emitOutput(mlir::OwningOpRef<ModuleOp> &module,
879
898
int compileModule (mlir::OwningOpRef<ModuleOp> &module,
880
899
mlir::MLIRContext &context, std::string outputNameNoExt,
881
900
EmissionTargetType emissionTarget) {
901
+ auto rootScope = timingManager.getRootScope ();
902
+ auto compileModuleScope = rootScope.nest (" [onnx-mlir] Compile Module Setup" );
903
+
882
904
int rc = setupModule (module, context, outputNameNoExt);
883
905
if (rc != CompilerSuccess)
884
906
return rc;
@@ -904,7 +926,11 @@ int compileModule(mlir::OwningOpRef<ModuleOp> &module,
904
926
heapLogFileame, reportHeapBefore, reportHeapAfter));
905
927
}
906
928
(void )mlir::applyPassManagerCLOptions (pm);
907
- mlir::applyDefaultTimingPassManagerCLOptions (pm);
929
+
930
+ if (enableTiming) {
931
+ pm.enableTiming (rootScope);
932
+ }
933
+ compileModuleScope.stop ();
908
934
909
935
if (mlir::failed (pm.run (*module)))
910
936
return CompilerFailure;
0 commit comments