[llvm] 0629cce - ManagedStatic: remove from DebugCounter

Nicolai Hähnle via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 30 12:47:21 PDT 2022


Author: Nicolai Hähnle
Date: 2022-10-30T20:47:12+01:00
New Revision: 0629cce2939017a66101f94976a70743cfe0eb6c

URL: https://github.com/llvm/llvm-project/commit/0629cce2939017a66101f94976a70743cfe0eb6c
DIFF: https://github.com/llvm/llvm-project/commit/0629cce2939017a66101f94976a70743cfe0eb6c.diff

LOG: ManagedStatic: remove from DebugCounter

[Re-submit after earlier revert due to a test failure. Commit dce78646f07
("clang-tblgen build: avoid duplicate inclusion of libLLVMSupport")
is believe to address the root cause of the test failure.]

Follow the pattern used in MLIR for the cl::opt instances.

v2:
- make DebugCounter::isCountingEnabled public so that the
  DebugCounterOwner doesn't have to be a nested class. This simplifies
  later changes

v3:
- remove the indirection via DebugCounterOwner::instance()

Differential Revision: https://reviews.llvm.org/D129116

Added: 
    

Modified: 
    llvm/include/llvm/Support/DebugCounter.h
    llvm/lib/Support/DebugCounter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h
index cd9474a4d9184..9fa4620ade3c8 100644
--- a/llvm/include/llvm/Support/DebugCounter.h
+++ b/llvm/include/llvm/Support/DebugCounter.h
@@ -55,8 +55,6 @@ class raw_ostream;
 
 class DebugCounter {
 public:
-  ~DebugCounter();
-
   /// Returns a reference to the singleton instance.
   static DebugCounter &instance();
 
@@ -149,7 +147,6 @@ class DebugCounter {
   // 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
@@ -159,6 +156,7 @@ class DebugCounter {
 #endif
   }
 
+private:
   unsigned addCounter(const std::string &Name, const std::string &Desc) {
     unsigned Result = RegisteredCounters.insert(Name);
     Counters[Result] = {};

diff  --git a/llvm/lib/Support/DebugCounter.cpp b/llvm/lib/Support/DebugCounter.cpp
index bc2df37e773d4..26293bf92a42e 100644
--- a/llvm/lib/Support/DebugCounter.cpp
+++ b/llvm/lib/Support/DebugCounter.cpp
@@ -4,7 +4,6 @@
 
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Format.h"
-#include "llvm/Support/ManagedStatic.h"
 
 using namespace llvm;
 
@@ -44,37 +43,41 @@ class DebugCounterList : public cl::list<std::string, DebugCounter> {
   }
 };
 
-struct CreateDebugCounterOption {
-  static void *call() {
-    return new DebugCounterList(
-        "debug-counter", cl::Hidden,
-        cl::desc("Comma separated list of debug counter skip and count"),
-        cl::CommaSeparated, cl::location(DebugCounter::instance()));
+// All global objects associated to the DebugCounter, including the DebugCounter
+// itself, are owned by a single global instance of the DebugCounterOwner
+// struct. This makes it easier to control the order in which constructors and
+// destructors are run.
+struct DebugCounterOwner {
+  DebugCounter DC;
+  DebugCounterList DebugCounterOption{
+      "debug-counter", cl::Hidden,
+      cl::desc("Comma separated list of debug counter skip and count"),
+      cl::CommaSeparated, cl::location(DC)};
+  cl::opt<bool> PrintDebugCounter{
+      "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional,
+      cl::desc("Print out debug counter info after all counters accumulated")};
+
+  DebugCounterOwner() {
+    // Our destructor uses the debug stream. By referencing it here, we
+    // ensure that its destructor runs after our destructor.
+    (void)dbgs();
+  }
+
+  // Print information when destroyed, iff command line option is specified.
+  ~DebugCounterOwner() {
+    if (DC.isCountingEnabled() && PrintDebugCounter)
+      DC.print(dbgs());
   }
 };
-} // namespace
-
-static ManagedStatic<DebugCounterList, CreateDebugCounterOption>
-    DebugCounterOption;
-static bool PrintDebugCounter;
-
-void llvm::initDebugCounterOptions() {
-  *DebugCounterOption;
-  static cl::opt<bool, true> RegisterPrintDebugCounter(
-      "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter),
-      cl::init(false), cl::Optional,
-      cl::desc("Print out debug counter info after all counters accumulated"));
-}
 
-static ManagedStatic<DebugCounter> DC;
+} // anonymous namespace
 
-// Print information when destroyed, iff command line option is specified.
-DebugCounter::~DebugCounter() {
-  if (isCountingEnabled() && PrintDebugCounter)
-    print(dbgs());
-}
+void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
 
-DebugCounter &DebugCounter::instance() { return *DC; }
+DebugCounter &DebugCounter::instance() {
+  static DebugCounterOwner O;
+  return O.DC;
+}
 
 // This is called by the command line parser when it sees a value for the
 // debug-counter option defined above.


        


More information about the llvm-commits mailing list