[llvm] [Support] Introduce a function to reset all debug counters (PR #194864)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 06:54:30 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Benjamin Stott (BStott6)
<details>
<summary>Changes</summary>
This PR adds a function to reset all debug counters, and extends the unit test to verify that the debug counters are reset as expected. This is required for running tools repeatedly in the same process.
---
Full diff: https://github.com/llvm/llvm-project/pull/194864.diff
2 Files Affected:
- (modified) llvm/include/llvm/Support/DebugCounter.h (+13)
- (modified) llvm/unittests/Support/DebugCounterTest.cpp (+13)
``````````diff
diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h
index 7e83fc8ff8767..61dcdb23e8da7 100644
--- a/llvm/include/llvm/Support/DebugCounter.h
+++ b/llvm/include/llvm/Support/DebugCounter.h
@@ -78,6 +78,14 @@ class DebugCounter {
CounterInfo(StringRef Name, StringRef Desc) : Name(Name), Desc(Desc) {
DebugCounter::registerCounter(this);
}
+
+ void reset() {
+ Active = false;
+ IsSet = false;
+ Count = 0;
+ CurrChunkIdx = 0;
+ Chunks.clear();
+ }
};
LLVM_ABI static void
@@ -165,6 +173,11 @@ class DebugCounter {
Counter->Active = true;
}
+ void resetAllCounters() {
+ for (auto &[_, Counter] : Counters)
+ Counter->reset();
+ }
+
protected:
void addCounter(CounterInfo *Info) { Counters[Info->Name] = Info; }
bool handleCounterIncrement(CounterInfo &Info);
diff --git a/llvm/unittests/Support/DebugCounterTest.cpp b/llvm/unittests/Support/DebugCounterTest.cpp
index 820b1ce5ca0d3..da10e71e49c04 100644
--- a/llvm/unittests/Support/DebugCounterTest.cpp
+++ b/llvm/unittests/Support/DebugCounterTest.cpp
@@ -35,6 +35,19 @@ TEST(DebugCounterTest, Basic) {
llvm::raw_string_ostream OS(Str);
DC->print(OS);
EXPECT_TRUE(StringRef(Str).contains("{200,1:3-5:78:79:89:100-102:150}"));
+
+ DC->resetAllCounters();
+
+ // After resetting, counter is no longer set.
+ EXPECT_FALSE(DebugCounter::isCounterSet(TestCounter));
+
+ DC->push_back("test-counter=1");
+ EXPECT_TRUE(DebugCounter::isCounterSet(TestCounter));
+ EXPECT_EQ(DC->getCounterState(TestCounter).Count, 0);
+
+ DC->shouldExecute(TestCounter);
+
+ EXPECT_EQ(DC->getCounterState(TestCounter).Count, 1);
}
#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/194864
More information about the llvm-commits
mailing list