[clang] a845aeb - [Driver] Allow to collect `-save-stats` data to a file specified in the environment variable.
Volodymyr Sapsai via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 16 11:58:35 PDT 2023
Author: Volodymyr Sapsai
Date: 2023-03-16T11:57:59-07:00
New Revision: a845aeb5d6c869146fa24194a7d0182a4787cad8
URL: https://github.com/llvm/llvm-project/commit/a845aeb5d6c869146fa24194a7d0182a4787cad8
DIFF: https://github.com/llvm/llvm-project/commit/a845aeb5d6c869146fa24194a7d0182a4787cad8.diff
LOG: [Driver] Allow to collect `-save-stats` data to a file specified in the environment variable.
Using two environment variables `CC_PRINT_INTERNAL_STAT` and
`CC_PRINT_INTERNAL_STAT_FILE` to work like `CC_PRINT_PROC_STAT`.
The purpose of the change is to allow collecting the internal stats
without modifying the build scripts. Write all stats to a single file
to simplify aggregating the data.
Differential Revision: https://reviews.llvm.org/D144981
Added:
Modified:
clang/docs/CommandGuide/clang.rst
clang/include/clang/Driver/Driver.h
clang/include/clang/Driver/Options.td
clang/include/clang/Frontend/FrontendOptions.h
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/lib/Frontend/CompilerInstance.cpp
clang/test/Driver/save-stats.c
clang/tools/driver/driver.cpp
Removed:
################################################################################
diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst
index e076818697eb1..0722979885afb 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -598,6 +598,16 @@ Driver Options
directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
of the output file ("-save-state=obj").
+ You can also use environment variables to control the statistics reporting.
+ Setting ``CC_PRINT_INTERNAL_STAT`` to ``1`` enables the feature, the report
+ goes to stdout in JSON format.
+
+ Setting ``CC_PRINT_INTERNAL_STAT_FILE`` to a file path makes it report
+ statistics to the given file in the JSON format.
+
+ Note that ``-save-stats`` take precedence over ``CC_PRINT_INTERNAL_STAT``
+ and ``CC_PRINT_INTERNAL_STAT_FILE``.
+
.. option:: -integrated-as, -no-integrated-as
Used to enable and disable, respectively, the use of the integrated
diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index c85658d7c56e3..e3e98bad99127 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -194,6 +194,9 @@ class Driver {
/// The file to log CC_PRINT_PROC_STAT_FILE output to, if enabled.
std::string CCPrintStatReportFilename;
+ /// The file to log CC_PRINT_INTERNAL_STAT_FILE output to, if enabled.
+ std::string CCPrintInternalStatReportFilename;
+
/// The file to log CC_PRINT_OPTIONS output to, if enabled.
std::string CCPrintOptionsFilename;
@@ -258,6 +261,10 @@ class Driver {
/// performance report to CC_PRINT_PROC_STAT_FILE or to stdout.
unsigned CCPrintProcessStats : 1;
+ /// Set CC_PRINT_INTERNAL_STAT mode, which causes the driver to dump internal
+ /// performance report to CC_PRINT_INTERNAL_STAT_FILE or to stdout.
+ unsigned CCPrintInternalStats : 1;
+
/// Pointer to the ExecuteCC1Tool function, if available.
/// When the clangDriver lib is used through clang.exe, this provides a
/// shortcut for executing the -cc1 command-line directly, in the same
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index a2dbef1cc7cfe..a05e61ac0e92f 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6147,6 +6147,9 @@ def print_stats : Flag<["-"], "print-stats">,
def stats_file : Joined<["-"], "stats-file=">,
HelpText<"Filename to write statistics to">,
MarshallingInfoString<FrontendOpts<"StatsFile">>;
+def stats_file_append : Flag<["-"], "stats-file-append">,
+ HelpText<"If stats should be appended to stats-file instead of overwriting it">,
+ MarshallingInfoFlag<FrontendOpts<"AppendStats">>;
def fdump_record_layouts_simple : Flag<["-"], "fdump-record-layouts-simple">,
HelpText<"Dump record layout information in a simple form used for testing">,
MarshallingInfoFlag<LangOpts<"DumpRecordLayoutsSimple">>;
diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h
index ef04f3b236d87..85183a3812b42 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -278,6 +278,8 @@ class FrontendOptions {
/// Show frontend performance metrics and statistics.
unsigned ShowStats : 1;
+ unsigned AppendStats : 1;
+
/// print the supported cpus for the current target
unsigned PrintSupportedCPUs : 1;
@@ -511,16 +513,16 @@ class FrontendOptions {
public:
FrontendOptions()
: DisableFree(false), RelocatablePCH(false), ShowHelp(false),
- ShowStats(false), TimeTrace(false), ShowVersion(false),
- FixWhatYouCan(false), FixOnlyWarnings(false), FixAndRecompile(false),
- FixToTemporaries(false), ARCMTMigrateEmitARCErrors(false),
- SkipFunctionBodies(false), UseGlobalModuleIndex(true),
- GenerateGlobalModuleIndex(true), ASTDumpDecls(false),
- ASTDumpLookups(false), BuildingImplicitModule(false),
- BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
- IncludeTimestamps(true), UseTemporary(true),
- AllowPCMWithCompilerErrors(false), ModulesShareFileManager(true),
- TimeTraceGranularity(500) {}
+ ShowStats(false), AppendStats(false), TimeTrace(false),
+ ShowVersion(false), FixWhatYouCan(false), FixOnlyWarnings(false),
+ FixAndRecompile(false), FixToTemporaries(false),
+ ARCMTMigrateEmitARCErrors(false), SkipFunctionBodies(false),
+ UseGlobalModuleIndex(true), GenerateGlobalModuleIndex(true),
+ ASTDumpDecls(false), ASTDumpLookups(false),
+ BuildingImplicitModule(false), BuildingImplicitModuleUsesLock(true),
+ ModulesEmbedAllFiles(false), IncludeTimestamps(true),
+ UseTemporary(true), AllowPCMWithCompilerErrors(false),
+ ModulesShareFileManager(true), TimeTraceGranularity(500) {}
/// getInputKindForExtension - Return the appropriate input kind for a file
/// extension. For example, "c" would return Language::C.
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index c3765570d959e..ead6252776a28 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -199,8 +199,9 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
DriverTitle(Title), CCCPrintBindings(false), CCPrintOptions(false),
CCLogDiagnostics(false), CCGenDiagnostics(false),
- CCPrintProcessStats(false), TargetTriple(TargetTriple), Saver(Alloc),
- PrependArg(nullptr), CheckInputsExist(true), ProbePrecompiled(true),
+ CCPrintProcessStats(false), CCPrintInternalStats(false),
+ TargetTriple(TargetTriple), Saver(Alloc), PrependArg(nullptr),
+ CheckInputsExist(true), ProbePrecompiled(true),
SuppressMissingInputWarning(false) {
// Provide a sane fallback if no VFS is specified.
if (!this->VFS)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 2327086c91f1f..1c62cd2ecc9df 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7039,8 +7039,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// Setup statistics file output.
SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
- if (!StatsFile.empty())
+ if (!StatsFile.empty()) {
CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + StatsFile));
+ if (D.CCPrintInternalStats)
+ CmdArgs.push_back("-stats-file-append");
+ }
// Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
// parser.
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index bc32b8d8cac8d..774a1843fa2c8 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1766,22 +1766,29 @@ SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args,
const InputInfo &Input,
const Driver &D) {
const Arg *A = Args.getLastArg(options::OPT_save_stats_EQ);
- if (!A)
+ if (!A && !D.CCPrintInternalStats)
return {};
- StringRef SaveStats = A->getValue();
SmallString<128> StatsFile;
- if (SaveStats == "obj" && Output.isFilename()) {
- StatsFile.assign(Output.getFilename());
- llvm::sys::path::remove_filename(StatsFile);
- } else if (SaveStats != "cwd") {
- D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats;
- return {};
- }
+ if (A) {
+ StringRef SaveStats = A->getValue();
+ if (SaveStats == "obj" && Output.isFilename()) {
+ StatsFile.assign(Output.getFilename());
+ llvm::sys::path::remove_filename(StatsFile);
+ } else if (SaveStats != "cwd") {
+ D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats;
+ return {};
+ }
- StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput());
- llvm::sys::path::append(StatsFile, BaseName);
- llvm::sys::path::replace_extension(StatsFile, "stats");
+ StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput());
+ llvm::sys::path::append(StatsFile, BaseName);
+ llvm::sys::path::replace_extension(StatsFile, "stats");
+ } else {
+ assert(D.CCPrintInternalStats);
+ StatsFile.assign(D.CCPrintInternalStatReportFilename.empty()
+ ? "-"
+ : D.CCPrintInternalStatReportFilename);
+ }
return StatsFile;
}
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index f3510b0c15230..ecf939424e56c 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1087,9 +1087,12 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
}
StringRef StatsFile = getFrontendOpts().StatsFile;
if (!StatsFile.empty()) {
+ llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF;
+ if (getFrontendOpts().AppendStats)
+ FileFlags |= llvm::sys::fs::OF_Append;
std::error_code EC;
- auto StatS = std::make_unique<llvm::raw_fd_ostream>(
- StatsFile, EC, llvm::sys::fs::OF_TextWithCRLF);
+ auto StatS =
+ std::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, FileFlags);
if (EC) {
getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
<< StatsFile << EC.message();
diff --git a/clang/test/Driver/save-stats.c b/clang/test/Driver/save-stats.c
index d70220331ecbc..3c212f4ec4ab5 100644
--- a/clang/test/Driver/save-stats.c
+++ b/clang/test/Driver/save-stats.c
@@ -26,3 +26,14 @@
// RUN: %clang -target x86_64-linux-unknown -save-stats=obj -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO-OBJ
// CHECK-LTO-OBJ: "-plugin-opt=stats-file=obj/dir{{/|\\\\}}save-stats.stats"
+
+// RUN: env CC_PRINT_INTERNAL_STAT=1 \
+// RUN: %clang -target x86_64-apple-darwin %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-ENV
+// CHECK-ENV: "-stats-file=-"
+// CHECK-ENV-NO: "stats-file-append"
+
+// RUN: env CC_PRINT_INTERNAL_STAT=1 \
+// RUN: CC_PRINT_INTERNAL_STAT_FILE=/tmp/stats.json \
+// RUN: %clang -target x86_64-apple-darwin %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-ENV-FILE
+// CHECK-ENV-FILE: "-stats-file=/tmp/stats.json"
+// CHECK-ENV-FILE: "-stats-file-append"
diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index a05e0956a4796..471d0181ff088 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -307,6 +307,9 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
TheDriver.CCPrintProcessStats =
checkEnvVar<bool>("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
TheDriver.CCPrintStatReportFilename);
+ TheDriver.CCPrintInternalStats =
+ checkEnvVar<bool>("CC_PRINT_INTERNAL_STAT", "CC_PRINT_INTERNAL_STAT_FILE",
+ TheDriver.CCPrintInternalStatReportFilename);
return true;
}
More information about the cfe-commits
mailing list