Skip to content

Commit 42c4a4d

Browse files
graydonDavid Ungar
authored andcommitted
[BatchMode] Thread the ModuleOrSourceFile of each Post-SILGen run through.
1 parent 03c0791 commit 42c4a4d

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

lib/FrontendTool/FrontendTool.cpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,17 @@ createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
504504
return File;
505505
}
506506

507+
struct PostSILGenInputs {
508+
std::unique_ptr<SILModule> TheSILModule;
509+
bool astGuaranteedToCorrespondToSIL;
510+
ModuleOrSourceFile ModuleOrPrimarySourceFile;
511+
};
512+
507513
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
508514
CompilerInvocation &Invocation,
509515
std::unique_ptr<SILModule> SM,
510516
bool astGuaranteedToCorrespondToSIL,
517+
ModuleOrSourceFile MSF,
511518
bool moduleIsPublic,
512519
int &ReturnValue,
513520
FrontendObserver *observer,
@@ -783,16 +790,13 @@ static bool performCompile(CompilerInstance &Instance,
783790
assert(Action >= FrontendOptions::ActionType::EmitSILGen &&
784791
"All actions not requiring SILGen must have been handled!");
785792

786-
// The second boolean in each std::pair<> in this std::deque<> indicates
787-
// whether the SIL is guaranteed to correspond to the the AST. This might be
788-
// false if we loaded SIL from an SIB.
789-
std::deque<std::pair<std::unique_ptr<SILModule>, bool>> SMs;
793+
auto mod = Instance.getMainModule();
794+
std::deque<PostSILGenInputs> PSGIs;
790795
if (auto SM = Instance.takeSILModule()) {
791-
SMs.push_back(std::make_pair(std::move(SM), false));
796+
PSGIs.push_back(PostSILGenInputs{std::move(SM), false, mod});
792797
}
793798

794-
if (SMs.empty()) {
795-
auto mod = Instance.getMainModule();
799+
if (PSGIs.empty()) {
796800
auto fileIsSIB = [](const FileUnit *File) -> bool {
797801
auto SASTF = dyn_cast<SerializedASTFile>(File);
798802
return SASTF && SASTF->isSIB();
@@ -807,9 +811,10 @@ static bool performCompile(CompilerInstance &Instance,
807811
InputFile::
808812
convertBufferNameFromLLVM_getFileOrSTDIN_toSwiftConventions(
809813
SASTF->getFilename()))) {
810-
assert(SMs.empty() && "Can only handle one primary AST input");
814+
assert(PSGIs.empty() && "Can only handle one primary AST input");
811815
auto SM = performSILGeneration(*SASTF, SILOpts, None);
812-
SMs.push_back(std::make_pair(std::move(SM), !fileIsSIB(SASTF)));
816+
PSGIs.push_back(
817+
PostSILGenInputs{std::move(SM), !fileIsSIB(SASTF), mod});
813818
}
814819
}
815820
}
@@ -819,25 +824,26 @@ static bool performCompile(CompilerInstance &Instance,
819824
// once for each such input.
820825
for (auto *PrimaryFile : Instance.getPrimarySourceFiles()) {
821826
auto SM = performSILGeneration(*PrimaryFile, SILOpts, None);
822-
SMs.push_back(std::make_pair(std::move(SM), !fileIsSIB(PrimaryFile)));
827+
PSGIs.push_back(PostSILGenInputs{
828+
std::move(SM), !fileIsSIB(PrimaryFile), PrimaryFile});
823829
}
824830
}
825831
} else {
826832
// If we have no primary inputs we are in WMO mode and need to build a
827833
// SILModule for the entire module.
828834
auto SM = performSILGeneration(mod, SILOpts, true);
829-
SMs.push_back(std::make_pair(std::move(SM),
830-
llvm::none_of(mod->getFiles(),
831-
fileIsSIB)));
835+
PSGIs.push_back(PostSILGenInputs{
836+
std::move(SM), llvm::none_of(mod->getFiles(), fileIsSIB), mod});
832837
}
833838
}
834839

835-
while (!SMs.empty()) {
836-
auto pair = std::move(SMs.front());
837-
SMs.pop_front();
840+
while (!PSGIs.empty()) {
841+
auto PSGI = std::move(PSGIs.front());
842+
PSGIs.pop_front();
838843
if (performCompileStepsPostSILGen(Instance, Invocation,
839-
std::move(pair.first),
840-
pair.second,
844+
std::move(PSGI.TheSILModule),
845+
PSGI.astGuaranteedToCorrespondToSIL,
846+
PSGI.ModuleOrPrimarySourceFile,
841847
moduleIsPublic,
842848
ReturnValue, observer, Stats))
843849
return true;
@@ -850,6 +856,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
850856
CompilerInvocation &Invocation,
851857
std::unique_ptr<SILModule> SM,
852858
bool astGuaranteedToCorrespondToSIL,
859+
ModuleOrSourceFile MSF,
853860
bool moduleIsPublic,
854861
int &ReturnValue,
855862
FrontendObserver *observer,
@@ -883,14 +890,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
883890
if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll)
884891
performSILLinking(SM.get(), true);
885892

886-
auto DC = Instance.getPrimarySourceFileOrMainModule();
887893
if (!opts.ModuleOutputPath.empty()) {
888894
SerializationOptions serializationOpts;
889895
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
890896
serializationOpts.SerializeAllSIL = true;
891897
serializationOpts.IsSIB = true;
892898

893-
serialize(DC, serializationOpts, SM.get());
899+
serialize(MSF, serializationOpts, SM.get());
894900
}
895901
return Context.hadError();
896902
}
@@ -939,7 +945,6 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
939945

940946
auto SerializeSILModuleAction = [&]() {
941947
if (!opts.ModuleOutputPath.empty() || !opts.ModuleDocOutputPath.empty()) {
942-
auto DC = Instance.getPrimarySourceFileOrMainModule();
943948
if (!opts.ModuleOutputPath.empty()) {
944949
SerializationOptions serializationOpts;
945950
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
@@ -961,7 +966,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
961966
serializationOpts.SerializeOptionsForDebugging =
962967
!moduleIsPublic || opts.AlwaysSerializeDebuggingOptions;
963968

964-
serialize(DC, serializationOpts, SM.get());
969+
serialize(MSF, serializationOpts, SM.get());
965970
}
966971
}
967972
};
@@ -1011,8 +1016,8 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10111016
// Get the main source file's private discriminator and attach it to
10121017
// the compile unit's flags.
10131018
if (IRGenOpts.DebugInfoKind != IRGenDebugInfoKind::None &&
1014-
Instance.getPrimarySourceFile()) {
1015-
Identifier PD = Instance.getPrimarySourceFile()->getPrivateDiscriminator();
1019+
MSF.is<SourceFile*>()) {
1020+
Identifier PD = MSF.get<SourceFile*>()->getPrivateDiscriminator();
10161021
if (!PD.empty())
10171022
IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str();
10181023
}
@@ -1023,14 +1028,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10231028
}
10241029

10251030
if (Action == FrontendOptions::ActionType::EmitSIB) {
1026-
auto DC = Instance.getPrimarySourceFileOrMainModule();
10271031
if (!opts.ModuleOutputPath.empty()) {
10281032
SerializationOptions serializationOpts;
10291033
serializationOpts.OutputPath = opts.ModuleOutputPath.c_str();
10301034
serializationOpts.SerializeAllSIL = true;
10311035
serializationOpts.IsSIB = true;
10321036

1033-
serialize(DC, serializationOpts, SM.get());
1037+
serialize(MSF, serializationOpts, SM.get());
10341038
}
10351039
return Context.hadError();
10361040
}
@@ -1042,7 +1046,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10421046
if (Action == FrontendOptions::ActionType::MergeModules ||
10431047
Action == FrontendOptions::ActionType::EmitModuleOnly) {
10441048
if (shouldIndex) {
1045-
if (emitIndexData(Instance.getPrimarySourceFile(),
1049+
if (emitIndexData(MSF.dyn_cast<SourceFile*>(),
10461050
Invocation, Instance))
10471051
return true;
10481052
}
@@ -1074,7 +1078,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10741078
// TODO: remove once the frontend understands what action it should perform
10751079
IRGenOpts.OutputKind = getOutputKind(Action);
10761080
if (Action == FrontendOptions::ActionType::Immediate) {
1077-
assert(Instance.getPrimarySourceFiles().empty() &&
1081+
assert(!MSF.is<SourceFile*>() &&
10781082
"-i doesn't work in -primary-file mode");
10791083
IRGenOpts.UseJIT = true;
10801084
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
@@ -1096,14 +1100,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10961100
auto &LLVMContext = getGlobalLLVMContext();
10971101
std::unique_ptr<llvm::Module> IRModule;
10981102
llvm::GlobalVariable *HashGlobal;
1099-
if (!Instance.getPrimarySourceFiles().empty()) {
1103+
if (MSF.is<SourceFile*>()) {
11001104
IRModule = performIRGeneration(IRGenOpts,
1101-
*Instance.getPrimarySourceFile(),
1105+
*MSF.get<SourceFile*>(),
11021106
std::move(SM),
11031107
opts.getSingleOutputFilename(), LLVMContext,
11041108
0, &HashGlobal);
11051109
} else {
1106-
IRModule = performIRGeneration(IRGenOpts, Instance.getMainModule(),
1110+
IRModule = performIRGeneration(IRGenOpts, MSF.get<ModuleDecl*>(),
11071111
std::move(SM),
11081112
opts.getSingleOutputFilename(), LLVMContext,
11091113
&HashGlobal);
@@ -1112,7 +1116,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
11121116
// Walk the AST for indexing after IR generation. Walking it before seems
11131117
// to cause miscompilation issues.
11141118
if (shouldIndex) {
1115-
if (emitIndexData(Instance.getPrimarySourceFile(), Invocation, Instance))
1119+
if (emitIndexData(MSF.dyn_cast<SourceFile*>(), Invocation, Instance))
11161120
return true;
11171121
}
11181122

@@ -1141,12 +1145,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
11411145
const auto &SILOpts = Invocation.getSILOptions();
11421146
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs();
11431147
bool error;
1144-
if (!Instance.getPrimarySourceFiles().empty())
1145-
error = validateTBD(Instance.getPrimarySourceFile(),
1148+
if (MSF.is<SourceFile*>())
1149+
error = validateTBD(MSF.get<SourceFile*>(),
11461150
*IRModule, hasMultipleIGMs,
11471151
allSymbols);
11481152
else
1149-
error = validateTBD(Instance.getMainModule(), *IRModule, hasMultipleIGMs,
1153+
error = validateTBD(MSF.get<ModuleDecl*>(),
1154+
*IRModule, hasMultipleIGMs,
11501155
allSymbols);
11511156
if (error)
11521157
return true;

0 commit comments

Comments
 (0)