[clang] [time-trace] Add a new time trace scope variable named "ParseDeclarationOrFunctionDefinition". (PR #65268)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 4 08:07:21 PDT 2023
https://github.com/MaggieYingYi created https://github.com/llvm/llvm-project/pull/65268:
When profiling code using `-ftime-trace` with the default `time-trace-granularity` value (500 microseconds), in some code there is a large empty timeline in the flame chart profiling view (using Chrome Tracing view). If you pass `-ftime-trace-granularity=0` in order to show all time traces, the empty timeline consists of a large number of small time scopes named `EvaluateAsConstExpr`. This change adds an enclosing time trace scope for the function `Parser::ParseDeclarationOrFunctionDefinition` to record the time spent parsing the function's declaration or definition.
>From a9552dca6f9429ccbd1033f3e2e39c26775988f6 Mon Sep 17 00:00:00 2001
From: Ying Yi <maggieyi666 at gmail.com>
Date: Fri, 1 Sep 2023 15:30:44 +0100
Subject: [PATCH] - Add a new time trace scope variable named
"ParseDeclarationOrFunctionDefinition".
When profiling code using `-ftime-trace` with the default `time-trace-granularity` value (500 microseconds), in some code there is a large empty timeline in the flame chart profiling view (using Chrome Tracing view). If you pass `-ftime-trace-granularity=0` in order to show all time traces, the empty timeline consists of a large number of small time scopes named `EvaluateAsConstExpr`. This change adds an enclosing time trace scope for the function `Parser::ParseDeclarationOrFunctionDefinition` to record the time spent parsing the function's declaration or definition.
---
clang/lib/Parse/Parser.cpp | 6 ++-
...e-ParseDeclarationOrFunctionDefinition.cpp | 10 +++++
clang/unittests/Support/TimeProfilerTest.cpp | 40 +++++++++++--------
3 files changed, 38 insertions(+), 18 deletions(-)
create mode 100644 clang/test/Driver/check-time-trace-ParseDeclarationOrFunctionDefinition.cpp
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 09215b8303ecf9c..285ff2f2f8f56d2 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -13,8 +13,8 @@
#include "clang/Parse/Parser.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
-#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ASTLambda.h"
+#include "clang/AST/DeclTemplate.h"
#include "clang/Basic/FileManager.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/RAIIObjectsForParser.h"
@@ -22,6 +22,7 @@
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/Scope.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/TimeProfiler.h"
using namespace clang;
@@ -1224,6 +1225,9 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition(
ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
ParsingDeclSpec *DS, AccessSpecifier AS) {
+ // Add an enclosing time trace scope for a bunch of small scopes with
+ // "EvaluateAsConstExpr".
+ llvm::TimeTraceScope TimeScope("ParseDeclarationOrFunctionDefinition");
if (DS) {
return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS);
} else {
diff --git a/clang/test/Driver/check-time-trace-ParseDeclarationOrFunctionDefinition.cpp b/clang/test/Driver/check-time-trace-ParseDeclarationOrFunctionDefinition.cpp
new file mode 100644
index 000000000000000..be1cc51cc6669f3
--- /dev/null
+++ b/clang/test/Driver/check-time-trace-ParseDeclarationOrFunctionDefinition.cpp
@@ -0,0 +1,10 @@
+// RUN: %clangxx -S -ftime-trace -ftime-trace-granularity=0 -o %T/check-time-trace-ParseDeclarationOrFunctionDefinition %s
+// RUN: cat %T/check-time-trace-ParseDeclarationOrFunctionDefinition.json \
+// RUN: | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
+// RUN: | FileCheck %s
+
+// CHECK: "name": "ParseDeclarationOrFunctionDefinition"
+
+template <typename T>
+void foo(T) {}
+void bar() { foo(0); }
diff --git a/clang/unittests/Support/TimeProfilerTest.cpp b/clang/unittests/Support/TimeProfilerTest.cpp
index fdfbbfe4e3a9dff..13e06cc465cf916 100644
--- a/clang/unittests/Support/TimeProfilerTest.cpp
+++ b/clang/unittests/Support/TimeProfilerTest.cpp
@@ -177,21 +177,26 @@ constexpr int slow_init_list[] = {1, 1, 2, 3, 5, 8, 13, 21}; // 25th line
std::string TraceGraph = buildTraceGraph(Json);
ASSERT_TRUE(TraceGraph == R"(
Frontend
-| EvaluateAsRValue (<test.cc:8:21>)
-| EvaluateForOverflow (<test.cc:8:21, col:25>)
-| EvaluateForOverflow (<test.cc:8:30, col:32>)
-| EvaluateAsRValue (<test.cc:9:14>)
-| EvaluateForOverflow (<test.cc:9:9, col:14>)
-| isPotentialConstantExpr (slow_namespace::slow_func)
-| EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
-| | EvaluateAsRValue (<test.cc:8:21, col:25>)
-| EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
-| | EvaluateAsRValue (<test.cc:8:21, col:25>)
-| EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr (<test.cc:17:33, col:59>)
-| EvaluateAsConstantExpr (<test.cc:18:11, col:37>)
-| EvaluateAsRValue (<test.cc:22:14, line:23:58>)
-| EvaluateAsInitializer (slow_init_list)
+| ParseDeclarationOrFunctionDefinition
+| ParseDeclarationOrFunctionDefinition
+| | EvaluateAsRValue (<test.cc:8:21>)
+| | EvaluateForOverflow (<test.cc:8:21, col:25>)
+| | EvaluateForOverflow (<test.cc:8:30, col:32>)
+| | EvaluateAsRValue (<test.cc:9:14>)
+| | EvaluateForOverflow (<test.cc:9:9, col:14>)
+| | isPotentialConstantExpr (slow_namespace::slow_func)
+| | EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
+| | | EvaluateAsRValue (<test.cc:8:21, col:25>)
+| | EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
+| | | EvaluateAsRValue (<test.cc:8:21, col:25>)
+| ParseDeclarationOrFunctionDefinition
+| | EvaluateAsInitializer (slow_value)
+| | EvaluateAsConstantExpr (<test.cc:17:33, col:59>)
+| | EvaluateAsConstantExpr (<test.cc:18:11, col:37>)
+| ParseDeclarationOrFunctionDefinition
+| | EvaluateAsRValue (<test.cc:22:14, line:23:58>)
+| ParseDeclarationOrFunctionDefinition
+| | EvaluateAsInitializer (slow_init_list)
| PerformPendingInstantiations
)");
@@ -212,8 +217,9 @@ struct {
std::string TraceGraph = buildTraceGraph(Json);
ASSERT_TRUE(TraceGraph == R"(
Frontend
-| isIntegerConstantExpr (<test.c:3:18>)
-| EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)
+| ParseDeclarationOrFunctionDefinition
+| | isIntegerConstantExpr (<test.c:3:18>)
+| | EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)
| PerformPendingInstantiations
)");
More information about the cfe-commits
mailing list