Skip to content

Commit 33b8586

Browse files
committed
Add two time-trace scope variables.
A time trace scope variable of `ParseDeclarationOrFunctionDefinition` with the function's source location is added to record the time spent parsing the function's declaration or definition. Another time trace scope variable of `ParseFunctionDefinition` is also added to record the name of the defined function. A release note is added as well. Reviewed by: Aaron Ballman Pull request: llvm#65268
1 parent 61b9176 commit 33b8586

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ Improvements to Clang's diagnostics
427427
(or, more commonly, ``NULL`` when the platform defines it as ``__null``) to be more consistent
428428
with GCC.
429429

430+
Improvements to Clang's time-trace
431+
----------------------------------
432+
- Two time-trace scope variables are added. A time trace scope variable of
433+
``ParseDeclarationOrFunctionDefinition`` with the function's source location
434+
is added to record the time spent parsing the function's declaration or
435+
definition. Another time trace scope variable of ``ParseFunctionDefinition``
436+
is also added to record the name of the defined function.
437+
430438
Bug Fixes in This Version
431439
-------------------------
432440
- Fixed an issue where a class template specialization whose declaration is

clang/lib/Parse/Parser.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
#include "clang/Parse/Parser.h"
1414
#include "clang/AST/ASTConsumer.h"
1515
#include "clang/AST/ASTContext.h"
16-
#include "clang/AST/DeclTemplate.h"
1716
#include "clang/AST/ASTLambda.h"
17+
#include "clang/AST/DeclTemplate.h"
1818
#include "clang/Basic/FileManager.h"
1919
#include "clang/Parse/ParseDiagnostic.h"
2020
#include "clang/Parse/RAIIObjectsForParser.h"
2121
#include "clang/Sema/DeclSpec.h"
2222
#include "clang/Sema/ParsedTemplate.h"
2323
#include "clang/Sema/Scope.h"
2424
#include "llvm/Support/Path.h"
25+
#include "llvm/Support/TimeProfiler.h"
2526
using namespace clang;
2627

2728

@@ -1229,6 +1230,13 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal(
12291230
Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition(
12301231
ParsedAttributes &Attrs, ParsedAttributes &DeclSpecAttrs,
12311232
ParsingDeclSpec *DS, AccessSpecifier AS) {
1233+
// Add an enclosing time trace scope for a bunch of small scopes with
1234+
// "EvaluateAsConstExpr".
1235+
llvm::TimeTraceScope TimeScope(
1236+
"ParseDeclarationOrFunctionDefinition",
1237+
Tok.getLocation().printToString(
1238+
Actions.getASTContext().getSourceManager()));
1239+
12321240
if (DS) {
12331241
return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS);
12341242
} else {
@@ -1259,6 +1267,10 @@ Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition(
12591267
Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
12601268
const ParsedTemplateInfo &TemplateInfo,
12611269
LateParsedAttrList *LateParsedAttrs) {
1270+
llvm::TimeTraceScope TimeScope(
1271+
"ParseFunctionDefinition",
1272+
Actions.GetNameForDeclarator(D).getName().getAsString());
1273+
12621274
// Poison SEH identifiers so they are flagged as illegal in function bodies.
12631275
PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
12641276
const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clangxx -S -ftime-trace -ftime-trace-granularity=0 -o %T/check-time-trace-ParseDeclarationOrFunctionDefinition %s
2+
// RUN: cat %T/check-time-trace-ParseDeclarationOrFunctionDefinition.json \
3+
// RUN: | %python -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
4+
// RUN: | FileCheck %s
5+
6+
// CHECK-DAG: "name": "ParseDeclarationOrFunctionDefinition"
7+
// CHECK-DAG: "detail": "{{.*}}check-time-trace-ParseDeclarationOrFunctionDefinition.cpp:15:1"
8+
// CHECK-DAG: "name": "ParseFunctionDefinition"
9+
// CHECK-DAG: "detail": "foo"
10+
// CHECK-DAG: "name": "ParseFunctionDefinition"
11+
// CHECK-DAG: "detail": "bar"
12+
13+
template <typename T>
14+
void foo(T) {}
15+
void bar() { foo(0); }

clang/unittests/Support/TimeProfilerTest.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,29 @@ constexpr int slow_init_list[] = {1, 1, 2, 3, 5, 8, 13, 21}; // 25th line
177177
std::string TraceGraph = buildTraceGraph(Json);
178178
ASSERT_TRUE(TraceGraph == R"(
179179
Frontend
180-
| EvaluateAsRValue (<test.cc:8:21>)
181-
| EvaluateForOverflow (<test.cc:8:21, col:25>)
182-
| EvaluateForOverflow (<test.cc:8:30, col:32>)
183-
| EvaluateAsRValue (<test.cc:9:14>)
184-
| EvaluateForOverflow (<test.cc:9:9, col:14>)
185-
| isPotentialConstantExpr (slow_namespace::slow_func)
186-
| EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
187-
| | EvaluateAsRValue (<test.cc:8:21, col:25>)
188-
| EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
189-
| | EvaluateAsRValue (<test.cc:8:21, col:25>)
190-
| EvaluateAsInitializer (slow_value)
191-
| EvaluateAsConstantExpr (<test.cc:17:33, col:59>)
192-
| EvaluateAsConstantExpr (<test.cc:18:11, col:37>)
193-
| EvaluateAsConstantExpr (<test.cc:23:31, col:57>)
194-
| EvaluateAsRValue (<test.cc:22:14, line:23:58>)
195-
| EvaluateAsInitializer (slow_init_list)
180+
| ParseDeclarationOrFunctionDefinition (test.cc:2:1)
181+
| ParseDeclarationOrFunctionDefinition (test.cc:6:1)
182+
| | ParseFunctionDefinition (slow_func)
183+
| | | EvaluateAsRValue (<test.cc:8:21>)
184+
| | | EvaluateForOverflow (<test.cc:8:21, col:25>)
185+
| | | EvaluateForOverflow (<test.cc:8:30, col:32>)
186+
| | | EvaluateAsRValue (<test.cc:9:14>)
187+
| | | EvaluateForOverflow (<test.cc:9:9, col:14>)
188+
| | | isPotentialConstantExpr (slow_namespace::slow_func)
189+
| | | EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
190+
| | | | EvaluateAsRValue (<test.cc:8:21, col:25>)
191+
| | | EvaluateAsBooleanCondition (<test.cc:8:21, col:25>)
192+
| | | | EvaluateAsRValue (<test.cc:8:21, col:25>)
193+
| ParseDeclarationOrFunctionDefinition (test.cc:16:1)
194+
| | ParseFunctionDefinition (slow_test)
195+
| | | EvaluateAsInitializer (slow_value)
196+
| | | EvaluateAsConstantExpr (<test.cc:17:33, col:59>)
197+
| | | EvaluateAsConstantExpr (<test.cc:18:11, col:37>)
198+
| ParseDeclarationOrFunctionDefinition (test.cc:22:1)
199+
| | EvaluateAsConstantExpr (<test.cc:23:31, col:57>)
200+
| | EvaluateAsRValue (<test.cc:22:14, line:23:58>)
201+
| ParseDeclarationOrFunctionDefinition (test.cc:25:1)
202+
| | EvaluateAsInitializer (slow_init_list)
196203
| PerformPendingInstantiations
197204
)");
198205

@@ -213,8 +220,9 @@ struct {
213220
std::string TraceGraph = buildTraceGraph(Json);
214221
ASSERT_TRUE(TraceGraph == R"(
215222
Frontend
216-
| isIntegerConstantExpr (<test.c:3:18>)
217-
| EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)
223+
| ParseDeclarationOrFunctionDefinition (test.c:2:1)
224+
| | isIntegerConstantExpr (<test.c:3:18>)
225+
| | EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)
218226
| PerformPendingInstantiations
219227
)");
220228

0 commit comments

Comments
 (0)