[llc] Support -time-trace in llc

Mostly copied from opt.cpp.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D111466
This commit is contained in:
Arthur Eubanks 2021-10-11 10:16:30 -07:00
parent 7f55209cee
commit 337cf0a5ab
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,8 @@
; RUN: llc -o /dev/null -O2 -time-trace -time-trace-granularity=100 -time-trace-file=%t.json
; RUN: FileCheck --input-file=%t.json %s
; CHECK: "traceEvents"
define void @f() {
ret void
}

View File

@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/CodeGen/CommandFlags.h"
@ -49,6 +50,7 @@
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
@ -82,6 +84,19 @@ TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
cl::value_desc("N"),
cl::desc("Repeat compilation N times for timing"));
static cl::opt<bool> TimeTrace("time-trace", cl::desc("Record time trace"));
static cl::opt<unsigned> TimeTraceGranularity(
"time-trace-granularity",
cl::desc(
"Minimum time granularity (in microseconds) traced by time profiler"),
cl::init(500), cl::Hidden);
static cl::opt<std::string>
TimeTraceFile("time-trace-file",
cl::desc("Specify time trace file destination"),
cl::value_desc("filename"));
static cl::opt<std::string>
BinutilsVersion("binutils-version", cl::Hidden,
cl::desc("Produced object files can use all ELF features "
@ -363,6 +378,20 @@ int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
if (TimeTrace)
timeTraceProfilerInitialize(TimeTraceGranularity, argv[0]);
auto TimeTraceScopeExit = make_scope_exit([]() {
if (TimeTrace) {
if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
handleAllErrors(std::move(E), [&](const StringError &SE) {
errs() << SE.getMessage() << "\n";
});
return;
}
timeTraceProfilerCleanup();
}
});
LLVMContext Context;
Context.setDiscardValueNames(DiscardValueNames);