@@ -426,7 +426,7 @@ static void countStatsPostSILOpt(UnifiedStatsReporter &Stats,
426
426
// / \param Instance Will be reset after performIRGeneration when the verifier
427
427
// / mode is NoVerify and there were no errors.
428
428
// / \returns true on error
429
- static bool performCompile (std::unique_ptr< CompilerInstance> &Instance,
429
+ static bool performCompile (CompilerInstance &Instance,
430
430
CompilerInvocation &Invocation,
431
431
ArrayRef<const char *> Args,
432
432
int &ReturnValue,
@@ -439,7 +439,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
439
439
// avoid touching any other inputs and just parse, emit and exit.
440
440
if (Action == FrontendOptions::EmitPCH) {
441
441
auto clangImporter = static_cast <ClangImporter *>(
442
- Instance-> getASTContext ().getClangModuleLoader ());
442
+ Instance. getASTContext ().getClangModuleLoader ());
443
443
return clangImporter->emitBridgingPCH (
444
444
Invocation.getInputFilenames ()[0 ],
445
445
opts.getSingleOutputFilename ());
@@ -457,7 +457,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
457
457
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
458
458
llvm::MemoryBuffer::getFileOrSTDIN (Invocation.getInputFilenames ()[0 ]);
459
459
if (!FileBufOrErr) {
460
- Instance-> getASTContext ().Diags .diagnose (SourceLoc (),
460
+ Instance. getASTContext ().Diags .diagnose (SourceLoc (),
461
461
diag::error_open_input_file,
462
462
Invocation.getInputFilenames ()[0 ],
463
463
FileBufOrErr.getError ().message ());
@@ -472,7 +472,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
472
472
if (!Module) {
473
473
// TODO: Translate from the diagnostic info to the SourceManager location
474
474
// if available.
475
- Instance-> getASTContext ().Diags .diagnose (SourceLoc (),
475
+ Instance. getASTContext ().Diags .diagnose (SourceLoc (),
476
476
diag::error_parse_input_file,
477
477
Invocation.getInputFilenames ()[0 ],
478
478
Err.getMessage ());
@@ -482,31 +482,31 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
482
482
// TODO: remove once the frontend understands what action it should perform
483
483
IRGenOpts.OutputKind = getOutputKind (Action);
484
484
485
- return performLLVM (IRGenOpts, Instance-> getASTContext (), Module.get ());
485
+ return performLLVM (IRGenOpts, Instance. getASTContext (), Module.get ());
486
486
}
487
487
488
488
ReferencedNameTracker nameTracker;
489
489
bool shouldTrackReferences = !opts.ReferenceDependenciesFilePath .empty ();
490
490
if (shouldTrackReferences)
491
- Instance-> setReferencedNameTracker (&nameTracker);
491
+ Instance. setReferencedNameTracker (&nameTracker);
492
492
493
493
if (Action == FrontendOptions::Parse ||
494
494
Action == FrontendOptions::DumpParse ||
495
495
Action == FrontendOptions::DumpInterfaceHash ||
496
496
Action == FrontendOptions::EmitImportedModules)
497
- Instance-> performParseOnly ();
497
+ Instance. performParseOnly ();
498
498
else
499
- Instance-> performSema ();
499
+ Instance. performSema ();
500
500
501
501
if (Action == FrontendOptions::Parse)
502
- return Instance-> getASTContext ().hadError ();
502
+ return Instance. getASTContext ().hadError ();
503
503
504
504
if (observer) {
505
- observer->performedSemanticAnalysis (* Instance);
505
+ observer->performedSemanticAnalysis (Instance);
506
506
}
507
507
508
508
if (Stats) {
509
- countStatsPostSema (*Stats, * Instance);
509
+ countStatsPostSema (*Stats, Instance);
510
510
}
511
511
512
512
FrontendOptions::DebugCrashMode CrashMode = opts.CrashMode ;
@@ -515,19 +515,19 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
515
515
else if (CrashMode == FrontendOptions::DebugCrashMode::CrashAfterParse)
516
516
debugFailWithCrash ();
517
517
518
- ASTContext &Context = Instance-> getASTContext ();
518
+ ASTContext &Context = Instance. getASTContext ();
519
519
520
520
if (Invocation.getMigratorOptions ().shouldRunMigrator ()) {
521
- migrator::updateCodeAndEmitRemap (Instance. get () , Invocation);
521
+ migrator::updateCodeAndEmitRemap (& Instance, Invocation);
522
522
}
523
523
524
524
if (Action == FrontendOptions::REPL) {
525
- runREPL (* Instance, ProcessCmdLine (Args.begin (), Args.end ()),
525
+ runREPL (Instance, ProcessCmdLine (Args.begin (), Args.end ()),
526
526
Invocation.getParseStdlib ());
527
527
return Context.hadError ();
528
528
}
529
529
530
- SourceFile *PrimarySourceFile = Instance-> getPrimarySourceFile ();
530
+ SourceFile *PrimarySourceFile = Instance. getPrimarySourceFile ();
531
531
532
532
// We've been told to dump the AST (either after parsing or type-checking,
533
533
// which is already differentiated in CompilerInstance::performSema()),
@@ -541,7 +541,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
541
541
SourceFile *SF = PrimarySourceFile;
542
542
if (!SF) {
543
543
SourceFileKind Kind = Invocation.getSourceFileKind ();
544
- SF = &Instance-> getMainModule ()->getMainSourceFile (Kind);
544
+ SF = &Instance. getMainModule ()->getMainSourceFile (Kind);
545
545
}
546
546
if (Action == FrontendOptions::PrintAST)
547
547
SF->print (llvm::outs (), PrintOptions::printEverything ());
@@ -551,7 +551,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
551
551
if (opts.DumpScopeMapLocations .empty ()) {
552
552
scope.expandAll ();
553
553
} else if (auto bufferID = SF->getBufferID ()) {
554
- SourceManager &sourceMgr = Instance-> getSourceMgr ();
554
+ SourceManager &sourceMgr = Instance. getSourceMgr ();
555
555
// Probe each of the locations, and dump what we find.
556
556
for (auto lineColumn : opts.DumpScopeMapLocations ) {
557
557
SourceLoc loc = sourceMgr.getLocForLineCol (*bufferID,
@@ -597,7 +597,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
597
597
SF->dump ();
598
598
return Context.hadError ();
599
599
} else if (Action == FrontendOptions::EmitImportedModules) {
600
- emitImportedModules (Context, Instance-> getMainModule (), opts);
600
+ emitImportedModules (Context, Instance. getMainModule (), opts);
601
601
return Context.hadError ();
602
602
}
603
603
@@ -606,15 +606,15 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
606
606
Context.getClangModuleLoader ()->printStatistics ();
607
607
608
608
if (!opts.DependenciesFilePath .empty ())
609
- (void )emitMakeDependencies (Context.Diags , *Instance-> getDependencyTracker (),
609
+ (void )emitMakeDependencies (Context.Diags , *Instance. getDependencyTracker (),
610
610
opts);
611
611
612
612
if (shouldTrackReferences)
613
- emitReferenceDependencies (Context.Diags , Instance-> getPrimarySourceFile (),
614
- *Instance-> getDependencyTracker (), opts);
613
+ emitReferenceDependencies (Context.Diags , Instance. getPrimarySourceFile (),
614
+ *Instance. getDependencyTracker (), opts);
615
615
616
616
if (!opts.LoadedModuleTracePath .empty ())
617
- (void )emitLoadedModuleTrace (Context, *Instance-> getDependencyTracker (),
617
+ (void )emitLoadedModuleTrace (Context, *Instance. getDependencyTracker (),
618
618
opts);
619
619
620
620
if (Context.hadError ())
@@ -623,39 +623,39 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
623
623
// FIXME: This is still a lousy approximation of whether the module file will
624
624
// be externally consumed.
625
625
bool moduleIsPublic =
626
- !Instance-> getMainModule ()->hasEntryPoint () &&
626
+ !Instance. getMainModule ()->hasEntryPoint () &&
627
627
opts.ImplicitObjCHeaderPath .empty () &&
628
628
!Context.LangOpts .EnableAppExtensionRestrictions ;
629
629
630
630
// We've just been told to perform a typecheck, so we can return now.
631
631
if (Action == FrontendOptions::Typecheck) {
632
632
if (!opts.ObjCHeaderOutputPath .empty ())
633
- return printAsObjC (opts.ObjCHeaderOutputPath , Instance-> getMainModule (),
633
+ return printAsObjC (opts.ObjCHeaderOutputPath , Instance. getMainModule (),
634
634
opts.ImplicitObjCHeaderPath , moduleIsPublic);
635
635
return Context.hadError ();
636
636
}
637
637
638
638
if (Action == FrontendOptions::EmitTBD) {
639
639
auto hasMultipleIRGenThreads = Invocation.getSILOptions ().NumThreads > 1 ;
640
- return writeTBD (Instance-> getMainModule (), hasMultipleIRGenThreads,
640
+ return writeTBD (Instance. getMainModule (), hasMultipleIRGenThreads,
641
641
opts.getSingleOutputFilename ());
642
642
}
643
643
644
644
assert (Action >= FrontendOptions::EmitSILGen &&
645
645
" All actions not requiring SILGen must have been handled!" );
646
646
647
- std::unique_ptr<SILModule> SM = Instance-> takeSILModule ();
647
+ std::unique_ptr<SILModule> SM = Instance. takeSILModule ();
648
648
if (!SM) {
649
649
if (opts.PrimaryInput .hasValue () && opts.PrimaryInput .getValue ().isFilename ()) {
650
650
FileUnit *PrimaryFile = PrimarySourceFile;
651
651
if (!PrimaryFile) {
652
652
auto Index = opts.PrimaryInput .getValue ().Index ;
653
- PrimaryFile = Instance-> getMainModule ()->getFiles ()[Index];
653
+ PrimaryFile = Instance. getMainModule ()->getFiles ()[Index];
654
654
}
655
655
SM = performSILGeneration (*PrimaryFile, Invocation.getSILOptions (),
656
656
None, opts.SILSerializeAll );
657
657
} else {
658
- SM = performSILGeneration (Instance-> getMainModule (), Invocation.getSILOptions (),
658
+ SM = performSILGeneration (Instance. getMainModule (), Invocation.getSILOptions (),
659
659
opts.SILSerializeAll ,
660
660
true );
661
661
}
@@ -673,7 +673,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
673
673
// If we are asked to link all, link all.
674
674
if (Invocation.getSILOptions ().LinkMode == SILOptions::LinkAll)
675
675
performSILLinking (SM.get (), true );
676
- return writeSIL (*SM, Instance-> getMainModule (), opts.EmitVerboseSIL ,
676
+ return writeSIL (*SM, Instance. getMainModule (), opts.EmitVerboseSIL ,
677
677
opts.getSingleOutputFilename (), opts.EmitSortedSIL );
678
678
}
679
679
@@ -683,7 +683,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
683
683
performSILLinking (SM.get (), true );
684
684
685
685
auto DC = PrimarySourceFile ? ModuleOrSourceFile (PrimarySourceFile) :
686
- Instance-> getMainModule ();
686
+ Instance. getMainModule ();
687
687
if (!opts.ModuleOutputPath .empty ()) {
688
688
SerializationOptions serializationOpts;
689
689
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
@@ -769,13 +769,13 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
769
769
}
770
770
771
771
if (!opts.ObjCHeaderOutputPath .empty ()) {
772
- (void )printAsObjC (opts.ObjCHeaderOutputPath , Instance-> getMainModule (),
772
+ (void )printAsObjC (opts.ObjCHeaderOutputPath , Instance. getMainModule (),
773
773
opts.ImplicitObjCHeaderPath , moduleIsPublic);
774
774
}
775
775
776
776
if (Action == FrontendOptions::EmitSIB) {
777
777
auto DC = PrimarySourceFile ? ModuleOrSourceFile (PrimarySourceFile) :
778
- Instance-> getMainModule ();
778
+ Instance. getMainModule ();
779
779
if (!opts.ModuleOutputPath .empty ()) {
780
780
SerializationOptions serializationOpts;
781
781
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
@@ -789,7 +789,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
789
789
790
790
if (!opts.ModuleOutputPath .empty () || !opts.ModuleDocOutputPath .empty ()) {
791
791
auto DC = PrimarySourceFile ? ModuleOrSourceFile (PrimarySourceFile) :
792
- Instance-> getMainModule ();
792
+ Instance. getMainModule ();
793
793
if (!opts.ModuleOutputPath .empty ()) {
794
794
SerializationOptions serializationOpts;
795
795
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
@@ -824,7 +824,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
824
824
825
825
// We've been told to write canonical SIL, so write it now.
826
826
if (Action == FrontendOptions::EmitSIL) {
827
- return writeSIL (*SM, Instance-> getMainModule (), opts.EmitVerboseSIL ,
827
+ return writeSIL (*SM, Instance. getMainModule (), opts.EmitVerboseSIL ,
828
828
opts.getSingleOutputFilename (), opts.EmitSortedSIL );
829
829
}
830
830
@@ -848,14 +848,14 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
848
848
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
849
849
const ProcessCmdLine &CmdLine = ProcessCmdLine (opts.ImmediateArgv .begin (),
850
850
opts.ImmediateArgv .end ());
851
- Instance-> setSILModule (std::move (SM));
851
+ Instance. setSILModule (std::move (SM));
852
852
853
853
if (observer) {
854
- observer->aboutToRunImmediately (* Instance);
854
+ observer->aboutToRunImmediately (Instance);
855
855
}
856
856
857
857
ReturnValue =
858
- RunImmediately (* Instance, CmdLine, IRGenOpts, Invocation.getSILOptions ());
858
+ RunImmediately (Instance, CmdLine, IRGenOpts, Invocation.getSILOptions ());
859
859
return Context.hadError ();
860
860
}
861
861
@@ -869,14 +869,14 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
869
869
opts.getSingleOutputFilename (), LLVMContext,
870
870
0 , &HashGlobal);
871
871
} else {
872
- IRModule = performIRGeneration (IRGenOpts, Instance-> getMainModule (),
872
+ IRModule = performIRGeneration (IRGenOpts, Instance. getMainModule (),
873
873
std::move (SM),
874
874
opts.getSingleOutputFilename (), LLVMContext,
875
875
&HashGlobal);
876
876
}
877
877
878
878
// Just because we had an AST error it doesn't mean we can't performLLVM.
879
- bool HadError = Instance-> getASTContext ().hadError ();
879
+ bool HadError = Instance. getASTContext ().hadError ();
880
880
881
881
// If the AST Context has no errors but no IRModule is available,
882
882
// parallelIRGen happened correctly, since parallel IRGen produces multiple
@@ -896,7 +896,7 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
896
896
error =
897
897
validateTBD (PrimarySourceFile, *IRModule, hasMultipleIRGenThreads);
898
898
else
899
- error = validateTBD (Instance-> getMainModule (), *IRModule,
899
+ error = validateTBD (Instance. getMainModule (), *IRModule,
900
900
hasMultipleIRGenThreads);
901
901
if (error)
902
902
return true ;
@@ -906,15 +906,11 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
906
906
createTargetMachine (IRGenOpts, Context);
907
907
version::Version EffectiveLanguageVersion =
908
908
Context.LangOpts .EffectiveLanguageVersion ;
909
+
910
+ // Free up some compiler resources now that we have an IRModule.
911
+ Instance.freeContextAndSIL ();
912
+
909
913
DiagnosticEngine &Diags = Context.Diags ;
910
- const DiagnosticOptions &DiagOpts = Invocation.getDiagnosticOptions ();
911
-
912
- // Delete the compiler instance now that we have an IRModule.
913
- if (DiagOpts.VerifyMode == DiagnosticOptions::NoVerify) {
914
- SM.reset ();
915
- Instance.reset ();
916
- }
917
-
918
914
// Now that we have a single IR Module, hand it over to performLLVM.
919
915
return performLLVM (IRGenOpts, &Diags, nullptr , HashGlobal, IRModule.get (),
920
916
TargetMachine.get (), EffectiveLanguageVersion,
@@ -1189,7 +1185,7 @@ int swift::performFrontend(ArrayRef<const char *> Args,
1189
1185
1190
1186
int ReturnValue = 0 ;
1191
1187
bool HadError =
1192
- performCompile (Instance, Invocation, Args, ReturnValue, observer,
1188
+ performCompile (* Instance, Invocation, Args, ReturnValue, observer,
1193
1189
StatsReporter.get ());
1194
1190
1195
1191
if (!HadError) {
0 commit comments