Skip to content

Commit cd3255a

Browse files
authored
[gold] Enable time trace profiler in LLVMgold (#94293)
To get the time trace of LTO in the gold plugin, now we can pass the `time-trace=<time trace file>` as well as `time-trace-granularity=<granularity>` flags to LLVMgold. Note that we still have to populate `LTOConfig::TimeTraceEnabled` and `LTOConfig::TimeTraceGranularity` because ThinLTO backend needs them.
1 parent eca9caf commit cd3255a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llvm-as %s -o %t.o
2+
3+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
4+
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
5+
; RUN: -shared %t.o -o /dev/null
6+
; RUN: FileCheck --input-file %t2.json %s
7+
8+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
9+
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
10+
; RUN: --plugin-opt=time-trace-granularity=250 \
11+
; RUN: -shared %t.o -o /dev/null
12+
; RUN: FileCheck --input-file %t2.json %s
13+
14+
; RUN: not %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
15+
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
16+
; RUN: --plugin-opt=time-trace-granularity=hello \
17+
; RUN: -shared %t.o -o /dev/null 2> %t4.txt
18+
; RUN: FileCheck --input-file %t4.txt %s --check-prefix=ERR
19+
20+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
21+
target triple = "x86_64-unknown-linux-gnu"
22+
23+
define void @f1() {
24+
ret void
25+
}
26+
27+
define void @f2() {
28+
ret void
29+
}
30+
31+
; CHECK: "traceEvents":
32+
; ERR: Invalid time trace granularity: hello

llvm/tools/gold/gold-plugin.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/ADT/ScopeExit.h"
1415
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/Bitcode/BitcodeReader.h"
1617
#include "llvm/Bitcode/BitcodeWriter.h"
@@ -31,6 +32,7 @@
3132
#include "llvm/Support/Path.h"
3233
#include "llvm/Support/TargetSelect.h"
3334
#include "llvm/Support/Threading.h"
35+
#include "llvm/Support/TimeProfiler.h"
3436
#include "llvm/Support/raw_ostream.h"
3537
#include "llvm/TargetParser/Host.h"
3638
#include <list>
@@ -220,6 +222,10 @@ namespace options {
220222
static std::string cs_profile_path;
221223
static bool cs_pgo_gen = false;
222224

225+
// Time trace options.
226+
static std::string time_trace_file;
227+
static unsigned time_trace_granularity = 500;
228+
223229
static void process_plugin_option(const char *opt_)
224230
{
225231
if (opt_ == nullptr)
@@ -308,6 +314,14 @@ namespace options {
308314
RemarksFormat = std::string(opt);
309315
} else if (opt.consume_front("stats-file=")) {
310316
stats_file = std::string(opt);
317+
} else if (opt.consume_front("time-trace=")) {
318+
time_trace_file = std::string(opt);
319+
} else if (opt.consume_front("time-trace-granularity=")) {
320+
unsigned Granularity;
321+
if (opt.getAsInteger(10, Granularity))
322+
message(LDPL_FATAL, "Invalid time trace granularity: %s", opt);
323+
else
324+
time_trace_granularity = Granularity;
311325
} else {
312326
// Save this option to pass to the code generator.
313327
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
@@ -953,6 +967,10 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
953967
Conf.HasWholeProgramVisibility = options::whole_program_visibility;
954968

955969
Conf.StatsFile = options::stats_file;
970+
971+
Conf.TimeTraceEnabled = !options::time_trace_file.empty();
972+
Conf.TimeTraceGranularity = options::time_trace_granularity;
973+
956974
return std::make_unique<LTO>(std::move(Conf), Backend,
957975
options::ParallelCodeGenParallelismLevel);
958976
}
@@ -1113,6 +1131,19 @@ static ld_plugin_status allSymbolsReadHook() {
11131131
if (unsigned NumOpts = options::extra.size())
11141132
cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
11151133

1134+
// Initialize time trace profiler
1135+
if (!options::time_trace_file.empty())
1136+
llvm::timeTraceProfilerInitialize(options::time_trace_granularity,
1137+
options::extra.size() ? options::extra[0]
1138+
: "LLVMgold");
1139+
auto FinalizeTimeTrace = llvm::make_scope_exit([&]() {
1140+
if (!llvm::timeTraceProfilerEnabled())
1141+
return;
1142+
assert(!options::time_trace_file.empty());
1143+
check(llvm::timeTraceProfilerWrite(options::time_trace_file, output_name));
1144+
llvm::timeTraceProfilerCleanup();
1145+
});
1146+
11161147
std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
11171148

11181149
if (options::TheOutputType == options::OT_DISABLE ||

0 commit comments

Comments
 (0)