Skip to content

[Frontend] Clean Up Usage of UnifiedStatsReporter #30109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ class CompilerInstance {

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

mutable ModuleDecl *MainModule = nullptr;
SerializedModuleLoader *SML = nullptr;
Expand Down Expand Up @@ -488,6 +491,8 @@ class CompilerInstance {
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }

UnifiedStatsReporter *getStatsReporter() const { return Stats.get(); }

SILModule *getSILModule() {
return TheSILModule.get();
}
Expand Down Expand Up @@ -571,6 +576,7 @@ class CompilerInstance {

bool setUpInputs();
bool setUpASTContextIfNeeded();
void setupStatsReporter();
Optional<unsigned> setUpCodeCompletionBuffer();

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

private:
SourceFile *
Expand Down
20 changes: 11 additions & 9 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace swift {
class SILModule;
class SILParserTUState;
class SourceFile;
enum class SourceFileKind;
class SourceManager;
class SyntaxParseActions;
class SyntaxParsingCache;
Expand All @@ -70,7 +71,6 @@ namespace swift {
class TypeCheckerOptions;
struct TypeLoc;
class UnifiedStatsReporter;
enum class SourceFileKind;

namespace Lowering {
class TypeConverter;
Expand Down Expand Up @@ -286,29 +286,31 @@ namespace swift {
StringRef OutputPath);

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

/// Run the LLVM passes. In multi-threaded compilation this will be done for
/// multiple LLVM modules in parallel.
/// \param Diags may be null if LLVM code gen diagnostics are not required.
/// \param DiagMutex may also be null if a mutex around \p Diags is not
/// required.
/// \param Diags The Diagnostic Engine.
/// \param DiagMutex in contexts that require parallel codegen, a mutex that the
/// diagnostic engine uses to synchronize emission.
/// \param HashGlobal used with incremental LLVMCodeGen to know if a module
/// was already compiled, may be null if not desired.
/// \param Module LLVM module to code gen, required.
/// \param TargetMachine target of code gen, required.
/// \param effectiveLanguageVersion version of the language, effectively.
/// \param OutputFilename Filename for output.
bool performLLVM(const IRGenOptions &Opts, DiagnosticEngine *Diags,
bool performLLVM(const IRGenOptions &Opts,
DiagnosticEngine &Diags,
llvm::sys::Mutex *DiagMutex,
llvm::GlobalVariable *HashGlobal,
llvm::Module *Module,
llvm::TargetMachine *TargetMachine,
const version::Version &effectiveLanguageVersion,
StringRef OutputFilename,
UnifiedStatsReporter *Stats=nullptr);
UnifiedStatsReporter *Stats);

/// Dump YAML describing all fixed-size types imported from the given module.
bool performDumpTypeInfo(const IRGenOptions &Opts,
Expand Down
5 changes: 2 additions & 3 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,12 @@ llvm::BumpPtrAllocator &ASTContext::getAllocator(AllocationArena arena) const {

/// Set a new stats reporter.
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
Stats = stats;
evaluator.setStatsReporter(stats);

if (stats) {
stats->getFrontendCounters().NumASTBytesAllocated =
getAllocator().getBytesAllocated();
}
evaluator.setStatsReporter(stats);
Stats = stats;
}

RC<syntax::SyntaxArena> ASTContext::getSyntaxArena() const {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5018,8 +5018,8 @@ ProtocolDecl::setLazyRequirementSignature(LazyMemberLoader *lazyLoader,

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

ArrayRef<Requirement> ProtocolDecl::getCachedRequirementSignature() const {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3458,8 +3458,8 @@ void EquivalenceClass::modified(GenericSignatureBuilder &builder) {
GenericSignatureBuilder::GenericSignatureBuilder(
ASTContext &ctx)
: Context(ctx), Diags(Context.Diags), Impl(new Implementation) {
if (Context.Stats)
Context.Stats->getFrontendCounters().NumGenericSignatureBuilders++;
if (auto *Stats = Context.Stats)
Stats->getFrontendCounters().NumGenericSignatureBuilders++;
}

GenericSignatureBuilder::GenericSignatureBuilder(
Expand Down
6 changes: 4 additions & 2 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ void ModuleDecl::lookupClassMember(AccessPathTy accessPath,
stats->getFrontendCounters().NumModuleLookupClassMember++;

if (isParsedModule(this)) {
FrontendStatsTracer tracer(getASTContext().Stats, "source-file-lookup-class-member");
FrontendStatsTracer tracer(getASTContext().Stats,
"source-file-lookup-class-member");
auto &cache = getSourceLookupCache();
cache.populateMemberCache(*this);
cache.lookupClassMember(accessPath, name, results);
Expand All @@ -642,7 +643,8 @@ void ModuleDecl::lookupClassMember(AccessPathTy accessPath,
void SourceFile::lookupClassMember(ModuleDecl::AccessPathTy accessPath,
DeclName name,
SmallVectorImpl<ValueDecl*> &results) const {
FrontendStatsTracer tracer(getASTContext().Stats, "source-file-lookup-class-member");
FrontendStatsTracer tracer(getASTContext().Stats,
"source-file-lookup-class-member");
auto &cache = getCache();
cache.populateMemberCache(*this);
cache.lookupClassMember(accessPath, name, results);
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/UnqualifiedLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
auto localCounter = lookupCounter;
(void)localCounter; // for debugging
#endif
FrontendStatsTracer StatsTracer(Ctx.Stats, "performUnqualifedLookup",
FrontendStatsTracer StatsTracer(Ctx.Stats,
"performUnqualifedLookup",
DC->getParentSourceFile());

const Optional<bool> initialIsCascadingUse = getInitialIsCascadingUse();
Expand Down
7 changes: 4 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7859,8 +7859,8 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
void ClangImporter::Implementation::startedImportingEntity() {
++NumTotalImportedEntities;
// FIXME: (transitional) increment the redundant "always-on" counter.
if (SwiftContext.Stats)
SwiftContext.Stats->getFrontendCounters().NumTotalClangImportedEntities++;
if (auto *Stats = SwiftContext.Stats)
Stats->getFrontendCounters().NumTotalClangImportedEntities++;
}

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

FrontendStatsTracer tracer(D->getASTContext().Stats, "load-all-members", D);
FrontendStatsTracer tracer(D->getASTContext().Stats,
"load-all-members", D);
assert(D);

// Check whether we're importing an Objective-C container of some sort.
Expand Down
76 changes: 64 additions & 12 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "clang/AST/ASTContext.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
Expand Down Expand Up @@ -238,6 +239,56 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
return false;
}

void CompilerInstance::setupStatsReporter() {
const auto &Invok = getInvocation();
const std::string &StatsOutputDir =
Invok.getFrontendOptions().StatsOutputDir;
if (StatsOutputDir.empty())
return;

auto silOptModeArgStr = [](OptimizationMode mode) -> StringRef {
switch (mode) {
case OptimizationMode::ForSpeed:
return "O";
case OptimizationMode::ForSize:
return "Osize";
default:
return "Onone";
}
};

auto getClangSourceManager = [](ASTContext &Ctx) -> clang::SourceManager * {
if (auto *clangImporter = static_cast<ClangImporter *>(
Ctx.getClangModuleLoader())) {
return &clangImporter->getClangASTContext().getSourceManager();
}
return nullptr;
};

const auto &FEOpts = Invok.getFrontendOptions();
const auto &LangOpts = Invok.getLangOptions();
const auto &SILOpts = Invok.getSILOptions();
const std::string &OutFile =
FEOpts.InputsAndOutputs.lastInputProducingOutput().outputFilename();
auto Reporter = std::make_unique<UnifiedStatsReporter>(
"swift-frontend",
FEOpts.ModuleName,
FEOpts.InputsAndOutputs.getStatsFileMangledInputName(),
LangOpts.Target.normalize(),
llvm::sys::path::extension(OutFile),
silOptModeArgStr(SILOpts.OptMode),
StatsOutputDir,
&getSourceMgr(),
getClangSourceManager(getASTContext()),
Invok.getFrontendOptions().TraceStats,
Invok.getFrontendOptions().ProfileEvents,
Invok.getFrontendOptions().ProfileEntities);
// Hand the stats reporter down to the ASTContext so the rest of the compiler
// can use it.
getASTContext().setStatsReporter(Reporter.get());
Stats = std::move(Reporter);
}

bool CompilerInstance::setup(const CompilerInvocation &Invok) {
Invocation = Invok;

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

setupStatsReporter();

return false;
}

Expand Down Expand Up @@ -712,7 +765,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
return performParseOnly();
}

FrontendStatsTracer tracer(Context->Stats, "perform-sema");
FrontendStatsTracer tracer(getStatsReporter(), "perform-sema");

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

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

if (!M) {
Expand All @@ -789,7 +842,7 @@ bool CompilerInstance::loadStdlib() {
}

ModuleDecl *CompilerInstance::importUnderlyingModule() {
FrontendStatsTracer tracer(Context->Stats, "import-underlying-module");
FrontendStatsTracer tracer(getStatsReporter(), "import-underlying-module");
ModuleDecl *objCModuleUnderlyingMixedFramework =
static_cast<ClangImporter *>(Context->getClangModuleLoader())
->loadModule(SourceLoc(),
Expand All @@ -802,7 +855,7 @@ ModuleDecl *CompilerInstance::importUnderlyingModule() {
}

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

void CompilerInstance::getImplicitlyImportedModules(
SmallVectorImpl<ModuleDecl *> &importModules) {
FrontendStatsTracer tracer(Context->Stats, "get-implicitly-imported-modules");
FrontendStatsTracer tracer(getStatsReporter(), "get-implicitly-imported-modules");
for (auto &ImplicitImportModuleName :
Invocation.getFrontendOptions().ImplicitImportModuleNames) {
if (Lexer::isIdentifier(ImplicitImportModuleName)) {
Expand Down Expand Up @@ -851,7 +904,7 @@ void CompilerInstance::addMainFileToModule(

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

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

Expand Down Expand Up @@ -913,7 +966,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(

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

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

bool CompilerInstance::parsePartialModulesAndLibraryFiles(
const ImplicitImports &implicitImports) {
FrontendStatsTracer tracer(Context->Stats,
FrontendStatsTracer tracer(getStatsReporter(),
"parse-partial-modules-and-library-files");
bool hadLoadError = false;
// Parse all the partial modules first.
Expand All @@ -960,7 +1013,7 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
void CompilerInstance::parseAndTypeCheckMainFileUpTo(
SourceFile::ASTStage_t LimitStage) {
assert(LimitStage >= SourceFile::NameBound);
FrontendStatsTracer tracer(Context->Stats,
FrontendStatsTracer tracer(getStatsReporter(),
"parse-and-typecheck-main-file");
bool mainIsPrimary =
(isWholeModuleCompilation() || isPrimaryInput(MainBufferID));
Expand Down Expand Up @@ -1183,8 +1236,7 @@ static void countStatsPostSILOpt(UnifiedStatsReporter &Stats,
C.NumSILOptGlobalVariables += Module.getSILGlobalList().size();
}

bool CompilerInstance::performSILProcessing(SILModule *silModule,
UnifiedStatsReporter *stats) {
bool CompilerInstance::performSILProcessing(SILModule *silModule) {
if (performMandatorySILPasses(Invocation, silModule))
return true;

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

performSILOptimizations(Invocation, silModule);

if (stats)
if (auto *stats = getStatsReporter())
countStatsPostSILOpt(*stats, *silModule);

{
Expand Down
Loading