Skip to content

Commit 0de18e7

Browse files
authored
-ftime-report: reorganize timers
The code generation time is unclear in the -ftime-report output: * The two clang timers "Code Generation Time" and "LLVM IR Generation Time" are in the default group "Miscellaneous Ungrouped Timers". * There is also a "Clang front-end time" group, which actually includes code generation time. ``` ===-------------------------------------------------------------------------=== Miscellaneous Ungrouped Timers ===-------------------------------------------------------------------------=== ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name --- 0.0611 ( 1.7%) 0.0099 ( 4.4%) 0.0710 ( 1.9%) 0.0713 ( 1.9%) LLVM IR Generation Time 3.5140 ( 98.3%) 0.2165 ( 95.6%) 3.7306 ( 98.1%) 3.7342 ( 98.1%) Code Generation Time 3.5751 (100.0%) 0.2265 (100.0%) 3.8016 (100.0%) 3.8055 (100.0%) Total ... ===-------------------------------------------------------------------------=== Clang front-end time report ===-------------------------------------------------------------------------=== Total Execution Time: 3.9108 seconds (3.9146 wall clock) ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name --- 3.6802 (100.0%) 0.2306 (100.0%) 3.9108 (100.0%) 3.9146 (100.0%) Clang front-end timer 3.6802 (100.0%) 0.2306 (100.0%) 3.9108 (100.0%) 3.9146 (100.0%) Total ``` This patch * renames "Clang front-end time report" (FrontendAction time) to "Clang time report", * renames "Clang front-end" to "Front end", * moves "LLVM IR Generation" into the group, * replaces "Code Generation time" with "Optimizer" (middle end) and "Machine code generation" (back end). ``` % clang -c sqlite3.i -w -ftime-report -mllvm -sort-timers=0 ... ===-------------------------------------------------------------------------=== Clang time report ===-------------------------------------------------------------------------=== Total Execution Time: 1.5922 seconds (1.5972 wall clock) ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name --- 0.5107 ( 35.9%) 0.0105 ( 6.2%) 0.5211 ( 32.7%) 0.5222 ( 32.7%) Front end 0.2464 ( 17.3%) 0.0340 ( 20.0%) 0.2804 ( 17.6%) 0.2814 ( 17.6%) LLVM IR generation 0.6240 ( 43.9%) 0.1235 ( 72.7%) 0.7475 ( 47.0%) 0.7503 ( 47.0%) Machine code generation 0.0413 ( 2.9%) 0.0018 ( 1.0%) 0.0431 ( 2.7%) 0.0433 ( 2.7%) Optimizer 1.4224 (100.0%) 0.1698 (100.0%) 1.5922 (100.0%) 1.5972 (100.0%) Total ``` Pull Request: llvm#122225
1 parent df808df commit 0de18e7

File tree

8 files changed

+62
-52
lines changed

8 files changed

+62
-52
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ Non-comprehensive list of changes in this release
445445
- Matrix types (a Clang extension) can now be used in pseudo-destructor expressions,
446446
which allows them to be stored in STL containers.
447447

448+
- In the ``-ftime-report`` output, the new "Clang time report" group replaces
449+
the old "Clang front-end time report" and includes "Front end", "LLVM IR
450+
generation", "Optimizer", and "Machine code generation".
451+
448452
New Compiler Flags
449453
------------------
450454

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class CompilerInstance : public ModuleLoader {
118118
std::unique_ptr<Sema> TheSema;
119119

120120
/// The frontend timer group.
121-
std::unique_ptr<llvm::TimerGroup> FrontendTimerGroup;
121+
std::unique_ptr<llvm::TimerGroup> timerGroup;
122122

123123
/// The frontend timer.
124124
std::unique_ptr<llvm::Timer> FrontendTimer;
@@ -630,6 +630,8 @@ class CompilerInstance : public ModuleLoader {
630630
/// @name Frontend timer
631631
/// @{
632632

633+
llvm::TimerGroup &getTimerGroup() const { return *timerGroup; }
634+
633635
bool hasFrontendTimer() const { return (bool)FrontendTimer; }
634636

635637
llvm::Timer &getFrontendTimer() const {

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/Frontend/FrontendDiagnostic.h"
1717
#include "clang/Frontend/Utils.h"
1818
#include "clang/Lex/HeaderSearchOptions.h"
19+
#include "llvm/ADT/ScopeExit.h"
1920
#include "llvm/ADT/StringExtras.h"
2021
#include "llvm/ADT/StringSwitch.h"
2122
#include "llvm/Analysis/GlobalsModRef.h"
@@ -137,8 +138,6 @@ class EmitAssemblyHelper {
137138
llvm::Module *TheModule;
138139
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
139140

140-
Timer CodeGenerationTime;
141-
142141
std::unique_ptr<raw_pwrite_stream> OS;
143142

144143
Triple TargetTriple;
@@ -208,7 +207,6 @@ class EmitAssemblyHelper {
208207
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
209208
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
210209
TheModule(M), VFS(std::move(VFS)),
211-
CodeGenerationTime("codegen", "Code Generation Time"),
212210
TargetTriple(TheModule->getTargetTriple()) {}
213211

214212
~EmitAssemblyHelper() {
@@ -1157,7 +1155,14 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11571155
{
11581156
PrettyStackTraceString CrashInfo("Optimizer");
11591157
llvm::TimeTraceScope TimeScope("Optimizer");
1158+
Timer timer;
1159+
if (CI.getCodeGenOpts().TimePasses) {
1160+
timer.init("optimizer", "Optimizer", CI.getTimerGroup());
1161+
CI.getFrontendTimer().yieldTo(timer);
1162+
}
11601163
MPM.run(*TheModule, MAM);
1164+
if (CI.getCodeGenOpts().TimePasses)
1165+
timer.yieldTo(CI.getFrontendTimer());
11611166
}
11621167
}
11631168

@@ -1200,14 +1205,20 @@ void EmitAssemblyHelper::RunCodegenPipeline(
12001205
{
12011206
PrettyStackTraceString CrashInfo("Code generation");
12021207
llvm::TimeTraceScope TimeScope("CodeGenPasses");
1208+
Timer timer;
1209+
if (CI.getCodeGenOpts().TimePasses) {
1210+
timer.init("codegen", "Machine code generation", CI.getTimerGroup());
1211+
CI.getFrontendTimer().yieldTo(timer);
1212+
}
12031213
CodeGenPasses.run(*TheModule);
1214+
if (CI.getCodeGenOpts().TimePasses)
1215+
timer.yieldTo(CI.getFrontendTimer());
12041216
}
12051217
}
12061218

12071219
void EmitAssemblyHelper::emitAssembly(BackendAction Action,
12081220
std::unique_ptr<raw_pwrite_stream> OS,
12091221
BackendConsumer *BC) {
1210-
TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
12111222
setCommandLineOpts(CodeGenOpts);
12121223

12131224
bool RequiresCodeGen = actionRequiresCodeGen(Action);

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,16 @@ BackendConsumer::BackendConsumer(CompilerInstance &CI, BackendAction Action,
115115
llvm::Module *CurLinkModule)
116116
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
117117
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
118-
AsmOutStream(std::move(OS)), FS(VFS),
119-
LLVMIRGeneration("irgen", "LLVM IR Generation Time"), Action(Action),
118+
AsmOutStream(std::move(OS)), FS(VFS), Action(Action),
120119
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(VFS),
121120
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
122121
CI.getCodeGenOpts(), C, CoverageInfo)),
123122
LinkModules(std::move(LinkModules)), CurLinkModule(CurLinkModule) {
124123
TimerIsEnabled = CodeGenOpts.TimePasses;
125124
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
126125
llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
126+
if (CodeGenOpts.TimePasses)
127+
LLVMIRGeneration.init("irgen", "LLVM IR generation", CI.getTimerGroup());
127128
}
128129

129130
llvm::Module* BackendConsumer::getModule() const {
@@ -162,19 +163,13 @@ bool BackendConsumer::HandleTopLevelDecl(DeclGroupRef D) {
162163
"LLVM IR generation of declaration");
163164

164165
// Recurse.
165-
if (TimerIsEnabled) {
166-
LLVMIRGenerationRefCount += 1;
167-
if (LLVMIRGenerationRefCount == 1)
168-
LLVMIRGeneration.startTimer();
169-
}
166+
if (TimerIsEnabled && !LLVMIRGenerationRefCount++)
167+
CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
170168

171169
Gen->HandleTopLevelDecl(D);
172170

173-
if (TimerIsEnabled) {
174-
LLVMIRGenerationRefCount -= 1;
175-
if (LLVMIRGenerationRefCount == 0)
176-
LLVMIRGeneration.stopTimer();
177-
}
171+
if (TimerIsEnabled && !--LLVMIRGenerationRefCount)
172+
LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
178173

179174
return true;
180175
}
@@ -184,12 +179,12 @@ void BackendConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
184179
Context->getSourceManager(),
185180
"LLVM IR generation of inline function");
186181
if (TimerIsEnabled)
187-
LLVMIRGeneration.startTimer();
182+
CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
188183

189184
Gen->HandleInlineFunctionDefinition(D);
190185

191186
if (TimerIsEnabled)
192-
LLVMIRGeneration.stopTimer();
187+
LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
193188
}
194189

195190
void BackendConsumer::HandleInterestingDecl(DeclGroupRef D) {
@@ -239,19 +234,13 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
239234
{
240235
llvm::TimeTraceScope TimeScope("Frontend");
241236
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
242-
if (TimerIsEnabled) {
243-
LLVMIRGenerationRefCount += 1;
244-
if (LLVMIRGenerationRefCount == 1)
245-
LLVMIRGeneration.startTimer();
246-
}
237+
if (TimerIsEnabled && !LLVMIRGenerationRefCount++)
238+
CI.getFrontendTimer().yieldTo(LLVMIRGeneration);
247239

248240
Gen->HandleTranslationUnit(C);
249241

250-
if (TimerIsEnabled) {
251-
LLVMIRGenerationRefCount -= 1;
252-
if (LLVMIRGenerationRefCount == 0)
253-
LLVMIRGeneration.stopTimer();
254-
}
242+
if (TimerIsEnabled && !--LLVMIRGenerationRefCount)
243+
LLVMIRGeneration.yieldTo(CI.getFrontendTimer());
255244

256245
IRGenFinished = true;
257246
}

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,8 @@ void CompilerInstance::createCodeCompletionConsumer() {
722722
}
723723

724724
void CompilerInstance::createFrontendTimer() {
725-
FrontendTimerGroup.reset(
726-
new llvm::TimerGroup("frontend", "Clang front-end time report"));
727-
FrontendTimer.reset(
728-
new llvm::Timer("frontend", "Clang front-end timer",
729-
*FrontendTimerGroup));
725+
timerGroup.reset(new llvm::TimerGroup("clang", "Clang time report"));
726+
FrontendTimer.reset(new llvm::Timer("frontend", "Front end", *timerGroup));
730727
}
731728

732729
CodeCompleteConsumer *
@@ -1726,10 +1723,9 @@ void CompilerInstance::createASTReader() {
17261723
const FrontendOptions &FEOpts = getFrontendOpts();
17271724
std::unique_ptr<llvm::Timer> ReadTimer;
17281725

1729-
if (FrontendTimerGroup)
1726+
if (timerGroup)
17301727
ReadTimer = std::make_unique<llvm::Timer>("reading_modules",
1731-
"Reading modules",
1732-
*FrontendTimerGroup);
1728+
"Reading modules", *timerGroup);
17331729
TheASTReader = new ASTReader(
17341730
getPreprocessor(), getModuleCache(), &getASTContext(),
17351731
getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions,
@@ -1758,10 +1754,10 @@ void CompilerInstance::createASTReader() {
17581754
bool CompilerInstance::loadModuleFile(
17591755
StringRef FileName, serialization::ModuleFile *&LoadedModuleFile) {
17601756
llvm::Timer Timer;
1761-
if (FrontendTimerGroup)
1757+
if (timerGroup)
17621758
Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
1763-
*FrontendTimerGroup);
1764-
llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1759+
*timerGroup);
1760+
llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
17651761

17661762
// If we don't already have an ASTReader, create one now.
17671763
if (!TheASTReader)
@@ -1892,10 +1888,10 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
18921888

18931889
// Time how long it takes to load the module.
18941890
llvm::Timer Timer;
1895-
if (FrontendTimerGroup)
1891+
if (timerGroup)
18961892
Timer.init("loading." + ModuleFilename, "Loading " + ModuleFilename,
1897-
*FrontendTimerGroup);
1898-
llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1893+
*timerGroup);
1894+
llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
18991895
llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
19001896

19011897
// Try to load the module file. If we are not trying to load from the

clang/test/Frontend/ftime-report-template-decl.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// RUN: %clang_cc1 %s -emit-llvm -o - -ftime-report 2>&1 | FileCheck %s
2-
// RUN: %clang_cc1 %s -emit-llvm -o - -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING -ftime-report 2>&1 | FileCheck %s
1+
// REQUIRES: x86-registered-target
2+
// RUN: %clang_cc1 %s -S -triple=x86_64 -mllvm -sort-timers=0 -o - -ftime-report 2>&1 | FileCheck %s
3+
// RUN: %clang_cc1 %s -S -triple=x86_64 -mllvm -sort-timers=0 -o - -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING -ftime-report 2>&1 | FileCheck %s
34

45
// Template function declarations
56
template <typename T>
@@ -150,10 +151,9 @@ struct _Wrap_alloc {
150151
};
151152
_Wrap_alloc<int>::rebind<int> w;
152153

153-
// CHECK: Miscellaneous Ungrouped Timers
154-
// CHECK-DAG: LLVM IR Generation Time
155-
// CHECK-DAG: Code Generation Time
156-
// CHECK: Total
157-
// CHECK: Clang front-end time report
158-
// CHECK: Clang front-end timer
159-
// CHECK: Total
154+
// CHECK: Clang time report
155+
// CHECK: Front end
156+
// CHECK-NEXT: LLVM IR generation
157+
// CHECK-NEXT: Machine code generation
158+
// CHECK-NEXT: Optimizer
159+
// CHECK-NEXT: Total

llvm/include/llvm/Support/Timer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class Timer {
131131
/// Clear the timer state.
132132
void clear();
133133

134+
/// Stop the timer and start another timer.
135+
void yieldTo(Timer &);
136+
134137
/// Return the duration for which this timer has been running.
135138
TimeRecord getTotalTime() const { return Time; }
136139

llvm/lib/Support/Timer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ void Timer::clear() {
166166
Time = StartTime = TimeRecord();
167167
}
168168

169+
void Timer::yieldTo(Timer &O) {
170+
stopTimer();
171+
O.startTimer();
172+
}
173+
169174
static void printVal(double Val, double Total, raw_ostream &OS) {
170175
if (Total < 1e-7) // Avoid dividing by zero.
171176
OS << " ----- ";

0 commit comments

Comments
 (0)