aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/Timer.cpp
diff options
context:
space:
mode:
authorMehdi Amini <joker.eph@gmail.com>2021-07-15 23:52:44 +0000
committerMehdi Amini <joker.eph@gmail.com>2021-07-16 03:33:20 +0000
commit42f588f39c5ce6f521e3709b8871d1fdd076292f (patch)
tree5ad9009fe09e82045e3a57cf39167e223ab00a27 /llvm/lib/Support/Timer.cpp
parentb4c93ece8e4f6e98a15daca10c8a3db33cf8c195 (diff)
downloadllvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.zip
llvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.tar.gz
llvm-42f588f39c5ce6f521e3709b8871d1fdd076292f.tar.bz2
Use ManagedStatic and lazy initialization of cl::opt in libSupport to make it free of global initializer
We can build it with -Werror=global-constructors now. This helps in situation where libSupport is embedded as a shared library, potential with dlopen/dlclose scenario, and when command-line parsing or other facilities may not be involved. Avoiding the implicit construction of these cl::opt can avoid double-registration issues and other kind of behavior. Reviewed By: lattner, jpienaar Differential Revision: https://reviews.llvm.org/D105959
Diffstat (limited to 'llvm/lib/Support/Timer.cpp')
-rw-r--r--llvm/lib/Support/Timer.cpp53
1 files changed, 39 insertions, 14 deletions
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 8d421db..f025ecd 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -11,6 +11,9 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/Timer.h"
+
+#include "DebugOptions.h"
+
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Config/config.h"
@@ -53,20 +56,41 @@ static ManagedStatic<sys::SmartMutex<true> > TimerLock;
static ManagedStatic<SignpostEmitter> Signposts;
namespace {
- static cl::opt<bool>
- TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
+struct CreateTrackSpace {
+ static void *call() {
+ return new cl::opt<bool>("track-memory",
+ cl::desc("Enable -time-passes memory "
"tracking (this may be slow)"),
- cl::Hidden);
-
- static cl::opt<std::string, true>
- InfoOutputFilename("info-output-file", cl::value_desc("filename"),
- cl::desc("File to append -stats and -timer output to"),
- cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
+ cl::Hidden);
+ }
+};
+static ManagedStatic<cl::opt<bool>, CreateTrackSpace> TrackSpace;
+struct CreateInfoOutputFilename {
+ static void *call() {
+ return new cl::opt<std::string, true>(
+ "info-output-file", cl::value_desc("filename"),
+ cl::desc("File to append -stats and -timer output to"), cl::Hidden,
+ cl::location(getLibSupportInfoOutputFilename()));
+ }
+};
+static ManagedStatic<cl::opt<std::string, true>, CreateInfoOutputFilename>
+ InfoOutputFilename;
+struct CreateSortTimers {
+ static void *call() {
+ return new cl::opt<bool>(
+ "sort-timers",
+ cl::desc("In the report, sort the timers in each group "
+ "in wall clock time order"),
+ cl::init(true), cl::Hidden);
+ }
+};
+ManagedStatic<cl::opt<bool>, CreateSortTimers> SortTimers;
+} // namespace
- static cl::opt<bool>
- SortTimers("sort-timers", cl::desc("In the report, sort the timers in each group "
- "in wall clock time order"),
- cl::init(true), cl::Hidden);
+void llvm::initTimerOptions() {
+ *TrackSpace;
+ *InfoOutputFilename;
+ *SortTimers;
}
std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
@@ -125,7 +149,8 @@ Timer::~Timer() {
}
static inline size_t getMemUsage() {
- if (!TrackSpace) return 0;
+ if (!*TrackSpace)
+ return 0;
return sys::Process::GetMallocUsage();
}
@@ -331,7 +356,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 (SortTimers)
+ if (*SortTimers)
llvm::sort(TimersToPrint);
TimeRecord Total;