diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -1133,13 +1133,7 @@ sortSegmentsAndSections(); createLoadCommands(); finalizeAddresses(); - threadPool.async([&] { - if (LLVM_ENABLE_THREADS && config->timeTraceEnabled) - timeTraceProfilerInitialize(config->timeTraceGranularity, "writeMapFile"); - writeMapFile(); - if (LLVM_ENABLE_THREADS && config->timeTraceEnabled) - timeTraceProfilerFinishThread(); - }); + threadPool.async(writeMapFile); finalizeLinkEditSegment(); writeOutputFile(); } diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h --- a/llvm/include/llvm/Support/TimeProfiler.h +++ b/llvm/include/llvm/Support/TimeProfiler.h @@ -25,6 +25,10 @@ void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName); +/// Initialize the time trace profiler with the same settings as Profiler. +void timeTraceProfilerInitialize(const TimeTraceProfiler &Profiler, + StringRef ProcName); + /// Cleanup the time trace profiler, if it was initialized. void timeTraceProfilerCleanup(); diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -1268,9 +1268,6 @@ &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, MapVector &ModuleMap) { - if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled) - timeTraceProfilerInitialize(Conf.TimeTraceGranularity, - "thin backend"); Error E = runThinLTOBackendThread( AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList, ResolvedODR, DefinedGlobals, ModuleMap); @@ -1281,8 +1278,6 @@ else Err = std::move(E); } - if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled) - timeTraceProfilerFinishThread(); }, BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap)); diff --git a/llvm/lib/Support/ThreadPool.cpp b/llvm/lib/Support/ThreadPool.cpp --- a/llvm/lib/Support/ThreadPool.cpp +++ b/llvm/lib/Support/ThreadPool.cpp @@ -15,7 +15,9 @@ #include "llvm/Config/llvm-config.h" #if LLVM_ENABLE_THREADS +#include "llvm/ADT/ScopeExit.h" #include "llvm/Support/Threading.h" +#include "llvm/Support/TimeProfiler.h" #else #include "llvm/Support/raw_ostream.h" #endif @@ -32,9 +34,16 @@ if (Threads.size() >= MaxThreadCount) return; // Already hit the max thread pool size. int newThreadCount = std::min(requested, MaxThreadCount); + TimeTraceProfiler *MainThreadProfiler = getTimeTraceProfilerInstance(); while (static_cast(Threads.size()) < newThreadCount) { int ThreadID = Threads.size(); - Threads.emplace_back([this, ThreadID] { + Threads.emplace_back([this, MainThreadProfiler, ThreadID] { + if (MainThreadProfiler) + timeTraceProfilerInitialize(*MainThreadProfiler, "worker"); + auto ProfilerFinish = make_scope_exit([] { + if (timeTraceProfilerEnabled()) + timeTraceProfilerFinishThread(); + }); Strategy.apply_thread_strategy(ThreadID); while (true) { std::function Task; diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp --- a/llvm/lib/Support/TimeProfiler.cpp +++ b/llvm/lib/Support/TimeProfiler.cpp @@ -267,6 +267,11 @@ TimeTraceGranularity, llvm::sys::path::filename(ProcName)); } +void llvm::timeTraceProfilerInitialize(const TimeTraceProfiler &Profiler, + StringRef ProcName) { + timeTraceProfilerInitialize(Profiler.TimeTraceGranularity, ProcName); +} + // Removes all TimeTraceProfilerInstances. // Called from main thread. void llvm::timeTraceProfilerCleanup() {