Skip to content

Commit 345fd5b

Browse files
alanzhao1mahesh-attarde
authored andcommitted
[clang][timers][stats] Add a flag to enable timers in the stats file (llvm#149946)
As reported in llvm#138173, enabling `-ftime-report` adds pass timing info to the stats file if `-stats-file` is specified. This was determined to be WAI. However, if one intentionally wants to put timer information in the stats file, using `-ftime-report` may lead to a lot of logspam (that can't be removed by directing stderr to `/dev/null` as that would also redirect compiler errors). To address this, this PR adds a flag `-stats-file-timers` that adds timer data to the stats file without outputting to stderr.
1 parent e65cedc commit 345fd5b

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ CODEGENOPT(SpeculativeLoadHardening, 1, 0, Benign) ///< Enable speculative load
313313
CODEGENOPT(FineGrainedBitfieldAccesses, 1, 0, Benign) ///< Enable fine-grained bitfield accesses.
314314
CODEGENOPT(StrictEnums , 1, 0, Benign) ///< Optimize based on strict enum definition.
315315
CODEGENOPT(StrictVTablePointers, 1, 0, Benign) ///< Optimize based on the strict vtable pointers
316-
CODEGENOPT(TimePasses , 1, 0, Benign) ///< Set when -ftime-report or -ftime-report= or -ftime-report-json is enabled.
316+
CODEGENOPT(TimePasses , 1, 0, Benign) ///< Set when -ftime-report, -ftime-report=, -ftime-report-json, or -stats-file-timers is enabled.
317317
CODEGENOPT(TimePassesPerRun , 1, 0, Benign) ///< Set when -ftime-report=per-pass-run is enabled.
318318
CODEGENOPT(TimePassesJson , 1, 0, Benign) ///< Set when -ftime-report-json is enabled.
319+
CODEGENOPT(TimePassesStatsFile , 1, 0, Benign) ///< Set when -stats-file-timers is enabled.
319320
CODEGENOPT(TimeTrace , 1, 0, Benign) ///< Set when -ftime-trace is enabled.
320321
VALUE_CODEGENOPT(TimeTraceGranularity, 32, 500, Benign) ///< Minimum time granularity (in microseconds),
321322
///< traced by time profiler

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8261,6 +8261,9 @@ def stats_file : Joined<["-"], "stats-file=">,
82618261
def stats_file_append : Flag<["-"], "stats-file-append">,
82628262
HelpText<"If stats should be appended to stats-file instead of overwriting it">,
82638263
MarshallingInfoFlag<FrontendOpts<"AppendStats">>;
8264+
def stats_file_timers : Flag<["-"], "stats-file-timers">,
8265+
HelpText<"If stats should include timers.">,
8266+
MarshallingInfoFlag<CodeGenOpts<"TimePassesStatsFile">>;
82648267
def fdump_record_layouts_simple : Flag<["-"], "fdump-record-layouts-simple">,
82658268
HelpText<"Dump record layout information in a simple form used for testing">,
82668269
MarshallingInfoFlag<LangOpts<"DumpRecordLayoutsSimple">>;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,8 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
20132013
: llvm::codegenoptions::DebugTemplateNamesKind::Mangled);
20142014
}
20152015

2016-
if (const Arg *A = Args.getLastArg(OPT_ftime_report, OPT_ftime_report_EQ,
2017-
OPT_ftime_report_json)) {
2016+
if (Args.hasArg(OPT_ftime_report, OPT_ftime_report_EQ, OPT_ftime_report_json,
2017+
OPT_stats_file_timers)) {
20182018
Opts.TimePasses = true;
20192019

20202020
// -ftime-report= is only for new pass manager.
@@ -2026,7 +2026,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
20262026
Opts.TimePassesPerRun = true;
20272027
else
20282028
Diags.Report(diag::err_drv_invalid_value)
2029-
<< A->getAsString(Args) << A->getValue();
2029+
<< EQ->getAsString(Args) << EQ->getValue();
20302030
}
20312031

20322032
if (Args.getLastArg(OPT_ftime_report_json))

clang/test/Misc/time-passes.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
// RUN: -ftime-report-json %s -o /dev/null \
2020
// RUN: -mllvm -info-output-file=%t
2121
// RUN: cat %t | FileCheck %s --check-prefixes=JSON
22+
// Check that -stats-file-timers only outputs pass time info in the stats file
23+
// and not stderr.
24+
// RUN: %clang_cc1 -emit-obj -O1 \
25+
// RUN: %s -o /dev/null \
26+
// RUN: -stats-file=%t -stats-file-timers 2>&1 | count 0
27+
// RUN: FileCheck %s -input-file=%t -check-prefixes=JSON
2228

2329
// TIME: Pass execution timing report
2430
// TIME: Total Execution Time:

clang/tools/driver/cc1_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
304304
*IOFile << "{\n";
305305
llvm::TimerGroup::printAllJSONValues(*IOFile, "");
306306
*IOFile << "\n}\n";
307-
} else {
307+
} else if (!Clang->getCodeGenOpts().TimePassesStatsFile) {
308308
llvm::TimerGroup::printAll(*IOFile);
309309
}
310310
llvm::TimerGroup::clearAll();

0 commit comments

Comments
 (0)