[llvm-branch-commits] [llvm-branch] r338846 - Merging r338762:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Aug 3 03:23:43 PDT 2018


Author: hans
Date: Fri Aug  3 03:23:43 2018
New Revision: 338846

URL: http://llvm.org/viewvc/llvm-project?rev=338846&view=rev
Log:
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

------------------------------------------------------------------------

Modified:
    llvm/branches/release_70/   (props changed)
    llvm/branches/release_70/include/llvm/Support/DebugCounter.h
    llvm/branches/release_70/lib/Support/DebugCounter.cpp

Propchange: llvm/branches/release_70/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Aug  3 03:23:43 2018
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,338552,338554,338658,338682,338703,338709,338751
+/llvm/trunk:155241,338552,338554,338658,338682,338703,338709,338751,338762

Modified: llvm/branches/release_70/include/llvm/Support/DebugCounter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_70/include/llvm/Support/DebugCounter.h?rev=338846&r1=338845&r2=338846&view=diff
==============================================================================
--- llvm/branches/release_70/include/llvm/Support/DebugCounter.h (original)
+++ llvm/branches/release_70/include/llvm/Support/DebugCounter.h Fri Aug  3 03:23:43 2018
@@ -70,10 +70,9 @@ public:
     return instance().addCounter(Name, Desc);
   }
   inline static bool shouldExecute(unsigned CounterName) {
-// Compile to nothing when debugging is off
-#ifdef NDEBUG
-    return true;
-#else
+    if (!isCountingEnabled())
+      return true;
+
     auto &Us = instance();
     auto Result = Us.Counters.find(CounterName);
     if (Result != Us.Counters.end()) {
@@ -93,7 +92,6 @@ public:
     }
     // Didn't find the counter, should we warn?
     return true;
-#endif // NDEBUG
   }
 
   // Return true if a given counter had values set (either programatically or on
@@ -142,7 +140,23 @@ public:
   }
   CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
 
+  // Force-enables counting all DebugCounters.
+  //
+  // Since DebugCounters are incompatible with threading (not only do they not
+  // make sense, but we'll also see data races), this should only be used in
+  // contexts where we're certain we won't spawn threads.
+  static void enableAllCounters() { instance().Enabled = true; }
+
 private:
+  static bool isCountingEnabled() {
+// Compile to nothing when debugging is off
+#ifdef NDEBUG
+    return false;
+#else
+    return instance().Enabled;
+#endif
+  }
+
   unsigned addCounter(const std::string &Name, const std::string &Desc) {
     unsigned Result = RegisteredCounters.insert(Name);
     Counters[Result] = {};
@@ -159,6 +173,10 @@ private:
   };
   DenseMap<unsigned, CounterInfo> Counters;
   CounterVector RegisteredCounters;
+
+  // Whether we should do DebugCounting at all. DebugCounters aren't
+  // thread-safe, so this should always be false in multithreaded scenarios.
+  bool Enabled = false;
 };
 
 #define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)                              \

Modified: llvm/branches/release_70/lib/Support/DebugCounter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_70/lib/Support/DebugCounter.cpp?rev=338846&r1=338845&r2=338846&view=diff
==============================================================================
--- llvm/branches/release_70/lib/Support/DebugCounter.cpp (original)
+++ llvm/branches/release_70/lib/Support/DebugCounter.cpp Fri Aug  3 03:23:43 2018
@@ -82,6 +82,7 @@ void DebugCounter::push_back(const std::
              << " is not a registered counter\n";
       return;
     }
+    enableAllCounters();
     Counters[CounterID].Skip = CounterVal;
     Counters[CounterID].IsSet = true;
   } else if (CounterPair.first.endswith("-count")) {
@@ -92,6 +93,7 @@ void DebugCounter::push_back(const std::
              << " is not a registered counter\n";
       return;
     }
+    enableAllCounters();
     Counters[CounterID].StopAfter = CounterVal;
     Counters[CounterID].IsSet = true;
   } else {




More information about the llvm-branch-commits mailing list