35
35
#include " swift/Serialization/SerializedModuleLoader.h"
36
36
#include " swift/Strings.h"
37
37
#include " swift/Subsystems.h"
38
+ #include " clang/AST/ASTContext.h"
38
39
#include " llvm/ADT/Hashing.h"
39
40
#include " llvm/ADT/SmallVector.h"
40
41
#include " llvm/ADT/Triple.h"
@@ -238,6 +239,56 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
238
239
return false ;
239
240
}
240
241
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
+
241
292
bool CompilerInstance::setup (const CompilerInvocation &Invok) {
242
293
Invocation = Invok;
243
294
@@ -281,6 +332,8 @@ bool CompilerInstance::setup(const CompilerInvocation &Invok) {
281
332
if (setUpASTContextIfNeeded ())
282
333
return true ;
283
334
335
+ setupStatsReporter ();
336
+
284
337
return false ;
285
338
}
286
339
@@ -712,7 +765,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
712
765
return performParseOnly ();
713
766
}
714
767
715
- FrontendStatsTracer tracer (Context-> Stats , " perform-sema" );
768
+ FrontendStatsTracer tracer (getStatsReporter () , " perform-sema" );
716
769
717
770
ModuleDecl *mainModule = getMainModule ();
718
771
Context->LoadedModules [mainModule->getName ()] = mainModule;
@@ -770,7 +823,7 @@ CompilerInstance::ImplicitImports::ImplicitImports(CompilerInstance &compiler) {
770
823
}
771
824
772
825
bool CompilerInstance::loadStdlib () {
773
- FrontendStatsTracer tracer (Context-> Stats , " load-stdlib" );
826
+ FrontendStatsTracer tracer (getStatsReporter () , " load-stdlib" );
774
827
ModuleDecl *M = Context->getStdlibModule (true );
775
828
776
829
if (!M) {
@@ -789,7 +842,7 @@ bool CompilerInstance::loadStdlib() {
789
842
}
790
843
791
844
ModuleDecl *CompilerInstance::importUnderlyingModule () {
792
- FrontendStatsTracer tracer (Context-> Stats , " import-underlying-module" );
845
+ FrontendStatsTracer tracer (getStatsReporter () , " import-underlying-module" );
793
846
ModuleDecl *objCModuleUnderlyingMixedFramework =
794
847
static_cast <ClangImporter *>(Context->getClangModuleLoader ())
795
848
->loadModule (SourceLoc (),
@@ -802,7 +855,7 @@ ModuleDecl *CompilerInstance::importUnderlyingModule() {
802
855
}
803
856
804
857
ModuleDecl *CompilerInstance::importBridgingHeader () {
805
- FrontendStatsTracer tracer (Context-> Stats , " import-bridging-header" );
858
+ FrontendStatsTracer tracer (getStatsReporter () , " import-bridging-header" );
806
859
const StringRef implicitHeaderPath =
807
860
Invocation.getFrontendOptions ().ImplicitObjCHeaderPath ;
808
861
auto clangImporter =
@@ -817,7 +870,7 @@ ModuleDecl *CompilerInstance::importBridgingHeader() {
817
870
818
871
void CompilerInstance::getImplicitlyImportedModules (
819
872
SmallVectorImpl<ModuleDecl *> &importModules) {
820
- FrontendStatsTracer tracer (Context-> Stats , " get-implicitly-imported-modules" );
873
+ FrontendStatsTracer tracer (getStatsReporter () , " get-implicitly-imported-modules" );
821
874
for (auto &ImplicitImportModuleName :
822
875
Invocation.getFrontendOptions ().ImplicitImportModuleNames ) {
823
876
if (Lexer::isIdentifier (ImplicitImportModuleName)) {
@@ -851,7 +904,7 @@ void CompilerInstance::addMainFileToModule(
851
904
852
905
void CompilerInstance::parseAndCheckTypesUpTo (
853
906
const ImplicitImports &implicitImports, SourceFile::ASTStage_t limitStage) {
854
- FrontendStatsTracer tracer (Context-> Stats , " parse-and-check-types" );
907
+ FrontendStatsTracer tracer (getStatsReporter () , " parse-and-check-types" );
855
908
856
909
PersistentState = std::make_unique<PersistentParserState>();
857
910
@@ -913,7 +966,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
913
966
914
967
void CompilerInstance::parseLibraryFile (
915
968
unsigned BufferID, const ImplicitImports &implicitImports) {
916
- FrontendStatsTracer tracer (Context-> Stats , " parse-library-file" );
969
+ FrontendStatsTracer tracer (getStatsReporter () , " parse-library-file" );
917
970
918
971
auto *NextInput = createSourceFileForMainModule (
919
972
SourceFileKind::Library, implicitImports.kind , BufferID);
@@ -935,7 +988,7 @@ void CompilerInstance::parseLibraryFile(
935
988
936
989
bool CompilerInstance::parsePartialModulesAndLibraryFiles (
937
990
const ImplicitImports &implicitImports) {
938
- FrontendStatsTracer tracer (Context-> Stats ,
991
+ FrontendStatsTracer tracer (getStatsReporter () ,
939
992
" parse-partial-modules-and-library-files" );
940
993
bool hadLoadError = false ;
941
994
// Parse all the partial modules first.
@@ -960,7 +1013,7 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
960
1013
void CompilerInstance::parseAndTypeCheckMainFileUpTo (
961
1014
SourceFile::ASTStage_t LimitStage) {
962
1015
assert (LimitStage >= SourceFile::NameBound);
963
- FrontendStatsTracer tracer (Context-> Stats ,
1016
+ FrontendStatsTracer tracer (getStatsReporter () ,
964
1017
" parse-and-typecheck-main-file" );
965
1018
bool mainIsPrimary =
966
1019
(isWholeModuleCompilation () || isPrimaryInput (MainBufferID));
@@ -1183,8 +1236,7 @@ static void countStatsPostSILOpt(UnifiedStatsReporter &Stats,
1183
1236
C.NumSILOptGlobalVariables += Module.getSILGlobalList ().size ();
1184
1237
}
1185
1238
1186
- bool CompilerInstance::performSILProcessing (SILModule *silModule,
1187
- UnifiedStatsReporter *stats) {
1239
+ bool CompilerInstance::performSILProcessing (SILModule *silModule) {
1188
1240
if (performMandatorySILPasses (Invocation, silModule))
1189
1241
return true ;
1190
1242
@@ -1196,7 +1248,7 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule,
1196
1248
1197
1249
performSILOptimizations (Invocation, silModule);
1198
1250
1199
- if (stats)
1251
+ if (auto * stats = getStatsReporter () )
1200
1252
countStatsPostSILOpt (*stats, *silModule);
1201
1253
1202
1254
{
0 commit comments