[llvm] [Timer] initialize timer options before group (PR #113719)

Princeton Ferro via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 10:55:31 PDT 2024


https://github.com/Prince781 created https://github.com/llvm/llvm-project/pull/113719

Ensures timer options are initialized before NamedGroupTimers. A crash can occur if a user does bitcode parsing before initialization of command-line options.

>From e7e2842ba205b2872e3d0ab2bb88db120b3f587a Mon Sep 17 00:00:00 2001
From: Princeton Ferro <pferro at nvidia.com>
Date: Tue, 8 Oct 2024 16:56:18 -0700
Subject: [PATCH] [Timer] initialize timer options before group

Ensures timer options are initialized before NamedGroupTimers. A crash
can occur if a user does bitcode parsing before initialization of
command-line options.
---
 llvm/lib/Support/Timer.cpp | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp
index c1b0fdbc077bb0..e8db0d42c4dbfe 100644
--- a/llvm/lib/Support/Timer.cpp
+++ b/llvm/lib/Support/Timer.cpp
@@ -501,6 +501,25 @@ const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
 
 void TimerGroup::constructForStatistics() {
   (void)getLibSupportInfoOutputFilename();
+  // Make sure initTimerOptions() is called here, and before NamedGroupTimers is
+  // initialized. Rationale:
+  //
+  // TimerGroup will print itself on destruction, and its destructor accesses
+  // one of the timer options (SortTimers) to determine how to do this:
+  // ~TimerGroup::TimerGroup()
+  //   -> TimerGroup::removeTimer()
+  //   -> TimerGroup::PrintQueuedTimers()
+  //   -> if (*SortTimers) llvm::sort(TimersToPrint);
+  //
+  // It's possible the timer options may not be initialized at this point. This
+  // could happen if a client of LLVM calls the bitcode parser before
+  // initializing command-line options, for example. Inside the parser are
+  // statistic counters, which when incremented will initialize the TimerGroup
+  // here. If NamedGroupTimers is created before timer options, it will later be
+  // destroyed AFTER the timer options (ManagedStatics are destroyed in reverse
+  // order), causing a crash when attempting to access the already-destroyed
+  // timer option.
+  initTimerOptions();
   (void)*NamedGroupedTimers;
 }
 



More information about the llvm-commits mailing list