Skip to content

[gold] Enable time trace profiler in LLVMgold #94293

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 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions llvm/test/tools/gold/X86/time-trace.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; RUN: llvm-as %s -o %t.o

; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
; RUN: -shared %t.o -o /dev/null
; RUN: FileCheck --input-file %t2.json %s

; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
; RUN: --plugin-opt=time-trace-granularity=250 \
; RUN: -shared %t.o -o /dev/null
; RUN: FileCheck --input-file %t2.json %s

; RUN: not %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
; RUN: -m elf_x86_64 --plugin-opt=time-trace=%t2.json \
; RUN: --plugin-opt=time-trace-granularity=hello \
; RUN: -shared %t.o -o /dev/null 2> %t4.txt
; RUN: FileCheck --input-file %t4.txt %s --check-prefix=ERR

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @f1() {
ret void
}

define void @f2() {
ret void
}

; CHECK: "traceEvents":
; ERR: Invalid time trace granularity: hello
31 changes: 31 additions & 0 deletions llvm/tools/gold/gold-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
Expand All @@ -31,6 +32,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
#include <list>
Expand Down Expand Up @@ -220,6 +222,10 @@ namespace options {
static std::string cs_profile_path;
static bool cs_pgo_gen = false;

// Time trace options.
static std::string time_trace_file;
static unsigned time_trace_granularity = 500;

static void process_plugin_option(const char *opt_)
{
if (opt_ == nullptr)
Expand Down Expand Up @@ -308,6 +314,14 @@ namespace options {
RemarksFormat = std::string(opt);
} else if (opt.consume_front("stats-file=")) {
stats_file = std::string(opt);
} else if (opt.consume_front("time-trace=")) {
time_trace_file = std::string(opt);
} else if (opt.consume_front("time-trace-granularity=")) {
unsigned Granularity;
if (opt.getAsInteger(10, Granularity))
message(LDPL_FATAL, "Invalid time trace granularity: %s", opt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is passing a StringRef to a varargs function looking for const char *. This is obviously wrong, but it looks like the ABI happens to work out fine on non-s390x architectures...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put up #95083 to fix this.

else
time_trace_granularity = Granularity;
} else {
// Save this option to pass to the code generator.
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
Expand Down Expand Up @@ -953,6 +967,10 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
Conf.HasWholeProgramVisibility = options::whole_program_visibility;

Conf.StatsFile = options::stats_file;

Conf.TimeTraceEnabled = !options::time_trace_file.empty();
Conf.TimeTraceGranularity = options::time_trace_granularity;

return std::make_unique<LTO>(std::move(Conf), Backend,
options::ParallelCodeGenParallelismLevel);
}
Expand Down Expand Up @@ -1113,6 +1131,19 @@ static ld_plugin_status allSymbolsReadHook() {
if (unsigned NumOpts = options::extra.size())
cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);

// Initialize time trace profiler
if (!options::time_trace_file.empty())
llvm::timeTraceProfilerInitialize(options::time_trace_granularity,
options::extra.size() ? options::extra[0]
: "LLVMgold");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At

extra.push_back("LLVMgold");
we set extra[0] to "LLVMgold" if extra is empty, so this shouldn't be needed (extra should never be empty and extra[0] can always be used).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we reach this line (line 316) only if there are additional -plugin-opt that LLVMgold doesn't recognize. If there aren't any of those, I think options::extra will remain empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it thanks

auto FinalizeTimeTrace = llvm::make_scope_exit([&]() {
if (!llvm::timeTraceProfilerEnabled())
return;
assert(!options::time_trace_file.empty());
check(llvm::timeTraceProfilerWrite(options::time_trace_file, output_name));
llvm::timeTraceProfilerCleanup();
});

std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();

if (options::TheOutputType == options::OT_DISABLE ||
Expand Down
Loading