Skip to content

Commit 4c365a7

Browse files
author
David Ungar
authored
Merge pull request swiftlang#28702 from davidungar/only-one-dependency-file
[Incremental, Driver] Only emit a real make-style dependency file for one frontend job.
2 parents b48b786 + 2fc0f78 commit 4c365a7

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

include/swift/Driver/Compilation.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ class Compilation {
257257
/// limit filelists will be used.
258258
size_t FilelistThreshold;
259259

260+
/// Because each frontend job outputs the same info in its .d file, only do it
261+
/// on the first job that actually runs. Write out dummies for the rest of the
262+
/// jobs. This hack saves a lot of time in the build system when incrementally
263+
/// building a project with many files. Record if a scheduled job has already
264+
/// added -emit-dependency-path.
265+
bool HaveAlreadyAddedDependencyPath = false;
266+
267+
/// When set, only the first scheduled frontend job gets the argument needed
268+
/// to produce a make-style dependency file. The other jobs create dummy files
269+
/// in the driver. This hack speeds up incremental compilation by reducing the
270+
/// time for the build system to read each dependency file, which are all
271+
/// identical. This optimization can be disabled by passing
272+
/// -disable-only-one-dependency-file on the command line.
273+
const bool OnlyOneDependencyFile;
274+
260275
/// Scaffolding to permit experimentation with finer-grained dependencies and
261276
/// faster rebuilds.
262277
const bool EnableFineGrainedDependencies;
@@ -309,6 +324,7 @@ class Compilation {
309324
bool SaveTemps = false,
310325
bool ShowDriverTimeCompilation = false,
311326
std::unique_ptr<UnifiedStatsReporter> Stats = nullptr,
327+
bool OnlyOneDependencyFile = false,
312328
bool EnableFineGrainedDependencies = false,
313329
bool VerifyFineGrainedDependencyGraphAfterEveryImport = false,
314330
bool EmitFineGrainedDependencyDotFileAfterEveryImport = false,
@@ -427,6 +443,14 @@ class Compilation {
427443
return FilelistThreshold;
428444
}
429445

446+
/// Called to decide whether to add a dependency path argument, or whether to
447+
/// create a dummy file. Responds by invoking one of the two passed-in
448+
/// functions.
449+
void addDependencyPathOrCreateDummy(
450+
const CommandOutput &Output,
451+
function_ref<void(StringRef)> addDependencyPath,
452+
function_ref<void(StringRef)> createDummy);
453+
430454
UnifiedStatsReporter *getStatsReporter() const {
431455
return Stats.get();
432456
}

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def enable_fine_grained_dependencies :
139139
Flag<["-"], "enable-fine-grained-dependencies">, Flags<[FrontendOption, HelpHidden]>,
140140
HelpText<"Experimental work-in-progress to be more selective about incremental recompilation">;
141141

142+
143+
def disable_only_one_dependency_file :
144+
Flag<["-"], "disable-only-one-dependency-file">, Flags<[DoesNotAffectIncrementalBuild]>,
145+
HelpText<"Disables incremental build optimization that only produces one dependencies file">;
146+
142147
def enable_source_range_dependencies :
143148
Flag<["-"], "enable-source-range-dependencies">, Flags<[]>,
144149
HelpText<"Try using source range information">;

lib/Driver/Compilation.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,15 @@ Compilation::Compilation(DiagnosticEngine &Diags,
121121
bool SaveTemps,
122122
bool ShowDriverTimeCompilation,
123123
std::unique_ptr<UnifiedStatsReporter> StatsReporter,
124+
bool OnlyOneDependencyFile,
124125
bool EnableFineGrainedDependencies,
125126
bool VerifyFineGrainedDependencyGraphAfterEveryImport,
126127
bool EmitFineGrainedDependencyDotFileAfterEveryImport,
127128
bool FineGrainedDependenciesIncludeIntrafileOnes,
128129
bool EnableSourceRangeDependencies,
129130
bool CompareIncrementalSchemes,
130-
StringRef CompareIncrementalSchemesPath)
131+
StringRef CompareIncrementalSchemesPath
132+
)
131133
: Diags(Diags), TheToolChain(TC),
132134
TheOutputInfo(OI),
133135
Level(Level),
@@ -149,14 +151,16 @@ Compilation::Compilation(DiagnosticEngine &Diags,
149151
ShowDriverTimeCompilation(ShowDriverTimeCompilation),
150152
Stats(std::move(StatsReporter)),
151153
FilelistThreshold(FilelistThreshold),
154+
OnlyOneDependencyFile(OnlyOneDependencyFile),
152155
EnableFineGrainedDependencies(EnableFineGrainedDependencies),
153156
VerifyFineGrainedDependencyGraphAfterEveryImport(
154157
VerifyFineGrainedDependencyGraphAfterEveryImport),
155158
EmitFineGrainedDependencyDotFileAfterEveryImport(
156159
EmitFineGrainedDependencyDotFileAfterEveryImport),
157160
FineGrainedDependenciesIncludeIntrafileOnes(
158161
FineGrainedDependenciesIncludeIntrafileOnes),
159-
EnableSourceRangeDependencies(EnableSourceRangeDependencies) {
162+
EnableSourceRangeDependencies(EnableSourceRangeDependencies)
163+
{
160164
if (CompareIncrementalSchemes)
161165
IncrementalComparator.emplace(
162166
// Ensure the references are to inst vars, NOT arguments
@@ -2032,3 +2036,19 @@ unsigned Compilation::countSwiftInputs() const {
20322036
++inputCount;
20332037
return inputCount;
20342038
}
2039+
2040+
void Compilation::addDependencyPathOrCreateDummy(
2041+
const CommandOutput &Output,
2042+
function_ref<void(StringRef)> addDependencyPath,
2043+
function_ref<void(StringRef)> createDummy) {
2044+
StringRef path =
2045+
Output.getAdditionalOutputForType(file_types::TY_Dependencies);
2046+
if (path.empty())
2047+
return;
2048+
if (HaveAlreadyAddedDependencyPath && OnlyOneDependencyFile)
2049+
createDummy(path);
2050+
else {
2051+
addDependencyPath(path);
2052+
HaveAlreadyAddedDependencyPath = true;
2053+
}
2054+
}

lib/Driver/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,11 @@ Driver::buildCompilation(const ToolChain &TC,
951951
ArgList->hasArg(options::OPT_driver_time_compilation);
952952
std::unique_ptr<UnifiedStatsReporter> StatsReporter =
953953
createStatsReporter(ArgList.get(), Inputs, OI, DefaultTargetTriple);
954+
955+
const bool OnlyOneDependencyFile =
956+
!ArgList->hasArg(options::OPT_disable_only_one_dependency_file);
954957
// relies on the new dependency graph
958+
955959
const bool EnableFineGrainedDependencies =
956960
ArgList->hasArg(options::OPT_enable_fine_grained_dependencies);
957961

@@ -984,6 +988,7 @@ Driver::buildCompilation(const ToolChain &TC,
984988
SaveTemps,
985989
ShowDriverTimeCompilation,
986990
std::move(StatsReporter),
991+
OnlyOneDependencyFile,
987992
EnableFineGrainedDependencies,
988993
VerifyFineGrainedDependencyGraphAfterEveryImport,
989994
EmitFineGrainedDependencyDotFileAfterEveryImport,

lib/Driver/ToolChains.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "llvm/Support/Process.h"
3535
#include "llvm/Support/Program.h"
3636

37+
#include <fstream>
38+
3739
using namespace swift;
3840
using namespace swift::driver;
3941
using namespace llvm::opt;
@@ -671,8 +673,17 @@ void ToolChain::JobContext::addFrontendSupplementaryOutputArguments(
671673
"mode!");
672674
}
673675

674-
addOutputsOfType(arguments, Output, Args, file_types::TY_Dependencies,
675-
"-emit-dependencies-path");
676+
C.addDependencyPathOrCreateDummy(
677+
Output,
678+
[&](StringRef) {
679+
addOutputsOfType(arguments, Output, Args, file_types::TY_Dependencies,
680+
"-emit-dependencies-path");
681+
},
682+
[&](StringRef dependencyFile) {
683+
// Create an empty file
684+
std::ofstream(dependencyFile.str().c_str());
685+
});
686+
676687
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftDeps,
677688
"-emit-reference-dependencies-path");
678689
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftRanges,

0 commit comments

Comments
 (0)