diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -1108,6 +1108,8 @@ } void LinkerDriver::link(ArrayRef argsArr) { + ScopedTimer rootTimer(Timer::root()); + // Needed for LTO. InitializeAllTargetInfos(); InitializeAllTargets(); @@ -1166,7 +1168,6 @@ config->showSummary = args.hasArg(OPT_summary); - ScopedTimer t(Timer::root()); // Handle --version, which is an lld extension. This option is a bit odd // because it doesn't start with "/", but we deliberately chose "--" to // avoid conflict with /version and for compatibility with clang-cl. @@ -2042,7 +2043,7 @@ writeResult(); // Stop early so we can print the results. - Timer::root().stop(); + rootTimer.stop(); if (config->showTiming) Timer::root().print(); } diff --git a/lld/Common/Timer.cpp b/lld/Common/Timer.cpp --- a/lld/Common/Timer.cpp +++ b/lld/Common/Timer.cpp @@ -13,29 +13,22 @@ using namespace lld; using namespace llvm; -ScopedTimer::ScopedTimer(Timer &t) : t(&t) { t.start(); } +ScopedTimer::ScopedTimer(Timer &t) : t(&t) { + startTime = std::chrono::high_resolution_clock::now(); +} void ScopedTimer::stop() { if (!t) return; - t->stop(); + t->addToTotal(std::chrono::high_resolution_clock::now() - startTime); t = nullptr; } ScopedTimer::~ScopedTimer() { stop(); } -Timer::Timer(llvm::StringRef name) : name(std::string(name)), parent(nullptr) {} -Timer::Timer(llvm::StringRef name, Timer &parent) - : name(std::string(name)), parent(&parent) {} - -void Timer::start() { - if (parent && total.count() == 0) - parent->children.push_back(this); - startTime = std::chrono::high_resolution_clock::now(); -} - -void Timer::stop() { - total += (std::chrono::high_resolution_clock::now() - startTime); +Timer::Timer(llvm::StringRef name) : name(std::string(name)) {} +Timer::Timer(llvm::StringRef name, Timer &parent) : name(std::string(name)) { + parent.children.push_back(this); } Timer &Timer::root() { @@ -49,7 +42,8 @@ // We want to print the grand total under all the intermediate phases, so we // print all children first, then print the total under that. for (const auto &child : children) - child->print(1, totalDuration); + if (child->total > 0) + child->print(1, totalDuration); message(std::string(49, '-')); @@ -58,7 +52,7 @@ double Timer::millis() const { return std::chrono::duration_cast>( - total) + std::chrono::nanoseconds(total)) .count(); } @@ -74,6 +68,7 @@ if (recurse) { for (const auto &child : children) - child->print(depth + 1, totalDuration); + if (child->total > 0) + child->print(depth + 1, totalDuration); } } diff --git a/lld/include/lld/Common/Timer.h b/lld/include/lld/Common/Timer.h --- a/lld/include/lld/Common/Timer.h +++ b/lld/include/lld/Common/Timer.h @@ -12,6 +12,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringRef.h" #include +#include #include #include #include @@ -27,6 +28,8 @@ void stop(); + std::chrono::time_point startTime; + Timer *t = nullptr; }; @@ -36,8 +39,7 @@ static Timer &root(); - void start(); - void stop(); + void addToTotal(std::chrono::nanoseconds time) { total += time.count(); } void print(); double millis() const; @@ -46,11 +48,9 @@ explicit Timer(llvm::StringRef name); void print(int depth, double totalDuration, bool recurse = true) const; - std::chrono::time_point startTime; - std::chrono::nanoseconds total; + std::atomic total; std::vector children; std::string name; - Timer *parent; }; } // namespace lld