Skip to content

Commit

Permalink
[Support] Wrap extern TLS variable in getter function
Browse files Browse the repository at this point in the history
This patch wraps an external thread local storage variable inside of a
getter function and makes it have internal linkage. This allows LLVM to
be built with BUILD_SHARED_LIBS on windows with MinGW. Additionally it
allows Clang versions prior to 10 to compile current trunk for MinGW.

Differential Revision: https://reviews.llvm.org/D73639
  • Loading branch information
zero9178 authored and mstorsjo committed Jan 31, 2020
1 parent bf8357d commit 3f6a2f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions llvm/include/llvm/Support/TimeProfiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +30,7 @@ void timeTraceProfilerFinishThread();

/// Is the time trace profiler enabled, i.e. initialized?
inline bool timeTraceProfilerEnabled() {
return TimeTraceProfilerInstance != nullptr;
return getTimeTraceProfilerInstance() != nullptr;
}

/// Write profiling data to output file.
Expand Down Expand Up @@ -62,19 +62,19 @@ struct TimeTraceScope {
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<std::string()> Detail) {
if (TimeTraceProfilerInstance != nullptr)
if (getTimeTraceProfilerInstance() != nullptr)
timeTraceProfilerBegin(Name, Detail);
}
~TimeTraceScope() {
if (TimeTraceProfilerInstance != nullptr)
if (getTimeTraceProfilerInstance() != nullptr)
timeTraceProfilerEnd();
}
};
Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/Support/TimeProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@
#include <vector>

using namespace std::chrono;
using namespace llvm;

namespace {
std::mutex Mu;
std::vector<llvm::TimeTraceProfiler *>
// List of all instances
std::vector<TimeTraceProfiler *>
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<steady_clock::rep, steady_clock::period> DurationType;
typedef time_point<steady_clock> TimePointType;
Expand Down

0 comments on commit 3f6a2f1

Please sign in to comment.