Skip to content

Commit 2fb72bb

Browse files
authored
Merge pull request #76958 from rjmansfield/stats-assert-test
Add -print-zero-stats to fix ensure_no_astgen.swift in release mode
2 parents 24ed33b + 1001f9e commit 2fb72bb

File tree

9 files changed

+35
-15
lines changed

9 files changed

+35
-15
lines changed

include/swift/Basic/Statistic.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ class UnifiedStatsReporter {
175175
/// record any additional stats until we've finished.
176176
bool IsFlushingTracesAndProfiles;
177177

178+
/// Whether we are printing all stats even if they are zero.
179+
bool IsPrintingZeroStats;
180+
178181
void publishAlwaysOnStatsToLLVM();
179182
void printAlwaysOnStatsAndTimers(raw_ostream &OS);
180183

@@ -186,7 +189,8 @@ class UnifiedStatsReporter {
186189
bool FineGrainedTimers,
187190
bool TraceEvents,
188191
bool ProfileEvents,
189-
bool ProfileEntities);
192+
bool ProfileEntities,
193+
bool PrintZeroStats);
190194
public:
191195
UnifiedStatsReporter(StringRef ProgramName,
192196
StringRef ModuleName,
@@ -200,7 +204,8 @@ class UnifiedStatsReporter {
200204
bool FineGrainedTimers,
201205
bool TraceEvents,
202206
bool ProfileEvents,
203-
bool ProfileEntities);
207+
bool ProfileEntities,
208+
bool PrintZeroStats);
204209
~UnifiedStatsReporter();
205210

206211
bool fineGrainedTimers() const { return FineGrainedTimers; }

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ class FrontendOptions {
232232
/// runtime overhead.
233233
bool FineGrainedTimers = false;
234234

235+
/// Whether we are printing all stats even if they are zero.
236+
bool PrintZeroStats = false;
237+
235238
/// Trace changes to stats to files in StatsOutputDir.
236239
bool TraceStats = false;
237240

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ def driver_time_compilation : Flag<["-"], "driver-time-compilation">,
361361
def stats_output_dir: Separate<["-"], "stats-output-dir">,
362362
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
363363
HelpText<"Directory to write unified compilation-statistics files to">;
364+
def print_zero_stats: Flag<["-"], "print-zero-stats">,
365+
Flags<[FrontendOption, HelpHidden]>,
366+
HelpText<"Prints all stats even if they are zero">;
364367
def fine_grained_timers: Flag<["-"], "fine-grained-timers">,
365368
Flags<[FrontendOption, HelpHidden]>,
366369
HelpText<"Enable per-request timers">;

lib/Basic/Statistic.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
322322
bool FineGrainedTimers,
323323
bool TraceEvents,
324324
bool ProfileEvents,
325-
bool ProfileEntities)
325+
bool ProfileEntities,
326+
bool PrintZeroStats)
326327
: UnifiedStatsReporter(ProgramName,
327328
auxName(ModuleName,
328329
InputName,
@@ -331,7 +332,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
331332
OptType),
332333
Directory,
333334
SM, CSM, FineGrainedTimers,
334-
TraceEvents, ProfileEvents, ProfileEntities)
335+
TraceEvents, ProfileEvents, ProfileEntities,
336+
PrintZeroStats)
335337
{
336338
}
337339

@@ -343,7 +345,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
343345
bool FineGrainedTimers,
344346
bool TraceEvents,
345347
bool ProfileEvents,
346-
bool ProfileEntities)
348+
bool ProfileEntities,
349+
bool PrintZeroStats)
347350
: currentProcessExitStatusSet(false),
348351
currentProcessExitStatus(EXIT_FAILURE),
349352
StatsFilename(Directory),
@@ -358,7 +361,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
358361
ClangSourceMgr(CSM),
359362
RecursiveTimers(std::make_unique<RecursionSafeTimers>()),
360363
FineGrainedTimers(FineGrainedTimers),
361-
IsFlushingTracesAndProfiles(false)
364+
IsFlushingTracesAndProfiles(false),
365+
IsPrintingZeroStats(PrintZeroStats)
362366
{
363367
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
364368
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
@@ -430,7 +434,8 @@ UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
430434
#define FRONTEND_STATISTIC(TY, NAME) \
431435
do { \
432436
static Statistic Stat = {#TY, #NAME, #NAME}; \
433-
Stat = 0; \
437+
if (IsPrintingZeroStats) \
438+
Stat = 0; \
434439
Stat += (C).NAME; \
435440
} while (0);
436441
#include "swift/Basic/Statistics.def"
@@ -441,7 +446,8 @@ UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
441446
#define DRIVER_STATISTIC(NAME) \
442447
do { \
443448
static Statistic Stat = {"Driver", #NAME, #NAME}; \
444-
Stat = 0; \
449+
if (IsPrintingZeroStats) \
450+
Stat = 0; \
445451
Stat += (C).NAME; \
446452
} while (0);
447453
#include "swift/Basic/Statistics.def"
@@ -458,7 +464,7 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
458464
auto &C = getFrontendCounters();
459465
#define FRONTEND_STATISTIC(TY, NAME) \
460466
do { \
461-
if (C.NAME) { \
467+
if (C.NAME || IsPrintingZeroStats) { \
462468
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
463469
delim = ",\n"; \
464470
} \
@@ -470,7 +476,7 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
470476
auto &C = getDriverCounters();
471477
#define DRIVER_STATISTIC(NAME) \
472478
do { \
473-
if (C.NAME) { \
479+
if (C.NAME || IsPrintingZeroStats) { \
474480
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
475481
delim = ",\n"; \
476482
} \

lib/Driver/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ createStatsReporter(const llvm::opt::InputArgList *ArgList,
522522
false,
523523
false,
524524
false,
525+
false,
525526
false);
526527
}
527528

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ void ArgsToFrontendOptionsConverter::computePrintStatsOptions() {
406406
using namespace options;
407407
Opts.PrintStats |= Args.hasArg(OPT_print_stats);
408408
Opts.PrintClangStats |= Args.hasArg(OPT_print_clang_stats);
409-
#if defined(NDEBUG) && !defined(LLVM_ENABLE_STATS)
409+
Opts.PrintZeroStats |= Args.hasArg(OPT_print_zero_stats);
410+
#if defined(NDEBUG) && !LLVM_ENABLE_STATS
410411
if (Opts.PrintStats || Opts.PrintClangStats)
411412
Diags.diagnose(SourceLoc(), diag::stats_disabled);
412413
#endif

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ void CompilerInstance::setupStatsReporter() {
384384
Invoke.getFrontendOptions().FineGrainedTimers,
385385
Invoke.getFrontendOptions().TraceStats,
386386
Invoke.getFrontendOptions().ProfileEvents,
387-
Invoke.getFrontendOptions().ProfileEntities);
387+
Invoke.getFrontendOptions().ProfileEntities,
388+
Invoke.getFrontendOptions().PrintZeroStats);
388389
// Hand the stats reporter down to the ASTContext so the rest of the compiler
389390
// can use it.
390391
getASTContext().setStatsReporter(Reporter.get());

test/Macros/lazy_parsing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
// 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
1212

13-
// 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
14-
// 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
13+
// 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
14+
// 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
1515

1616
// We use '<=' here instead of '==' to take account of the fact that in debug
1717
// builds we'll be doing round-trip checking, which will parse the syntax tree

test/ScanDependencies/ensure_no_astgen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// This test ensures we don't attempt to do syntax tree parsing for dependency
1010
// scanning.
1111

12-
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json %t/a.swift %t/b.swift -I %t/deps -stats-output-dir %t/stats
12+
// 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
1313
// RUN: %{python} %utils/process-stats-dir.py --evaluate 'ExportedSourceFileRequest == 0' %t/stats
1414

1515
//--- Foo.swiftinterface

0 commit comments

Comments
 (0)