Skip to content

Commit 9fd23bb

Browse files
David Ungardavidungar
authored andcommitted
Minor refactor of buildCompilation
1. Compute things closer to use, and use const, and 2. Factor out some computations into subroutines in order to shorten buildCompilation.
1 parent 1edc3be commit 9fd23bb

File tree

1 file changed

+136
-95
lines changed

1 file changed

+136
-95
lines changed

lib/Driver/Driver.cpp

Lines changed: 136 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,88 @@ getDriverBatchCount(llvm::opt::InputArgList &ArgList,
672672
return None;
673673
}
674674

675+
static bool computeIncremental(const llvm::opt::InputArgList *ArgList,
676+
const bool ShowIncrementalBuildDecisions) {
677+
{
678+
const bool WasIncrementalRequested =
679+
ArgList->hasArg(options::OPT_incremental);
680+
if (!WasIncrementalRequested)
681+
return false;
682+
}
683+
const char *ReasonToDisable =
684+
ArgList->hasArg(options::OPT_whole_module_optimization)
685+
? "is not compatible with whole module optimization."
686+
: ArgList->hasArg(options::OPT_embed_bitcode)
687+
? "is not currently compatible with embedding LLVM IR bitcode"
688+
: nullptr;
689+
690+
if (!ReasonToDisable)
691+
return true;
692+
693+
if (ShowIncrementalBuildDecisions) {
694+
llvm::outs() << "Incremental compilation has been disabled, because it "
695+
<< ReasonToDisable;
696+
}
697+
return false;
698+
}
699+
700+
static std::string
701+
computeWorkingDirectory(const llvm::opt::InputArgList *ArgList) {
702+
if (auto *A = ArgList->getLastArg(options::OPT_working_directory)) {
703+
SmallString<128> workingDirectory;
704+
workingDirectory = A->getValue();
705+
llvm::sys::fs::make_absolute(workingDirectory);
706+
return workingDirectory.str();
707+
}
708+
return std::string();
709+
}
710+
711+
static std::unique_ptr<UnifiedStatsReporter>
712+
computeStatsReporter(const llvm::opt::InputArgList *ArgList,
713+
const InputFileList &Inputs, const OutputInfo OI,
714+
StringRef DefaultTargetTriple) {
715+
const Arg *A = ArgList->getLastArgNoClaim(options::OPT_stats_output_dir);
716+
if (!A)
717+
return nullptr;
718+
719+
StringRef OptType;
720+
if (const Arg *OptA = ArgList->getLastArgNoClaim(options::OPT_O_Group)) {
721+
OptType = OptA->getSpelling();
722+
}
723+
StringRef InputName;
724+
if (Inputs.size() == 1) {
725+
InputName = Inputs[0].second->getSpelling();
726+
}
727+
StringRef OutputType = file_types::getTypeTempSuffix(OI.CompilerOutputType);
728+
return llvm::make_unique<UnifiedStatsReporter>("swift-driver",
729+
OI.ModuleName,
730+
InputName,
731+
DefaultTargetTriple,
732+
OutputType,
733+
OptType,
734+
A->getValue());
735+
}
736+
737+
static bool
738+
computeContinueBuildingAfterErrors(const bool BatchMode,
739+
const llvm::opt::InputArgList *ArgList) {
740+
// Note: Batch mode handling of serialized diagnostics requires that all
741+
// batches get to run, in order to make sure that all diagnostics emitted
742+
// during the compilation end up in at least one serialized diagnostic file.
743+
// Therefore, treat batch mode as implying -continue-building-after-errors.
744+
// (This behavior could be limited to only when serialized diagnostics are
745+
// being emitted, but this seems more consistent and less surprising for
746+
// users.)
747+
// FIXME: We don't really need (or want) a full ContinueBuildingAfterErrors.
748+
// If we fail to precompile a bridging header, for example, there's no need
749+
// to go on to compilation of source files, and if compilation of source files
750+
// fails, we shouldn't try to link. Instead, we'd want to let all jobs finish
751+
// but not schedule any new ones.
752+
return BatchMode ||
753+
ArgList->hasArg(options::OPT_continue_building_after_errors);
754+
755+
}
756+
675757
std::unique_ptr<Compilation>
676758
Driver::buildCompilation(const ToolChain &TC,
677759
std::unique_ptr<llvm::opt::InputArgList> ArgList) {
@@ -682,47 +764,9 @@ Driver::buildCompilation(const ToolChain &TC,
682764
// Claim --driver-mode here, since it's already been handled.
683765
(void) ArgList->hasArg(options::OPT_driver_mode);
684766

685-
bool DriverPrintActions = ArgList->hasArg(options::OPT_driver_print_actions);
686-
bool DriverPrintOutputFileMap =
687-
ArgList->hasArg(options::OPT_driver_print_output_file_map);
688-
bool DriverPrintDerivedOutputFileMap =
689-
ArgList->hasArg(options::OPT_driver_print_derived_output_file_map);
690767
DriverPrintBindings = ArgList->hasArg(options::OPT_driver_print_bindings);
691-
bool ShowIncrementalBuildDecisions =
692-
ArgList->hasArg(options::OPT_driver_show_incremental);
693-
bool ShowJobLifecycle =
694-
ArgList->hasArg(options::OPT_driver_show_job_lifecycle);
695-
unsigned DriverBatchSeed = getDriverBatchSeed(*ArgList, Diags);
696-
Optional<unsigned> DriverBatchCount = getDriverBatchCount(*ArgList, Diags);
697-
bool DriverForceOneBatchRepartition =
698-
ArgList->hasArg(options::OPT_driver_force_one_batch_repartition);
699-
700-
bool Incremental = ArgList->hasArg(options::OPT_incremental);
701-
if (ArgList->hasArg(options::OPT_whole_module_optimization)) {
702-
if (Incremental && ShowIncrementalBuildDecisions) {
703-
llvm::outs() << "Incremental compilation has been disabled, because it "
704-
<< "is not compatible with whole module optimization.";
705-
}
706-
Incremental = false;
707-
}
708-
if (ArgList->hasArg(options::OPT_embed_bitcode)) {
709-
if (Incremental && ShowIncrementalBuildDecisions) {
710-
llvm::outs() << "Incremental compilation has been disabled, because it "
711-
<< "is not currently compatible with embedding LLVM IR "
712-
<< "bitcode.";
713-
}
714-
Incremental = false;
715-
}
716768

717-
bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
718-
bool ShowDriverTimeCompilation =
719-
ArgList->hasArg(options::OPT_driver_time_compilation);
720-
721-
SmallString<128> workingDirectory;
722-
if (auto *A = ArgList->getLastArg(options::OPT_working_directory)) {
723-
workingDirectory = A->getValue();
724-
llvm::sys::fs::make_absolute(workingDirectory);
725-
}
769+
const std::string workingDirectory = computeWorkingDirectory(ArgList.get());
726770

727771
std::unique_ptr<DerivedArgList> TranslatedArgList(
728772
translateInputAndPathArgs(*ArgList, workingDirectory));
@@ -749,45 +793,9 @@ Driver::buildCompilation(const ToolChain &TC,
749793
OI.CompilerMode = computeCompilerMode(*TranslatedArgList, Inputs, BatchMode);
750794
buildOutputInfo(TC, *TranslatedArgList, BatchMode, Inputs, OI);
751795

752-
// Note: Batch mode handling of serialized diagnostics requires that all
753-
// batches get to run, in order to make sure that all diagnostics emitted
754-
// during the compilation end up in at least one serialized diagnostic file.
755-
// Therefore, treat batch mode as implying -continue-building-after-errors.
756-
// (This behavior could be limited to only when serialized diagnostics are
757-
// being emitted, but this seems more consistent and less surprising for
758-
// users.)
759-
// FIXME: We don't really need (or want) a full ContinueBuildingAfterErrors.
760-
// If we fail to precompile a bridging header, for example, there's no need
761-
// to go on to compilation of source files, and if compilation of source files
762-
// fails, we shouldn't try to link. Instead, we'd want to let all jobs finish
763-
// but not schedule any new ones.
764-
const bool ContinueBuildingAfterErrors =
765-
BatchMode || ArgList->hasArg(options::OPT_continue_building_after_errors);
766-
767796
if (Diags.hadAnyError())
768797
return nullptr;
769798

770-
std::unique_ptr<UnifiedStatsReporter> StatsReporter;
771-
if (const Arg *A =
772-
ArgList->getLastArgNoClaim(options::OPT_stats_output_dir)) {
773-
StringRef OptType;
774-
if (const Arg *OptA = ArgList->getLastArgNoClaim(options::OPT_O_Group)) {
775-
OptType = OptA->getSpelling();
776-
}
777-
StringRef InputName;
778-
if (Inputs.size() == 1) {
779-
InputName = Inputs[0].second->getSpelling();
780-
}
781-
StringRef OutputType = file_types::getTypeTempSuffix(OI.CompilerOutputType);
782-
StatsReporter = llvm::make_unique<UnifiedStatsReporter>("swift-driver",
783-
OI.ModuleName,
784-
InputName,
785-
DefaultTargetTriple,
786-
OutputType,
787-
OptType,
788-
A->getValue());
789-
}
790-
791799
assert(OI.CompilerOutputType != file_types::ID::TY_INVALID &&
792800
"buildOutputInfo() must set a valid output type!");
793801

@@ -803,14 +811,19 @@ Driver::buildCompilation(const ToolChain &TC,
803811
if (Diags.hadAnyError())
804812
return nullptr;
805813

806-
if (DriverPrintOutputFileMap) {
814+
if (ArgList->hasArg(options::OPT_driver_print_output_file_map)) {
807815
if (OFM)
808816
OFM->dump(llvm::errs(), true);
809817
else
810818
Diags.diagnose(SourceLoc(), diag::error_no_output_file_map_specified);
811819
return nullptr;
812820
}
813821

822+
const bool ShowIncrementalBuildDecisions =
823+
ArgList->hasArg(options::OPT_driver_show_incremental);
824+
const bool Incremental =
825+
computeIncremental(ArgList.get(), ShowIncrementalBuildDecisions);
826+
814827
std::string buildRecordPath;
815828
bool outputBuildRecordForModuleOnlyBuild = false;
816829
getCompilationRecordPath(buildRecordPath, outputBuildRecordForModuleOnlyBuild,
@@ -848,25 +861,53 @@ Driver::buildCompilation(const ToolChain &TC,
848861
llvm_unreachable("Unknown OutputLevel argument!");
849862
}
850863

851-
std::unique_ptr<Compilation> C(
852-
new Compilation(Diags, TC, OI, Level,
853-
std::move(ArgList),
854-
std::move(TranslatedArgList),
855-
std::move(Inputs),
856-
buildRecordPath,
857-
outputBuildRecordForModuleOnlyBuild,
858-
ArgsHash,
859-
StartTime,
860-
LastBuildTime,
861-
DriverFilelistThreshold,
862-
Incremental,
863-
BatchMode,
864-
DriverBatchSeed,
865-
DriverBatchCount,
866-
DriverForceOneBatchRepartition,
867-
SaveTemps,
868-
ShowDriverTimeCompilation,
869-
std::move(StatsReporter)));
864+
865+
// About to move argument list, so capture some flags that will be needed
866+
// later.
867+
const bool DriverPrintActions =
868+
ArgList->hasArg(options::OPT_driver_print_actions);
869+
const bool DriverPrintDerivedOutputFileMap =
870+
ArgList->hasArg(options::OPT_driver_print_derived_output_file_map);
871+
const bool ContinueBuildingAfterErrors =
872+
computeContinueBuildingAfterErrors(BatchMode, ArgList.get());
873+
const bool ShowJobLifecycle =
874+
ArgList->hasArg(options::OPT_driver_show_job_lifecycle);
875+
876+
// In order to confine the values below, while still moving the argument
877+
// list, and preserving the interface to Compilation, enclose the call to the
878+
// constructor in a block:
879+
std::unique_ptr<Compilation> C;
880+
{
881+
const unsigned DriverBatchSeed = getDriverBatchSeed(*ArgList, Diags);
882+
const Optional<unsigned> DriverBatchCount = getDriverBatchCount(*ArgList, Diags);
883+
const bool DriverForceOneBatchRepartition =
884+
ArgList->hasArg(options::OPT_driver_force_one_batch_repartition);
885+
const bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
886+
const bool ShowDriverTimeCompilation =
887+
ArgList->hasArg(options::OPT_driver_time_compilation);
888+
std::unique_ptr<UnifiedStatsReporter> StatsReporter =
889+
computeStatsReporter(ArgList.get(), Inputs, OI, DefaultTargetTriple);
890+
891+
C = llvm::make_unique<Compilation>(
892+
Diags, TC, OI, Level,
893+
std::move(ArgList),
894+
std::move(TranslatedArgList),
895+
std::move(Inputs),
896+
buildRecordPath,
897+
outputBuildRecordForModuleOnlyBuild,
898+
ArgsHash,
899+
StartTime,
900+
LastBuildTime,
901+
DriverFilelistThreshold,
902+
Incremental,
903+
BatchMode,
904+
DriverBatchSeed,
905+
DriverBatchCount,
906+
DriverForceOneBatchRepartition,
907+
SaveTemps,
908+
ShowDriverTimeCompilation,
909+
std::move(StatsReporter));
910+
}
870911

871912
// Construct the graph of Actions.
872913
SmallVector<const Action *, 8> TopLevelActions;

0 commit comments

Comments
 (0)