Skip to content

Commit 974933c

Browse files
graydonDavid Ungar
authored and
David Ungar
committed
[BatchMode] Thread the ModuleOrSourceFile of each Post-SILGen run through.
# Conflicts: # lib/FrontendTool/FrontendTool.cpp
1 parent 47d2c58 commit 974933c

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

Diff for: lib/FrontendTool/FrontendTool.cpp

+43-38
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,17 @@ createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
509509
return File;
510510
}
511511

512+
struct PostSILGenInputs {
513+
std::unique_ptr<SILModule> TheSILModule;
514+
bool astGuaranteedToCorrespondToSIL;
515+
ModuleOrSourceFile ModuleOrPrimarySourceFile;
516+
};
517+
512518
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
513519
CompilerInvocation &Invocation,
514520
std::unique_ptr<SILModule> SM,
515521
bool astGuaranteedToCorrespondToSIL,
522+
ModuleOrSourceFile MSF,
516523
bool moduleIsPublic,
517524
int &ReturnValue,
518525
FrontendObserver *observer,
@@ -797,16 +804,13 @@ static bool performCompile(CompilerInstance &Instance,
797804
assert(Action >= FrontendOptions::ActionType::EmitSILGen &&
798805
"All actions not requiring SILGen must have been handled!");
799806

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;
804809
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});
806811
}
807812

808-
if (SMs.empty()) {
809-
auto mod = Instance.getMainModule();
813+
if (PSGIs.empty()) {
810814
auto fileIsSIB = [](const FileUnit *File) -> bool {
811815
auto SASTF = dyn_cast<SerializedASTFile>(File);
812816
return SASTF && SASTF->isSIB();
@@ -821,9 +825,10 @@ static bool performCompile(CompilerInstance &Instance,
821825
InputFile::
822826
convertBufferNameFromLLVM_getFileOrSTDIN_toSwiftConventions(
823827
SASTF->getFilename()))) {
824-
assert(SMs.empty() && "Can only handle one primary AST input");
828+
assert(PSGIs.empty() && "Can only handle one primary AST input");
825829
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});
827832
}
828833
}
829834
}
@@ -833,25 +838,26 @@ static bool performCompile(CompilerInstance &Instance,
833838
// once for each such input.
834839
for (auto *PrimaryFile : Instance.getPrimarySourceFiles()) {
835840
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});
837843
}
838844
}
839845
} else {
840846
// If we have no primary inputs we are in WMO mode and need to build a
841847
// SILModule for the entire module.
842848
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});
846851
}
847852
}
848853

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();
852857
if (performCompileStepsPostSILGen(Instance, Invocation,
853-
std::move(pair.first),
854-
pair.second,
858+
std::move(PSGI.TheSILModule),
859+
PSGI.astGuaranteedToCorrespondToSIL,
860+
PSGI.ModuleOrPrimarySourceFile,
855861
moduleIsPublic,
856862
ReturnValue, observer, Stats))
857863
return true;
@@ -864,6 +870,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
864870
CompilerInvocation &Invocation,
865871
std::unique_ptr<SILModule> SM,
866872
bool astGuaranteedToCorrespondToSIL,
873+
ModuleOrSourceFile MSF,
867874
bool moduleIsPublic,
868875
int &ReturnValue,
869876
FrontendObserver *observer,
@@ -898,15 +905,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
898905
if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll)
899906
performSILLinking(SM.get(), true);
900907

901-
auto DC = Instance.getPrimarySourceFileOrMainModule();
902908
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
903909
SerializationOptions serializationOpts;
904910
serializationOpts.OutputPath =
905911
opts.InputsAndOutputs.preBatchModeModuleOutputPath().c_str();
906912
serializationOpts.SerializeAllSIL = true;
907913
serializationOpts.IsSIB = true;
908914

909-
serialize(DC, serializationOpts, SM.get());
915+
serialize(MSF, serializationOpts, SM.get());
910916
}
911917
return Context.hadError();
912918
}
@@ -956,7 +962,6 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
956962
auto SerializeSILModuleAction = [&]() {
957963
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty() ||
958964
!opts.InputsAndOutputs.preBatchModeModuleDocOutputPath().empty()) {
959-
auto DC = Instance.getPrimarySourceFileOrMainModule();
960965
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
961966
SerializationOptions serializationOpts;
962967
serializationOpts.OutputPath =
@@ -980,7 +985,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
980985
serializationOpts.SerializeOptionsForDebugging =
981986
!moduleIsPublic || opts.AlwaysSerializeDebuggingOptions;
982987

983-
serialize(DC, serializationOpts, SM.get());
988+
serialize(MSF, serializationOpts, SM.get());
984989
}
985990
}
986991
};
@@ -1030,8 +1035,8 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10301035
// Get the main source file's private discriminator and attach it to
10311036
// the compile unit's flags.
10321037
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();
10351040
if (!PD.empty())
10361041
IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str();
10371042
}
@@ -1043,15 +1048,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10431048
}
10441049

10451050
if (Action == FrontendOptions::ActionType::EmitSIB) {
1046-
auto DC = Instance.getPrimarySourceFileOrMainModule();
10471051
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
10481052
SerializationOptions serializationOpts;
10491053
serializationOpts.OutputPath =
10501054
opts.InputsAndOutputs.preBatchModeModuleOutputPath().c_str();
10511055
serializationOpts.SerializeAllSIL = true;
10521056
serializationOpts.IsSIB = true;
10531057

1054-
serialize(DC, serializationOpts, SM.get());
1058+
serialize(MSF, serializationOpts, SM.get());
10551059
}
10561060
return Context.hadError();
10571061
}
@@ -1064,7 +1068,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10641068
if (Action == FrontendOptions::ActionType::MergeModules ||
10651069
Action == FrontendOptions::ActionType::EmitModuleOnly) {
10661070
if (shouldIndex) {
1067-
if (emitIndexData(Instance.getPrimarySourceFile(),
1071+
if (emitIndexData(MSF.dyn_cast<SourceFile*>(),
10681072
Invocation, Instance))
10691073
return true;
10701074
}
@@ -1097,7 +1101,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10971101
// TODO: remove once the frontend understands what action it should perform
10981102
IRGenOpts.OutputKind = getOutputKind(Action);
10991103
if (Action == FrontendOptions::ActionType::Immediate) {
1100-
assert(Instance.getPrimarySourceFiles().empty() &&
1104+
assert(!MSF.is<SourceFile*>() &&
11011105
"-i doesn't work in -primary-file mode");
11021106
IRGenOpts.UseJIT = true;
11031107
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
@@ -1119,23 +1123,23 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
11191123
auto &LLVMContext = getGlobalLLVMContext();
11201124
std::unique_ptr<llvm::Module> IRModule;
11211125
llvm::GlobalVariable *HashGlobal;
1122-
if (!Instance.getPrimarySourceFiles().empty()) {
1126+
if (MSF.is<SourceFile*>()) {
11231127
IRModule = performIRGeneration(IRGenOpts,
1124-
*Instance.getPrimarySourceFile(),
1128+
*MSF.get<SourceFile*>(),
11251129
std::move(SM),
11261130
opts.InputsAndOutputs.preBatchModeGetSingleOutputFilename()(), LLVMContext,
11271131
0, &HashGlobal);
11281132
} 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);
11331137
}
11341138

11351139
// Walk the AST for indexing after IR generation. Walking it before seems
11361140
// to cause miscompilation issues.
11371141
if (shouldIndex) {
1138-
if (emitIndexData(Instance.getPrimarySourceFile(), Invocation, Instance))
1142+
if (emitIndexData(MSF.dyn_cast<SourceFile*>(), Invocation, Instance))
11391143
return true;
11401144
}
11411145

@@ -1164,12 +1168,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
11641168
const auto &SILOpts = Invocation.getSILOptions();
11651169
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs();
11661170
bool error;
1167-
if (!Instance.getPrimarySourceFiles().empty())
1168-
error = validateTBD(Instance.getPrimarySourceFile(),
1171+
if (MSF.is<SourceFile*>())
1172+
error = validateTBD(MSF.get<SourceFile*>(),
11691173
*IRModule, hasMultipleIGMs,
11701174
allSymbols);
11711175
else
1172-
error = validateTBD(Instance.getMainModule(), *IRModule, hasMultipleIGMs,
1176+
error = validateTBD(MSF.get<ModuleDecl*>(),
1177+
*IRModule, hasMultipleIGMs,
11731178
allSymbols);
11741179
if (error)
11751180
return true;

0 commit comments

Comments
 (0)