Skip to content

Commit c1c9cb8

Browse files
author
David Ungar
authored
Merge pull request #16762 from davidungar/buildCompilation-refactor-1
NFC: Some small refactoring in `buildCompilation`
2 parents 05845fd + 62d8501 commit c1c9cb8

File tree

1 file changed

+134
-95
lines changed

1 file changed

+134
-95
lines changed

lib/Driver/Driver.cpp

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

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

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);
690765
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-
}
716-
717-
bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
718-
bool ShowDriverTimeCompilation =
719-
ArgList->hasArg(options::OPT_driver_time_compilation);
720766

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-
}
767+
const std::string workingDirectory = computeWorkingDirectory(ArgList.get());
726768

727769
std::unique_ptr<DerivedArgList> TranslatedArgList(
728770
translateInputAndPathArgs(*ArgList, workingDirectory));
@@ -749,45 +791,9 @@ Driver::buildCompilation(const ToolChain &TC,
749791
OI.CompilerMode = computeCompilerMode(*TranslatedArgList, Inputs, BatchMode);
750792
buildOutputInfo(TC, *TranslatedArgList, BatchMode, Inputs, OI);
751793

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-
767794
if (Diags.hadAnyError())
768795
return nullptr;
769796

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-
791797
assert(OI.CompilerOutputType != file_types::ID::TY_INVALID &&
792798
"buildOutputInfo() must set a valid output type!");
793799

@@ -803,14 +809,19 @@ Driver::buildCompilation(const ToolChain &TC,
803809
if (Diags.hadAnyError())
804810
return nullptr;
805811

806-
if (DriverPrintOutputFileMap) {
812+
if (ArgList->hasArg(options::OPT_driver_print_output_file_map)) {
807813
if (OFM)
808814
OFM->dump(llvm::errs(), true);
809815
else
810816
Diags.diagnose(SourceLoc(), diag::error_no_output_file_map_specified);
811817
return nullptr;
812818
}
813819

820+
const bool ShowIncrementalBuildDecisions =
821+
ArgList->hasArg(options::OPT_driver_show_incremental);
822+
const bool Incremental =
823+
computeIncremental(ArgList.get(), ShowIncrementalBuildDecisions);
824+
814825
std::string buildRecordPath;
815826
bool outputBuildRecordForModuleOnlyBuild = false;
816827
getCompilationRecordPath(buildRecordPath, outputBuildRecordForModuleOnlyBuild,
@@ -848,25 +859,53 @@ Driver::buildCompilation(const ToolChain &TC,
848859
llvm_unreachable("Unknown OutputLevel argument!");
849860
}
850861

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

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

0 commit comments

Comments
 (0)