Skip to content

Commit ee17f61

Browse files
author
David Ungar
committed
factor out comparator and report supp jobs
1 parent d2146d1 commit ee17f61

File tree

3 files changed

+116
-84
lines changed

3 files changed

+116
-84
lines changed

include/swift/Driver/Compilation.h

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,56 @@ enum class PreserveOnSignal : bool {
7474
Yes
7575
};
7676

77+
using CommandSet = llvm::SmallPtrSet<const Job *, 16>;
78+
7779
class Compilation {
80+
public:
81+
class IncrementalSchemeComparator {
82+
const bool EnableSourceRangeDependencies;
83+
const bool &UseSourceRangeDependencies;
84+
85+
/// If not empty, the path to use to log the comparision.
86+
const StringRef CompareIncrementalSchemesPath;
87+
88+
const unsigned SwiftInputCount;
89+
90+
DiagnosticEngine &Diags;
91+
92+
CommandSet DependencyCompileJobs;
93+
CommandSet SourceRangeCompileJobs;
94+
CommandSet SourceRangeLackingSuppJobs;
95+
96+
public:
97+
IncrementalSchemeComparator(bool EnableSourceRangeDependencies,
98+
const bool &UseSourceRangeDependencies,
99+
const StringRef CompareIncrementalSchemesPath,
100+
unsigned SwiftInputCount,
101+
DiagnosticEngine &Diags)
102+
: EnableSourceRangeDependencies(EnableSourceRangeDependencies),
103+
UseSourceRangeDependencies(UseSourceRangeDependencies),
104+
CompareIncrementalSchemesPath(CompareIncrementalSchemesPath),
105+
SwiftInputCount(SwiftInputCount), Diags(Diags) {}
106+
107+
/// Record scheduled jobs in support of the
108+
/// -compare-incremental-schemes[-path] options
109+
///
110+
/// \param depJobs A vector-like collection of jobs that the dependency
111+
/// scheme would run \param rangeJobs A vector-like collection of jobs that
112+
/// the range scheme would run because of changes \param lackingSuppJobs A
113+
/// vector-like collection of jobs that the range scheme would run because
114+
/// there are no incremental supplementary outputs such as swiftdeps,
115+
/// swiftranges, compiledsource
116+
void update(ArrayRef<const Job *> depJobs, ArrayRef<const Job *> rangeJobs,
117+
ArrayRef<const Job *> lackingSuppJobs);
118+
119+
/// Write the information for the -compare-incremental-schemes[-path]
120+
/// options
121+
void outputComparison() const;
122+
123+
private:
124+
void outputComparison(llvm::raw_ostream &) const;
125+
};
126+
78127
public:
79128
/// The filelist threshold value to pass to ensure file lists are never used
80129
static const size_t NEVER_USE_FILELIST = SIZE_MAX;
@@ -228,30 +277,10 @@ class Compilation {
228277
bool UseSourceRangeDependencies = false;
229278

230279
public:
231-
/// How many .swift input files?
232-
unsigned countSwiftInputs() const;
233-
234-
/// Print out a short message comparing dependencies w/ source-ranges, or
235-
/// output it to the path below
236-
const bool CompareIncrementalSchemes;
237-
238-
/// If not empty, the path to use to log the comparision.
239-
const StringRef CompareIncrementalSchemesPath;
240-
241-
template <typename DepJobsT, typename RangeJobsT>
242-
void updateJobsForComparison(const DepJobsT &depJobs,
243-
const RangeJobsT &rangeJobs);
244-
void setFallingBackForComparison();
245-
void outputComparison() const;
280+
/// Will contain a comparator if an argument demands it.
281+
Optional<IncrementalSchemeComparator> IncrementalComparator;
246282

247283
private:
248-
void outputComparison(llvm::raw_ostream &) const;
249-
250-
private:
251-
llvm::SmallPtrSet<const Job *, 16> DependencyCompileJobs;
252-
llvm::SmallPtrSet<const Job *, 16> SourceRangeCompileJobs;
253-
bool FallingBackToDependiesFromSourceRanges = false;
254-
255284
template <typename T>
256285
static T *unwrap(const std::unique_ptr<T> &p) {
257286
return p.get();
@@ -473,6 +502,17 @@ class Compilation {
473502
}
474503
}
475504

505+
/// How many .swift input files?
506+
unsigned countSwiftInputs() const;
507+
508+
void updateIncrementalComparison(ArrayRef<const Job *> depJobs,
509+
ArrayRef<const Job *> rangeJobs,
510+
ArrayRef<const Job *> lackingSuppJobs) {
511+
if (IncrementalComparator.hasValue())
512+
IncrementalComparator.getValue().update(depJobs, rangeJobs,
513+
lackingSuppJobs);
514+
}
515+
476516
private:
477517
/// Perform all jobs.
478518
///

lib/Driver/Compilation.cpp

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,17 @@ Compilation::Compilation(DiagnosticEngine &Diags,
156156
EmitExperimentalDependencyDotFileAfterEveryImport),
157157
ExperimentalDependenciesIncludeIntrafileOnes(
158158
ExperimentalDependenciesIncludeIntrafileOnes),
159-
EnableSourceRangeDependencies(EnableSourceRangeDependencies),
160-
CompareIncrementalSchemes(CompareIncrementalSchemes),
161-
CompareIncrementalSchemesPath(CompareIncrementalSchemesPath) {
159+
EnableSourceRangeDependencies(EnableSourceRangeDependencies) {
160+
if (CompareIncrementalSchemes)
161+
IncrementalComparator.emplace(
162+
EnableSourceRangeDependencies, UseSourceRangeDependencies,
163+
CompareIncrementalSchemesPath, countSwiftInputs(), getDiags());
162164
};
163165
// clang-format on
164166

165167
static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
166168
DiagnosticEngine &diags);
167169

168-
using CommandSet = llvm::SmallPtrSet<const Job *, 16>;
169170
using CommandSetVector = llvm::SetVector<const Job*>;
170171
using BatchPartition = std::vector<std::vector<const Job*>>;
171172

@@ -265,7 +266,7 @@ namespace driver {
265266
if (ScheduledCommands.count(cmd))
266267
return;
267268
if (!Comp.getEnableSourceRangeDependencies() &&
268-
!Comp.CompareIncrementalSchemes && !willBeBuilding)
269+
!Comp.IncrementalComparator && !willBeBuilding)
269270
return; // preserve legacy behavior
270271
const bool isHypothetical =
271272
Comp.getUseSourceRangeDependencies() == forRanges;
@@ -634,7 +635,7 @@ namespace driver {
634635

635636
SmallVector<const Job *, 16> Dependents;
636637
if (!Comp.getUseSourceRangeDependencies()) {
637-
if (!Comp.CompareIncrementalSchemes)
638+
if (!Comp.IncrementalComparator)
638639
Dependents = subsequentJobsNeededForDeps(FinishedCmd, ReturnCode);
639640
else {
640641
SmallVector<const Job *, 16> DependentsForDeps;
@@ -775,10 +776,7 @@ namespace driver {
775776
SmallVector<const Job *, 16> jobsForRanges =
776777
fallBack ? jobsForDependencies : SmallVector<const Job *, 16>();
777778

778-
if (fallBack)
779-
Comp.setFallingBackForComparison();
780-
781-
Comp.updateJobsForComparison(jobsForDependencies, jobsForRanges);
779+
Comp.updateIncrementalComparison(jobsForDependencies, jobsForRanges, {});
782780

783781
return {jobsForRanges, jobsForDependencies};
784782
}
@@ -882,7 +880,7 @@ namespace driver {
882880
void scheduleFirstRoundJobsForIncrementalCompilation(
883881
DependencyGraphT &DepGraph) {
884882

885-
llvm::SmallPtrSet<const Job *, 16> compileJobsToSchedule;
883+
CommandSet compileJobsToSchedule;
886884
for (const Job *Cmd :
887885
computeFirstRoundCompileJobsForIncrementalCompilation(DepGraph))
888886
compileJobsToSchedule.insert(Cmd);
@@ -904,8 +902,8 @@ namespace driver {
904902
auto compileJobsToScheduleViaDependencies =
905903
computeDependenciesAndGetNeededCompileJobs(DepGraph);
906904

907-
const bool mustConsultRanges = Comp.getEnableSourceRangeDependencies() ||
908-
Comp.CompareIncrementalSchemes;
905+
const bool mustConsultRanges =
906+
Comp.getEnableSourceRangeDependencies() || Comp.IncrementalComparator;
909907

910908
if (!mustConsultRanges)
911909
return compileJobsToScheduleViaDependencies;
@@ -921,30 +919,32 @@ namespace driver {
921919
compileJobsToScheduleViaSourceRanges,
922920
jobsLackingSourceRangeSupplementaryOutputs);
923921

924-
Comp.updateJobsForComparison(compileJobsToScheduleViaDependencies,
925-
compileJobsToScheduleViaSourceRanges);
926-
if (shouldFallBack)
927-
Comp.setFallingBackForComparison();
922+
Comp.updateIncrementalComparison(
923+
compileJobsToScheduleViaDependencies,
924+
compileJobsToScheduleViaSourceRanges,
925+
jobsLackingSourceRangeSupplementaryOutputs);
928926

929927
if (!Comp.getEnableSourceRangeDependencies())
930928
return compileJobsToScheduleViaDependencies;
931929

932930
Comp.setUseSourceRangeDependencies(!shouldFallBack);
933931

934-
if (shouldFallBack) {
935-
// Even if dependencies would not schedule these, we want them to run
936-
// to create the supplementary outputs for next time.
937-
for (const Job *Cmd : jobsLackingSourceRangeSupplementaryOutputs)
938-
compileJobsToScheduleViaDependencies.push_back(Cmd);
939-
}
940-
return shouldFallBack ? compileJobsToScheduleViaDependencies
941-
: compileJobsToScheduleViaSourceRanges;
932+
if (!shouldFallBack)
933+
return compileJobsToScheduleViaSourceRanges;
934+
935+
auto compileJobsToScheduleWhenFallingBack =
936+
std::move(compileJobsToScheduleViaDependencies);
937+
// Even if dependencies would not schedule these, we want them to run
938+
// to create the supplementary outputs for next time.
939+
for (const Job *Cmd : jobsLackingSourceRangeSupplementaryOutputs)
940+
compileJobsToScheduleWhenFallingBack.push_back(Cmd);
941+
942+
return compileJobsToScheduleWhenFallingBack;
942943
}
943944

944945
bool decideAndExplainWhetherToFallBackToDependencies(
945-
llvm::SmallVector<const Job *, 16>
946-
&compileJobsToScheduleViaSourceRanges,
947-
const llvm::SmallVector<const Job *, 16>
946+
const ArrayRef<const Job *> compileJobsToScheduleViaSourceRanges,
947+
const ArrayRef<const Job *>
948948
&jobsLackingSourceRangeSupplementaryOutputs) {
949949
if (!jobsLackingSourceRangeSupplementaryOutputs.empty()) {
950950
if (Comp.getShowIncrementalBuildDecisions()) {
@@ -961,7 +961,7 @@ namespace driver {
961961
}
962962
// Unless the source-range scheme would compile every file,
963963
// it's likely a better bet.
964-
llvm::SmallPtrSet<const Job *, 16> uniqueJobs;
964+
CommandSet uniqueJobs;
965965
for (const auto *J : compileJobsToScheduleViaSourceRanges)
966966
uniqueJobs.insert(J);
967967
if (uniqueJobs.size() < Comp.countSwiftInputs()) {
@@ -1921,7 +1921,8 @@ int Compilation::performJobs(std::unique_ptr<TaskQueue> &&TQ) {
19211921
bool abnormalExit;
19221922
int result = performJobsImpl(abnormalExit, std::move(TQ));
19231923

1924-
outputComparison();
1924+
if (IncrementalComparator)
1925+
IncrementalComparator->outputComparison();
19251926

19261927
if (!SaveTemps) {
19271928
for (const auto &pathPair : TempFilePaths) {
@@ -1955,23 +1956,18 @@ const char *Compilation::getAllSourcesPath() const {
19551956
return AllSourceFilesPath;
19561957
}
19571958

1958-
template <typename DepJobsT, typename RangeJobsT>
1959-
void Compilation::updateJobsForComparison(const DepJobsT &depJobs,
1960-
const RangeJobsT &rangeJobs) {
1959+
void Compilation::IncrementalSchemeComparator::update(
1960+
const ArrayRef<const Job *> depJobs, const ArrayRef<const Job *> rangeJobs,
1961+
const ArrayRef<const Job *> lackingSuppJobs) {
19611962
for (const auto *cmd : depJobs)
19621963
DependencyCompileJobs.insert(cmd);
19631964
for (const auto *cmd : rangeJobs)
19641965
SourceRangeCompileJobs.insert(cmd);
1966+
for (const auto *cmd : lackingSuppJobs)
1967+
SourceRangeLackingSuppJobs.insert(cmd);
19651968
}
19661969

1967-
void Compilation::setFallingBackForComparison() {
1968-
FallingBackToDependiesFromSourceRanges = true;
1969-
}
1970-
1971-
void Compilation::outputComparison() const {
1972-
if (!CompareIncrementalSchemes)
1973-
return;
1974-
1970+
void Compilation::IncrementalSchemeComparator::outputComparison() const {
19751971
if (CompareIncrementalSchemesPath.empty()) {
19761972
outputComparison(llvm::outs());
19771973
return;
@@ -1983,30 +1979,25 @@ void Compilation::outputComparison() const {
19831979
FA_Write, OF_Append | OF_Text);
19841980

19851981
if (EC) {
1986-
getDiags().diagnose(SourceLoc(),
1987-
diag::unable_to_open_incremental_comparison_log,
1988-
CompareIncrementalSchemesPath);
1982+
Diags.diagnose(SourceLoc(), diag::unable_to_open_incremental_comparison_log,
1983+
CompareIncrementalSchemesPath);
19891984
return;
19901985
}
19911986
outputComparison(OS);
19921987
}
19931988

1994-
void Compilation::outputComparison(llvm::raw_ostream &out) const {
1995-
if (!getIncrementalBuildEnabled())
1996-
out << "*** Comparing incremental strategies is moot: incremental "
1997-
"compilation disabled ***\n";
1998-
else if (FallingBackToDependiesFromSourceRanges)
1999-
out << "*** Comparing deps: " << DependencyCompileJobs.size()
2000-
<< ", ranges: " << SourceRangeCompileJobs.size()
2001-
<< ", total: " << countSwiftInputs() << " ***\n";
2002-
else
2003-
out << "*** Comparing incremental strategies is moot: would fall "
2004-
"back and run "
2005-
<< DependencyCompileJobs.size()
2006-
<< ", ranges: " << SourceRangeCompileJobs.size()
2007-
<< ", total: " << countSwiftInputs()
2008-
<< ", plus more to create source-range and compiled-source files"
2009-
<< " ***\n";
1989+
void Compilation::IncrementalSchemeComparator::outputComparison(
1990+
llvm::raw_ostream &out) const {
1991+
out << "*** Comparing incremental schemes: "
1992+
<< "deps: " << DependencyCompileJobs.size() << ", "
1993+
<< "ranges: " << SourceRangeCompileJobs.size() << ", "
1994+
<< "supplementary output creation: " << SourceRangeLackingSuppJobs.size()
1995+
<< ", "
1996+
<< "total: " << SwiftInputCount << ", "
1997+
<< "scheme requested: "
1998+
<< (EnableSourceRangeDependencies ? "ranges" : "deps") << ", "
1999+
<< "scheme used: " << (UseSourceRangeDependencies ? "ranges" : "deps")
2000+
<< "\n";
20102001
}
20112002

20122003
unsigned Compilation::countSwiftInputs() const {

lib/Driver/Driver.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ Driver::buildCompilation(const ToolChain &TC,
864864

865865
Optional<OutputFileMap> OFM = buildOutputFileMap(
866866
*TranslatedArgList, workingDirectory,
867-
EnableSourceRangeDependencies || CompareIncrementalSchemes);
867+
/*addEntriesForSourceFileDependencies=*/EnableSourceRangeDependencies ||
868+
CompareIncrementalSchemes);
868869

869870
if (Diags.hadAnyError())
870871
return nullptr;
@@ -2997,7 +2998,7 @@ void Driver::chooseDependenciesOutputPaths(Compilation &C,
29972998
}
29982999
if (C.getIncrementalBuildEnabled()) {
29993000
file_types::forEachIncrementalOutputType([&](file_types::ID type) {
3000-
if (C.getEnableSourceRangeDependencies() || C.CompareIncrementalSchemes ||
3001+
if (C.getEnableSourceRangeDependencies() || C.IncrementalComparator ||
30013002
type == file_types::TY_SwiftDeps)
30023003
addAuxiliaryOutput(C, *Output, type, OutputMap, workingDirectory);
30033004
});

0 commit comments

Comments
 (0)