diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -1129,13 +1129,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 @@ -1267,9 +1267,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); @@ -1280,8 +1277,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 @@ -16,6 +16,7 @@ #if LLVM_ENABLE_THREADS #include "llvm/Support/Threading.h" +#include "llvm/Support/TimeProfiler.h" #else #include "llvm/Support/raw_ostream.h" #endif @@ -32,9 +33,12 @@ 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"); Strategy.apply_thread_strategy(ThreadID); while (true) { std::function Task; @@ -44,8 +48,11 @@ QueueCondition.wait(LockGuard, [&] { return !EnableFlag || !Tasks.empty(); }); // Exit condition - if (!EnableFlag && Tasks.empty()) + if (!EnableFlag && Tasks.empty()) { + if (timeTraceProfilerEnabled()) + timeTraceProfilerFinishThread(); return; + } // Yeah, we have a task, grab it and release the lock on the queue // We first need to signal that we are active before popping the queue 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() {