https://github.com/ivanaivanovska created https://github.com/llvm/llvm-project/pull/103039
Added an instant event type and issuing such events each time a template instantiation is deferred. >From 1294a50b0da22e2904d3e43942a6be702c93d133 Mon Sep 17 00:00:00 2001 From: Ivana Ivanovska <iivanov...@google.com> Date: Tue, 13 Aug 2024 10:30:34 +0000 Subject: [PATCH] Added instant events and marking defered templated instantiation. --- clang/lib/Sema/SemaExpr.cpp | 18 ++++++ .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 17 +++++ clang/unittests/Support/TimeProfilerTest.cpp | 51 ++++++++++++++- llvm/include/llvm/Support/TimeProfiler.h | 19 +++++- llvm/lib/Support/TimeProfiler.cpp | 64 +++++++++++++------ 5 files changed, 145 insertions(+), 24 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 74c0e017059055..adc021c8cd237b 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -18,6 +18,7 @@ #include "clang/AST/ASTLambda.h" #include "clang/AST/ASTMutationListener.h" #include "clang/AST/CXXInheritance.h" +#include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/EvaluatedExprVisitor.h" @@ -63,6 +64,7 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/SaveAndRestore.h" +#include "llvm/Support/TimeProfiler.h" #include "llvm/Support/TypeSize.h" #include <optional> @@ -18013,6 +18015,22 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, std::make_pair(Func, PointOfInstantiation)); // Notify the consumer that a function was implicitly instantiated. Consumer.HandleCXXImplicitFunctionInstantiation(Func); + + llvm::TimeTraceScope TimeScope( + "DeferInstantiation", + [&]() { + llvm::TimeTraceMetadata M; + llvm::raw_string_ostream OS(M.Detail); + Func->getNameForDiagnostic(OS, getPrintingPolicy(), + /*Qualified=*/true); + if (llvm::isTimeTraceVerbose()) { + auto Loc = SourceMgr.getExpansionLoc(Func->getLocation()); + M.File = SourceMgr.getFilename(Loc); + M.Line = SourceMgr.getExpansionLineNumber(Loc); + } + return M; + }, + llvm::TimeTraceEventType::InstantEvent); } } } else { diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index f93cd113988ae4..d6d580d399457d 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4941,6 +4941,23 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, Function->setInstantiationIsPending(true); PendingInstantiations.push_back( std::make_pair(Function, PointOfInstantiation)); + + llvm::TimeTraceScope TimeScope( + "DeferInstantiation", + [&]() { + llvm::TimeTraceMetadata M; + llvm::raw_string_ostream OS(M.Detail); + Function->getNameForDiagnostic(OS, getPrintingPolicy(), + /*Qualified=*/true); + if (llvm::isTimeTraceVerbose()) { + auto Loc = SourceMgr.getExpansionLoc(Function->getLocation()); + M.File = SourceMgr.getFilename(Loc); + M.Line = SourceMgr.getExpansionLineNumber(Loc); + } + return M; + }, + llvm::TimeTraceEventType::InstantEvent); + } else if (TSK == TSK_ImplicitInstantiation) { if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { diff --git a/clang/unittests/Support/TimeProfilerTest.cpp b/clang/unittests/Support/TimeProfilerTest.cpp index f53fe71d630bf5..ccda12e943dd73 100644 --- a/clang/unittests/Support/TimeProfilerTest.cpp +++ b/clang/unittests/Support/TimeProfilerTest.cpp @@ -238,13 +238,55 @@ Frontend (test.cc) buildTraceGraph(Json)); } +TEST(TimeProfilerTest, ClassTemplateInstantiations) { + std::string Code = R"( + template<class T> + struct S + { + void foo() {} + void bar(); + }; + + template struct S<double>; // explicit instantiation of S<double> + + void user() { + S<int> a; // implicit instantiation of S<int> + S<float>* b; + b->foo(); // implicit instatiation of S<float> and S<float>::foo() + } + )"; + + setupProfiler(); + ASSERT_TRUE(compileFromString(Code, "-std=c++20", "test.cc")); + std::string Json = teardownProfiler(); + ASSERT_EQ(R"( +Frontend (test.cc) +| ParseClass (S) +| InstantiateClass (S<double>, test.cc:9) +| InstantiateFunction (S<double>::foo, test.cc:5) +| ParseDeclarationOrFunctionDefinition (test.cc:11:5) +| | ParseFunctionDefinition (user) +| | | InstantiateClass (S<int>, test.cc:3) +| | | InstantiateClass (S<float>, test.cc:3) +| | | DeferInstantiation (S<float>::foo, test.cc:5) +| PerformPendingInstantiations +| | InstantiateFunction (S<float>::foo, test.cc:5) +)", + buildTraceGraph(Json)); +} + TEST(TimeProfilerTest, TemplateInstantiations) { std::string B_H = R"( template <typename T> - T fooB(T t) { + T fooC(T t) { return T(); } + template <typename T> + constexpr T fooB(T t) { + return fooC(t); + } + #define MacroTemp(x) template <typename T> void foo##x(T) { T(); } )"; @@ -267,14 +309,19 @@ TEST(TimeProfilerTest, TemplateInstantiations) { std::string Json = teardownProfiler(); ASSERT_EQ(R"( Frontend (test.cc) +| ParseFunctionDefinition (fooC) | ParseFunctionDefinition (fooB) | ParseFunctionDefinition (fooMTA) | ParseFunctionDefinition (fooA) | ParseDeclarationOrFunctionDefinition (test.cc:3:5) | | ParseFunctionDefinition (user) +| | | DeferInstantiation (fooA<int>, a.h:7) | PerformPendingInstantiations | | InstantiateFunction (fooA<int>, a.h:7) -| | | InstantiateFunction (fooB<int>, b.h:3) +| | | InstantiateFunction (fooB<int>, b.h:8) +| | | | DeferInstantiation (fooC<int>, b.h:3) +| | | DeferInstantiation (fooMTA<int>, a.h:4) +| | | InstantiateFunction (fooC<int>, b.h:3) | | | InstantiateFunction (fooMTA<int>, a.h:4) )", buildTraceGraph(Json)); diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h index 9e2ba31991f542..f4952b18e4f6ef 100644 --- a/llvm/include/llvm/Support/TimeProfiler.h +++ b/llvm/include/llvm/Support/TimeProfiler.h @@ -83,6 +83,8 @@ namespace llvm { class raw_pwrite_stream; +enum class TimeTraceEventType { CompleteEvent, InstantEvent, AsyncEvent }; + struct TimeTraceMetadata { std::string Detail; // Source file and line number information for the event. @@ -152,6 +154,10 @@ timeTraceProfilerBegin(StringRef Name, TimeTraceProfilerEntry *timeTraceAsyncProfilerBegin(StringRef Name, StringRef Detail); +// Mark an instant event. +TimeTraceProfilerEntry *timeTraceInstantEventProfilerBegin(StringRef Name, + llvm::function_ref<TimeTraceMetadata()> Metadata); + /// Manually end the last time section. void timeTraceProfilerEnd(); void timeTraceProfilerEnd(TimeTraceProfilerEntry *E); @@ -181,9 +187,18 @@ class TimeTraceScope { Entry = timeTraceProfilerBegin(Name, Detail); } TimeTraceScope(StringRef Name, - llvm::function_ref<TimeTraceMetadata()> Metadata) { - if (getTimeTraceProfilerInstance() != nullptr) + llvm::function_ref<TimeTraceMetadata()> Metadata, TimeTraceEventType Et = TimeTraceEventType::CompleteEvent) { + if (getTimeTraceProfilerInstance() == nullptr) + return; + assert((Et == TimeTraceEventType::InstantEvent || + Et == TimeTraceEventType::CompleteEvent) && + "Event Type not supported."); + + if (Et == TimeTraceEventType::CompleteEvent) { Entry = timeTraceProfilerBegin(Name, Metadata); + } else { + Entry = timeTraceInstantEventProfilerBegin(Name, Metadata); + } } ~TimeTraceScope() { if (getTimeTraceProfilerInstance() != nullptr) diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp index c2014028ddadca..738f5f5842bb0d 100644 --- a/llvm/lib/Support/TimeProfiler.cpp +++ b/llvm/lib/Support/TimeProfiler.cpp @@ -13,6 +13,7 @@ #include "llvm/Support/TimeProfiler.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLFunctionalExtras.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/JSON.h" #include "llvm/Support/Path.h" @@ -70,23 +71,23 @@ using NameAndCountAndDurationType = /// Represents an open or completed time section entry to be captured. struct llvm::TimeTraceProfilerEntry { - const TimePointType Start; + TimePointType Start; TimePointType End; - const std::string Name; + std::string Name; TimeTraceMetadata Metadata; + TimeTraceEventType EventType; - const bool AsyncEvent = false; TimeTraceProfilerEntry(TimePointType &&S, TimePointType &&E, std::string &&N, - std::string &&Dt, bool Ae) + std::string &&Dt, TimeTraceEventType Et) : Start(std::move(S)), End(std::move(E)), Name(std::move(N)), Metadata(), - AsyncEvent(Ae) { + EventType(Et) { Metadata.Detail = std::move(Dt); } TimeTraceProfilerEntry(TimePointType &&S, TimePointType &&E, std::string &&N, - TimeTraceMetadata &&Mt, bool Ae) + TimeTraceMetadata &&Mt, TimeTraceEventType Et) : Start(std::move(S)), End(std::move(E)), Name(std::move(N)), - Metadata(std::move(Mt)), AsyncEvent(Ae) {} + Metadata(std::move(Mt)), EventType(Et) {} // Calculate timings for FlameGraph. Cast time points to microsecond precision // rather than casting duration. This avoids truncation issues causing inner @@ -116,19 +117,19 @@ struct llvm::TimeTraceProfiler { TimeTraceProfilerEntry *begin(std::string Name, llvm::function_ref<std::string()> Detail, - bool AsyncEvent = false) { + TimeTraceEventType Et = TimeTraceEventType::CompleteEvent) { Stack.emplace_back(std::make_unique<TimeTraceProfilerEntry>( ClockType::now(), TimePointType(), std::move(Name), Detail(), - AsyncEvent)); + Et)); return Stack.back().get(); } TimeTraceProfilerEntry * begin(std::string Name, llvm::function_ref<TimeTraceMetadata()> Metadata, - bool AsyncEvent = false) { + TimeTraceEventType Et = TimeTraceEventType::CompleteEvent) { Stack.emplace_back(std::make_unique<TimeTraceProfilerEntry>( ClockType::now(), TimePointType(), std::move(Name), Metadata(), - AsyncEvent)); + Et)); return Stack.back().get(); } @@ -144,9 +145,22 @@ struct llvm::TimeTraceProfiler { // Calculate duration at full precision for overall counts. DurationType Duration = E.End - E.Start; - // Only include sections longer or equal to TimeTraceGranularity msec. - if (duration_cast<microseconds>(Duration).count() >= TimeTraceGranularity) + // Only include Instant Events or events with a duration longer or equal to + // TimeTraceGranularity msec. + if (E.EventType == TimeTraceEventType::InstantEvent || + duration_cast<microseconds>(Duration).count() >= TimeTraceGranularity) { Entries.emplace_back(E); + } else { + // if the event is not included, exclude also all instant events that + // happened during this event. + for (SmallVector<TimeTraceProfilerEntry, 128>::iterator it = Entries.begin(); it != Entries.end();) { + if (TimeTraceEventType::InstantEvent == it->EventType && + it->Start > E.Start && it->Start < E.End) + it = Entries.erase(it); + else + ++it; + } + } // Track total time taken by each "name", but only the topmost levels of // them; e.g. if there's a template instantiation that instantiates other @@ -194,13 +208,15 @@ struct llvm::TimeTraceProfiler { J.attribute("pid", Pid); J.attribute("tid", int64_t(Tid)); J.attribute("ts", StartUs); - if (E.AsyncEvent) { + if (E.EventType == TimeTraceEventType::AsyncEvent) { J.attribute("cat", E.Name); J.attribute("ph", "b"); J.attribute("id", 0); - } else { + } else if (E.EventType == TimeTraceEventType::CompleteEvent) { J.attribute("ph", "X"); J.attribute("dur", DurUs); + } else { // instant event + J.attribute("ph", "i"); } J.attribute("name", E.Name); if (!E.Metadata.isEmpty()) { @@ -215,7 +231,7 @@ struct llvm::TimeTraceProfiler { } }); - if (E.AsyncEvent) { + if (E.EventType == TimeTraceEventType::AsyncEvent) { J.object([&] { J.attribute("pid", Pid); J.attribute("tid", int64_t(Tid)); @@ -406,7 +422,7 @@ TimeTraceProfilerEntry *llvm::timeTraceProfilerBegin(StringRef Name, StringRef Detail) { if (TimeTraceProfilerInstance != nullptr) return TimeTraceProfilerInstance->begin( - std::string(Name), [&]() { return std::string(Detail); }, false); + std::string(Name), [&]() { return std::string(Detail); }, TimeTraceEventType::CompleteEvent); return nullptr; } @@ -414,7 +430,7 @@ TimeTraceProfilerEntry * llvm::timeTraceProfilerBegin(StringRef Name, llvm::function_ref<std::string()> Detail) { if (TimeTraceProfilerInstance != nullptr) - return TimeTraceProfilerInstance->begin(std::string(Name), Detail, false); + return TimeTraceProfilerInstance->begin(std::string(Name), Detail, TimeTraceEventType::CompleteEvent); return nullptr; } @@ -422,7 +438,15 @@ TimeTraceProfilerEntry * llvm::timeTraceProfilerBegin(StringRef Name, llvm::function_ref<TimeTraceMetadata()> Metadata) { if (TimeTraceProfilerInstance != nullptr) - return TimeTraceProfilerInstance->begin(std::string(Name), Metadata, false); + return TimeTraceProfilerInstance->begin(std::string(Name), Metadata, TimeTraceEventType::CompleteEvent); + return nullptr; +} + +TimeTraceProfilerEntry * +llvm::timeTraceInstantEventProfilerBegin(StringRef Name, + llvm::function_ref<TimeTraceMetadata()> Metadata) { + if (TimeTraceProfilerInstance != nullptr) + return TimeTraceProfilerInstance->begin(std::string(Name), Metadata, TimeTraceEventType::InstantEvent); return nullptr; } @@ -430,7 +454,7 @@ TimeTraceProfilerEntry *llvm::timeTraceAsyncProfilerBegin(StringRef Name, StringRef Detail) { if (TimeTraceProfilerInstance != nullptr) return TimeTraceProfilerInstance->begin( - std::string(Name), [&]() { return std::string(Detail); }, true); + std::string(Name), [&]() { return std::string(Detail); }, TimeTraceEventType::AsyncEvent); return nullptr; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits