Skip to content

Commit 535bb9b

Browse files
committed
[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.
1 parent 2dfd569 commit 535bb9b

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 6 additions & 0 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.

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
{

lib/FrontendTool/FrontendTool.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,51 +1779,6 @@ static bool dumpAPI(ModuleDecl *Mod, StringRef OutDir) {
17791779
return false;
17801780
}
17811781

1782-
static StringRef
1783-
silOptModeArgStr(OptimizationMode mode) {
1784-
switch (mode) {
1785-
case OptimizationMode::ForSpeed:
1786-
return "O";
1787-
case OptimizationMode::ForSize:
1788-
return "Osize";
1789-
default:
1790-
return "Onone";
1791-
}
1792-
}
1793-
1794-
static std::unique_ptr<UnifiedStatsReporter>
1795-
computeStatsReporter(const CompilerInvocation &Invocation, CompilerInstance *Instance) {
1796-
const std::string &StatsOutputDir =
1797-
Invocation.getFrontendOptions().StatsOutputDir;
1798-
std::unique_ptr<UnifiedStatsReporter> StatsReporter;
1799-
if (StatsOutputDir.empty())
1800-
return std::unique_ptr<UnifiedStatsReporter>();
1801-
1802-
auto &FEOpts = Invocation.getFrontendOptions();
1803-
auto &LangOpts = Invocation.getLangOptions();
1804-
auto &SILOpts = Invocation.getSILOptions();
1805-
std::string InputName =
1806-
FEOpts.InputsAndOutputs.getStatsFileMangledInputName();
1807-
StringRef OptType = silOptModeArgStr(SILOpts.OptMode);
1808-
const std::string &OutFile =
1809-
FEOpts.InputsAndOutputs.lastInputProducingOutput().outputFilename();
1810-
StringRef OutputType = llvm::sys::path::extension(OutFile);
1811-
std::string TripleName = LangOpts.Target.normalize();
1812-
auto Trace = Invocation.getFrontendOptions().TraceStats;
1813-
auto ProfileEvents = Invocation.getFrontendOptions().ProfileEvents;
1814-
auto ProfileEntities = Invocation.getFrontendOptions().ProfileEntities;
1815-
SourceManager *SM = &Instance->getSourceMgr();
1816-
clang::SourceManager *CSM = nullptr;
1817-
if (auto *clangImporter = static_cast<ClangImporter *>(
1818-
Instance->getASTContext().getClangModuleLoader())) {
1819-
CSM = &clangImporter->getClangASTContext().getSourceManager();
1820-
}
1821-
return std::make_unique<UnifiedStatsReporter>(
1822-
"swift-frontend", FEOpts.ModuleName, InputName, TripleName, OutputType,
1823-
OptType, StatsOutputDir, SM, CSM, Trace,
1824-
ProfileEvents, ProfileEntities);
1825-
}
1826-
18271782
/// Creates a diagnostic consumer that handles dispatching diagnostics to
18281783
/// multiple output files, based on the supplementary output paths specified by
18291784
/// \p inputsAndOutputs.
@@ -2184,15 +2139,6 @@ int swift::performFrontend(ArrayRef<const char *> Args,
21842139
return finishDiagProcessing(1);
21852140
}
21862141

2187-
std::unique_ptr<UnifiedStatsReporter> StatsReporter =
2188-
computeStatsReporter(Invocation, Instance.get());
2189-
if (StatsReporter) {
2190-
// Install stats-reporter somewhere visible for subsystems that
2191-
// need to bump counters as they work, rather than measure
2192-
// accumulated work on completion (mostly: TypeChecker).
2193-
Instance->getASTContext().setStatsReporter(StatsReporter.get());
2194-
}
2195-
21962142
// The compiler instance has been configured; notify our observer.
21972143
if (observer) {
21982144
observer->configuredCompiler(*Instance);

0 commit comments

Comments
 (0)