[llvm] Expose TimeTraceProfiler for multiple traces (PR #83778)

Takuto Ikuta via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 3 23:55:07 PST 2024


https://github.com/atetubou created https://github.com/llvm/llvm-project/pull/83778

To avoid issue like https://github.com/llvm/llvm-project/issues/56554 and https://github.com/llvm/llvm-project/issues/83236 due to no guarantees for nested relationships between file level span and syntax tree level span, I'd like to have a `TimeTraceProfiler` specific to trace handling only `Source` spans around https://github.com/llvm/llvm-project/blob/8715f256911786520bb727ce067098d7082ac45c/clang/lib/Sema/Sema.cpp#L153-L170.

This is a preparation patch to do that in following PR.

>From f069b2ad46e5795dbd7ac9cc7398e37176d0268b Mon Sep 17 00:00:00 2001
From: Takuto Ikuta <tikuta at google.com>
Date: Mon, 4 Mar 2024 16:38:28 +0900
Subject: [PATCH] Expose TimeTraceProfiler for multiple traces

---
 llvm/include/llvm/Support/TimeProfiler.h |  6 ++++++
 llvm/lib/Support/TimeProfiler.cpp        | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h
index 454a65f70231f4..cef4110c10deae 100644
--- a/llvm/include/llvm/Support/TimeProfiler.h
+++ b/llvm/include/llvm/Support/TimeProfiler.h
@@ -86,6 +86,9 @@ class raw_pwrite_stream;
 struct TimeTraceProfiler;
 TimeTraceProfiler *getTimeTraceProfilerInstance();
 
+TimeTraceProfiler *newTimeTraceProfiler(unsigned TimeTraceGranularity = 0,
+                                        StringRef ProcName = "");
+
 /// Initialize the time trace profiler.
 /// This sets up the global \p TimeTraceProfilerInstance
 /// variable to be the profiler instance.
@@ -123,9 +126,12 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
 void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
 void timeTraceProfilerBegin(StringRef Name,
                             llvm::function_ref<std::string()> Detail);
+void timeTraceProfilerBegin(TimeTraceProfiler *Profiler, StringRef Name,
+                            StringRef Detail);
 
 /// Manually end the last time section.
 void timeTraceProfilerEnd();
+void timeTraceProfilerEnd(TimeTraceProfiler *Profiler);
 
 /// The TimeTraceScope is a helper class to call the begin and end functions
 /// of the time trace profiler.  When the object is constructed, it begins
diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp
index 4d625b3eb5b170..9cd5ea6d81758f 100644
--- a/llvm/lib/Support/TimeProfiler.cpp
+++ b/llvm/lib/Support/TimeProfiler.cpp
@@ -293,6 +293,16 @@ void llvm::timeTraceProfilerInitialize(unsigned TimeTraceGranularity,
       TimeTraceGranularity, llvm::sys::path::filename(ProcName));
 }
 
+TimeTraceProfiler *llvm::newTimeTraceProfiler(unsigned TimeTraceGranularity,
+                                              StringRef ProcName) {
+  TimeTraceProfiler *Profiler =
+      new TimeTraceProfiler(TimeTraceGranularity, ProcName);
+  auto &Instances = getTimeTraceProfilerInstances();
+  std::lock_guard<std::mutex> Lock(Instances.Lock);
+  Instances.List.push_back(Profiler);
+  return Profiler;
+}
+
 // Removes all TimeTraceProfilerInstances.
 // Called from main thread.
 void llvm::timeTraceProfilerCleanup() {
@@ -353,7 +363,18 @@ void llvm::timeTraceProfilerBegin(StringRef Name,
     TimeTraceProfilerInstance->begin(std::string(Name), Detail);
 }
 
+void llvm::timeTraceProfilerBegin(TimeTraceProfiler *Profiler, StringRef Name,
+                                  StringRef Detail) {
+  if (Profiler != nullptr)
+    Profiler->begin(std::string(Name), [&]() { return std::string(Detail); });
+}
+
 void llvm::timeTraceProfilerEnd() {
   if (TimeTraceProfilerInstance != nullptr)
     TimeTraceProfilerInstance->end();
 }
+
+void llvm::timeTraceProfilerEnd(TimeTraceProfiler *Profiler) {
+  if (Profiler != nullptr)
+    Profiler->end();
+}



More information about the llvm-commits mailing list