https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/98320
>From 03cc5fbebaf0c0c737e9304b8b3310ab4908fcaa Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Wed, 10 Jul 2024 13:52:46 +0000 Subject: [PATCH 1/5] Add an option to add source file info to -ftime-trace --- clang/include/clang/Driver/Options.td | 4 ++++ clang/include/clang/Frontend/FrontendOptions.h | 3 +++ clang/lib/Sema/SemaTemplateInstantiate.cpp | 2 ++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 ++ 4 files changed, 11 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index be7c3b60c20f1..c15f1b57bafc9 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3989,6 +3989,10 @@ def ftime_trace_EQ : Joined<["-"], "ftime-trace=">, Group<f_Group>, HelpText<"Similar to -ftime-trace. Specify the JSON file or a directory which will contain the JSON file">, Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>, MarshallingInfoString<FrontendOpts<"TimeTracePath">>; +def ftime_trace_add_filename : Flag<["-"], "ftime-trace-add-filename">, Group<f_Group>, + HelpText<"Adds filename to event details wherever supported. Eg: For template instantiation A<int>, details would include A<int>@source_file.h.">, + Visibility<[ClangOption, CLOption, DXCOption]>; + MarshallingInfoString<FrontendOpts<"TimeTraceAddFilename">>; def fproc_stat_report : Joined<["-"], "fproc-stat-report">, Group<f_Group>, HelpText<"Print subprocess statistics">; def fproc_stat_report_EQ : Joined<["-"], "fproc-stat-report=">, Group<f_Group>, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 5e5034fe01eb5..019a6737f8129 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -583,6 +583,9 @@ class FrontendOptions { /// Path which stores the output files for -ftime-trace std::string TimeTracePath; + /// Adds filename to event details wherever supported + bool TimeTraceAddFilename = false; + /// Output Path for module output file. std::string ModuleOutputPath; diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index a7bc6749c5852..f0fa7fd427906 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3430,6 +3430,8 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); + if (llvm::timeTraceAddFilename()) + OS << "@" << SourceMgr.getFilename(Instantiation->getLocation()); return Name; }); diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 88f6af80cbc55..ba55db4117c34 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4966,6 +4966,8 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Function->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); + if (llvm::timeTraceAddFilename()) + OS << "@" << SourceMgr.getFilename(Function->getLocation()); return Name; }); >From ae991f8c88c21a0872a5fa63219b3cb9b2787d9a Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Wed, 10 Jul 2024 15:52:55 +0000 Subject: [PATCH 2/5] Add tests --- clang/include/clang/Driver/Options.td | 4 ---- clang/include/clang/Frontend/FrontendOptions.h | 3 --- clang/lib/Sema/SemaTemplateInstantiate.cpp | 3 +-- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 +-- clang/test/Driver/ftime-trace-sections.py | 16 +++++++++++++++- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c15f1b57bafc9..be7c3b60c20f1 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3989,10 +3989,6 @@ def ftime_trace_EQ : Joined<["-"], "ftime-trace=">, Group<f_Group>, HelpText<"Similar to -ftime-trace. Specify the JSON file or a directory which will contain the JSON file">, Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>, MarshallingInfoString<FrontendOpts<"TimeTracePath">>; -def ftime_trace_add_filename : Flag<["-"], "ftime-trace-add-filename">, Group<f_Group>, - HelpText<"Adds filename to event details wherever supported. Eg: For template instantiation A<int>, details would include A<int>@source_file.h.">, - Visibility<[ClangOption, CLOption, DXCOption]>; - MarshallingInfoString<FrontendOpts<"TimeTraceAddFilename">>; def fproc_stat_report : Joined<["-"], "fproc-stat-report">, Group<f_Group>, HelpText<"Print subprocess statistics">; def fproc_stat_report_EQ : Joined<["-"], "fproc-stat-report=">, Group<f_Group>, diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index 019a6737f8129..5e5034fe01eb5 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -583,9 +583,6 @@ class FrontendOptions { /// Path which stores the output files for -ftime-trace std::string TimeTracePath; - /// Adds filename to event details wherever supported - bool TimeTraceAddFilename = false; - /// Output Path for module output file. std::string ModuleOutputPath; diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index f0fa7fd427906..69f46a77d8067 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3430,8 +3430,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - if (llvm::timeTraceAddFilename()) - OS << "@" << SourceMgr.getFilename(Instantiation->getLocation()); + OS << "@" << SourceMgr.getFilename(Instantiation->getLocation()); return Name; }); diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index ba55db4117c34..d835a217802ac 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4966,8 +4966,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Function->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - if (llvm::timeTraceAddFilename()) - OS << "@" << SourceMgr.getFilename(Function->getLocation()); + OS << "@" << SourceMgr.getFilename(Function->getLocation()); return Name; }); diff --git a/clang/test/Driver/ftime-trace-sections.py b/clang/test/Driver/ftime-trace-sections.py index 02afa4ac54eb7..cfec77fc97a8e 100644 --- a/clang/test/Driver/ftime-trace-sections.py +++ b/clang/test/Driver/ftime-trace-sections.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import json, sys, time +import json, sys, time, re def is_inside(range1, range2): @@ -16,12 +16,18 @@ def is_before(range1, range2): c = range2["ts"] return b <= c +instantiation_pattern = re.compile("^.*<.*>@.*.cpp$") + +def is_valid_instantiation(instantiation): + return instantiation_pattern.match(instantiation["args"]["detail"]) + log_contents = json.loads(sys.stdin.read()) events = log_contents["traceEvents"] codegens = [event for event in events if event["name"] == "CodeGen Function"] frontends = [event for event in events if event["name"] == "Frontend"] backends = [event for event in events if event["name"] == "Backend"] +instantiations = [event for event in events if event["name"].startswith("Instantiate")] beginning_of_time = log_contents["beginningOfTime"] / 1000000 seconds_since_epoch = time.time() @@ -48,3 +54,11 @@ def is_before(range1, range2): ] ): sys.exit("Not all Frontend section are before all Backend sections!") + +if not all( + [ + is_valid_instantiation(instantiation) + for instantiation in instantiations + ] +): + sys.exit("Not all Frontend section are before all Backend sections!") >From afdaaddcb490668d0565c4a0a4de1bc49b9f41e9 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Wed, 10 Jul 2024 15:57:55 +0000 Subject: [PATCH 3/5] use , file: as separator --- clang/lib/Sema/SemaTemplateInstantiate.cpp | 2 +- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +- clang/test/Driver/ftime-trace-sections.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 69f46a77d8067..3105f3eefa0ba 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3430,7 +3430,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - OS << "@" << SourceMgr.getFilename(Instantiation->getLocation()); + OS << ", file:" << SourceMgr.getFilename(Instantiation->getLocation()); return Name; }); diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index d835a217802ac..88390d82260f5 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4966,7 +4966,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, llvm::raw_string_ostream OS(Name); Function->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - OS << "@" << SourceMgr.getFilename(Function->getLocation()); + OS << ", file:" << SourceMgr.getFilename(Function->getLocation()); return Name; }); diff --git a/clang/test/Driver/ftime-trace-sections.py b/clang/test/Driver/ftime-trace-sections.py index cfec77fc97a8e..cec62db6b0d7b 100644 --- a/clang/test/Driver/ftime-trace-sections.py +++ b/clang/test/Driver/ftime-trace-sections.py @@ -16,7 +16,7 @@ def is_before(range1, range2): c = range2["ts"] return b <= c -instantiation_pattern = re.compile("^.*<.*>@.*.cpp$") +instantiation_pattern = re.compile("^.*<.*>, file:.*.cpp$") def is_valid_instantiation(instantiation): return instantiation_pattern.match(instantiation["args"]["detail"]) >From f3a5c475935f8519d85332750bb43c4ac99d7668 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Wed, 10 Jul 2024 16:01:08 +0000 Subject: [PATCH 4/5] format and fix error messages --- clang/test/Driver/ftime-trace-sections.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/clang/test/Driver/ftime-trace-sections.py b/clang/test/Driver/ftime-trace-sections.py index cec62db6b0d7b..c42fa3c406ff4 100644 --- a/clang/test/Driver/ftime-trace-sections.py +++ b/clang/test/Driver/ftime-trace-sections.py @@ -16,8 +16,10 @@ def is_before(range1, range2): c = range2["ts"] return b <= c + instantiation_pattern = re.compile("^.*<.*>, file:.*.cpp$") + def is_valid_instantiation(instantiation): return instantiation_pattern.match(instantiation["args"]["detail"]) @@ -55,10 +57,6 @@ def is_valid_instantiation(instantiation): ): sys.exit("Not all Frontend section are before all Backend sections!") -if not all( - [ - is_valid_instantiation(instantiation) - for instantiation in instantiations - ] -): - sys.exit("Not all Frontend section are before all Backend sections!") +for instantiation in instantiations: + if not is_valid_instantiation(instantiation): + sys.exit("Invalid instantiation: " + str(instantiation)) >From ecb326f79facf5287cf6cfa345d3c7c6d6ad3464 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Tue, 16 Jul 2024 15:57:25 +0000 Subject: [PATCH 5/5] add filename as structured json --- clang/lib/Sema/SemaTemplateInstantiate.cpp | 8 ++-- .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 8 ++-- clang/test/Driver/ftime-trace-sections.py | 5 +-- llvm/include/llvm/Support/TimeProfiler.h | 14 +++++++ llvm/lib/Support/TimeProfiler.cpp | 38 +++++++++++++++++-- 5 files changed, 57 insertions(+), 16 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 3105f3eefa0ba..71880641e33af 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3426,12 +3426,12 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation, return true; llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() { - std::string Name; - llvm::raw_string_ostream OS(Name); + llvm::TimeTraceMetadata M; + llvm::raw_string_ostream OS(M.Details); Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - OS << ", file:" << SourceMgr.getFilename(Instantiation->getLocation()); - return Name; + M.Filename = SourceMgr.getFilename(Instantiation->getLocation()); + return M; }); Pattern = PatternDef; diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 88390d82260f5..e3f12b37d45c7 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4962,12 +4962,12 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, } llvm::TimeTraceScope TimeScope("InstantiateFunction", [&]() { - std::string Name; - llvm::raw_string_ostream OS(Name); + llvm::TimeTraceMetadata M; + llvm::raw_string_ostream OS(M.Details); Function->getNameForDiagnostic(OS, getPrintingPolicy(), /*Qualified=*/true); - OS << ", file:" << SourceMgr.getFilename(Function->getLocation()); - return Name; + M.Filename = SourceMgr.getFilename(Function->getLocation()); + return M; }); // If we're performing recursive template instantiation, create our own diff --git a/clang/test/Driver/ftime-trace-sections.py b/clang/test/Driver/ftime-trace-sections.py index c42fa3c406ff4..ab05f90a1358d 100644 --- a/clang/test/Driver/ftime-trace-sections.py +++ b/clang/test/Driver/ftime-trace-sections.py @@ -17,11 +17,8 @@ def is_before(range1, range2): return b <= c -instantiation_pattern = re.compile("^.*<.*>, file:.*.cpp$") - - def is_valid_instantiation(instantiation): - return instantiation_pattern.match(instantiation["args"]["detail"]) + return instantiation["args"]["filename"].endswith(".cpp") log_contents = json.loads(sys.stdin.read()) diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h index 31f7df10916db..d669c0d2a5e23 100644 --- a/llvm/include/llvm/Support/TimeProfiler.h +++ b/llvm/include/llvm/Support/TimeProfiler.h @@ -83,6 +83,12 @@ namespace llvm { class raw_pwrite_stream; +struct TimeTraceMetadata { + std::string Details; + // Source file information for the event. + std::string Filename; +}; + struct TimeTraceProfiler; TimeTraceProfiler *getTimeTraceProfilerInstance(); @@ -128,6 +134,10 @@ TimeTraceProfilerEntry * timeTraceProfilerBegin(StringRef Name, llvm::function_ref<std::string()> Detail); +TimeTraceProfilerEntry * +timeTraceProfilerBegin(StringRef Name, + llvm::function_ref<TimeTraceMetadata()> MetaData); + /// Manually begin a time section, with the given \p Name and \p Detail. /// This starts Async Events having \p Name as a category which is shown /// separately from other traces. See @@ -164,6 +174,10 @@ class TimeTraceScope { if (getTimeTraceProfilerInstance() != nullptr) Entry = timeTraceProfilerBegin(Name, Detail); } + TimeTraceScope(StringRef Name, llvm::function_ref<TimeTraceMetadata()> Metadata) { + if (getTimeTraceProfilerInstance() != nullptr) + Entry = timeTraceProfilerBegin(Name, Metadata); + } ~TimeTraceScope() { if (getTimeTraceProfilerInstance() != nullptr) timeTraceProfilerEnd(Entry); diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp index 9612db7d30f98..4b0466b265db4 100644 --- a/llvm/lib/Support/TimeProfiler.cpp +++ b/llvm/lib/Support/TimeProfiler.cpp @@ -73,12 +73,20 @@ struct llvm::TimeTraceProfilerEntry { const TimePointType Start; TimePointType End; const std::string Name; - const std::string Detail; + TimeTraceMetadata Metadata; + const bool AsyncEvent = false; TimeTraceProfilerEntry(TimePointType &&S, TimePointType &&E, std::string &&N, std::string &&Dt, bool Ae) + : Start(std::move(S)), End(std::move(E)), Name(std::move(N)), Metadata(), + AsyncEvent(Ae) { + Metadata.Details = std::move(Dt); + } + + TimeTraceProfilerEntry(TimePointType &&S, TimePointType &&E, std::string &&N, + TimeTraceMetadata &&Mt, bool Ae) : Start(std::move(S)), End(std::move(E)), Name(std::move(N)), - Detail(std::move(Dt)), AsyncEvent(Ae) {} + Metadata(std::move(Mt)), AsyncEvent(Ae) {} // Calculate timings for FlameGraph. Cast time points to microsecond precision // rather than casting duration. This avoids truncation issues causing inner @@ -113,6 +121,15 @@ struct llvm::TimeTraceProfiler { return Stack.back().get(); } + TimeTraceProfilerEntry *begin(std::string Name, + llvm::function_ref<TimeTraceMetadata()>Metadata, + bool AsyncEvent = false) { + Stack.emplace_back(std::make_unique<TimeTraceProfilerEntry>( + ClockType::now(), TimePointType(), std::move(Name), Metadata(), + AsyncEvent)); + return Stack.back().get(); + } + void end() { assert(!Stack.empty() && "Must call begin() first"); end(*Stack.back()); @@ -184,8 +201,13 @@ struct llvm::TimeTraceProfiler { J.attribute("dur", DurUs); } J.attribute("name", E.Name); - if (!E.Detail.empty()) { - J.attributeObject("args", [&] { J.attribute("detail", E.Detail); }); + if (!E.Metadata.Details.empty()) { + J.attributeObject("args", + [&] { J.attribute("detail", E.Metadata.Details); }); + } + if (!E.Metadata.Filename.empty()) { + J.attributeObject( + "args", [&] { J.attribute("filename", E.Metadata.Filename); }); } }); @@ -381,6 +403,14 @@ llvm::timeTraceProfilerBegin(StringRef Name, return nullptr; } +TimeTraceProfilerEntry * +llvm::timeTraceProfilerBegin(StringRef Name, + llvm::function_ref<TimeTraceMetadata()> Metadata) { + if (TimeTraceProfilerInstance != nullptr) + return TimeTraceProfilerInstance->begin(std::string(Name), Metadata, false); + return nullptr; +} + TimeTraceProfilerEntry *llvm::timeTraceAsyncProfilerBegin(StringRef Name, StringRef Detail) { if (TimeTraceProfilerInstance != nullptr) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits