Skip to content

Commit 9824930

Browse files
authored
[NFC] Add PrintOnExit parameter to a llvm::TimerGroup (llvm#164407)
Clean up AnalysisConsumer code from the timer-related branches that are not used most of the time, and move this logic to Timer.cpp, which is a more relevant place and allows for a cleaner implementation.
1 parent 202bcc4 commit 9824930

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ class AnalysisConsumer : public AnalysisASTConsumer,
128128
std::unique_ptr<llvm::Timer> SyntaxCheckTimer;
129129
std::unique_ptr<llvm::Timer> ExprEngineTimer;
130130
std::unique_ptr<llvm::Timer> BugReporterTimer;
131-
bool ShouldClearTimersToPreventDisplayingThem;
132131

133132
/// The information about analyzed functions shared throughout the
134133
/// translation unit.
@@ -149,7 +148,10 @@ class AnalysisConsumer : public AnalysisASTConsumer,
149148
if (Opts.AnalyzerDisplayProgress || Opts.PrintStats ||
150149
Opts.ShouldSerializeStats || !Opts.DumpEntryPointStatsToCSV.empty()) {
151150
AnalyzerTimers = std::make_unique<llvm::TimerGroup>(
152-
"analyzer", "Analyzer timers");
151+
"analyzer", "Analyzer timers",
152+
/*PrintOnExit=*/
153+
(Opts.AnalyzerDisplayProgress || Opts.PrintStats ||
154+
Opts.ShouldSerializeStats));
153155
SyntaxCheckTimer = std::make_unique<llvm::Timer>(
154156
"syntaxchecks", "Syntax-based analysis time", *AnalyzerTimers);
155157
ExprEngineTimer = std::make_unique<llvm::Timer>(
@@ -159,12 +161,6 @@ class AnalysisConsumer : public AnalysisASTConsumer,
159161
*AnalyzerTimers);
160162
}
161163

162-
// Avoid displaying the timers created above in case we only want to record
163-
// per-entry-point stats.
164-
ShouldClearTimersToPreventDisplayingThem = !Opts.AnalyzerDisplayProgress &&
165-
!Opts.PrintStats &&
166-
!Opts.ShouldSerializeStats;
167-
168164
if (Opts.PrintStats || Opts.ShouldSerializeStats) {
169165
llvm::EnableStatistics(/* DoPrintOnExit= */ false);
170166
}
@@ -287,9 +283,6 @@ class AnalysisConsumer : public AnalysisASTConsumer,
287283
checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
288284
if (SyntaxCheckTimer)
289285
SyntaxCheckTimer->stopTimer();
290-
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
291-
AnalyzerTimers->clear();
292-
}
293286
}
294287
return true;
295288
}
@@ -583,9 +576,6 @@ void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
583576
checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
584577
if (SyntaxCheckTimer)
585578
SyntaxCheckTimer->stopTimer();
586-
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
587-
AnalyzerTimers->clear();
588-
}
589579

590580
// Run the AST-only checks using the order in which functions are defined.
591581
// If inlining is not turned on, use the simplest function order for path
@@ -765,9 +755,6 @@ void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
765755
FunctionSummaries.findOrInsertSummary(D)->second.SyntaxRunningTime =
766756
std::lround(CheckerDuration.getWallTime() * 1000);
767757
DisplayTime(CheckerDuration);
768-
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
769-
AnalyzerTimers->clear();
770-
}
771758
}
772759
}
773760

@@ -830,9 +817,6 @@ void AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
830817
PathRunningTime.set(static_cast<unsigned>(
831818
std::lround(ExprEngineDuration.getWallTime() * 1000)));
832819
DisplayTime(ExprEngineDuration);
833-
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
834-
AnalyzerTimers->clear();
835-
}
836820
}
837821

838822
if (!Mgr->options.DumpExplodedGraphTo.empty())
@@ -843,9 +827,6 @@ void AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
843827
Eng.ViewGraph(Mgr->options.TrimGraph);
844828

845829
flushReports(BugReporterTimer.get(), Eng.getBugReporter());
846-
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
847-
AnalyzerTimers->clear();
848-
}
849830
}
850831

851832
//===----------------------------------------------------------------------===//

llvm/include/llvm/Support/Timer.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class TimerGroup {
209209
std::string Description;
210210
Timer *FirstTimer = nullptr; ///< First timer in the group.
211211
std::vector<PrintRecord> TimersToPrint;
212+
bool PrintOnExit;
212213

213214
TimerGroup **Prev; ///< Pointer to Next field of previous timergroup in list.
214215
TimerGroup *Next; ///< Pointer to next timergroup in list.
@@ -217,13 +218,15 @@ class TimerGroup {
217218

218219
friend class TimerGlobals;
219220
explicit TimerGroup(StringRef Name, StringRef Description,
220-
sys::SmartMutex<true> &lock);
221+
sys::SmartMutex<true> &lock, bool PrintOnExit);
221222

222223
public:
223-
LLVM_ABI explicit TimerGroup(StringRef Name, StringRef Description);
224+
LLVM_ABI explicit TimerGroup(StringRef Name, StringRef Description,
225+
bool PrintOnExit = true);
224226

225227
LLVM_ABI explicit TimerGroup(StringRef Name, StringRef Description,
226-
const StringMap<TimeRecord> &Records);
228+
const StringMap<TimeRecord> &Records,
229+
bool PrintOnExit = true);
227230

228231
LLVM_ABI ~TimerGroup();
229232

llvm/lib/Support/Timer.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ class Name2PairMap {
240240
getGroupEntry(StringRef GroupName, StringRef GroupDescription) {
241241
std::pair<TimerGroup *, Name2TimerMap> &GroupEntry = Map[GroupName];
242242
if (!GroupEntry.first)
243-
GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
243+
GroupEntry.first =
244+
new TimerGroup(GroupName, GroupDescription, /*PrintOnExit=*/true);
244245

245246
return GroupEntry;
246247
}
@@ -270,9 +271,10 @@ TimerGroup &NamedRegionTimer::getNamedTimerGroup(StringRef GroupName,
270271
static TimerGroup *TimerGroupList = nullptr;
271272

272273
TimerGroup::TimerGroup(StringRef Name, StringRef Description,
273-
sys::SmartMutex<true> &lock)
274+
sys::SmartMutex<true> &lock, bool PrintOnExit)
274275
: Name(Name.begin(), Name.end()),
275-
Description(Description.begin(), Description.end()) {
276+
Description(Description.begin(), Description.end()),
277+
PrintOnExit(PrintOnExit) {
276278
// Add the group to TimerGroupList.
277279
sys::SmartScopedLock<true> L(lock);
278280
if (TimerGroupList)
@@ -282,12 +284,12 @@ TimerGroup::TimerGroup(StringRef Name, StringRef Description,
282284
TimerGroupList = this;
283285
}
284286

285-
TimerGroup::TimerGroup(StringRef Name, StringRef Description)
286-
: TimerGroup(Name, Description, timerLock()) {}
287+
TimerGroup::TimerGroup(StringRef Name, StringRef Description, bool PrintOnExit)
288+
: TimerGroup(Name, Description, timerLock(), PrintOnExit) {}
287289

288290
TimerGroup::TimerGroup(StringRef Name, StringRef Description,
289-
const StringMap<TimeRecord> &Records)
290-
: TimerGroup(Name, Description) {
291+
const StringMap<TimeRecord> &Records, bool PrintOnExit)
292+
: TimerGroup(Name, Description, PrintOnExit) {
291293
TimersToPrint.reserve(Records.size());
292294
for (const auto &P : Records)
293295
TimersToPrint.emplace_back(P.getValue(), std::string(P.getKey()),
@@ -301,7 +303,7 @@ TimerGroup::~TimerGroup() {
301303
while (FirstTimer)
302304
removeTimer(*FirstTimer);
303305

304-
if (!TimersToPrint.empty()) {
306+
if (!TimersToPrint.empty() && PrintOnExit) {
305307
std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
306308
PrintQueuedTimers(*OutStream);
307309
}
@@ -530,7 +532,7 @@ class llvm::TimerGlobals {
530532

531533
sys::SmartMutex<true> TimerLock;
532534
TimerGroup DefaultTimerGroup{"misc", "Miscellaneous Ungrouped Timers",
533-
TimerLock};
535+
TimerLock, /*PrintOnExit=*/true};
534536
SignpostEmitter Signposts;
535537

536538
// Order of these members and initialization below is important. For example

0 commit comments

Comments
 (0)