[llvm] [llvm][Support] Enable `TimeTraceProfiler` to accept deferred detail string (PR #74935)

Vlad Serebrennikov via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 9 06:36:36 PST 2023


https://github.com/Endilll created https://github.com/llvm/llvm-project/pull/74935

This patch enables `TimeTraceProfiler` and `TimeTraceScope` to accept detail string not yet available at the time new trace entry is created. This is important for Clang parser use cases, e.g. when parsing a class member we don't yet know name of.

>From 4e8f48947d59474e07e08cdc98e4a6fb2e3c80c1 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Sat, 9 Dec 2023 17:21:35 +0300
Subject: [PATCH] [llvm][Support] Enable `TimeTraceProfiler` to accept deferred
 detail string

This patch enables `TimeTraceProfiler` and `TimeTraceScope` to accept detail string, which is not yet available at the time new trace entry is created. This is important for Clang parser use cases, e.g. when parsing a class member we don't yet know name of.
---
 llvm/include/llvm/Support/TimeProfiler.h | 35 ++++++++++++++++++++----
 llvm/lib/Support/TimeProfiler.cpp        | 34 +++++++++++++++--------
 2 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h
index 454a65f70231f4..5b8af16ed4a9a6 100644
--- a/llvm/include/llvm/Support/TimeProfiler.h
+++ b/llvm/include/llvm/Support/TimeProfiler.h
@@ -86,6 +86,8 @@ class raw_pwrite_stream;
 struct TimeTraceProfiler;
 TimeTraceProfiler *getTimeTraceProfilerInstance();
 
+struct TimeTraceProfilerEntry;
+
 /// Initialize the time trace profiler.
 /// This sets up the global \p TimeTraceProfilerInstance
 /// variable to be the profiler instance.
@@ -120,18 +122,29 @@ Error timeTraceProfilerWrite(StringRef PreferredFileName,
 /// Profiler copies the string data, so the pointers can be given into
 /// temporaries. Time sections can be hierarchical; every Begin must have a
 /// matching End pair but they can nest.
-void timeTraceProfilerBegin(StringRef Name, StringRef Detail);
-void timeTraceProfilerBegin(StringRef Name,
+TimeTraceProfilerEntry* timeTraceProfilerBegin(StringRef Name, StringRef Detail);
+TimeTraceProfilerEntry* timeTraceProfilerBegin(StringRef Name,
                             llvm::function_ref<std::string()> Detail);
 
 /// Manually end the last time section.
 void timeTraceProfilerEnd();
 
+/// Set detail string for an existing trace entry.
+/// This function sets \p Detail string for an entry previously created by
+/// `timeTraceProfilerBegin`. String is handled in the same way as
+/// `timeTraceProfilerBegin` does. This is useful when contents of
+/// \p Detail is not (fully) known at the time trace entry is created.
+void timeTraceProfilerEntrySetDetail(TimeTraceProfilerEntry* Entry, StringRef Detail);
+void timeTraceProfilerEntrySetDetail(TimeTraceProfilerEntry* Entry, llvm::function_ref<std::string()> Detail);
+
 /// 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
 /// the section; and when it is destroyed, it stops it. If the time profiler
 /// is not initialized, the overhead is a single branch.
-struct TimeTraceScope {
+class TimeTraceScope {
+  TimeTraceProfilerEntry* Entry;
+
+public:
 
   TimeTraceScope() = delete;
   TimeTraceScope(const TimeTraceScope &) = delete;
@@ -141,20 +154,30 @@ struct TimeTraceScope {
 
   TimeTraceScope(StringRef Name) {
     if (getTimeTraceProfilerInstance() != nullptr)
-      timeTraceProfilerBegin(Name, StringRef(""));
+      Entry = timeTraceProfilerBegin(Name, StringRef(""));
   }
   TimeTraceScope(StringRef Name, StringRef Detail) {
     if (getTimeTraceProfilerInstance() != nullptr)
-      timeTraceProfilerBegin(Name, Detail);
+      Entry = timeTraceProfilerBegin(Name, Detail);
   }
   TimeTraceScope(StringRef Name, llvm::function_ref<std::string()> Detail) {
     if (getTimeTraceProfilerInstance() != nullptr)
-      timeTraceProfilerBegin(Name, Detail);
+      Entry = timeTraceProfilerBegin(Name, Detail);
   }
   ~TimeTraceScope() {
     if (getTimeTraceProfilerInstance() != nullptr)
       timeTraceProfilerEnd();
   }
+  void setDetail(StringRef Detail) {
+    if (Entry != nullptr) {
+      timeTraceProfilerEntrySetDetail(Entry, Detail);
+    }
+  }
+  void setDetail(llvm::function_ref<std::string()> Detail) {
+    if (Entry != nullptr) {
+      timeTraceProfilerEntrySetDetail(Entry, Detail);
+    }
+  }
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp
index 4d625b3eb5b170..bd8a04ed8962dd 100644
--- a/llvm/lib/Support/TimeProfiler.cpp
+++ b/llvm/lib/Support/TimeProfiler.cpp
@@ -64,12 +64,14 @@ using CountAndDurationType = std::pair<size_t, DurationType>;
 using NameAndCountAndDurationType =
     std::pair<std::string, CountAndDurationType>;
 
+} // anonymous namespace
+
 /// Represents an open or completed time section entry to be captured.
-struct TimeTraceProfilerEntry {
+struct llvm::TimeTraceProfilerEntry {
   const TimePointType Start;
   TimePointType End;
   const std::string Name;
-  const std::string Detail;
+  std::string Detail;
 
   TimeTraceProfilerEntry(TimePointType &&S, TimePointType &&E, std::string &&N,
                          std::string &&Dt)
@@ -92,8 +94,6 @@ struct TimeTraceProfilerEntry {
   }
 };
 
-} // anonymous namespace
-
 struct llvm::TimeTraceProfiler {
   TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
       : BeginningOfTime(system_clock::now()), StartTime(ClockType::now()),
@@ -102,8 +102,8 @@ struct llvm::TimeTraceProfiler {
     llvm::get_thread_name(ThreadName);
   }
 
-  void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
-    Stack.emplace_back(ClockType::now(), TimePointType(), std::move(Name),
+  llvm::TimeTraceProfilerEntry* begin(std::string Name, llvm::function_ref<std::string()> Detail) {
+    return &Stack.emplace_back(ClockType::now(), TimePointType(), std::move(Name),
                        Detail());
   }
 
@@ -341,19 +341,31 @@ Error llvm::timeTraceProfilerWrite(StringRef PreferredFileName,
   return Error::success();
 }
 
-void llvm::timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
+llvm::TimeTraceProfilerEntry* llvm::timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
   if (TimeTraceProfilerInstance != nullptr)
-    TimeTraceProfilerInstance->begin(std::string(Name),
-                                     [&]() { return std::string(Detail); });
+    return TimeTraceProfilerInstance->begin(std::string(Name),
+                                             [&]() { return std::string(Detail); });
 }
 
-void llvm::timeTraceProfilerBegin(StringRef Name,
+llvm::TimeTraceProfilerEntry* llvm::timeTraceProfilerBegin(StringRef Name,
                                   llvm::function_ref<std::string()> Detail) {
   if (TimeTraceProfilerInstance != nullptr)
-    TimeTraceProfilerInstance->begin(std::string(Name), Detail);
+    return TimeTraceProfilerInstance->begin(std::string(Name), Detail);
 }
 
 void llvm::timeTraceProfilerEnd() {
   if (TimeTraceProfilerInstance != nullptr)
     TimeTraceProfilerInstance->end();
 }
+
+void llvm::timeTraceProfilerEntrySetDetail(llvm::TimeTraceProfilerEntry* Entry, StringRef Detail) {
+  if (Entry != nullptr) {
+    Entry->Detail = Detail;
+  }
+}
+
+void llvm::timeTraceProfilerEntrySetDetail(llvm::TimeTraceProfilerEntry* Entry, llvm::function_ref<std::string()> Detail) {
+  if (Entry != nullptr) {
+    Entry->Detail = Detail();
+  }
+}



More information about the llvm-commits mailing list