[clang] Record mainfile name in the Frontend time trace (PR #99866)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 07:51:02 PDT 2024


https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/99866

>From e0d76b97e386421ac2e653c206f49f827b24e65b Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Mon, 22 Jul 2024 12:16:48 +0000
Subject: [PATCH 1/2] Record mainfile name in the Frontend time trace

---
 clang/lib/Parse/ParseAST.cpp                 | 8 +++++++-
 clang/unittests/Support/TimeProfilerTest.cpp | 7 +++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Parse/ParseAST.cpp b/clang/lib/Parse/ParseAST.cpp
index 77ab3b556da58..53b0785b3e181 100644
--- a/clang/lib/Parse/ParseAST.cpp
+++ b/clang/lib/Parse/ParseAST.cpp
@@ -152,7 +152,13 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
   bool HaveLexer = S.getPreprocessor().getCurrentLexer();
 
   if (HaveLexer) {
-    llvm::TimeTraceScope TimeScope("Frontend");
+    llvm::TimeTraceScope TimeScope("Frontend", [&]() {
+      llvm::TimeTraceMetadata M;
+      const SourceManager &SM = S.getSourceManager();
+      if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))
+        M.File = FE->tryGetRealPathName();
+      return M;
+    });
     P.Initialize();
     Parser::DeclGroupPtrTy ADecl;
     Sema::ModuleImportState ImportState;
diff --git a/clang/unittests/Support/TimeProfilerTest.cpp b/clang/unittests/Support/TimeProfilerTest.cpp
index 56d880cffde61..9e2494da2bf35 100644
--- a/clang/unittests/Support/TimeProfilerTest.cpp
+++ b/clang/unittests/Support/TimeProfilerTest.cpp
@@ -81,7 +81,6 @@ std::string GetMetadata(json::Object *Event) {
   if (json::Object *Args = Event->getObject("args")) {
     if (auto Detail = Args->getString("detail"))
       OS << Detail;
-    // Use only filename to not include os-specific path separators.
     if (auto File = Args->getString("file"))
       OS << ", " << llvm::sys::path::filename(*File);
     if (auto Line = Args->getInteger("line"))
@@ -209,7 +208,7 @@ constexpr int slow_init_list[] = {1, 1, 2, 3, 5, 8, 13, 21}; // 25th line
   ASSERT_TRUE(compileFromString(Code, "-std=c++20", "test.cc"));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend
+Frontend (, test.cc)
 | ParseDeclarationOrFunctionDefinition (test.cc:2:1)
 | ParseDeclarationOrFunctionDefinition (test.cc:6:1)
 | | ParseFunctionDefinition (slow_func)
@@ -266,7 +265,7 @@ TEST(TimeProfilerTest, TemplateInstantiations) {
                                 /*Headers=*/{{"a.h", A_H}, {"b.h", B_H}}));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend
+Frontend (, test.cc)
 | ParseFunctionDefinition (fooB)
 | ParseFunctionDefinition (fooMTA)
 | ParseFunctionDefinition (fooA)
@@ -291,7 +290,7 @@ struct {
   ASSERT_TRUE(compileFromString(Code, "-std=c99", "test.c"));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend
+Frontend (, test.c)
 | ParseDeclarationOrFunctionDefinition (test.c:2:1)
 | | isIntegerConstantExpr (<test.c:3:18>)
 | | EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)

>From 18851b286297edb8ccc7bbadc2ec1c1dde1ab856 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Mon, 22 Jul 2024 14:50:18 +0000
Subject: [PATCH 2/2] address comments

---
 clang/lib/Parse/ParseAST.cpp                 |  8 +++++---
 clang/unittests/Support/TimeProfilerTest.cpp | 15 ++++++++-------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Parse/ParseAST.cpp b/clang/lib/Parse/ParseAST.cpp
index 53b0785b3e181..e008cc0e38ced 100644
--- a/clang/lib/Parse/ParseAST.cpp
+++ b/clang/lib/Parse/ParseAST.cpp
@@ -154,9 +154,11 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
   if (HaveLexer) {
     llvm::TimeTraceScope TimeScope("Frontend", [&]() {
       llvm::TimeTraceMetadata M;
-      const SourceManager &SM = S.getSourceManager();
-      if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))
-        M.File = FE->tryGetRealPathName();
+      if (llvm::isTimeTraceVerbose()) {
+        const SourceManager &SM = S.getSourceManager();
+        if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))
+          M.File = FE->tryGetRealPathName();
+      }
       return M;
     });
     P.Initialize();
diff --git a/clang/unittests/Support/TimeProfilerTest.cpp b/clang/unittests/Support/TimeProfilerTest.cpp
index 9e2494da2bf35..f53fe71d630bf 100644
--- a/clang/unittests/Support/TimeProfilerTest.cpp
+++ b/clang/unittests/Support/TimeProfilerTest.cpp
@@ -76,17 +76,18 @@ bool compileFromString(StringRef Code, StringRef Standard, StringRef File,
 }
 
 std::string GetMetadata(json::Object *Event) {
-  std::string Metadata;
-  llvm::raw_string_ostream OS(Metadata);
+  std::string M;
+  llvm::raw_string_ostream OS(M);
   if (json::Object *Args = Event->getObject("args")) {
     if (auto Detail = Args->getString("detail"))
       OS << Detail;
+    // Use only filename to not include os-specific path separators.
     if (auto File = Args->getString("file"))
-      OS << ", " << llvm::sys::path::filename(*File);
+      OS << (M.empty() ? "" : ", ") << llvm::sys::path::filename(*File);
     if (auto Line = Args->getInteger("line"))
       OS << ":" << *Line;
   }
-  return Metadata;
+  return M;
 }
 
 // Returns pretty-printed trace graph.
@@ -208,7 +209,7 @@ constexpr int slow_init_list[] = {1, 1, 2, 3, 5, 8, 13, 21}; // 25th line
   ASSERT_TRUE(compileFromString(Code, "-std=c++20", "test.cc"));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend (, test.cc)
+Frontend (test.cc)
 | ParseDeclarationOrFunctionDefinition (test.cc:2:1)
 | ParseDeclarationOrFunctionDefinition (test.cc:6:1)
 | | ParseFunctionDefinition (slow_func)
@@ -265,7 +266,7 @@ TEST(TimeProfilerTest, TemplateInstantiations) {
                                 /*Headers=*/{{"a.h", A_H}, {"b.h", B_H}}));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend (, test.cc)
+Frontend (test.cc)
 | ParseFunctionDefinition (fooB)
 | ParseFunctionDefinition (fooMTA)
 | ParseFunctionDefinition (fooA)
@@ -290,7 +291,7 @@ struct {
   ASSERT_TRUE(compileFromString(Code, "-std=c99", "test.c"));
   std::string Json = teardownProfiler();
   ASSERT_EQ(R"(
-Frontend (, test.c)
+Frontend (test.c)
 | ParseDeclarationOrFunctionDefinition (test.c:2:1)
 | | isIntegerConstantExpr (<test.c:3:18>)
 | | EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)



More information about the cfe-commits mailing list