[llvm] [llvm][Support] Enable `TimeTraceProfiler` to accept deferred detail string (PR #74935)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 9 06:36:51 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Vlad Serebrennikov (Endilll)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/74935.diff
2 Files Affected:
- (modified) llvm/include/llvm/Support/TimeProfiler.h (+29-6)
- (modified) llvm/lib/Support/TimeProfiler.cpp (+23-11)
``````````diff
diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h
index 454a65f70231f..5b8af16ed4a9a 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 4d625b3eb5b17..bd8a04ed8962d 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();
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/74935
More information about the llvm-commits
mailing list