aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/TimeProfiler.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/TimeProfiler.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/TimeProfiler.cpp')
-rw-r--r--llvm/lib/Support/TimeProfiler.cpp18
1 files changed, 9 insertions, 9 deletions
diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp
index c37c74c..8f2544e 100644
--- a/llvm/lib/Support/TimeProfiler.cpp
+++ b/llvm/lib/Support/TimeProfiler.cpp
@@ -30,7 +30,7 @@ using namespace llvm;
static std::mutex Mu;
// List of all instances
-static std::vector<TimeTraceProfiler *>
+static ManagedStatic<std::vector<TimeTraceProfiler *>>
ThreadTimeTraceProfilerInstances; // GUARDED_BY(Mu)
// Per Thread instance
static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
@@ -128,7 +128,7 @@ struct llvm::TimeTraceProfiler {
std::lock_guard<std::mutex> Lock(Mu);
assert(Stack.empty() &&
"All profiler sections should be ended when calling write");
- assert(llvm::all_of(ThreadTimeTraceProfilerInstances,
+ assert(llvm::all_of(*ThreadTimeTraceProfilerInstances,
[](const auto &TTP) { return TTP->Stack.empty(); }) &&
"All profiler sections should be ended when calling write");
@@ -156,7 +156,7 @@ struct llvm::TimeTraceProfiler {
};
for (const Entry &E : Entries)
writeEvent(E, this->Tid);
- for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
+ for (const TimeTraceProfiler *TTP : *ThreadTimeTraceProfilerInstances)
for (const Entry &E : TTP->Entries)
writeEvent(E, TTP->Tid);
@@ -164,7 +164,7 @@ struct llvm::TimeTraceProfiler {
// longest one.
// Find highest used thread id.
uint64_t MaxTid = this->Tid;
- for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
+ for (const TimeTraceProfiler *TTP : *ThreadTimeTraceProfilerInstances)
MaxTid = std::max(MaxTid, TTP->Tid);
// Combine all CountAndTotalPerName from threads into one.
@@ -178,7 +178,7 @@ struct llvm::TimeTraceProfiler {
};
for (const auto &Stat : CountAndTotalPerName)
combineStat(Stat);
- for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
+ for (const TimeTraceProfiler *TTP : *ThreadTimeTraceProfilerInstances)
for (const auto &Stat : TTP->CountAndTotalPerName)
combineStat(Stat);
@@ -229,7 +229,7 @@ struct llvm::TimeTraceProfiler {
writeMetadataEvent("process_name", Tid, ProcName);
writeMetadataEvent("thread_name", Tid, ThreadName);
- for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
+ for (const TimeTraceProfiler *TTP : *ThreadTimeTraceProfilerInstances)
writeMetadataEvent("thread_name", TTP->Tid, TTP->ThreadName);
J.arrayEnd();
@@ -273,16 +273,16 @@ void llvm::timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
void llvm::timeTraceProfilerCleanup() {
delete TimeTraceProfilerInstance;
std::lock_guard<std::mutex> Lock(Mu);
- for (auto TTP : ThreadTimeTraceProfilerInstances)
+ for (auto TTP : *ThreadTimeTraceProfilerInstances)
delete TTP;
- ThreadTimeTraceProfilerInstances.clear();
+ ThreadTimeTraceProfilerInstances->clear();
}
// Finish TimeTraceProfilerInstance on a worker thread.
// This doesn't remove the instance, just moves the pointer to global vector.
void llvm::timeTraceProfilerFinishThread() {
std::lock_guard<std::mutex> Lock(Mu);
- ThreadTimeTraceProfilerInstances.push_back(TimeTraceProfilerInstance);
+ ThreadTimeTraceProfilerInstances->push_back(TimeTraceProfilerInstance);
TimeTraceProfilerInstance = nullptr;
}