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

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 10:56:06 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Princeton Ferro (Prince781)

<details>
<summary>Changes</summary>

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

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


1 Files Affected:

- (modified) llvm/lib/Support/Timer.cpp (+19) 


``````````diff
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;
 }
 

``````````

</details>


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


More information about the llvm-commits mailing list