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 07:38:16 +0000
commit76374573ce829b083b95b74937a11e9b91f8f45f (patch)
treed3d4858038abe40a2eb763e04e6c93f8902691af /llvm/lib/Support/Timer.cpp
parent8d051d854619956de633047409149cdab1e3319a (diff)
downloadllvm-76374573ce829b083b95b74937a11e9b91f8f45f.zip
llvm-76374573ce829b083b95b74937a11e9b91f8f45f.tar.gz
llvm-76374573ce829b083b95b74937a11e9b91f8f45f.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;