@@ -509,10 +509,17 @@ createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
509
509
return File;
510
510
}
511
511
512
+ struct PostSILGenInputs {
513
+ std::unique_ptr<SILModule> TheSILModule;
514
+ bool astGuaranteedToCorrespondToSIL;
515
+ ModuleOrSourceFile ModuleOrPrimarySourceFile;
516
+ };
517
+
512
518
static bool performCompileStepsPostSILGen (CompilerInstance &Instance,
513
519
CompilerInvocation &Invocation,
514
520
std::unique_ptr<SILModule> SM,
515
521
bool astGuaranteedToCorrespondToSIL,
522
+ ModuleOrSourceFile MSF,
516
523
bool moduleIsPublic,
517
524
int &ReturnValue,
518
525
FrontendObserver *observer,
@@ -797,16 +804,13 @@ static bool performCompile(CompilerInstance &Instance,
797
804
assert (Action >= FrontendOptions::ActionType::EmitSILGen &&
798
805
" All actions not requiring SILGen must have been handled!" );
799
806
800
- // The second boolean in each std::pair<> in this std::deque<> indicates
801
- // whether the SIL is guaranteed to correspond to the the AST. This might be
802
- // false if we loaded SIL from an SIB.
803
- std::deque<std::pair<std::unique_ptr<SILModule>, bool >> SMs;
807
+ auto mod = Instance.getMainModule ();
808
+ std::deque<PostSILGenInputs> PSGIs;
804
809
if (auto SM = Instance.takeSILModule ()) {
805
- SMs .push_back (std::make_pair ( std:: move (SM), false ) );
810
+ PSGIs .push_back (PostSILGenInputs{ std::move (SM), false , mod} );
806
811
}
807
812
808
- if (SMs.empty ()) {
809
- auto mod = Instance.getMainModule ();
813
+ if (PSGIs.empty ()) {
810
814
auto fileIsSIB = [](const FileUnit *File) -> bool {
811
815
auto SASTF = dyn_cast<SerializedASTFile>(File);
812
816
return SASTF && SASTF->isSIB ();
@@ -821,9 +825,10 @@ static bool performCompile(CompilerInstance &Instance,
821
825
InputFile::
822
826
convertBufferNameFromLLVM_getFileOrSTDIN_toSwiftConventions (
823
827
SASTF->getFilename ()))) {
824
- assert (SMs .empty () && " Can only handle one primary AST input" );
828
+ assert (PSGIs .empty () && " Can only handle one primary AST input" );
825
829
auto SM = performSILGeneration (*SASTF, SILOpts, None);
826
- SMs.push_back (std::make_pair (std::move (SM), !fileIsSIB (SASTF)));
830
+ PSGIs.push_back (
831
+ PostSILGenInputs{std::move (SM), !fileIsSIB (SASTF), mod});
827
832
}
828
833
}
829
834
}
@@ -833,25 +838,26 @@ static bool performCompile(CompilerInstance &Instance,
833
838
// once for each such input.
834
839
for (auto *PrimaryFile : Instance.getPrimarySourceFiles ()) {
835
840
auto SM = performSILGeneration (*PrimaryFile, SILOpts, None);
836
- SMs.push_back (std::make_pair (std::move (SM), !fileIsSIB (PrimaryFile)));
841
+ PSGIs.push_back (PostSILGenInputs{
842
+ std::move (SM), !fileIsSIB (PrimaryFile), PrimaryFile});
837
843
}
838
844
}
839
845
} else {
840
846
// If we have no primary inputs we are in WMO mode and need to build a
841
847
// SILModule for the entire module.
842
848
auto SM = performSILGeneration (mod, SILOpts, true );
843
- SMs.push_back (std::make_pair (std::move (SM),
844
- llvm::none_of (mod->getFiles (),
845
- fileIsSIB)));
849
+ PSGIs.push_back (PostSILGenInputs{
850
+ std::move (SM), llvm::none_of (mod->getFiles (), fileIsSIB), mod});
846
851
}
847
852
}
848
853
849
- while (!SMs .empty ()) {
850
- auto pair = std::move (SMs .front ());
851
- SMs .pop_front ();
854
+ while (!PSGIs .empty ()) {
855
+ auto PSGI = std::move (PSGIs .front ());
856
+ PSGIs .pop_front ();
852
857
if (performCompileStepsPostSILGen (Instance, Invocation,
853
- std::move (pair.first ),
854
- pair.second ,
858
+ std::move (PSGI.TheSILModule ),
859
+ PSGI.astGuaranteedToCorrespondToSIL ,
860
+ PSGI.ModuleOrPrimarySourceFile ,
855
861
moduleIsPublic,
856
862
ReturnValue, observer, Stats))
857
863
return true ;
@@ -864,6 +870,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
864
870
CompilerInvocation &Invocation,
865
871
std::unique_ptr<SILModule> SM,
866
872
bool astGuaranteedToCorrespondToSIL,
873
+ ModuleOrSourceFile MSF,
867
874
bool moduleIsPublic,
868
875
int &ReturnValue,
869
876
FrontendObserver *observer,
@@ -898,15 +905,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
898
905
if (Invocation.getSILOptions ().LinkMode == SILOptions::LinkAll)
899
906
performSILLinking (SM.get (), true );
900
907
901
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
902
908
if (!opts.InputsAndOutputs .preBatchModeModuleOutputPath ().empty ()) {
903
909
SerializationOptions serializationOpts;
904
910
serializationOpts.OutputPath =
905
911
opts.InputsAndOutputs .preBatchModeModuleOutputPath ().c_str ();
906
912
serializationOpts.SerializeAllSIL = true ;
907
913
serializationOpts.IsSIB = true ;
908
914
909
- serialize (DC , serializationOpts, SM.get ());
915
+ serialize (MSF , serializationOpts, SM.get ());
910
916
}
911
917
return Context.hadError ();
912
918
}
@@ -956,7 +962,6 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
956
962
auto SerializeSILModuleAction = [&]() {
957
963
if (!opts.InputsAndOutputs .preBatchModeModuleOutputPath ().empty () ||
958
964
!opts.InputsAndOutputs .preBatchModeModuleDocOutputPath ().empty ()) {
959
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
960
965
if (!opts.InputsAndOutputs .preBatchModeModuleOutputPath ().empty ()) {
961
966
SerializationOptions serializationOpts;
962
967
serializationOpts.OutputPath =
@@ -980,7 +985,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
980
985
serializationOpts.SerializeOptionsForDebugging =
981
986
!moduleIsPublic || opts.AlwaysSerializeDebuggingOptions ;
982
987
983
- serialize (DC , serializationOpts, SM.get ());
988
+ serialize (MSF , serializationOpts, SM.get ());
984
989
}
985
990
}
986
991
};
@@ -1030,8 +1035,8 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1030
1035
// Get the main source file's private discriminator and attach it to
1031
1036
// the compile unit's flags.
1032
1037
if (IRGenOpts.DebugInfoKind != IRGenDebugInfoKind::None &&
1033
- Instance. getPrimarySourceFile ()) {
1034
- Identifier PD = Instance. getPrimarySourceFile ()->getPrivateDiscriminator ();
1038
+ MSF. is <SourceFile*> ()) {
1039
+ Identifier PD = MSF. get <SourceFile*> ()->getPrivateDiscriminator ();
1035
1040
if (!PD.empty ())
1036
1041
IRGenOpts.DWARFDebugFlags += (" -private-discriminator " +PD.str ()).str ();
1037
1042
}
@@ -1043,15 +1048,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1043
1048
}
1044
1049
1045
1050
if (Action == FrontendOptions::ActionType::EmitSIB) {
1046
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
1047
1051
if (!opts.InputsAndOutputs .preBatchModeModuleOutputPath ().empty ()) {
1048
1052
SerializationOptions serializationOpts;
1049
1053
serializationOpts.OutputPath =
1050
1054
opts.InputsAndOutputs .preBatchModeModuleOutputPath ().c_str ();
1051
1055
serializationOpts.SerializeAllSIL = true ;
1052
1056
serializationOpts.IsSIB = true ;
1053
1057
1054
- serialize (DC , serializationOpts, SM.get ());
1058
+ serialize (MSF , serializationOpts, SM.get ());
1055
1059
}
1056
1060
return Context.hadError ();
1057
1061
}
@@ -1064,7 +1068,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1064
1068
if (Action == FrontendOptions::ActionType::MergeModules ||
1065
1069
Action == FrontendOptions::ActionType::EmitModuleOnly) {
1066
1070
if (shouldIndex) {
1067
- if (emitIndexData (Instance. getPrimarySourceFile (),
1071
+ if (emitIndexData (MSF. dyn_cast <SourceFile*> (),
1068
1072
Invocation, Instance))
1069
1073
return true ;
1070
1074
}
@@ -1097,7 +1101,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1097
1101
// TODO: remove once the frontend understands what action it should perform
1098
1102
IRGenOpts.OutputKind = getOutputKind (Action);
1099
1103
if (Action == FrontendOptions::ActionType::Immediate) {
1100
- assert (Instance. getPrimarySourceFiles (). empty () &&
1104
+ assert (!MSF. is <SourceFile*> () &&
1101
1105
" -i doesn't work in -primary-file mode" );
1102
1106
IRGenOpts.UseJIT = true ;
1103
1107
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
@@ -1119,23 +1123,23 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1119
1123
auto &LLVMContext = getGlobalLLVMContext ();
1120
1124
std::unique_ptr<llvm::Module> IRModule;
1121
1125
llvm::GlobalVariable *HashGlobal;
1122
- if (!Instance. getPrimarySourceFiles (). empty ()) {
1126
+ if (MSF. is <SourceFile*> ()) {
1123
1127
IRModule = performIRGeneration (IRGenOpts,
1124
- *Instance. getPrimarySourceFile (),
1128
+ *MSF. get <SourceFile*> (),
1125
1129
std::move (SM),
1126
1130
opts.InputsAndOutputs .preBatchModeGetSingleOutputFilename ()(), LLVMContext,
1127
1131
0 , &HashGlobal);
1128
1132
} else {
1129
- IRModule = performIRGeneration (
1130
- IRGenOpts, Instance. getMainModule (), std::move (SM),
1131
- opts.InputsAndOutputs .preBatchModeGetSingleOutputFilename (),
1132
- LLVMContext, &HashGlobal);
1133
+ IRModule = performIRGeneration (IRGenOpts, MSF. get <ModuleDecl*>(),
1134
+ std::move (SM),
1135
+ opts.InputsAndOutputs .preBatchModeGetSingleOutputFilename (), LLVMContext ,
1136
+ &HashGlobal);
1133
1137
}
1134
1138
1135
1139
// Walk the AST for indexing after IR generation. Walking it before seems
1136
1140
// to cause miscompilation issues.
1137
1141
if (shouldIndex) {
1138
- if (emitIndexData (Instance. getPrimarySourceFile (), Invocation, Instance))
1142
+ if (emitIndexData (MSF. dyn_cast <SourceFile*> (), Invocation, Instance))
1139
1143
return true ;
1140
1144
}
1141
1145
@@ -1164,12 +1168,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1164
1168
const auto &SILOpts = Invocation.getSILOptions ();
1165
1169
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs ();
1166
1170
bool error;
1167
- if (!Instance. getPrimarySourceFiles (). empty ())
1168
- error = validateTBD (Instance. getPrimarySourceFile (),
1171
+ if (MSF. is <SourceFile*> ())
1172
+ error = validateTBD (MSF. get <SourceFile*> (),
1169
1173
*IRModule, hasMultipleIGMs,
1170
1174
allSymbols);
1171
1175
else
1172
- error = validateTBD (Instance.getMainModule (), *IRModule, hasMultipleIGMs,
1176
+ error = validateTBD (MSF.get <ModuleDecl*>(),
1177
+ *IRModule, hasMultipleIGMs,
1173
1178
allSymbols);
1174
1179
if (error)
1175
1180
return true ;
0 commit comments