Skip to content

Commit 6d8abb0

Browse files
committed
Merging r338762:
------------------------------------------------------------------------ r338762 | gbiv | 2018-08-02 21:50:27 +0200 (Thu, 02 Aug 2018) | 15 lines [Support] Add an enable bit to our DebugCounters r337748 made us start incrementing DebugCounters all of the time. This makes tsan unhappy in multithreaded environments. Since it doesn't make much sense to use DebugCounters with multiple threads, this patch makes us only count anything if the user passed a -debug-counter option or if some other piece of code explicitly asks for it (e.g. the pass in D50031). The amount of global state here makes writing a unittest for this behavior somewhat awkward. So, no test is provided. Differential Revision: https://reviews.llvm.org/D50150 ------------------------------------------------------------------------ llvm-svn: 338846
1 parent 9176458 commit 6d8abb0

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

llvm/include/llvm/Support/DebugCounter.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ class DebugCounter {
7070
return instance().addCounter(Name, Desc);
7171
}
7272
inline static bool shouldExecute(unsigned CounterName) {
73-
// Compile to nothing when debugging is off
74-
#ifdef NDEBUG
75-
return true;
76-
#else
73+
if (!isCountingEnabled())
74+
return true;
75+
7776
auto &Us = instance();
7877
auto Result = Us.Counters.find(CounterName);
7978
if (Result != Us.Counters.end()) {
@@ -93,7 +92,6 @@ class DebugCounter {
9392
}
9493
// Didn't find the counter, should we warn?
9594
return true;
96-
#endif // NDEBUG
9795
}
9896

9997
// Return true if a given counter had values set (either programatically or on
@@ -142,7 +140,23 @@ class DebugCounter {
142140
}
143141
CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
144142

143+
// Force-enables counting all DebugCounters.
144+
//
145+
// Since DebugCounters are incompatible with threading (not only do they not
146+
// make sense, but we'll also see data races), this should only be used in
147+
// contexts where we're certain we won't spawn threads.
148+
static void enableAllCounters() { instance().Enabled = true; }
149+
145150
private:
151+
static bool isCountingEnabled() {
152+
// Compile to nothing when debugging is off
153+
#ifdef NDEBUG
154+
return false;
155+
#else
156+
return instance().Enabled;
157+
#endif
158+
}
159+
146160
unsigned addCounter(const std::string &Name, const std::string &Desc) {
147161
unsigned Result = RegisteredCounters.insert(Name);
148162
Counters[Result] = {};
@@ -159,6 +173,10 @@ class DebugCounter {
159173
};
160174
DenseMap<unsigned, CounterInfo> Counters;
161175
CounterVector RegisteredCounters;
176+
177+
// Whether we should do DebugCounting at all. DebugCounters aren't
178+
// thread-safe, so this should always be false in multithreaded scenarios.
179+
bool Enabled = false;
162180
};
163181

164182
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \

llvm/lib/Support/DebugCounter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ void DebugCounter::push_back(const std::string &Val) {
8282
<< " is not a registered counter\n";
8383
return;
8484
}
85+
enableAllCounters();
8586
Counters[CounterID].Skip = CounterVal;
8687
Counters[CounterID].IsSet = true;
8788
} else if (CounterPair.first.endswith("-count")) {
@@ -92,6 +93,7 @@ void DebugCounter::push_back(const std::string &Val) {
9293
<< " is not a registered counter\n";
9394
return;
9495
}
96+
enableAllCounters();
9597
Counters[CounterID].StopAfter = CounterVal;
9698
Counters[CounterID].IsSet = true;
9799
} else {

0 commit comments

Comments
 (0)