[llvm] [llvm][Timer] Don't print timers in TimerGroup when all Timers are removed (PR #131026)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 12 13:46:05 PDT 2025


https://github.com/aeubanks created https://github.com/llvm/llvm-project/pull/131026

Only print them on TimerGroup destruction (or eagerly when TimerGroup::printAll() is called).

We should be able to destroy all Timers in a TimerGroup while delaying printing the stored TimeRecords.

>From d661c4c3e8a2583ba29348e25f74069e8d2ad9ac Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 12 Mar 2025 20:41:47 +0000
Subject: [PATCH] [llvm][Timer] Don't print timers in TimerGroup when all
 Timers are removed

Only print them on TimerGroup destruction (or eagerly when TimerGroup::printAll() is called).

We should be able to destroy all Timers in a TimerGroup while delaying printing the stored TimeRecords.
---
 llvm/lib/Support/Timer.cpp           | 13 +++++--------
 llvm/unittests/Support/TimerTest.cpp | 18 +++++++++++++++++-
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 089dae2886f22..eca726828c697 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -284,6 +284,11 @@ TimerGroup::~TimerGroup() {
   while (FirstTimer)
     removeTimer(*FirstTimer);
 
+  if (!TimersToPrint.empty()) {
+    std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
+    PrintQueuedTimers(*OutStream);
+  }
+
   // Remove the group from the TimerGroupList.
   sys::SmartScopedLock<true> L(timerLock());
   *Prev = Next;
@@ -305,14 +310,6 @@ void TimerGroup::removeTimer(Timer &T) {
   *T.Prev = T.Next;
   if (T.Next)
     T.Next->Prev = T.Prev;
-
-  // Print the report when all timers in this group are destroyed if some of
-  // them were started.
-  if (FirstTimer || TimersToPrint.empty())
-    return;
-
-  std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
-  PrintQueuedTimers(*OutStream);
 }
 
 void TimerGroup::addTimer(Timer &T) {
diff --git a/llvm/unittests/Support/TimerTest.cpp b/llvm/unittests/Support/TimerTest.cpp
index 5686b394e16cd..612fd7231da70 100644
--- a/llvm/unittests/Support/TimerTest.cpp
+++ b/llvm/unittests/Support/TimerTest.cpp
@@ -66,4 +66,20 @@ TEST(Timer, CheckIfTriggered) {
   EXPECT_FALSE(T1.hasTriggered());
 }
 
-} // end anon namespace
+TEST(Timer, TimerGroupTimerDestructed) {
+  testing::internal::CaptureStderr();
+
+  {
+    TimerGroup TG("tg", "desc");
+    {
+      Timer T1("T1", "T1", TG);
+      T1.startTimer();
+      T1.stopTimer();
+    }
+    EXPECT_TRUE(testing::internal::GetCapturedStderr().empty());
+    testing::internal::CaptureStderr();
+  }
+  EXPECT_FALSE(testing::internal::GetCapturedStderr().empty());
+}
+
+} // namespace



More information about the llvm-commits mailing list