Add -print-zero-stats option and use it in ensure_no_astgen.swift

to verify ExportedSourceFileRequest == 0.

In release mode only non-zero stats are printed by default now.

Fix diagnostic when compiler is built without statistics support.
This commit is contained in:
Ryan Mansfield
2024-10-10 14:45:41 -04:00
parent 844d103f0d
commit 1001f9eb41
9 changed files with 35 additions and 15 deletions

View File

@@ -175,6 +175,9 @@ private:
/// record any additional stats until we've finished.
bool IsFlushingTracesAndProfiles;
/// Whether we are printing all stats even if they are zero.
bool IsPrintingZeroStats;
void publishAlwaysOnStatsToLLVM();
void printAlwaysOnStatsAndTimers(raw_ostream &OS);
@@ -186,7 +189,8 @@ private:
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities);
bool ProfileEntities,
bool PrintZeroStats);
public:
UnifiedStatsReporter(StringRef ProgramName,
StringRef ModuleName,
@@ -200,7 +204,8 @@ public:
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities);
bool ProfileEntities,
bool PrintZeroStats);
~UnifiedStatsReporter();
bool fineGrainedTimers() const { return FineGrainedTimers; }

View File

@@ -232,6 +232,9 @@ public:
/// runtime overhead.
bool FineGrainedTimers = false;
/// Whether we are printing all stats even if they are zero.
bool PrintZeroStats = false;
/// Trace changes to stats to files in StatsOutputDir.
bool TraceStats = false;

View File

@@ -361,6 +361,9 @@ def driver_time_compilation : Flag<["-"], "driver-time-compilation">,
def stats_output_dir: Separate<["-"], "stats-output-dir">,
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
HelpText<"Directory to write unified compilation-statistics files to">;
def print_zero_stats: Flag<["-"], "print-zero-stats">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Prints all stats even if they are zero">;
def fine_grained_timers: Flag<["-"], "fine-grained-timers">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Enable per-request timers">;

View File

@@ -322,7 +322,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
bool ProfileEntities,
bool PrintZeroStats)
: UnifiedStatsReporter(ProgramName,
auxName(ModuleName,
InputName,
@@ -331,7 +332,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
OptType),
Directory,
SM, CSM, FineGrainedTimers,
TraceEvents, ProfileEvents, ProfileEntities)
TraceEvents, ProfileEvents, ProfileEntities,
PrintZeroStats)
{
}
@@ -343,7 +345,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
bool ProfileEntities,
bool PrintZeroStats)
: currentProcessExitStatusSet(false),
currentProcessExitStatus(EXIT_FAILURE),
StatsFilename(Directory),
@@ -358,7 +361,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
ClangSourceMgr(CSM),
RecursiveTimers(std::make_unique<RecursionSafeTimers>()),
FineGrainedTimers(FineGrainedTimers),
IsFlushingTracesAndProfiles(false)
IsFlushingTracesAndProfiles(false),
IsPrintingZeroStats(PrintZeroStats)
{
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
@@ -430,6 +434,7 @@ UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
static Statistic Stat = {#TY, #NAME, #NAME}; \
if (IsPrintingZeroStats) \
Stat = 0; \
Stat += (C).NAME; \
} while (0);
@@ -441,6 +446,7 @@ UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
#define DRIVER_STATISTIC(NAME) \
do { \
static Statistic Stat = {"Driver", #NAME, #NAME}; \
if (IsPrintingZeroStats) \
Stat = 0; \
Stat += (C).NAME; \
} while (0);
@@ -458,7 +464,7 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
auto &C = getFrontendCounters();
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
if (C.NAME) { \
if (C.NAME || IsPrintingZeroStats) { \
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} \
@@ -470,7 +476,7 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
auto &C = getDriverCounters();
#define DRIVER_STATISTIC(NAME) \
do { \
if (C.NAME) { \
if (C.NAME || IsPrintingZeroStats) { \
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} \

View File

@@ -522,6 +522,7 @@ createStatsReporter(const llvm::opt::InputArgList *ArgList,
false,
false,
false,
false,
false);
}

View File

@@ -406,7 +406,8 @@ void ArgsToFrontendOptionsConverter::computePrintStatsOptions() {
using namespace options;
Opts.PrintStats |= Args.hasArg(OPT_print_stats);
Opts.PrintClangStats |= Args.hasArg(OPT_print_clang_stats);
#if defined(NDEBUG) && !defined(LLVM_ENABLE_STATS)
Opts.PrintZeroStats |= Args.hasArg(OPT_print_zero_stats);
#if defined(NDEBUG) && !LLVM_ENABLE_STATS
if (Opts.PrintStats || Opts.PrintClangStats)
Diags.diagnose(SourceLoc(), diag::stats_disabled);
#endif

View File

@@ -384,7 +384,8 @@ void CompilerInstance::setupStatsReporter() {
Invoke.getFrontendOptions().FineGrainedTimers,
Invoke.getFrontendOptions().TraceStats,
Invoke.getFrontendOptions().ProfileEvents,
Invoke.getFrontendOptions().ProfileEntities);
Invoke.getFrontendOptions().ProfileEntities,
Invoke.getFrontendOptions().PrintZeroStats);
// Hand the stats reporter down to the ASTContext so the rest of the compiler
// can use it.
getASTContext().setStatsReporter(Reporter.get());

View File

@@ -10,8 +10,8 @@
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -stats-output-dir %t/stats-no-lookup -fine-grained-timers -primary-file %t/b.swift %t/a.swift
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -stats-output-dir %t/stats-lookup -fine-grained-timers -primary-file %t/c.swift %t/a.swift
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -stats-output-dir %t/stats-no-lookup -fine-grained-timers -print-zero-stats -primary-file %t/b.swift %t/a.swift
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -stats-output-dir %t/stats-lookup -fine-grained-timers -print-zero-stats -primary-file %t/c.swift %t/a.swift
// We use '<=' here instead of '==' to take account of the fact that in debug
// builds we'll be doing round-trip checking, which will parse the syntax tree

View File

@@ -9,7 +9,7 @@
// This test ensures we don't attempt to do syntax tree parsing for dependency
// scanning.
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json %t/a.swift %t/b.swift -I %t/deps -stats-output-dir %t/stats
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json %t/a.swift %t/b.swift -I %t/deps -stats-output-dir %t/stats -print-zero-stats
// RUN: %{python} %utils/process-stats-dir.py --evaluate 'ExportedSourceFileRequest == 0' %t/stats
//--- Foo.swiftinterface