Skip to content

Commit cbd458c

Browse files
author
David Ungar
committed
Make tests more deterministic by sorting jobs before scheduling
1 parent 8c2c4ab commit cbd458c

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

include/swift/Driver/Compilation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,9 @@ class Compilation {
529529
/// sequence of inputs the driver was initially invoked with.
530530
///
531531
/// Also use to write out information in a consistent order.
532+
template <typename JobCollection>
532533
void sortJobsToMatchCompilationInputs(
533-
ArrayRef<const Job *> unsortedJobs,
534+
const JobCollection &unsortedJobs,
534535
SmallVectorImpl<const Job *> &sortedJobs) const;
535536

536537
private:

lib/Driver/Compilation.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ namespace driver {
355355
PendingExecution.insert(Cmd);
356356
}
357357

358+
// Sort for ease of testing
359+
template <typename Jobs>
360+
void scheduleCommandsInSortedOrder(const Jobs &jobs) {
361+
llvm::SmallVector<const Job *, 16> sortedJobs;
362+
Comp.sortJobsToMatchCompilationInputs(jobs, sortedJobs);
363+
for (const Job *Cmd : sortedJobs)
364+
scheduleCommandIfNecessaryAndPossible(Cmd);
365+
}
366+
358367
void addPendingJobToTaskQueue(const Job *Cmd) {
359368
// FIXME: Failing here should not take down the whole process.
360369
bool success =
@@ -393,8 +402,7 @@ namespace driver {
393402
<< LogJobArray(AllBlocked) << "\n";
394403
}
395404
BlockingCommands.erase(BlockedIter);
396-
for (auto *Blocked : AllBlocked)
397-
scheduleCommandIfNecessaryAndPossible(Blocked);
405+
scheduleCommandsInSortedOrder(AllBlocked);
398406
}
399407
}
400408

@@ -496,8 +504,7 @@ namespace driver {
496504
break;
497505
dependencyLoadFailed(DependenciesFile);
498506
// Better try compiling whatever was waiting on more info.
499-
for (const Job *Cmd : DeferredCommands)
500-
scheduleCommandIfNecessaryAndPossible(Cmd);
507+
scheduleCommandsInSortedOrder(DeferredCommands);
501508
DeferredCommands.clear();
502509
break;
503510

@@ -696,18 +703,9 @@ namespace driver {
696703
noteBuildingJobs(DependentsInEffect, useRangesForScheduling,
697704
"because of dependencies discovered later");
698705

699-
// Sort dependents for more deterministic behavior
700-
llvm::SmallVector<const Job *, 16> UnsortedDependents;
701-
for (const Job *j : DependentsInEffect)
702-
UnsortedDependents.push_back(j);
703-
llvm::SmallVector<const Job *, 16> SortedDependents;
704-
Comp.sortJobsToMatchCompilationInputs(UnsortedDependents,
705-
SortedDependents);
706-
707-
for (const Job *Cmd : SortedDependents) {
706+
scheduleCommandsInSortedOrder(DependentsInEffect);
707+
for (const Job *Cmd : DependentsInEffect)
708708
DeferredCommands.erase(Cmd);
709-
scheduleCommandIfNecessaryAndPossible(Cmd);
710-
}
711709
return TaskFinishedResponse::ContinueExecution;
712710
}
713711

@@ -2087,17 +2085,22 @@ void Compilation::addDependencyPathOrCreateDummy(
20872085
}
20882086
}
20892087

2088+
template <typename JobCollection>
20902089
void Compilation::sortJobsToMatchCompilationInputs(
2091-
const ArrayRef<const Job *> unsortedJobs,
2090+
const JobCollection &unsortedJobs,
20922091
SmallVectorImpl<const Job *> &sortedJobs) const {
20932092
llvm::DenseMap<StringRef, const Job *> jobsByInput;
20942093
for (const Job *J : unsortedJobs) {
2095-
const CompileJobAction *CJA = cast<CompileJobAction>(&J->getSource());
2096-
const InputAction *IA = CJA->findSingleSwiftInput();
2097-
auto R =
2098-
jobsByInput.insert(std::make_pair(IA->getInputArg().getValue(), J));
2099-
assert(R.second);
2100-
(void)R;
2094+
// Only worry about sorting compilation jobs
2095+
if (const CompileJobAction *CJA =
2096+
dyn_cast<CompileJobAction>(&J->getSource())) {
2097+
const InputAction *IA = CJA->findSingleSwiftInput();
2098+
auto R =
2099+
jobsByInput.insert(std::make_pair(IA->getInputArg().getValue(), J));
2100+
assert(R.second);
2101+
(void)R;
2102+
} else
2103+
sortedJobs.push_back(J);
21012104
}
21022105
for (const InputPair &P : getInputFiles()) {
21032106
auto I = jobsByInput.find(P.second->getValue());
@@ -2106,3 +2109,8 @@ void Compilation::sortJobsToMatchCompilationInputs(
21062109
}
21072110
}
21082111
}
2112+
2113+
template void
2114+
Compilation::sortJobsToMatchCompilationInputs<ArrayRef<const Job *>>(
2115+
const ArrayRef<const Job *> &,
2116+
SmallVectorImpl<const Job *> &sortedJobs) const;

0 commit comments

Comments
 (0)