Skip to content

Commit 3f3a257

Browse files
[Support][Time profiler] Make FE codegen blocks to be inside frontend blocks
Summary: Add `Frontend` time trace entry to `HandleTranslationUnit()` function. Add test to check all codegen blocks are inside frontend blocks. Also, change `--time-trace-granularity` option a bit to make sure very small time blocks are outputed to json-file when using `--time-trace-granularity=0`. This fixes http://llvm.org/pr41969 Reviewers: russell.gallop, lebedev.ri, thakis Reviewed By: russell.gallop Subscribers: vsapsai, aras-p, lebedev.ri, hiraditya, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D63325 llvm-svn: 369308
1 parent 2e8b575 commit 3f3a257

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Pass.h"
3939
#include "llvm/Support/MemoryBuffer.h"
4040
#include "llvm/Support/SourceMgr.h"
41+
#include "llvm/Support/TimeProfiler.h"
4142
#include "llvm/Support/Timer.h"
4243
#include "llvm/Support/ToolOutputFile.h"
4344
#include "llvm/Support/YAMLTraits.h"
@@ -229,6 +230,7 @@ namespace clang {
229230

230231
void HandleTranslationUnit(ASTContext &C) override {
231232
{
233+
llvm::TimeTraceScope TimeScope("Frontend", StringRef(""));
232234
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
233235
if (FrontendTimesIsEnabled) {
234236
LLVMIRGenerationRefCount += 1;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// REQUIRES: shell
2+
// RUN: %clangxx -S -ftime-trace -ftime-trace-granularity=0 -o %T/check-time-trace-sections %s
3+
// RUN: cat %T/check-time-trace-sections.json | %python %S/check-time-trace-sections.py
4+
5+
template <typename T>
6+
void foo(T) {}
7+
void bar() { foo(0); }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
import json, sys
4+
5+
def is_inside(range1, range2):
6+
a = range1["ts"]; b = a + range1["dur"]
7+
c = range2["ts"]; d = c + range2["dur"]
8+
return (a >= c and a <= d) and (b >= c and b <= d)
9+
10+
def is_before(range1, range2):
11+
b = range1["ts"] + range1["dur"]; c = range2["ts"]
12+
return b <= c
13+
14+
events = json.loads(sys.stdin.read())["traceEvents"]
15+
codegens = filter(lambda x: x["name"] == "CodeGen Function", events)
16+
frontends = filter(lambda x: x["name"] == "Frontend", events)
17+
backends = filter(lambda x: x["name"] == "Backend", events)
18+
19+
if not all([any([is_inside(codegen, frontend) for frontend in frontends])
20+
for codegen in codegens]):
21+
sys.exit("Not all CodeGen sections are inside any Frontend section!")
22+
23+
if not all([all([is_before(frontend, backend) for frontend in frontends])
24+
for backend in backends]):
25+
sys.exit("Not all Frontend section are before all Backend sections!")

llvm/lib/Support/TimeProfiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ struct TimeTraceProfiler {
5858
auto &E = Stack.back();
5959
E.Duration = steady_clock::now() - E.Start;
6060

61-
// Only include sections longer than TimeTraceGranularity msec.
62-
if (duration_cast<microseconds>(E.Duration).count() > TimeTraceGranularity)
61+
// Only include sections longer or equal to TimeTraceGranularity msec.
62+
if (duration_cast<microseconds>(E.Duration).count() >= TimeTraceGranularity)
6363
Entries.emplace_back(E);
6464

6565
// Track total time taken by each "name", but only the topmost levels of

0 commit comments

Comments
 (0)