Skip to content

Timing tool that adds the flag “--enable-timing" for a detailed report #2801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 25, 2024
7 changes: 7 additions & 0 deletions src/Compiler/CompilerOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ std::vector<std::string> extraLibs; // onnx-mlir only
ProfileIRs profileIR; // onnx-mlir only
OptReport optReport; // onnx-mlir only
bool useOldBufferization; // onnx-mlir only
bool enableTiming; // onnx-mlir only
bool split_input_file; // onnx-mlir-opt only
bool verify_diagnostics; // onnx-mlir-opt only
bool verify_passes; // onnx-mlir-opt only
Expand Down Expand Up @@ -546,6 +547,12 @@ static llvm::cl::opt<OptReport, true> optReportOpt("opt-report",
clEnumVal(Simd, "Provide report on how SIMD is applied to ONNX ops.")),
llvm::cl::init(OptReport::NoReport), llvm::cl::cat(OnnxMlirOptions));

static llvm::cl::opt<bool, true> enable_timing("enable-timing",
llvm::cl::desc("Enable compile timing (default is false)\n"
"Set to 'true' if you want to enable compile timing."),
llvm::cl::location(enableTiming), llvm::cl::init(false),
llvm::cl::cat(OnnxMlirOptions));

// Options for onnx-mlir-opt only
static llvm::cl::opt<bool, true> split_input_file_opt("split-input-file",
llvm::cl::desc("Split the input file into pieces and process each "
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/CompilerOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extern std::vector<std::string> extraLibs; // onnx-mlir only
extern ProfileIRs profileIR; // onnx-mlir only
extern OptReport optReport; // onnx-mlir only
extern bool useOldBufferization; // onnx-mlir only
extern bool enableTiming; // onnx-mlir only
extern bool split_input_file; // onnx-mlir-opt only
extern bool verify_diagnostics; // onnx-mlir-opt only
extern bool verify_passes; // onnx-mlir-opt only
Expand Down
26 changes: 23 additions & 3 deletions src/Compiler/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
#include "CompilerUtils.hpp"

#include <fstream>
#include <memory>
#include <regex>

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/Timing.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
Expand All @@ -35,6 +38,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"

#include "src/Accelerators/Accelerator.hpp"
Expand All @@ -49,6 +53,8 @@
using namespace mlir;
using namespace onnx_mlir;

mlir::DefaultTimingManager timingManager;
mlir::TimingScope rootTimingScope;
namespace onnx_mlir {

// Make a function that forces preserving all files using the runtime arguments
Expand Down Expand Up @@ -327,6 +333,8 @@ std::string getTargetFilename(
// Returns 0 on success, error code on failure.
static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt) {
auto llvmTiming = rootTimingScope.nest(
"[onnx-mlir] Compiling MLIR module to LLVM Optimized Bitcode");
std::error_code error;

// Write bitcode to a file.
Expand Down Expand Up @@ -397,7 +405,8 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module,
// Return 0 on success, error code on failure.
static int genModelObject(
std::string bitcodeNameWithExt, std::string &modelObjNameWithExt) {

auto objectTiming =
rootTimingScope.nest("[onnx-mlir] Compiling LLVM Bitcode to Object File");
std::string llcPath = getToolPath("llc");
Command llvmToObj(/*exePath=*/llcPath);
setXllcOption({"--code-model", modelSizeStr[modelSize]});
Expand All @@ -418,6 +427,8 @@ static int genModelObject(
// Return 0 on success, error code on failure.
static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
std::string jniSharedLibPath, std::string jniObjPath) {
auto jniTiming =
rootTimingScope.nest("[onnx-mlir] Compiling JNI Object File");
Command ar(/*exePath=*/getToolPath("ar", true));
int rc = ar.appendStr("x")
// old version of ar does not support --output so comment out
Expand All @@ -436,7 +447,8 @@ static int genJniObject(const mlir::OwningOpRef<ModuleOp> &module,
static int genSharedLib(std::string sharedLibNameWithExt,
std::vector<std::string> opts, std::vector<std::string> objs,
std::vector<std::string> libs, std::vector<std::string> libDirs) {

auto sharedLibTiming =
rootTimingScope.nest("[onnx-mlir] Linking Shared Library");
#ifdef _WIN32
std::vector<std::string> outputOpt = {"/Fe:" + sharedLibNameWithExt};
// link has to be before libpath since they need to be passed through to the
Expand Down Expand Up @@ -486,6 +498,7 @@ static int genSharedLib(std::string sharedLibNameWithExt,
// Return 0 on success, error code on failure.
static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module,
std::string modelSharedLibPath, std::string modelJniJarPath) {
auto jniJarTiming = rootTimingScope.nest("[onnx-mlir] Creating JNI Jar");
llvm::SmallString<8> libraryPath(getLibraryPath());
llvm::sys::path::append(libraryPath, "javaruntime.jar");
std::string javaRuntimeJarPath = llvm::StringRef(libraryPath).str();
Expand Down Expand Up @@ -879,6 +892,9 @@ static int emitOutput(mlir::OwningOpRef<ModuleOp> &module,
int compileModule(mlir::OwningOpRef<ModuleOp> &module,
mlir::MLIRContext &context, std::string outputNameNoExt,
EmissionTargetType emissionTarget) {
auto compileModuleTiming =
rootTimingScope.nest("[onnx-mlir] Compiling Module using MLIR");

int rc = setupModule(module, context, outputNameNoExt);
if (rc != CompilerSuccess)
return rc;
Expand All @@ -904,10 +920,14 @@ int compileModule(mlir::OwningOpRef<ModuleOp> &module,
heapLogFileame, reportHeapBefore, reportHeapAfter));
}
(void)mlir::applyPassManagerCLOptions(pm);
mlir::applyDefaultTimingPassManagerCLOptions(pm);

if (enableTiming) {
pm.enableTiming(compileModuleTiming);
}

if (mlir::failed(pm.run(*module)))
return CompilerFailure;
compileModuleTiming.stop();
return emitOutput(module, context, outputNameNoExt, pm, emissionTarget);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Compiler/CompilerUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/OwningOpRef.h"
#include "mlir/Support/Timing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Path.h"

#include <optional>
#include <string>
#include <vector>

extern mlir::DefaultTimingManager timingManager;
extern mlir::TimingScope rootTimingScope;

namespace onnx_mlir {

struct Command {
Expand Down
16 changes: 10 additions & 6 deletions src/onnx-mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// Implements main for onnx-mlir driver.
//===----------------------------------------------------------------------===//

#include <iostream>
#include <regex>

#include "mlir/Support/Timing.h"
#include "src/Compiler/CompilerOptions.hpp"
#include "src/Compiler/CompilerUtils.hpp"
#include "src/Version/Version.hpp"
Expand All @@ -24,12 +24,10 @@
using namespace onnx_mlir;

int main(int argc, char *argv[]) {

// Register MLIR command line options.
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
mlir::registerDefaultTimingManagerCLOptions();
mlir::registerAsmPrinterCLOptions();

llvm::cl::SetVersionPrinter(getVersionPrinter);
Expand All @@ -44,9 +42,13 @@ int main(int argc, char *argv[]) {
llvm::errs() << "Failed to parse options\n";
return 1;
}

initCompilerConfig();

// Timing manager reporting enabled via "--enable-timing" compiler flag
timingManager.setEnabled(enableTiming);
rootTimingScope = timingManager.getRootScope();
auto setupTiming = rootTimingScope.nest("[onnx-mlir] Loading Dialects");

// Special handling of outputBaseName to derive output filename.
// outputBaseName must specify a file, so ignore invalid values
// such as ".", "..", "./", "/.", etc.
Expand All @@ -71,7 +73,9 @@ int main(int argc, char *argv[]) {
LLVM_DEBUG(llvm::dbgs() << "multithreading is disabled\n");
}
loadDialects(context);

setupTiming.stop();
auto inputFileTiming =
rootTimingScope.nest("[onnx-mlir] Importing Input Model to MLIR");
mlir::OwningOpRef<mlir::ModuleOp> module;
std::string errorMessage;
int rc = processInputFile(inputFilename, context, module, &errorMessage);
Expand All @@ -80,6 +84,6 @@ int main(int argc, char *argv[]) {
llvm::errs() << errorMessage << "\n";
return 1;
}

inputFileTiming.stop();
return compileModule(module, context, outputBaseName, emissionTarget);
}
Loading