Index: llvm/include/llvm/Support/TimeProfiler.h =================================================================== --- llvm/include/llvm/Support/TimeProfiler.h +++ llvm/include/llvm/Support/TimeProfiler.h @@ -14,7 +14,7 @@ namespace llvm { struct TimeTraceProfiler; -extern LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance; +TimeTraceProfiler *getTimeTraceProfilerInstance(); /// Initialize the time trace profiler. /// This sets up the global \p TimeTraceProfilerInstance @@ -30,7 +30,7 @@ /// Is the time trace profiler enabled, i.e. initialized? inline bool timeTraceProfilerEnabled() { - return TimeTraceProfilerInstance != nullptr; + return getTimeTraceProfilerInstance() != nullptr; } /// Write profiling data to output file. @@ -62,19 +62,19 @@ TimeTraceScope &operator=(TimeTraceScope &&) = delete; TimeTraceScope(StringRef Name) { - if (TimeTraceProfilerInstance != nullptr) + if (getTimeTraceProfilerInstance() != nullptr) timeTraceProfilerBegin(Name, StringRef("")); } TimeTraceScope(StringRef Name, StringRef Detail) { - if (TimeTraceProfilerInstance != nullptr) + if (getTimeTraceProfilerInstance() != nullptr) timeTraceProfilerBegin(Name, Detail); } TimeTraceScope(StringRef Name, llvm::function_ref Detail) { - if (TimeTraceProfilerInstance != nullptr) + if (getTimeTraceProfilerInstance() != nullptr) timeTraceProfilerBegin(Name, Detail); } ~TimeTraceScope() { - if (TimeTraceProfilerInstance != nullptr) + if (getTimeTraceProfilerInstance() != nullptr) timeTraceProfilerEnd(); } }; Index: llvm/lib/Support/TimeProfiler.cpp =================================================================== --- llvm/lib/Support/TimeProfiler.cpp +++ llvm/lib/Support/TimeProfiler.cpp @@ -24,16 +24,22 @@ #include using namespace std::chrono; +using namespace llvm; namespace { std::mutex Mu; -std::vector +// List of all instances +std::vector ThreadTimeTraceProfilerInstances; // guarded by Mu +// Per Thread instance +LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; } // namespace namespace llvm { -LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr; +TimeTraceProfiler *getTimeTraceProfilerInstance() { + return TimeTraceProfilerInstance; +} typedef duration DurationType; typedef time_point TimePointType;