Skip to content

Commit ae64f47

Browse files
author
David Ungar
committed
Reporting refactoring
1 parent bc40342 commit ae64f47

File tree

4 files changed

+115
-74
lines changed

4 files changed

+115
-74
lines changed

include/swift/Driver/Compilation.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ using CommandSet = llvm::SmallPtrSet<const Job *, 16>;
7979
class Compilation {
8080
public:
8181
class IncrementalSchemeComparator {
82+
const bool &EnableIncrementalBuild;
8283
const bool EnableSourceRangeDependencies;
8384
const bool &UseSourceRangeDependencies;
8485

@@ -87,19 +88,25 @@ class Compilation {
8788

8889
const unsigned SwiftInputCount;
8990

91+
public:
92+
std::string WhyIncrementalWasDisabled = "";
93+
94+
private:
9095
DiagnosticEngine &Diags;
9196

9297
CommandSet DependencyCompileJobs;
9398
CommandSet SourceRangeCompileJobs;
9499
CommandSet SourceRangeLackingSuppJobs;
95100

96101
public:
97-
IncrementalSchemeComparator(bool EnableSourceRangeDependencies,
102+
IncrementalSchemeComparator(const bool &EnableIncrementalBuild,
103+
bool EnableSourceRangeDependencies,
98104
const bool &UseSourceRangeDependencies,
99105
const StringRef CompareIncrementalSchemesPath,
100106
unsigned SwiftInputCount,
101107
DiagnosticEngine &Diags)
102-
: EnableSourceRangeDependencies(EnableSourceRangeDependencies),
108+
: EnableIncrementalBuild(EnableIncrementalBuild),
109+
EnableSourceRangeDependencies(EnableSourceRangeDependencies),
103110
UseSourceRangeDependencies(UseSourceRangeDependencies),
104111
CompareIncrementalSchemesPath(CompareIncrementalSchemesPath),
105112
SwiftInputCount(SwiftInputCount), Diags(Diags) {}
@@ -374,9 +381,7 @@ class Compilation {
374381
bool getIncrementalBuildEnabled() const {
375382
return EnableIncrementalBuild;
376383
}
377-
void disableIncrementalBuild() {
378-
EnableIncrementalBuild = false;
379-
}
384+
void disableIncrementalBuild(Twine why);
380385

381386
bool getEnableExperimentalDependencies() const {
382387
return EnableExperimentalDependencies;

lib/Driver/Compilation.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ Compilation::Compilation(DiagnosticEngine &Diags,
159159
EnableSourceRangeDependencies(EnableSourceRangeDependencies) {
160160
if (CompareIncrementalSchemes)
161161
IncrementalComparator.emplace(
162-
EnableSourceRangeDependencies, UseSourceRangeDependencies,
162+
// Ensure the references are to inst vars, NOT arguments
163+
this->EnableIncrementalBuild,
164+
EnableSourceRangeDependencies,
165+
this->UseSourceRangeDependencies,
163166
CompareIncrementalSchemesPath, countSwiftInputs(), getDiags());
164167
};
165168
// clang-format on
@@ -418,10 +421,9 @@ namespace driver {
418421
Comp.getDiags().diagnose(SourceLoc(),
419422
diag::warn_unable_to_load_dependencies,
420423
DependenciesFile);
421-
Comp.disableIncrementalBuild();
422-
if (Comp.getShowIncrementalBuildDecisions())
423-
llvm::outs() << "Incremental compilation has been disabled due to "
424-
<< "malformed swift dependencies file '" << DependenciesFile << "'.\n";
424+
Comp.disableIncrementalBuild(
425+
Twine("Malformed swift dependencies file ' ") + DependenciesFile +
426+
"'");
425427
}
426428

427429
/// Helper that attempts to reload a job's .swiftdeps file after the job
@@ -1955,6 +1957,15 @@ const char *Compilation::getAllSourcesPath() const {
19551957
return AllSourceFilesPath;
19561958
}
19571959

1960+
void Compilation::disableIncrementalBuild(Twine why) {
1961+
if (getShowIncrementalBuildDecisions())
1962+
llvm::outs() << "Disabling incremental build: " << why << "\n";
1963+
1964+
EnableIncrementalBuild = false;
1965+
if (IncrementalComparator)
1966+
IncrementalComparator->WhyIncrementalWasDisabled = why.str();
1967+
}
1968+
19581969
void Compilation::IncrementalSchemeComparator::update(
19591970
const CommandSet &depJobs,
19601971
const CommandSet &rangeJobs,
@@ -1988,15 +1999,34 @@ void Compilation::IncrementalSchemeComparator::outputComparison() const {
19881999

19892000
void Compilation::IncrementalSchemeComparator::outputComparison(
19902001
llvm::raw_ostream &out) const {
2002+
if (!EnableIncrementalBuild) {
2003+
// No stats will have been gathered
2004+
assert(!WhyIncrementalWasDisabled.empty() && "Must be a reason");
2005+
out << "*** Incremental build disabled because "
2006+
<< WhyIncrementalWasDisabled << ", cannot compare ***\n";
2007+
return;
2008+
}
2009+
unsigned additionalDependencyJobsToCreateSupps = 0;
2010+
for (const Job *Cmd : SourceRangeLackingSuppJobs) {
2011+
if (!DependencyCompileJobs.count(Cmd))
2012+
++additionalDependencyJobsToCreateSupps;
2013+
}
2014+
unsigned jobsWhenEnablingDeps = DependencyCompileJobs.size();
2015+
unsigned jobsWhenEnablingRanges =
2016+
UseSourceRangeDependencies ? SourceRangeCompileJobs.size()
2017+
: DependencyCompileJobs.size() +
2018+
additionalDependencyJobsToCreateSupps;
2019+
19912020
out << "*** Comparing incremental schemes: "
1992-
<< "deps: " << DependencyCompileJobs.size() << ", "
1993-
<< "ranges: " << SourceRangeCompileJobs.size() << ", "
1994-
<< "supplementary output creation: " << SourceRangeLackingSuppJobs.size()
1995-
<< ", "
2021+
<< "deps: " << jobsWhenEnablingDeps << ", "
2022+
<< "ranges: " << jobsWhenEnablingRanges << ", "
19962023
<< "total: " << SwiftInputCount << ", "
1997-
<< "requested: "
1998-
<< (EnableSourceRangeDependencies ? "ranges" : "deps") << ", "
1999-
<< "using: " << (UseSourceRangeDependencies ? "ranges" : "deps")
2024+
<< "requested: " << (EnableSourceRangeDependencies ? "ranges" : "deps")
2025+
<< ", "
2026+
<< "using: "
2027+
<< (!EnableIncrementalBuild
2028+
? "neither"
2029+
: UseSourceRangeDependencies ? "ranges" : "deps")
20002030
<< "\n";
20012031
}
20022032

lib/Driver/Driver.cpp

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,19 @@ static bool getCompilationRecordPath(std::string &buildRecordPath,
375375
return false;
376376
}
377377

378-
static bool failedToReadOutOfDateMap(bool ShowIncrementalBuildDecisions,
379-
StringRef buildRecordPath,
380-
StringRef reason = "") {
378+
static StringRef failedToReadOutOfDateMap(bool ShowIncrementalBuildDecisions,
379+
StringRef buildRecordPath,
380+
StringRef reason = "") {
381+
std::string why = "malformed build record file";
382+
if (!reason.empty()) {
383+
why += " ";
384+
why += reason;
385+
}
381386
if (ShowIncrementalBuildDecisions) {
382-
llvm::outs() << "Incremental compilation has been disabled due to "
383-
<< "malformed build record file '" << buildRecordPath << "'.";
384-
if (!reason.empty()) {
385-
llvm::outs() << " " << reason;
386-
}
387-
llvm::outs() << "\n";
387+
llvm::outs() << "Incremental compilation has been disabled due to " << why
388+
<< " '" << buildRecordPath << "'.\n";
388389
}
389-
return true;
390+
return why;
390391
}
391392

392393
static SmallVector<StringRef, 8> findRemovedInputs(
@@ -396,20 +397,19 @@ static SmallVector<StringRef, 8> findRemovedInputs(
396397
static void dealWithRemovedInputs(ArrayRef<StringRef> removedInputs,
397398
bool ShowIncrementalBuildDecisions);
398399

399-
/// Returns true on error.
400-
static bool populateOutOfDateMap(InputInfoMap &map,
401-
llvm::sys::TimePoint<> &LastBuildTime,
402-
StringRef argsHashStr,
403-
const InputFileList &inputs,
404-
StringRef buildRecordPath,
405-
const bool EnableSourceRangeDependencies,
406-
const bool ShowIncrementalBuildDecisions) {
400+
/// Returns why ignore incrementality
401+
static StringRef
402+
populateOutOfDateMap(InputInfoMap &map, llvm::sys::TimePoint<> &LastBuildTime,
403+
StringRef argsHashStr, const InputFileList &inputs,
404+
StringRef buildRecordPath,
405+
const bool EnableSourceRangeDependencies,
406+
const bool ShowIncrementalBuildDecisions) {
407407
// Treat a missing file as "no previous build".
408408
auto buffer = llvm::MemoryBuffer::getFile(buildRecordPath);
409409
if (!buffer) {
410410
if (ShowIncrementalBuildDecisions)
411411
llvm::outs() << "Incremental compilation could not read build record.\n";
412-
return false;
412+
return "could not read build record";
413413
}
414414

415415
namespace yaml = llvm::yaml;
@@ -499,7 +499,7 @@ static bool populateOutOfDateMap(InputInfoMap &map,
499499
} else if (keyStr == compilation_record::getName(TopLevelKey::Options)) {
500500
auto *value = dyn_cast<yaml::ScalarNode>(i->getValue());
501501
if (!value)
502-
return true;
502+
return "no name node in build record";
503503
optionsMatch = (argsHashStr == value->getValue(scratch));
504504

505505
} else if (keyStr == compilation_record::getName(TopLevelKey::BuildTime)) {
@@ -512,7 +512,7 @@ static bool populateOutOfDateMap(InputInfoMap &map,
512512
}
513513
llvm::sys::TimePoint<> timeVal;
514514
if (readTimeValue(i->getValue(), timeVal))
515-
return true;
515+
return "could not read time value in build record";
516516
LastBuildTime = timeVal;
517517

518518
} else if (keyStr == compilation_record::getName(TopLevelKey::Inputs)) {
@@ -529,21 +529,21 @@ static bool populateOutOfDateMap(InputInfoMap &map,
529529
for (auto i = inputMap->begin(), e = inputMap->end(); i != e; ++i) {
530530
auto *key = dyn_cast<yaml::ScalarNode>(i->getKey());
531531
if (!key)
532-
return true;
532+
return "no input entry in build record";
533533

534534
auto *value = dyn_cast<yaml::SequenceNode>(i->getValue());
535535
if (!value)
536-
return true;
536+
return "no sequence node for input entry in build record";
537537

538538
using compilation_record::getInfoStatusForIdentifier;
539539
auto previousBuildState =
540540
getInfoStatusForIdentifier(value->getRawTag());
541541
if (!previousBuildState)
542-
return true;
542+
return "no previous build state in build record";
543543

544544
llvm::sys::TimePoint<> timeValue;
545545
if (readTimeValue(value, timeValue))
546-
return true;
546+
return "could not read time value in build record";
547547

548548
auto inputName = key->getValue(scratch);
549549
previousInputs[inputName] = { *previousBuildState, timeValue };
@@ -561,15 +561,15 @@ static bool populateOutOfDateMap(InputInfoMap &map,
561561
<< "\tPreviously compiled with: "
562562
<< CompilationRecordSwiftVersion << "\n";
563563
}
564-
return true;
564+
return "compiler version mismatch";
565565
}
566566

567567
if (!optionsMatch) {
568568
if (ShowIncrementalBuildDecisions) {
569569
llvm::outs() << "Incremental compilation has been disabled, because "
570570
<< "different arguments were passed to the compiler.\n";
571571
}
572-
return true;
572+
return "different arguments passed to compiler";
573573
}
574574

575575
unsigned numMatchingPreviouslyCompiledInputs = 0;
@@ -586,13 +586,14 @@ static bool populateOutOfDateMap(InputInfoMap &map,
586586
auto const wereAnyInputsRemoved =
587587
numMatchingPreviouslyCompiledInputs < previousInputs.size();
588588
if (!wereAnyInputsRemoved)
589-
return false;
589+
return "";
590590

591591
const auto removedInputs = findRemovedInputs(inputs, previousInputs);
592592
assert(!removedInputs.empty());
593593

594594
dealWithRemovedInputs(removedInputs, ShowIncrementalBuildDecisions);
595-
return true; // recompile everything; could do better someday
595+
return "an input was removed"; // recompile everything; could do better
596+
// someday
596597
}
597598

598599
static SmallVector<StringRef, 8> findRemovedInputs(
@@ -892,16 +893,16 @@ Driver::buildCompilation(const ToolChain &TC,
892893
computeArgsHash(ArgsHash, *TranslatedArgList);
893894
llvm::sys::TimePoint<> LastBuildTime = llvm::sys::TimePoint<>::min();
894895
InputInfoMap outOfDateMap;
895-
bool rebuildEverything = true;
896-
if (Incremental && !buildRecordPath.empty()) {
897-
if (populateOutOfDateMap(outOfDateMap, LastBuildTime, ArgsHash, Inputs,
898-
buildRecordPath, EnableSourceRangeDependencies,
899-
ShowIncrementalBuildDecisions)) {
900-
// FIXME: Distinguish errors from "file removed", which is benign.
901-
} else {
902-
rebuildEverything = false;
903-
}
904-
}
896+
StringRef whyIgnoreIncrementallity =
897+
!Incremental
898+
? ""
899+
: buildRecordPath.empty()
900+
? "no build record path"
901+
: populateOutOfDateMap(outOfDateMap, LastBuildTime, ArgsHash,
902+
Inputs, buildRecordPath,
903+
EnableSourceRangeDependencies,
904+
ShowIncrementalBuildDecisions);
905+
// FIXME: Distinguish errors from "file removed", which is benign.
905906

906907
size_t DriverFilelistThreshold;
907908
if (getFilelistThreshold(*TranslatedArgList, DriverFilelistThreshold, Diags))
@@ -994,7 +995,7 @@ Driver::buildCompilation(const ToolChain &TC,
994995
// Construct the graph of Actions.
995996
SmallVector<const Action *, 8> TopLevelActions;
996997
buildActions(TopLevelActions, TC, OI,
997-
rebuildEverything ? nullptr : &outOfDateMap, *C);
998+
whyIgnoreIncrementallity.empty() ? &outOfDateMap : nullptr, *C);
998999

9991000
if (Diags.hadAnyError())
10001001
return nullptr;
@@ -1025,8 +1026,8 @@ Driver::buildCompilation(const ToolChain &TC,
10251026

10261027
// This has to happen after building jobs, because otherwise we won't even
10271028
// emit .swiftdeps files for the next build.
1028-
if (rebuildEverything)
1029-
C->disableIncrementalBuild();
1029+
if (!whyIgnoreIncrementallity.empty())
1030+
C->disableIncrementalBuild(whyIgnoreIncrementallity.str());
10301031

10311032
if (Diags.hadAnyError())
10321033
return nullptr;

0 commit comments

Comments
 (0)