[NFC] UnifiedStatsReporter is owned by CompilerInstance

The lifetime of the UnifiedStatsReporter was not entirely clear from context. Stick it in the CompilerInstance instead so it can live as long as the compile job.

It is critical that its lifetime be extended beyond that of the ASTContext, as the context may be torn down by the time code generation happens, but additional statistics are recorded during LLVM codegen.
This commit is contained in:
Robert Widmann
2020-02-27 16:01:35 -08:00
parent 2dfd5694fa
commit 535bb9bf8f
3 changed files with 70 additions and 66 deletions

View File

@@ -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"
@@ -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;
@@ -281,6 +332,8 @@ bool CompilerInstance::setup(const CompilerInvocation &Invok) {
if (setUpASTContextIfNeeded())
return true;
setupStatsReporter();
return false;
}
@@ -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;
@@ -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) {
@@ -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(),
@@ -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 =
@@ -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)) {
@@ -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>();
@@ -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);
@@ -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.
@@ -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));
@@ -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;
@@ -1196,7 +1248,7 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule,
performSILOptimizations(Invocation, silModule);
if (stats)
if (auto *stats = getStatsReporter())
countStatsPostSILOpt(*stats, *silModule);
{