Skip to content

Revert "Use global TimerGroups for both new pass manager and old pass manager timers" #131173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

aeubanks
Copy link
Contributor

@llvmbot llvmbot added clang Clang issues not falling into any other category llvm:support llvm:ir labels Mar 13, 2025
@aeubanks aeubanks merged commit 31ebe66 into main Mar 13, 2025
9 of 13 checks passed
@aeubanks aeubanks deleted the revert-130375-feature/global-timers branch March 13, 2025 17:29
@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/pr-subscribers-llvm-ir

Author: Arthur Eubanks (aeubanks)

Changes

Reverts llvm/llvm-project#130375

Causes breakages, e.g. https://lab.llvm.org/buildbot/#/builders/160/builds/14607


Full diff: https://github.com/llvm/llvm-project/pull/131173.diff

6 Files Affected:

  • (modified) clang/test/Misc/time-passes.c (+1)
  • (modified) llvm/include/llvm/IR/PassTimingInfo.h (+7-11)
  • (modified) llvm/include/llvm/Support/Timer.h (-5)
  • (modified) llvm/lib/IR/PassTimingInfo.cpp (+21-9)
  • (modified) llvm/lib/Support/Timer.cpp (+5-21)
  • (modified) llvm/unittests/IR/TimePassesTest.cpp (+2-2)
diff --git a/clang/test/Misc/time-passes.c b/clang/test/Misc/time-passes.c
index c1669826b2268..395da216aad42 100644
--- a/clang/test/Misc/time-passes.c
+++ b/clang/test/Misc/time-passes.c
@@ -19,5 +19,6 @@
 // NPM:       InstCombinePass{{$}}
 // NPM-NOT:   InstCombinePass #
 // TIME: Total{{$}}
+// NPM: Pass execution timing report
 
 int foo(int x, int y) { return x + y; }
diff --git a/llvm/include/llvm/IR/PassTimingInfo.h b/llvm/include/llvm/IR/PassTimingInfo.h
index b47ba7f16ef37..1148399943186 100644
--- a/llvm/include/llvm/IR/PassTimingInfo.h
+++ b/llvm/include/llvm/IR/PassTimingInfo.h
@@ -39,7 +39,8 @@ Timer *getPassTimer(Pass *);
 /// This class implements -time-passes functionality for new pass manager.
 /// It provides the pass-instrumentation callbacks that measure the pass
 /// execution time. They collect timing info into individual timers as
-/// passes are being run.
+/// passes are being run. At the end of its life-time it prints the resulting
+/// timing report.
 class TimePassesHandler {
   /// Value of this type is capable of uniquely identifying pass invocations.
   /// It is a pair of string Pass-Identifier (which for now is common
@@ -47,10 +48,8 @@ class TimePassesHandler {
   using PassInvocationID = std::pair<StringRef, unsigned>;
 
   /// Groups of timers for passes and analyses.
-  TimerGroup &PassTG =
-      NamedRegionTimer::getNamedTimerGroup(PassGroupName, PassGroupDesc);
-  TimerGroup &AnalysisTG = NamedRegionTimer::getNamedTimerGroup(
-      AnalysisGroupName, AnalysisGroupDesc);
+  TimerGroup PassTG;
+  TimerGroup AnalysisTG;
 
   using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
   /// Map of timers for pass invocations
@@ -72,15 +71,12 @@ class TimePassesHandler {
   bool PerRun;
 
 public:
-  static constexpr StringRef PassGroupName = "pass";
-  static constexpr StringRef AnalysisGroupName = "analysis";
-  static constexpr StringRef PassGroupDesc = "Pass execution timing report";
-  static constexpr StringRef AnalysisGroupDesc =
-      "Analysis execution timing report";
-
   TimePassesHandler();
   TimePassesHandler(bool Enabled, bool PerRun = false);
 
+  /// Destructor handles the print action if it has not been handled before.
+  ~TimePassesHandler() { print(); }
+
   /// Prints out timing information and then resets the timers.
   void print();
 
diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h
index 5a5082b6718ed..abe30451dd2f2 100644
--- a/llvm/include/llvm/Support/Timer.h
+++ b/llvm/include/llvm/Support/Timer.h
@@ -169,11 +169,6 @@ struct NamedRegionTimer : public TimeRegion {
   explicit NamedRegionTimer(StringRef Name, StringRef Description,
                             StringRef GroupName,
                             StringRef GroupDescription, bool Enabled = true);
-
-  // Create or get a TimerGroup stored in the same global map owned by
-  // NamedRegionTimer.
-  static TimerGroup &getNamedTimerGroup(StringRef GroupName,
-                                        StringRef GroupDescription);
 };
 
 /// The TimerGroup class is used to group together related timers into a single
diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp
index 4e27086e97ac5..46db2c74a5c76 100644
--- a/llvm/lib/IR/PassTimingInfo.cpp
+++ b/llvm/lib/IR/PassTimingInfo.cpp
@@ -63,9 +63,16 @@ class PassTimingInfo {
 private:
   StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
   DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
-  TimerGroup *PassTG = nullptr;
+  TimerGroup TG;
 
 public:
+  /// Default constructor for yet-inactive timeinfo.
+  /// Use \p init() to activate it.
+  PassTimingInfo();
+
+  /// Print out timing information and release timers.
+  ~PassTimingInfo();
+
   /// Initializes the static \p TheTimeInfo member to a non-null value when
   /// -time-passes is enabled. Leaves it null otherwise.
   ///
@@ -87,6 +94,14 @@ class PassTimingInfo {
 
 static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
 
+PassTimingInfo::PassTimingInfo() : TG("pass", "Pass execution timing report") {}
+
+PassTimingInfo::~PassTimingInfo() {
+  // Deleting the timers accumulates their info into the TG member.
+  // Then TG member is (implicitly) deleted, actually printing the report.
+  TimingData.clear();
+}
+
 void PassTimingInfo::init() {
   if (TheTimeInfo || !TimePassesIsEnabled)
     return;
@@ -95,16 +110,12 @@ void PassTimingInfo::init() {
   // This guarantees that the object will be constructed after static globals,
   // thus it will be destroyed before them.
   static ManagedStatic<PassTimingInfo> TTI;
-  if (!TTI->PassTG)
-    TTI->PassTG = &NamedRegionTimer::getNamedTimerGroup(
-        TimePassesHandler::PassGroupName, TimePassesHandler::PassGroupDesc);
   TheTimeInfo = &*TTI;
 }
 
 /// Prints out timing information and then resets the timers.
 void PassTimingInfo::print(raw_ostream *OutStream) {
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  PassTG->print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
+  TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
 }
 
 Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
@@ -113,8 +124,7 @@ Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
   // Appending description with a pass-instance number for all but the first one
   std::string PassDescNumbered =
       num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  return new Timer(PassID, PassDescNumbered, *PassTG);
+  return new Timer(PassID, PassDescNumbered, TG);
 }
 
 Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
@@ -183,7 +193,9 @@ Timer &TimePassesHandler::getPassTimer(StringRef PassID, bool IsPass) {
 }
 
 TimePassesHandler::TimePassesHandler(bool Enabled, bool PerRun)
-    : Enabled(Enabled), PerRun(PerRun) {}
+    : PassTG("pass", "Pass execution timing report"),
+      AnalysisTG("analysis", "Analysis execution timing report"),
+      Enabled(Enabled), PerRun(PerRun) {}
 
 TimePassesHandler::TimePassesHandler()
     : TimePassesHandler(TimePassesIsEnabled, TimePassesPerRun) {}
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 4d3b4f7481edf..eca726828c697 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -222,26 +222,15 @@ class Name2PairMap {
              StringRef GroupDescription) {
     sys::SmartScopedLock<true> L(timerLock());
 
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry =
-        getGroupEntry(GroupName, GroupDescription);
-    Timer &T = GroupEntry.second[Name];
-    if (!T.isInitialized())
-      T.init(Name, Description, *GroupEntry.first);
-    return T;
-  }
-
-  TimerGroup &getTimerGroup(StringRef GroupName, StringRef GroupDescription) {
-    return *getGroupEntry(GroupName, GroupDescription).first;
-  }
+    std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
 
-private:
-  std::pair<TimerGroup *, Name2TimerMap> &
-  getGroupEntry(StringRef GroupName, StringRef GroupDescription) {
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry = Map[GroupName];
     if (!GroupEntry.first)
       GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
 
-    return GroupEntry;
+    Timer &T = GroupEntry.second[Name];
+    if (!T.isInitialized())
+      T.init(Name, Description, *GroupEntry.first);
+    return T;
   }
 };
 
@@ -255,11 +244,6 @@ NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
                      : &namedGroupedTimers().get(Name, Description, GroupName,
                                                  GroupDescription)) {}
 
-TimerGroup &NamedRegionTimer::getNamedTimerGroup(StringRef GroupName,
-                                                 StringRef GroupDescription) {
-  return namedGroupedTimers().getTimerGroup(GroupName, GroupDescription);
-}
-
 //===----------------------------------------------------------------------===//
 //   TimerGroup Implementation
 //===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/IR/TimePassesTest.cpp b/llvm/unittests/IR/TimePassesTest.cpp
index 85986132103ca..33f8e00b377d5 100644
--- a/llvm/unittests/IR/TimePassesTest.cpp
+++ b/llvm/unittests/IR/TimePassesTest.cpp
@@ -161,8 +161,8 @@ TEST(TimePassesTest, CustomOut) {
   PI.runBeforePass(Pass2, M);
   PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
 
-  // Clear and generate report again.
-  TimePasses->print();
+  // Generate report by deleting the handler.
+  TimePasses.reset();
 
   // There should be Pass2 in this report and no Pass1.
   EXPECT_FALSE(TimePassesStr.str().empty());

@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/pr-subscribers-clang

Author: Arthur Eubanks (aeubanks)

Changes

Reverts llvm/llvm-project#130375

Causes breakages, e.g. https://lab.llvm.org/buildbot/#/builders/160/builds/14607


Full diff: https://github.com/llvm/llvm-project/pull/131173.diff

6 Files Affected:

  • (modified) clang/test/Misc/time-passes.c (+1)
  • (modified) llvm/include/llvm/IR/PassTimingInfo.h (+7-11)
  • (modified) llvm/include/llvm/Support/Timer.h (-5)
  • (modified) llvm/lib/IR/PassTimingInfo.cpp (+21-9)
  • (modified) llvm/lib/Support/Timer.cpp (+5-21)
  • (modified) llvm/unittests/IR/TimePassesTest.cpp (+2-2)
diff --git a/clang/test/Misc/time-passes.c b/clang/test/Misc/time-passes.c
index c1669826b2268..395da216aad42 100644
--- a/clang/test/Misc/time-passes.c
+++ b/clang/test/Misc/time-passes.c
@@ -19,5 +19,6 @@
 // NPM:       InstCombinePass{{$}}
 // NPM-NOT:   InstCombinePass #
 // TIME: Total{{$}}
+// NPM: Pass execution timing report
 
 int foo(int x, int y) { return x + y; }
diff --git a/llvm/include/llvm/IR/PassTimingInfo.h b/llvm/include/llvm/IR/PassTimingInfo.h
index b47ba7f16ef37..1148399943186 100644
--- a/llvm/include/llvm/IR/PassTimingInfo.h
+++ b/llvm/include/llvm/IR/PassTimingInfo.h
@@ -39,7 +39,8 @@ Timer *getPassTimer(Pass *);
 /// This class implements -time-passes functionality for new pass manager.
 /// It provides the pass-instrumentation callbacks that measure the pass
 /// execution time. They collect timing info into individual timers as
-/// passes are being run.
+/// passes are being run. At the end of its life-time it prints the resulting
+/// timing report.
 class TimePassesHandler {
   /// Value of this type is capable of uniquely identifying pass invocations.
   /// It is a pair of string Pass-Identifier (which for now is common
@@ -47,10 +48,8 @@ class TimePassesHandler {
   using PassInvocationID = std::pair<StringRef, unsigned>;
 
   /// Groups of timers for passes and analyses.
-  TimerGroup &PassTG =
-      NamedRegionTimer::getNamedTimerGroup(PassGroupName, PassGroupDesc);
-  TimerGroup &AnalysisTG = NamedRegionTimer::getNamedTimerGroup(
-      AnalysisGroupName, AnalysisGroupDesc);
+  TimerGroup PassTG;
+  TimerGroup AnalysisTG;
 
   using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
   /// Map of timers for pass invocations
@@ -72,15 +71,12 @@ class TimePassesHandler {
   bool PerRun;
 
 public:
-  static constexpr StringRef PassGroupName = "pass";
-  static constexpr StringRef AnalysisGroupName = "analysis";
-  static constexpr StringRef PassGroupDesc = "Pass execution timing report";
-  static constexpr StringRef AnalysisGroupDesc =
-      "Analysis execution timing report";
-
   TimePassesHandler();
   TimePassesHandler(bool Enabled, bool PerRun = false);
 
+  /// Destructor handles the print action if it has not been handled before.
+  ~TimePassesHandler() { print(); }
+
   /// Prints out timing information and then resets the timers.
   void print();
 
diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h
index 5a5082b6718ed..abe30451dd2f2 100644
--- a/llvm/include/llvm/Support/Timer.h
+++ b/llvm/include/llvm/Support/Timer.h
@@ -169,11 +169,6 @@ struct NamedRegionTimer : public TimeRegion {
   explicit NamedRegionTimer(StringRef Name, StringRef Description,
                             StringRef GroupName,
                             StringRef GroupDescription, bool Enabled = true);
-
-  // Create or get a TimerGroup stored in the same global map owned by
-  // NamedRegionTimer.
-  static TimerGroup &getNamedTimerGroup(StringRef GroupName,
-                                        StringRef GroupDescription);
 };
 
 /// The TimerGroup class is used to group together related timers into a single
diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp
index 4e27086e97ac5..46db2c74a5c76 100644
--- a/llvm/lib/IR/PassTimingInfo.cpp
+++ b/llvm/lib/IR/PassTimingInfo.cpp
@@ -63,9 +63,16 @@ class PassTimingInfo {
 private:
   StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
   DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
-  TimerGroup *PassTG = nullptr;
+  TimerGroup TG;
 
 public:
+  /// Default constructor for yet-inactive timeinfo.
+  /// Use \p init() to activate it.
+  PassTimingInfo();
+
+  /// Print out timing information and release timers.
+  ~PassTimingInfo();
+
   /// Initializes the static \p TheTimeInfo member to a non-null value when
   /// -time-passes is enabled. Leaves it null otherwise.
   ///
@@ -87,6 +94,14 @@ class PassTimingInfo {
 
 static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
 
+PassTimingInfo::PassTimingInfo() : TG("pass", "Pass execution timing report") {}
+
+PassTimingInfo::~PassTimingInfo() {
+  // Deleting the timers accumulates their info into the TG member.
+  // Then TG member is (implicitly) deleted, actually printing the report.
+  TimingData.clear();
+}
+
 void PassTimingInfo::init() {
   if (TheTimeInfo || !TimePassesIsEnabled)
     return;
@@ -95,16 +110,12 @@ void PassTimingInfo::init() {
   // This guarantees that the object will be constructed after static globals,
   // thus it will be destroyed before them.
   static ManagedStatic<PassTimingInfo> TTI;
-  if (!TTI->PassTG)
-    TTI->PassTG = &NamedRegionTimer::getNamedTimerGroup(
-        TimePassesHandler::PassGroupName, TimePassesHandler::PassGroupDesc);
   TheTimeInfo = &*TTI;
 }
 
 /// Prints out timing information and then resets the timers.
 void PassTimingInfo::print(raw_ostream *OutStream) {
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  PassTG->print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
+  TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
 }
 
 Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
@@ -113,8 +124,7 @@ Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
   // Appending description with a pass-instance number for all but the first one
   std::string PassDescNumbered =
       num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  return new Timer(PassID, PassDescNumbered, *PassTG);
+  return new Timer(PassID, PassDescNumbered, TG);
 }
 
 Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
@@ -183,7 +193,9 @@ Timer &TimePassesHandler::getPassTimer(StringRef PassID, bool IsPass) {
 }
 
 TimePassesHandler::TimePassesHandler(bool Enabled, bool PerRun)
-    : Enabled(Enabled), PerRun(PerRun) {}
+    : PassTG("pass", "Pass execution timing report"),
+      AnalysisTG("analysis", "Analysis execution timing report"),
+      Enabled(Enabled), PerRun(PerRun) {}
 
 TimePassesHandler::TimePassesHandler()
     : TimePassesHandler(TimePassesIsEnabled, TimePassesPerRun) {}
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 4d3b4f7481edf..eca726828c697 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -222,26 +222,15 @@ class Name2PairMap {
              StringRef GroupDescription) {
     sys::SmartScopedLock<true> L(timerLock());
 
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry =
-        getGroupEntry(GroupName, GroupDescription);
-    Timer &T = GroupEntry.second[Name];
-    if (!T.isInitialized())
-      T.init(Name, Description, *GroupEntry.first);
-    return T;
-  }
-
-  TimerGroup &getTimerGroup(StringRef GroupName, StringRef GroupDescription) {
-    return *getGroupEntry(GroupName, GroupDescription).first;
-  }
+    std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
 
-private:
-  std::pair<TimerGroup *, Name2TimerMap> &
-  getGroupEntry(StringRef GroupName, StringRef GroupDescription) {
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry = Map[GroupName];
     if (!GroupEntry.first)
       GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
 
-    return GroupEntry;
+    Timer &T = GroupEntry.second[Name];
+    if (!T.isInitialized())
+      T.init(Name, Description, *GroupEntry.first);
+    return T;
   }
 };
 
@@ -255,11 +244,6 @@ NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
                      : &namedGroupedTimers().get(Name, Description, GroupName,
                                                  GroupDescription)) {}
 
-TimerGroup &NamedRegionTimer::getNamedTimerGroup(StringRef GroupName,
-                                                 StringRef GroupDescription) {
-  return namedGroupedTimers().getTimerGroup(GroupName, GroupDescription);
-}
-
 //===----------------------------------------------------------------------===//
 //   TimerGroup Implementation
 //===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/IR/TimePassesTest.cpp b/llvm/unittests/IR/TimePassesTest.cpp
index 85986132103ca..33f8e00b377d5 100644
--- a/llvm/unittests/IR/TimePassesTest.cpp
+++ b/llvm/unittests/IR/TimePassesTest.cpp
@@ -161,8 +161,8 @@ TEST(TimePassesTest, CustomOut) {
   PI.runBeforePass(Pass2, M);
   PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
 
-  // Clear and generate report again.
-  TimePasses->print();
+  // Generate report by deleting the handler.
+  TimePasses.reset();
 
   // There should be Pass2 in this report and no Pass1.
   EXPECT_FALSE(TimePassesStr.str().empty());

@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/pr-subscribers-llvm-support

Author: Arthur Eubanks (aeubanks)

Changes

Reverts llvm/llvm-project#130375

Causes breakages, e.g. https://lab.llvm.org/buildbot/#/builders/160/builds/14607


Full diff: https://github.com/llvm/llvm-project/pull/131173.diff

6 Files Affected:

  • (modified) clang/test/Misc/time-passes.c (+1)
  • (modified) llvm/include/llvm/IR/PassTimingInfo.h (+7-11)
  • (modified) llvm/include/llvm/Support/Timer.h (-5)
  • (modified) llvm/lib/IR/PassTimingInfo.cpp (+21-9)
  • (modified) llvm/lib/Support/Timer.cpp (+5-21)
  • (modified) llvm/unittests/IR/TimePassesTest.cpp (+2-2)
diff --git a/clang/test/Misc/time-passes.c b/clang/test/Misc/time-passes.c
index c1669826b2268..395da216aad42 100644
--- a/clang/test/Misc/time-passes.c
+++ b/clang/test/Misc/time-passes.c
@@ -19,5 +19,6 @@
 // NPM:       InstCombinePass{{$}}
 // NPM-NOT:   InstCombinePass #
 // TIME: Total{{$}}
+// NPM: Pass execution timing report
 
 int foo(int x, int y) { return x + y; }
diff --git a/llvm/include/llvm/IR/PassTimingInfo.h b/llvm/include/llvm/IR/PassTimingInfo.h
index b47ba7f16ef37..1148399943186 100644
--- a/llvm/include/llvm/IR/PassTimingInfo.h
+++ b/llvm/include/llvm/IR/PassTimingInfo.h
@@ -39,7 +39,8 @@ Timer *getPassTimer(Pass *);
 /// This class implements -time-passes functionality for new pass manager.
 /// It provides the pass-instrumentation callbacks that measure the pass
 /// execution time. They collect timing info into individual timers as
-/// passes are being run.
+/// passes are being run. At the end of its life-time it prints the resulting
+/// timing report.
 class TimePassesHandler {
   /// Value of this type is capable of uniquely identifying pass invocations.
   /// It is a pair of string Pass-Identifier (which for now is common
@@ -47,10 +48,8 @@ class TimePassesHandler {
   using PassInvocationID = std::pair<StringRef, unsigned>;
 
   /// Groups of timers for passes and analyses.
-  TimerGroup &PassTG =
-      NamedRegionTimer::getNamedTimerGroup(PassGroupName, PassGroupDesc);
-  TimerGroup &AnalysisTG = NamedRegionTimer::getNamedTimerGroup(
-      AnalysisGroupName, AnalysisGroupDesc);
+  TimerGroup PassTG;
+  TimerGroup AnalysisTG;
 
   using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
   /// Map of timers for pass invocations
@@ -72,15 +71,12 @@ class TimePassesHandler {
   bool PerRun;
 
 public:
-  static constexpr StringRef PassGroupName = "pass";
-  static constexpr StringRef AnalysisGroupName = "analysis";
-  static constexpr StringRef PassGroupDesc = "Pass execution timing report";
-  static constexpr StringRef AnalysisGroupDesc =
-      "Analysis execution timing report";
-
   TimePassesHandler();
   TimePassesHandler(bool Enabled, bool PerRun = false);
 
+  /// Destructor handles the print action if it has not been handled before.
+  ~TimePassesHandler() { print(); }
+
   /// Prints out timing information and then resets the timers.
   void print();
 
diff --git a/llvm/include/llvm/Support/Timer.h b/llvm/include/llvm/Support/Timer.h
index 5a5082b6718ed..abe30451dd2f2 100644
--- a/llvm/include/llvm/Support/Timer.h
+++ b/llvm/include/llvm/Support/Timer.h
@@ -169,11 +169,6 @@ struct NamedRegionTimer : public TimeRegion {
   explicit NamedRegionTimer(StringRef Name, StringRef Description,
                             StringRef GroupName,
                             StringRef GroupDescription, bool Enabled = true);
-
-  // Create or get a TimerGroup stored in the same global map owned by
-  // NamedRegionTimer.
-  static TimerGroup &getNamedTimerGroup(StringRef GroupName,
-                                        StringRef GroupDescription);
 };
 
 /// The TimerGroup class is used to group together related timers into a single
diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp
index 4e27086e97ac5..46db2c74a5c76 100644
--- a/llvm/lib/IR/PassTimingInfo.cpp
+++ b/llvm/lib/IR/PassTimingInfo.cpp
@@ -63,9 +63,16 @@ class PassTimingInfo {
 private:
   StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
   DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
-  TimerGroup *PassTG = nullptr;
+  TimerGroup TG;
 
 public:
+  /// Default constructor for yet-inactive timeinfo.
+  /// Use \p init() to activate it.
+  PassTimingInfo();
+
+  /// Print out timing information and release timers.
+  ~PassTimingInfo();
+
   /// Initializes the static \p TheTimeInfo member to a non-null value when
   /// -time-passes is enabled. Leaves it null otherwise.
   ///
@@ -87,6 +94,14 @@ class PassTimingInfo {
 
 static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
 
+PassTimingInfo::PassTimingInfo() : TG("pass", "Pass execution timing report") {}
+
+PassTimingInfo::~PassTimingInfo() {
+  // Deleting the timers accumulates their info into the TG member.
+  // Then TG member is (implicitly) deleted, actually printing the report.
+  TimingData.clear();
+}
+
 void PassTimingInfo::init() {
   if (TheTimeInfo || !TimePassesIsEnabled)
     return;
@@ -95,16 +110,12 @@ void PassTimingInfo::init() {
   // This guarantees that the object will be constructed after static globals,
   // thus it will be destroyed before them.
   static ManagedStatic<PassTimingInfo> TTI;
-  if (!TTI->PassTG)
-    TTI->PassTG = &NamedRegionTimer::getNamedTimerGroup(
-        TimePassesHandler::PassGroupName, TimePassesHandler::PassGroupDesc);
   TheTimeInfo = &*TTI;
 }
 
 /// Prints out timing information and then resets the timers.
 void PassTimingInfo::print(raw_ostream *OutStream) {
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  PassTG->print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
+  TG.print(OutStream ? *OutStream : *CreateInfoOutputFile(), true);
 }
 
 Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
@@ -113,8 +124,7 @@ Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
   // Appending description with a pass-instance number for all but the first one
   std::string PassDescNumbered =
       num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
-  assert(PassTG && "PassTG is null, did you call PassTimingInfo::Init()?");
-  return new Timer(PassID, PassDescNumbered, *PassTG);
+  return new Timer(PassID, PassDescNumbered, TG);
 }
 
 Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
@@ -183,7 +193,9 @@ Timer &TimePassesHandler::getPassTimer(StringRef PassID, bool IsPass) {
 }
 
 TimePassesHandler::TimePassesHandler(bool Enabled, bool PerRun)
-    : Enabled(Enabled), PerRun(PerRun) {}
+    : PassTG("pass", "Pass execution timing report"),
+      AnalysisTG("analysis", "Analysis execution timing report"),
+      Enabled(Enabled), PerRun(PerRun) {}
 
 TimePassesHandler::TimePassesHandler()
     : TimePassesHandler(TimePassesIsEnabled, TimePassesPerRun) {}
diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index 4d3b4f7481edf..eca726828c697 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -222,26 +222,15 @@ class Name2PairMap {
              StringRef GroupDescription) {
     sys::SmartScopedLock<true> L(timerLock());
 
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry =
-        getGroupEntry(GroupName, GroupDescription);
-    Timer &T = GroupEntry.second[Name];
-    if (!T.isInitialized())
-      T.init(Name, Description, *GroupEntry.first);
-    return T;
-  }
-
-  TimerGroup &getTimerGroup(StringRef GroupName, StringRef GroupDescription) {
-    return *getGroupEntry(GroupName, GroupDescription).first;
-  }
+    std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
 
-private:
-  std::pair<TimerGroup *, Name2TimerMap> &
-  getGroupEntry(StringRef GroupName, StringRef GroupDescription) {
-    std::pair<TimerGroup *, Name2TimerMap> &GroupEntry = Map[GroupName];
     if (!GroupEntry.first)
       GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
 
-    return GroupEntry;
+    Timer &T = GroupEntry.second[Name];
+    if (!T.isInitialized())
+      T.init(Name, Description, *GroupEntry.first);
+    return T;
   }
 };
 
@@ -255,11 +244,6 @@ NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
                      : &namedGroupedTimers().get(Name, Description, GroupName,
                                                  GroupDescription)) {}
 
-TimerGroup &NamedRegionTimer::getNamedTimerGroup(StringRef GroupName,
-                                                 StringRef GroupDescription) {
-  return namedGroupedTimers().getTimerGroup(GroupName, GroupDescription);
-}
-
 //===----------------------------------------------------------------------===//
 //   TimerGroup Implementation
 //===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/IR/TimePassesTest.cpp b/llvm/unittests/IR/TimePassesTest.cpp
index 85986132103ca..33f8e00b377d5 100644
--- a/llvm/unittests/IR/TimePassesTest.cpp
+++ b/llvm/unittests/IR/TimePassesTest.cpp
@@ -161,8 +161,8 @@ TEST(TimePassesTest, CustomOut) {
   PI.runBeforePass(Pass2, M);
   PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
 
-  // Clear and generate report again.
-  TimePasses->print();
+  // Generate report by deleting the handler.
+  TimePasses.reset();
 
   // There should be Pass2 in this report and no Pass1.
   EXPECT_FALSE(TimePassesStr.str().empty());

alanzhao1 added a commit to alanzhao1/llvm-project that referenced this pull request Mar 13, 2025
…s manager timers" (llvm#131173)

This reverts commit 31ebe66.

The reason for the test failure is likely due to
`Name2PairMap::getTimerGroup(...)` not holding a lock.
alanzhao1 added a commit that referenced this pull request Mar 13, 2025
…s manager timers" (#131173) (#131217)

This reverts commit 31ebe66.

The reason for the test failure is likely due to
`Name2PairMap::getTimerGroup(...)` not holding a lock.
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
…s manager timers" (llvm#131173) (llvm#131217)

This reverts commit 31ebe66.

The reason for the test failure is likely due to
`Name2PairMap::getTimerGroup(...)` not holding a lock.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category llvm:ir llvm:support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants