Skip to content

Commit f744fbe

Browse files
graydonDavid Ungar
authored andcommitted
[BatchMode] Sink a bunch of FrontendTool uses of getPrimarySourceFile()
# Conflicts: # lib/FrontendTool/FrontendTool.cpp
1 parent e824894 commit f744fbe

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,16 @@ class CompilerInstance {
426426
return PrimarySourceFiles;
427427
}
428428

429+
/// Gets the Primary Source File if one exists, otherwise the main
430+
/// module. If multiple Primary Source Files exist, fails with an
431+
/// assertion.
432+
ModuleOrSourceFile getPrimarySourceFileOrMainModule() {
433+
if (PrimarySourceFiles.empty())
434+
return getMainModule();
435+
else
436+
return getPrimarySourceFile();
437+
}
438+
429439
/// Gets the SourceFile which is the primary input for this CompilerInstance.
430440
/// \returns the primary SourceFile, or nullptr if there is no primary input;
431441
/// if there are _multiple_ primary inputs, fails with an assertion.

lib/FrontendTool/FrontendTool.cpp

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,9 @@ static void countStatsPostSema(UnifiedStatsReporter &Stats,
458458
C.NumReferencedMemberNames = R->getUsedMembers().size();
459459
}
460460

461-
if (auto *SF = Instance.getPrimarySourceFile()) {
462-
countStatsOfSourceFile(Stats, Instance, SF);
461+
if (!Instance.getPrimarySourceFiles().empty()) {
462+
for (auto SF : Instance.getPrimarySourceFiles())
463+
countStatsOfSourceFile(Stats, Instance, SF);
463464
} else if (auto *M = Instance.getMainModule()) {
464465
// No primary source file, but a main module; this is WMO-mode
465466
for (auto *F : M->getFiles()) {
@@ -642,8 +643,6 @@ static bool performCompile(CompilerInstance &Instance,
642643
return Context.hadError();
643644
}
644645

645-
SourceFile *PrimarySourceFile = Instance.getPrimarySourceFile();
646-
647646
// We've been told to dump the AST (either after parsing or type-checking,
648647
// which is already differentiated in CompilerInstance::performSema()),
649648
// so dump or print the main source file and return.
@@ -654,7 +653,7 @@ static bool performCompile(CompilerInstance &Instance,
654653
Action == FrontendOptions::ActionType::DumpScopeMaps ||
655654
Action == FrontendOptions::ActionType::DumpTypeRefinementContexts ||
656655
Action == FrontendOptions::ActionType::DumpInterfaceHash) {
657-
SourceFile *SF = PrimarySourceFile;
656+
SourceFile *SF = Instance.getPrimarySourceFile();
658657
if (!SF) {
659658
SourceFileKind Kind = Invocation.getSourceFileKind();
660659
SF = &Instance.getMainModule()->getMainSourceFile(Kind);
@@ -729,9 +728,12 @@ static bool performCompile(CompilerInstance &Instance,
729728
(void)emitMakeDependencies(Context.Diags, *Instance.getDependencyTracker(),
730729
opts);
731730

732-
if (shouldTrackReferences)
733-
emitReferenceDependencies(Context.Diags, Instance.getPrimarySourceFile(),
734-
*Instance.getDependencyTracker(), opts);
731+
if (shouldTrackReferences) {
732+
for (auto *SF : Instance.getPrimarySourceFiles()) {
733+
emitReferenceDependencies(Context.Diags, SF,
734+
*Instance.getDependencyTracker(), opts);
735+
}
736+
}
735737

736738
if (!opts.InputsAndOutputs.preBatchModeLoadedModuleTracePath().empty())
737739
(void)emitLoadedModuleTrace(Context, *Instance.getDependencyTracker(),
@@ -742,7 +744,8 @@ static bool performCompile(CompilerInstance &Instance,
742744
if (Context.hadError()) {
743745
if (shouldIndex) {
744746
// Emit the index store data even if there were compiler errors.
745-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
747+
if (emitIndexData(Instance.getPrimarySourceFile(),
748+
Invocation, Instance))
746749
return true;
747750
}
748751
return true;
@@ -763,7 +766,8 @@ static bool performCompile(CompilerInstance &Instance,
763766
Instance.getMainModule(), opts.ImplicitObjCHeaderPath,
764767
moduleIsPublic);
765768
if (shouldIndex) {
766-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
769+
if (emitIndexData(Instance.getPrimarySourceFile(),
770+
Invocation, Instance))
767771
return true;
768772
}
769773
return Context.hadError();
@@ -793,8 +797,8 @@ static bool performCompile(CompilerInstance &Instance,
793797
auto SASTF = dyn_cast<SerializedASTFile>(File);
794798
return SASTF && SASTF->isSIB();
795799
};
796-
if (opts.InputsAndOutputs.hasPrimaries()) {
797-
FileUnit *PrimaryFile = PrimarySourceFile;
800+
if (opts.Inputs.hasPrimaries()) {
801+
FileUnit *PrimaryFile = Instance.getPrimarySourceFile();
798802
if (!PrimaryFile) {
799803
for (FileUnit *fileUnit : Instance.getMainModule()->getFiles()) {
800804
if (auto SASTF = dyn_cast<SerializedASTFile>(fileUnit)) {
@@ -842,8 +846,7 @@ static bool performCompile(CompilerInstance &Instance,
842846
if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll)
843847
performSILLinking(SM.get(), true);
844848

845-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) :
846-
Instance.getMainModule();
849+
auto DC = Instance.getPrimarySourceFileOrMainModule();
847850
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
848851
SerializationOptions serializationOpts;
849852
serializationOpts.OutputPath =
@@ -899,10 +902,9 @@ static bool performCompile(CompilerInstance &Instance,
899902
// done, depending on the compiler setting.
900903

901904
auto SerializeSILModuleAction = [&]() {
902-
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty() ||
903-
!opts.InputsAndOutputs.preBatchModeModuleDocOutputPath().empty()) {
904-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile)
905-
: Instance.getMainModule();
905+
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty() ||
906+
!opts.InputsAndOutputs.preBatchModeModuleDocOutputPath().empty()) {
907+
auto DC = Instance.getPrimarySourceFileOrMainModule();
906908
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
907909
SerializationOptions serializationOpts;
908910
serializationOpts.OutputPath =
@@ -975,8 +977,9 @@ static bool performCompile(CompilerInstance &Instance,
975977

976978
// Get the main source file's private discriminator and attach it to
977979
// the compile unit's flags.
978-
if (PrimarySourceFile) {
979-
Identifier PD = PrimarySourceFile->getPrivateDiscriminator();
980+
if (IRGenOpts.DebugInfoKind != IRGenDebugInfoKind::None &&
981+
Instance.getPrimarySourceFile()) {
982+
Identifier PD = Instance.getPrimarySourceFile()->getPrivateDiscriminator();
980983
if (!PD.empty())
981984
IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str();
982985
}
@@ -988,8 +991,7 @@ static bool performCompile(CompilerInstance &Instance,
988991
}
989992

990993
if (Action == FrontendOptions::ActionType::EmitSIB) {
991-
auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) :
992-
Instance.getMainModule();
994+
auto DC = Instance.getPrimarySourceFileOrMainModule();
993995
if (!opts.InputsAndOutputs.preBatchModeModuleOutputPath().empty()) {
994996
SerializationOptions serializationOpts;
995997
serializationOpts.OutputPath =
@@ -1010,7 +1012,8 @@ static bool performCompile(CompilerInstance &Instance,
10101012
if (Action == FrontendOptions::ActionType::MergeModules ||
10111013
Action == FrontendOptions::ActionType::EmitModuleOnly) {
10121014
if (shouldIndex) {
1013-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
1015+
if (emitIndexData(Instance.getPrimarySourceFile(),
1016+
Invocation, Instance))
10141017
return true;
10151018
}
10161019
return Context.hadError();
@@ -1042,7 +1045,8 @@ static bool performCompile(CompilerInstance &Instance,
10421045
// TODO: remove once the frontend understands what action it should perform
10431046
IRGenOpts.OutputKind = getOutputKind(Action);
10441047
if (Action == FrontendOptions::ActionType::Immediate) {
1045-
assert(!PrimarySourceFile && "-i doesn't work in -primary-file mode");
1048+
assert(Instance.getPrimarySourceFiles().empty() &&
1049+
"-i doesn't work in -primary-file mode");
10461050
IRGenOpts.UseJIT = true;
10471051
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
10481052
const ProcessCmdLine &CmdLine = ProcessCmdLine(opts.ImmediateArgv.begin(),
@@ -1063,11 +1067,12 @@ static bool performCompile(CompilerInstance &Instance,
10631067
auto &LLVMContext = getGlobalLLVMContext();
10641068
std::unique_ptr<llvm::Module> IRModule;
10651069
llvm::GlobalVariable *HashGlobal;
1066-
if (PrimarySourceFile) {
1067-
IRModule = performIRGeneration(
1068-
IRGenOpts, *PrimarySourceFile, std::move(SM),
1069-
opts.InputsAndOutputs.preBatchModeGetSingleOutputFilename(),
1070-
LLVMContext, 0, &HashGlobal);
1070+
if (!Instance.getPrimarySourceFiles().empty()) {
1071+
IRModule = performIRGeneration(IRGenOpts,
1072+
*Instance.getPrimarySourceFile(),
1073+
std::move(SM),
1074+
opts.InputsAndOutputs.preBatchModeGetSingleOutputFilename()(), LLVMContext,
1075+
0, &HashGlobal);
10711076
} else {
10721077
IRModule = performIRGeneration(
10731078
IRGenOpts, Instance.getMainModule(), std::move(SM),
@@ -1078,7 +1083,7 @@ static bool performCompile(CompilerInstance &Instance,
10781083
// Walk the AST for indexing after IR generation. Walking it before seems
10791084
// to cause miscompilation issues.
10801085
if (shouldIndex) {
1081-
if (emitIndexData(PrimarySourceFile, Invocation, Instance))
1086+
if (emitIndexData(Instance.getPrimarySourceFile(), Invocation, Instance))
10821087
return true;
10831088
}
10841089

@@ -1107,8 +1112,9 @@ static bool performCompile(CompilerInstance &Instance,
11071112
const auto &SILOpts = Invocation.getSILOptions();
11081113
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs();
11091114
bool error;
1110-
if (PrimarySourceFile)
1111-
error = validateTBD(PrimarySourceFile, *IRModule, hasMultipleIGMs,
1115+
if (!Instance.getPrimarySourceFiles().empty())
1116+
error = validateTBD(Instance.getPrimarySourceFile(),
1117+
*IRModule, hasMultipleIGMs,
11121118
allSymbols);
11131119
else
11141120
error = validateTBD(Instance.getMainModule(), *IRModule, hasMultipleIGMs,

0 commit comments

Comments
 (0)