[Timer][Statistics] Make global constructor ordering more robust
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Wed, 3 Aug 2022 08:56:10 +0000 (10:56 +0200)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Thu, 25 Aug 2022 17:09:49 +0000 (19:09 +0200)
It was observed in D129117 that the subtle dependency between statistic
and timer code is not entirely robust: the global destructor
~StatisticInfo indirectly calls CreateInfoOutputFile, which requires
the LibSupportInfoOutputFilename to not have been destructed.

By constructing LibSupportInfoOutputFilename before the StatisticInfo
object, the order of destruction is guaranteed.

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

llvm/include/llvm/Support/Timer.h
llvm/lib/Support/Statistic.cpp
llvm/lib/Support/Timer.cpp

index 742d20c..d72af35 100644 (file)
@@ -231,10 +231,10 @@ public:
   /// Prints all timers as JSON key/value pairs.
   static const char *printAllJSONValues(raw_ostream &OS, const char *delim);
 
-  /// Ensure global timer group lists are initialized. This function is mostly
-  /// used by the Statistic code to influence the construction and destruction
-  /// order of the global timer lists.
-  static void ConstructTimerLists();
+  /// Ensure global objects required for statistics printing are initialized.
+  /// This function is used by the Statistic code to ensure correct order of
+  /// global constructors and destructors.
+  static void constructForStatistics();
 
   /// This makes the default group unmanaged, and lets the user manage the
   /// group's lifetime.
index df9f7c5..24ef3e9 100644 (file)
@@ -120,8 +120,9 @@ void TrackingStatistic::RegisterStatistic() {
 }
 
 StatisticInfo::StatisticInfo() {
-  // Ensure timergroup lists are created first so they are destructed after us.
-  TimerGroup::ConstructTimerLists();
+  // Ensure that necessary timer global objects are created first so they are
+  // destructed after us.
+  TimerGroup::constructForStatistics();
 }
 
 // Print information when destroyed, iff command line option is specified.
index 08e1a8a..c1b0fdb 100644 (file)
@@ -499,7 +499,8 @@ const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
   return delim;
 }
 
-void TimerGroup::ConstructTimerLists() {
+void TimerGroup::constructForStatistics() {
+  (void)getLibSupportInfoOutputFilename();
   (void)*NamedGroupedTimers;
 }