From 5e812e95804ae4d9aa1af46362203af18c1beaf7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolai=20H=C3=A4hnle?= Date: Fri, 26 Aug 2022 11:02:00 +0200 Subject: [PATCH] Revert "ManagedStatic: remove from DebugCounter" This reverts commit b5b6ef1500af29b6aba71330d8aaf82ecccb1f37. --- llvm/include/llvm/Support/DebugCounter.h | 4 ++- llvm/lib/Support/DebugCounter.cpp | 57 +++++++++++++++----------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h index 9fa4620a..cd9474a 100644 --- a/llvm/include/llvm/Support/DebugCounter.h +++ b/llvm/include/llvm/Support/DebugCounter.h @@ -55,6 +55,8 @@ class raw_ostream; class DebugCounter { public: + ~DebugCounter(); + /// Returns a reference to the singleton instance. static DebugCounter &instance(); @@ -147,6 +149,7 @@ public: // 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 @@ -156,7 +159,6 @@ public: #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 26293bf..bc2df37 100644 --- a/llvm/lib/Support/DebugCounter.cpp +++ b/llvm/lib/Support/DebugCounter.cpp @@ -4,6 +4,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Format.h" +#include "llvm/Support/ManagedStatic.h" using namespace llvm; @@ -43,42 +44,38 @@ private: } }; -// 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 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()); +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())); } }; +} // namespace + +static ManagedStatic + DebugCounterOption; +static bool PrintDebugCounter; + +void llvm::initDebugCounterOptions() { + *DebugCounterOption; + static cl::opt 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")); +} -} // anonymous namespace - -void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); } +static ManagedStatic DC; -DebugCounter &DebugCounter::instance() { - static DebugCounterOwner O; - return O.DC; +// Print information when destroyed, iff command line option is specified. +DebugCounter::~DebugCounter() { + if (isCountingEnabled() && PrintDebugCounter) + print(dbgs()); } +DebugCounter &DebugCounter::instance() { return *DC; } + // This is called by the command line parser when it sees a value for the // debug-counter option defined above. void DebugCounter::push_back(const std::string &Val) { -- 2.7.4