https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/122429
>From 320ab02a1a3602ac1191d507c67141f330da718b Mon Sep 17 00:00:00 2001 From: Fangrui Song <i...@maskray.me> Date: Thu, 9 Jan 2025 23:10:58 -0800 Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?= =?UTF-8?q?itial=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5-bogner --- llvm/lib/Support/Timer.cpp | 115 +++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 63 deletions(-) diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index 3f0926ae0f3cbe..9cf6cf058c1086 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -39,7 +39,7 @@ using namespace llvm; //===----------------------------------------------------------------------===// -// Forward declarations for Managed Timer Globals (mtg) getters. +// Forward declarations for Managed Timer Globals getters. // // Globals have been placed at the end of the file to restrict direct // access. Use of getters also has the benefit of making it a bit more explicit @@ -49,29 +49,21 @@ namespace { class Name2PairMap; } -namespace mtg { -static std::string &LibSupportInfoOutputFilename(); -static const std::string &InfoOutputFilename(); -static bool TrackSpace(); -static bool SortTimers(); -static SignpostEmitter &Signposts(); -static sys::SmartMutex<true> &TimerLock(); -static TimerGroup &DefaultTimerGroup(); +static std::string &libSupportInfoOutputFilename(); +static bool trackSpace(); +static bool sortTimers(); +static SignpostEmitter &signposts(); +static sys::SmartMutex<true> &timerLock(); +static TimerGroup &defaultTimerGroup(); static TimerGroup *claimDefaultTimerGroup(); -static Name2PairMap &NamedGroupedTimers(); -} // namespace mtg +static Name2PairMap &namedGroupedTimers(); //===----------------------------------------------------------------------===// // //===----------------------------------------------------------------------===// -void llvm::initTimerOptions() { - mtg::TrackSpace(); - mtg::InfoOutputFilename(); - mtg::SortTimers(); -} std::unique_ptr<raw_ostream> llvm::CreateInfoOutputFile() { - const std::string &OutputFilename = mtg::LibSupportInfoOutputFilename(); + const std::string &OutputFilename = libSupportInfoOutputFilename(); if (OutputFilename.empty()) return std::make_unique<raw_fd_ostream>(2, false); // stderr. if (OutputFilename == "-") @@ -97,7 +89,7 @@ std::unique_ptr<raw_ostream> llvm::CreateInfoOutputFile() { //===----------------------------------------------------------------------===// void Timer::init(StringRef TimerName, StringRef TimerDescription) { - init(TimerName, TimerDescription, mtg::DefaultTimerGroup()); + init(TimerName, TimerDescription, defaultTimerGroup()); } void Timer::init(StringRef TimerName, StringRef TimerDescription, @@ -116,7 +108,7 @@ Timer::~Timer() { } static inline size_t getMemUsage() { - if (!mtg::TrackSpace()) + if (!trackSpace()) return 0; return sys::Process::GetMallocUsage(); } @@ -157,7 +149,7 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) { void Timer::startTimer() { assert(!Running && "Cannot start a running timer"); Running = Triggered = true; - mtg::Signposts().startInterval(this, getName()); + signposts().startInterval(this, getName()); StartTime = TimeRecord::getCurrentTime(true); } @@ -166,7 +158,7 @@ void Timer::stopTimer() { Running = false; Time += TimeRecord::getCurrentTime(false); Time -= StartTime; - mtg::Signposts().endInterval(this, getName()); + signposts().endInterval(this, getName()); } void Timer::clear() { @@ -218,7 +210,7 @@ class Name2PairMap { Timer &get(StringRef Name, StringRef Description, StringRef GroupName, StringRef GroupDescription) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName]; @@ -237,17 +229,17 @@ class Name2PairMap { NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description, StringRef GroupName, StringRef GroupDescription, bool Enabled) - : TimeRegion(!Enabled ? nullptr - : &mtg::NamedGroupedTimers().get(Name, Description, - GroupName, - GroupDescription)) {} + : TimeRegion(!Enabled + ? nullptr + : &namedGroupedTimers().get(Name, Description, GroupName, + GroupDescription)) {} //===----------------------------------------------------------------------===// // TimerGroup Implementation //===----------------------------------------------------------------------===// /// This is the global list of TimerGroups, maintained by the TimerGroup -/// ctor/dtor and is protected by the TimerLock lock. +/// ctor/dtor and is protected by the timerLock lock. static TimerGroup *TimerGroupList = nullptr; TimerGroup::TimerGroup(StringRef Name, StringRef Description, @@ -264,7 +256,7 @@ TimerGroup::TimerGroup(StringRef Name, StringRef Description, } TimerGroup::TimerGroup(StringRef Name, StringRef Description) - : TimerGroup(Name, Description, mtg::TimerLock()) {} + : TimerGroup(Name, Description, timerLock()) {} TimerGroup::TimerGroup(StringRef Name, StringRef Description, const StringMap<TimeRecord> &Records) @@ -283,7 +275,7 @@ TimerGroup::~TimerGroup() { removeTimer(*FirstTimer); // Remove the group from the TimerGroupList. - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); *Prev = Next; if (Next) Next->Prev = Prev; @@ -291,7 +283,7 @@ TimerGroup::~TimerGroup() { void TimerGroup::removeTimer(Timer &T) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); // If the timer was started, move its data to TimersToPrint. if (T.hasTriggered()) @@ -314,7 +306,7 @@ void TimerGroup::removeTimer(Timer &T) { } void TimerGroup::addTimer(Timer &T) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); // Add the timer to our list. if (FirstTimer) @@ -326,7 +318,7 @@ void TimerGroup::addTimer(Timer &T) { void TimerGroup::PrintQueuedTimers(raw_ostream &OS) { // Perhaps sort the timers in descending order by amount of time taken. - if (mtg::SortTimers()) + if (sortTimers()) llvm::sort(TimersToPrint); TimeRecord Total; @@ -344,7 +336,7 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) { // If this is not an collection of ungrouped times, print the total time. // Ungrouped timers don't really make sense to add up. We still print the // TOTAL line to make the percentages make sense. - if (this != &mtg::DefaultTimerGroup()) + if (this != &defaultTimerGroup()) OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n", Total.getProcessTime(), Total.getWallTime()); OS << '\n'; @@ -396,7 +388,7 @@ void TimerGroup::prepareToPrintList(bool ResetTime) { void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) { { // After preparing the timers we can free the lock - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); prepareToPrintList(ResetAfterPrint); } @@ -406,20 +398,20 @@ void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) { } void TimerGroup::clear() { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); for (Timer *T = FirstTimer; T; T = T->Next) T->clear(); } void TimerGroup::printAll(raw_ostream &OS) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next) TG->print(OS); } void TimerGroup::clearAll() { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next) TG->clear(); } @@ -436,7 +428,7 @@ void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R, } const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); prepareToPrintList(false); for (const PrintRecord &R : TimersToPrint) { @@ -463,19 +455,19 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) { } const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) { - sys::SmartScopedLock<true> L(mtg::TimerLock()); + sys::SmartScopedLock<true> L(timerLock()); for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next) delim = TG->printJSONValues(OS, delim); return delim; } void TimerGroup::constructForStatistics() { - mtg::LibSupportInfoOutputFilename(); - mtg::NamedGroupedTimers(); + libSupportInfoOutputFilename(); + namedGroupedTimers(); } std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup() { - return std::unique_ptr<TimerGroup>(mtg::claimDefaultTimerGroup()); + return std::unique_ptr<TimerGroup>(claimDefaultTimerGroup()); } //===----------------------------------------------------------------------===// @@ -505,7 +497,7 @@ class llvm::TimerGlobals { private: // Order of these members and initialization below is important. For example - // the DefaultTimerGroup uses the TimerLock. Most of these also depend on the + // the defaultTimerGroup uses the timerLock. Most of these also depend on the // options above. std::once_flag InitDeferredFlag; std::unique_ptr<SignpostEmitter> SignpostsPtr; @@ -524,15 +516,15 @@ class llvm::TimerGlobals { } public: - SignpostEmitter &Signposts() { return *initDeferred().SignpostsPtr; } - sys::SmartMutex<true> &TimerLock() { return *initDeferred().TimerLockPtr; } - TimerGroup &DefaultTimerGroup() { + SignpostEmitter &signposts() { return *initDeferred().SignpostsPtr; } + sys::SmartMutex<true> &timerLock() { return *initDeferred().TimerLockPtr; } + TimerGroup &defaultTimerGroup() { return *initDeferred().DefaultTimerGroupPtr; } TimerGroup *claimDefaultTimerGroup() { return initDeferred().DefaultTimerGroupPtr.release(); } - Name2PairMap &NamedGroupedTimers() { + Name2PairMap &namedGroupedTimers() { return *initDeferred().NamedGroupedTimersPtr; } @@ -556,26 +548,23 @@ class llvm::TimerGlobals { static ManagedStatic<TimerGlobals> ManagedTimerGlobals; -static std::string &mtg::LibSupportInfoOutputFilename() { +static std::string &libSupportInfoOutputFilename() { return ManagedTimerGlobals->LibSupportInfoOutputFilename; } -static const std::string &mtg::InfoOutputFilename() { - return ManagedTimerGlobals->InfoOutputFilename.getValue(); -} -static bool mtg::TrackSpace() { return ManagedTimerGlobals->TrackSpace; } -static bool mtg::SortTimers() { return ManagedTimerGlobals->SortTimers; } -static SignpostEmitter &mtg::Signposts() { - return ManagedTimerGlobals->Signposts(); +static bool trackSpace() { return ManagedTimerGlobals->TrackSpace; } +static bool sortTimers() { return ManagedTimerGlobals->SortTimers; } +static SignpostEmitter &signposts() { return ManagedTimerGlobals->signposts(); } +static sys::SmartMutex<true> &timerLock() { + return ManagedTimerGlobals->timerLock(); } -static sys::SmartMutex<true> &mtg::TimerLock() { - return ManagedTimerGlobals->TimerLock(); +static TimerGroup &defaultTimerGroup() { + return ManagedTimerGlobals->defaultTimerGroup(); } -static TimerGroup &mtg::DefaultTimerGroup() { - return ManagedTimerGlobals->DefaultTimerGroup(); -} -static TimerGroup *mtg::claimDefaultTimerGroup() { +static TimerGroup *claimDefaultTimerGroup() { return ManagedTimerGlobals->claimDefaultTimerGroup(); } -static Name2PairMap &mtg::NamedGroupedTimers() { - return ManagedTimerGlobals->NamedGroupedTimers(); +static Name2PairMap &namedGroupedTimers() { + return ManagedTimerGlobals->namedGroupedTimers(); } + +void llvm::initTimerOptions() { *ManagedTimerGlobals; } >From cc490506b363e66e03f30590db028cdbbda51b96 Mon Sep 17 00:00:00 2001 From: Fangrui Song <i...@maskray.me> Date: Thu, 9 Jan 2025 23:40:32 -0800 Subject: [PATCH 2/3] . Created using spr 1.3.5-bogner --- llvm/lib/Support/Timer.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index 9cf6cf058c1086..a47991135c2019 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -27,6 +27,7 @@ #include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" #include <limits> +#include <optional> #if HAVE_UNISTD_H #include <unistd.h> @@ -500,17 +501,17 @@ class llvm::TimerGlobals { // the defaultTimerGroup uses the timerLock. Most of these also depend on the // options above. std::once_flag InitDeferredFlag; - std::unique_ptr<SignpostEmitter> SignpostsPtr; - std::unique_ptr<sys::SmartMutex<true>> TimerLockPtr; + std::optional<SignpostEmitter> SignpostsPtr; + std::optional<sys::SmartMutex<true>> TimerLockPtr; std::unique_ptr<TimerGroup> DefaultTimerGroupPtr; - std::unique_ptr<Name2PairMap> NamedGroupedTimersPtr; + std::optional<Name2PairMap> NamedGroupedTimersPtr; TimerGlobals &initDeferred() { std::call_once(InitDeferredFlag, [this]() { - SignpostsPtr = std::make_unique<SignpostEmitter>(); - TimerLockPtr = std::make_unique<sys::SmartMutex<true>>(); + SignpostsPtr.emplace(); + TimerLockPtr.emplace(); DefaultTimerGroupPtr.reset(new TimerGroup( "misc", "Miscellaneous Ungrouped Timers", *TimerLockPtr)); - NamedGroupedTimersPtr = std::make_unique<Name2PairMap>(); + NamedGroupedTimersPtr.emplace(); }); return *this; } >From 366d63f7f4390a1f68a614b162f22014fed1a8ae Mon Sep 17 00:00:00 2001 From: Fangrui Song <i...@maskray.me> Date: Fri, 10 Jan 2025 08:52:13 -0800 Subject: [PATCH 3/3] repair #121663 Created using spr 1.3.5-bogner --- clang/tools/driver/driver.cpp | 2 +- llvm/include/llvm/Support/Timer.h | 6 ++--- llvm/lib/Support/Timer.cpp | 37 +++++++++---------------------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp index ffd157e60997cd..74923247b7ee16 100644 --- a/clang/tools/driver/driver.cpp +++ b/clang/tools/driver/driver.cpp @@ -439,7 +439,7 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) { if (!UseNewCC1Process && IsCrash) { // When crashing in -fintegrated-cc1 mode, bury the timer pointers, because // the internal linked list might point to already released stack frames. - llvm::BuryPointer(llvm::TimerGroup::aquireDefaultGroup()); + llvm::BuryPointer(llvm::TimerGroup::acquireTimerGlobals()); } else { // If any timers were active but haven't been destroyed yet, print their // results now. This happens in -disable-free mode. diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h index c05389332b8045..d21859905d4a7b 100644 --- a/llvm/include/llvm/Support/Timer.h +++ b/llvm/include/llvm/Support/Timer.h @@ -240,9 +240,9 @@ class TimerGroup { /// global constructors and destructors. static void constructForStatistics(); - /// This makes the default group unmanaged, and lets the user manage the - /// group's lifetime. - static std::unique_ptr<TimerGroup> aquireDefaultGroup(); + /// This makes the timer globals unmanaged, and lets the user manage the + /// lifetime. + static void *acquireTimerGlobals(); private: friend class Timer; diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index a47991135c2019..8f0aedab0b015b 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -56,7 +56,6 @@ static bool sortTimers(); static SignpostEmitter &signposts(); static sys::SmartMutex<true> &timerLock(); static TimerGroup &defaultTimerGroup(); -static TimerGroup *claimDefaultTimerGroup(); static Name2PairMap &namedGroupedTimers(); //===----------------------------------------------------------------------===// @@ -467,10 +466,6 @@ void TimerGroup::constructForStatistics() { namedGroupedTimers(); } -std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup() { - return std::unique_ptr<TimerGroup>(claimDefaultTimerGroup()); -} - //===----------------------------------------------------------------------===// // Timer Globals // @@ -495,36 +490,27 @@ class llvm::TimerGlobals { cl::opt<std::string, true> InfoOutputFilename; cl::opt<bool> TrackSpace; cl::opt<bool> SortTimers; + sys::SmartMutex<true> TimerLock; + TimerGroup DefaultTimerGroup{"misc", "Miscellaneous Ungrouped Timers", + TimerLock}; + SignpostEmitter Signposts; private: // Order of these members and initialization below is important. For example // the defaultTimerGroup uses the timerLock. Most of these also depend on the // options above. std::once_flag InitDeferredFlag; - std::optional<SignpostEmitter> SignpostsPtr; - std::optional<sys::SmartMutex<true>> TimerLockPtr; - std::unique_ptr<TimerGroup> DefaultTimerGroupPtr; std::optional<Name2PairMap> NamedGroupedTimersPtr; TimerGlobals &initDeferred() { - std::call_once(InitDeferredFlag, [this]() { - SignpostsPtr.emplace(); - TimerLockPtr.emplace(); - DefaultTimerGroupPtr.reset(new TimerGroup( - "misc", "Miscellaneous Ungrouped Timers", *TimerLockPtr)); - NamedGroupedTimersPtr.emplace(); - }); + std::call_once(InitDeferredFlag, + [this]() { NamedGroupedTimersPtr.emplace(); }); return *this; } public: - SignpostEmitter &signposts() { return *initDeferred().SignpostsPtr; } - sys::SmartMutex<true> &timerLock() { return *initDeferred().TimerLockPtr; } - TimerGroup &defaultTimerGroup() { - return *initDeferred().DefaultTimerGroupPtr; - } - TimerGroup *claimDefaultTimerGroup() { - return initDeferred().DefaultTimerGroupPtr.release(); - } + SignpostEmitter &signposts() { return Signposts; } + sys::SmartMutex<true> &timerLock() { return TimerLock; } + TimerGroup &defaultTimerGroup() { return DefaultTimerGroup; } Name2PairMap &namedGroupedTimers() { return *initDeferred().NamedGroupedTimersPtr; } @@ -561,11 +547,10 @@ static sys::SmartMutex<true> &timerLock() { static TimerGroup &defaultTimerGroup() { return ManagedTimerGlobals->defaultTimerGroup(); } -static TimerGroup *claimDefaultTimerGroup() { - return ManagedTimerGlobals->claimDefaultTimerGroup(); -} static Name2PairMap &namedGroupedTimers() { return ManagedTimerGlobals->namedGroupedTimers(); } void llvm::initTimerOptions() { *ManagedTimerGlobals; } + +void *TimerGroup::acquireTimerGlobals() { return ManagedTimerGlobals.claim(); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits