Skip to content

Commit d494cc8

Browse files
authored
Merge pull request #30109 from CodaFi/lies-more-lies-and-statistics
[Frontend] Clean Up Usage of UnifiedStatsReporter
2 parents 87bb775 + a6651a9 commit d494cc8

28 files changed

+234
-179
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ class CompilerInstance {
404404

405405
/// Null if no tracker.
406406
std::unique_ptr<DependencyTracker> DepTracker;
407+
/// If there is no stats output directory by the time the
408+
/// instance has completed its setup, this will be null.
409+
std::unique_ptr<UnifiedStatsReporter> Stats;
407410

408411
mutable ModuleDecl *MainModule = nullptr;
409412
SerializedModuleLoader *SML = nullptr;
@@ -488,6 +491,8 @@ class CompilerInstance {
488491
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
489492
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }
490493

494+
UnifiedStatsReporter *getStatsReporter() const { return Stats.get(); }
495+
491496
SILModule *getSILModule() {
492497
return TheSILModule.get();
493498
}
@@ -571,6 +576,7 @@ class CompilerInstance {
571576

572577
bool setUpInputs();
573578
bool setUpASTContextIfNeeded();
579+
void setupStatsReporter();
574580
Optional<unsigned> setUpCodeCompletionBuffer();
575581

576582
/// Set up all state in the CompilerInstance to process the given input file.
@@ -619,8 +625,7 @@ class CompilerInstance {
619625
/// \param silModule The SIL module that was generated during SILGen.
620626
/// \param stats A stats reporter that will report optimization statistics.
621627
/// \returns true if any errors occurred.
622-
bool performSILProcessing(SILModule *silModule,
623-
UnifiedStatsReporter *stats = nullptr);
628+
bool performSILProcessing(SILModule *silModule);
624629

625630
private:
626631
SourceFile *

include/swift/Subsystems.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace swift {
6161
class SILModule;
6262
class SILParserTUState;
6363
class SourceFile;
64+
enum class SourceFileKind;
6465
class SourceManager;
6566
class SyntaxParseActions;
6667
class SyntaxParsingCache;
@@ -70,7 +71,6 @@ namespace swift {
7071
class TypeCheckerOptions;
7172
struct TypeLoc;
7273
class UnifiedStatsReporter;
73-
enum class SourceFileKind;
7474

7575
namespace Lowering {
7676
class TypeConverter;
@@ -286,29 +286,31 @@ namespace swift {
286286
StringRef OutputPath);
287287

288288
/// Turn the given LLVM module into native code and return true on error.
289-
bool performLLVM(const IRGenOptions &Opts, ASTContext &Ctx, llvm::Module *Module,
290-
StringRef OutputFilename,
291-
UnifiedStatsReporter *Stats=nullptr);
289+
bool performLLVM(const IRGenOptions &Opts,
290+
ASTContext &Ctx,
291+
llvm::Module *Module,
292+
StringRef OutputFilename);
292293

293294
/// Run the LLVM passes. In multi-threaded compilation this will be done for
294295
/// multiple LLVM modules in parallel.
295-
/// \param Diags may be null if LLVM code gen diagnostics are not required.
296-
/// \param DiagMutex may also be null if a mutex around \p Diags is not
297-
/// required.
296+
/// \param Diags The Diagnostic Engine.
297+
/// \param DiagMutex in contexts that require parallel codegen, a mutex that the
298+
/// diagnostic engine uses to synchronize emission.
298299
/// \param HashGlobal used with incremental LLVMCodeGen to know if a module
299300
/// was already compiled, may be null if not desired.
300301
/// \param Module LLVM module to code gen, required.
301302
/// \param TargetMachine target of code gen, required.
302303
/// \param effectiveLanguageVersion version of the language, effectively.
303304
/// \param OutputFilename Filename for output.
304-
bool performLLVM(const IRGenOptions &Opts, DiagnosticEngine *Diags,
305+
bool performLLVM(const IRGenOptions &Opts,
306+
DiagnosticEngine &Diags,
305307
llvm::sys::Mutex *DiagMutex,
306308
llvm::GlobalVariable *HashGlobal,
307309
llvm::Module *Module,
308310
llvm::TargetMachine *TargetMachine,
309311
const version::Version &effectiveLanguageVersion,
310312
StringRef OutputFilename,
311-
UnifiedStatsReporter *Stats=nullptr);
313+
UnifiedStatsReporter *Stats);
312314

313315
/// Dump YAML describing all fixed-size types imported from the given module.
314316
bool performDumpTypeInfo(const IRGenOptions &Opts,

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,12 @@ llvm::BumpPtrAllocator &ASTContext::getAllocator(AllocationArena arena) const {
617617

618618
/// Set a new stats reporter.
619619
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
620-
Stats = stats;
621-
evaluator.setStatsReporter(stats);
622-
623620
if (stats) {
624621
stats->getFrontendCounters().NumASTBytesAllocated =
625622
getAllocator().getBytesAllocated();
626623
}
624+
evaluator.setStatsReporter(stats);
625+
Stats = stats;
627626
}
628627

629628
RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5026,8 +5026,8 @@ ProtocolDecl::setLazyRequirementSignature(LazyMemberLoader *lazyLoader,
50265026

50275027
++NumLazyRequirementSignatures;
50285028
// FIXME: (transitional) increment the redundant "always-on" counter.
5029-
if (getASTContext().Stats)
5030-
getASTContext().Stats->getFrontendCounters().NumLazyRequirementSignatures++;
5029+
if (auto *Stats = getASTContext().Stats)
5030+
Stats->getFrontendCounters().NumLazyRequirementSignatures++;
50315031
}
50325032

50335033
ArrayRef<Requirement> ProtocolDecl::getCachedRequirementSignature() const {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,8 +3458,8 @@ void EquivalenceClass::modified(GenericSignatureBuilder &builder) {
34583458
GenericSignatureBuilder::GenericSignatureBuilder(
34593459
ASTContext &ctx)
34603460
: Context(ctx), Diags(Context.Diags), Impl(new Implementation) {
3461-
if (Context.Stats)
3462-
Context.Stats->getFrontendCounters().NumGenericSignatureBuilders++;
3461+
if (auto *Stats = Context.Stats)
3462+
Stats->getFrontendCounters().NumGenericSignatureBuilders++;
34633463
}
34643464

34653465
GenericSignatureBuilder::GenericSignatureBuilder(

lib/AST/Module.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ void ModuleDecl::lookupClassMember(AccessPathTy accessPath,
629629
stats->getFrontendCounters().NumModuleLookupClassMember++;
630630

631631
if (isParsedModule(this)) {
632-
FrontendStatsTracer tracer(getASTContext().Stats, "source-file-lookup-class-member");
632+
FrontendStatsTracer tracer(getASTContext().Stats,
633+
"source-file-lookup-class-member");
633634
auto &cache = getSourceLookupCache();
634635
cache.populateMemberCache(*this);
635636
cache.lookupClassMember(accessPath, name, results);
@@ -642,7 +643,8 @@ void ModuleDecl::lookupClassMember(AccessPathTy accessPath,
642643
void SourceFile::lookupClassMember(ModuleDecl::AccessPathTy accessPath,
643644
DeclName name,
644645
SmallVectorImpl<ValueDecl*> &results) const {
645-
FrontendStatsTracer tracer(getASTContext().Stats, "source-file-lookup-class-member");
646+
FrontendStatsTracer tracer(getASTContext().Stats,
647+
"source-file-lookup-class-member");
646648
auto &cache = getCache();
647649
cache.populateMemberCache(*this);
648650
cache.lookupClassMember(accessPath, name, results);

lib/AST/UnqualifiedLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
490490
auto localCounter = lookupCounter;
491491
(void)localCounter; // for debugging
492492
#endif
493-
FrontendStatsTracer StatsTracer(Ctx.Stats, "performUnqualifedLookup",
493+
FrontendStatsTracer StatsTracer(Ctx.Stats,
494+
"performUnqualifedLookup",
494495
DC->getParentSourceFile());
495496

496497
const Optional<bool> initialIsCascadingUse = getInitialIsCascadingUse();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7859,8 +7859,8 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
78597859
void ClangImporter::Implementation::startedImportingEntity() {
78607860
++NumTotalImportedEntities;
78617861
// FIXME: (transitional) increment the redundant "always-on" counter.
7862-
if (SwiftContext.Stats)
7863-
SwiftContext.Stats->getFrontendCounters().NumTotalClangImportedEntities++;
7862+
if (auto *Stats = SwiftContext.Stats)
7863+
Stats->getFrontendCounters().NumTotalClangImportedEntities++;
78647864
}
78657865

78667866
/// Look up associated type requirements in the conforming type.
@@ -8563,7 +8563,8 @@ static void loadAllMembersOfSuperclassIfNeeded(ClassDecl *CD) {
85638563
void
85648564
ClangImporter::Implementation::loadAllMembers(Decl *D, uint64_t extra) {
85658565

8566-
FrontendStatsTracer tracer(D->getASTContext().Stats, "load-all-members", D);
8566+
FrontendStatsTracer tracer(D->getASTContext().Stats,
8567+
"load-all-members", D);
85678568
assert(D);
85688569

85698570
// Check whether we're importing an Objective-C container of some sort.

lib/Frontend/Frontend.cpp

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "swift/Serialization/SerializedModuleLoader.h"
3636
#include "swift/Strings.h"
3737
#include "swift/Subsystems.h"
38+
#include "clang/AST/ASTContext.h"
3839
#include "llvm/ADT/Hashing.h"
3940
#include "llvm/ADT/SmallVector.h"
4041
#include "llvm/ADT/Triple.h"
@@ -238,6 +239,56 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
238239
return false;
239240
}
240241

242+
void CompilerInstance::setupStatsReporter() {
243+
const auto &Invok = getInvocation();
244+
const std::string &StatsOutputDir =
245+
Invok.getFrontendOptions().StatsOutputDir;
246+
if (StatsOutputDir.empty())
247+
return;
248+
249+
auto silOptModeArgStr = [](OptimizationMode mode) -> StringRef {
250+
switch (mode) {
251+
case OptimizationMode::ForSpeed:
252+
return "O";
253+
case OptimizationMode::ForSize:
254+
return "Osize";
255+
default:
256+
return "Onone";
257+
}
258+
};
259+
260+
auto getClangSourceManager = [](ASTContext &Ctx) -> clang::SourceManager * {
261+
if (auto *clangImporter = static_cast<ClangImporter *>(
262+
Ctx.getClangModuleLoader())) {
263+
return &clangImporter->getClangASTContext().getSourceManager();
264+
}
265+
return nullptr;
266+
};
267+
268+
const auto &FEOpts = Invok.getFrontendOptions();
269+
const auto &LangOpts = Invok.getLangOptions();
270+
const auto &SILOpts = Invok.getSILOptions();
271+
const std::string &OutFile =
272+
FEOpts.InputsAndOutputs.lastInputProducingOutput().outputFilename();
273+
auto Reporter = std::make_unique<UnifiedStatsReporter>(
274+
"swift-frontend",
275+
FEOpts.ModuleName,
276+
FEOpts.InputsAndOutputs.getStatsFileMangledInputName(),
277+
LangOpts.Target.normalize(),
278+
llvm::sys::path::extension(OutFile),
279+
silOptModeArgStr(SILOpts.OptMode),
280+
StatsOutputDir,
281+
&getSourceMgr(),
282+
getClangSourceManager(getASTContext()),
283+
Invok.getFrontendOptions().TraceStats,
284+
Invok.getFrontendOptions().ProfileEvents,
285+
Invok.getFrontendOptions().ProfileEntities);
286+
// Hand the stats reporter down to the ASTContext so the rest of the compiler
287+
// can use it.
288+
getASTContext().setStatsReporter(Reporter.get());
289+
Stats = std::move(Reporter);
290+
}
291+
241292
bool CompilerInstance::setup(const CompilerInvocation &Invok) {
242293
Invocation = Invok;
243294

@@ -281,6 +332,8 @@ bool CompilerInstance::setup(const CompilerInvocation &Invok) {
281332
if (setUpASTContextIfNeeded())
282333
return true;
283334

335+
setupStatsReporter();
336+
284337
return false;
285338
}
286339

@@ -712,7 +765,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
712765
return performParseOnly();
713766
}
714767

715-
FrontendStatsTracer tracer(Context->Stats, "perform-sema");
768+
FrontendStatsTracer tracer(getStatsReporter(), "perform-sema");
716769

717770
ModuleDecl *mainModule = getMainModule();
718771
Context->LoadedModules[mainModule->getName()] = mainModule;
@@ -770,7 +823,7 @@ CompilerInstance::ImplicitImports::ImplicitImports(CompilerInstance &compiler) {
770823
}
771824

772825
bool CompilerInstance::loadStdlib() {
773-
FrontendStatsTracer tracer(Context->Stats, "load-stdlib");
826+
FrontendStatsTracer tracer(getStatsReporter(), "load-stdlib");
774827
ModuleDecl *M = Context->getStdlibModule(true);
775828

776829
if (!M) {
@@ -789,7 +842,7 @@ bool CompilerInstance::loadStdlib() {
789842
}
790843

791844
ModuleDecl *CompilerInstance::importUnderlyingModule() {
792-
FrontendStatsTracer tracer(Context->Stats, "import-underlying-module");
845+
FrontendStatsTracer tracer(getStatsReporter(), "import-underlying-module");
793846
ModuleDecl *objCModuleUnderlyingMixedFramework =
794847
static_cast<ClangImporter *>(Context->getClangModuleLoader())
795848
->loadModule(SourceLoc(),
@@ -802,7 +855,7 @@ ModuleDecl *CompilerInstance::importUnderlyingModule() {
802855
}
803856

804857
ModuleDecl *CompilerInstance::importBridgingHeader() {
805-
FrontendStatsTracer tracer(Context->Stats, "import-bridging-header");
858+
FrontendStatsTracer tracer(getStatsReporter(), "import-bridging-header");
806859
const StringRef implicitHeaderPath =
807860
Invocation.getFrontendOptions().ImplicitObjCHeaderPath;
808861
auto clangImporter =
@@ -817,7 +870,7 @@ ModuleDecl *CompilerInstance::importBridgingHeader() {
817870

818871
void CompilerInstance::getImplicitlyImportedModules(
819872
SmallVectorImpl<ModuleDecl *> &importModules) {
820-
FrontendStatsTracer tracer(Context->Stats, "get-implicitly-imported-modules");
873+
FrontendStatsTracer tracer(getStatsReporter(), "get-implicitly-imported-modules");
821874
for (auto &ImplicitImportModuleName :
822875
Invocation.getFrontendOptions().ImplicitImportModuleNames) {
823876
if (Lexer::isIdentifier(ImplicitImportModuleName)) {
@@ -851,7 +904,7 @@ void CompilerInstance::addMainFileToModule(
851904

852905
void CompilerInstance::parseAndCheckTypesUpTo(
853906
const ImplicitImports &implicitImports, SourceFile::ASTStage_t limitStage) {
854-
FrontendStatsTracer tracer(Context->Stats, "parse-and-check-types");
907+
FrontendStatsTracer tracer(getStatsReporter(), "parse-and-check-types");
855908

856909
PersistentState = std::make_unique<PersistentParserState>();
857910

@@ -913,7 +966,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
913966

914967
void CompilerInstance::parseLibraryFile(
915968
unsigned BufferID, const ImplicitImports &implicitImports) {
916-
FrontendStatsTracer tracer(Context->Stats, "parse-library-file");
969+
FrontendStatsTracer tracer(getStatsReporter(), "parse-library-file");
917970

918971
auto *NextInput = createSourceFileForMainModule(
919972
SourceFileKind::Library, implicitImports.kind, BufferID);
@@ -935,7 +988,7 @@ void CompilerInstance::parseLibraryFile(
935988

936989
bool CompilerInstance::parsePartialModulesAndLibraryFiles(
937990
const ImplicitImports &implicitImports) {
938-
FrontendStatsTracer tracer(Context->Stats,
991+
FrontendStatsTracer tracer(getStatsReporter(),
939992
"parse-partial-modules-and-library-files");
940993
bool hadLoadError = false;
941994
// Parse all the partial modules first.
@@ -960,7 +1013,7 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
9601013
void CompilerInstance::parseAndTypeCheckMainFileUpTo(
9611014
SourceFile::ASTStage_t LimitStage) {
9621015
assert(LimitStage >= SourceFile::NameBound);
963-
FrontendStatsTracer tracer(Context->Stats,
1016+
FrontendStatsTracer tracer(getStatsReporter(),
9641017
"parse-and-typecheck-main-file");
9651018
bool mainIsPrimary =
9661019
(isWholeModuleCompilation() || isPrimaryInput(MainBufferID));
@@ -1183,8 +1236,7 @@ static void countStatsPostSILOpt(UnifiedStatsReporter &Stats,
11831236
C.NumSILOptGlobalVariables += Module.getSILGlobalList().size();
11841237
}
11851238

1186-
bool CompilerInstance::performSILProcessing(SILModule *silModule,
1187-
UnifiedStatsReporter *stats) {
1239+
bool CompilerInstance::performSILProcessing(SILModule *silModule) {
11881240
if (performMandatorySILPasses(Invocation, silModule))
11891241
return true;
11901242

@@ -1196,7 +1248,7 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule,
11961248

11971249
performSILOptimizations(Invocation, silModule);
11981250

1199-
if (stats)
1251+
if (auto *stats = getStatsReporter())
12001252
countStatsPostSILOpt(*stats, *silModule);
12011253

12021254
{

0 commit comments

Comments
 (0)