Skip to content

Commit f0b5833

Browse files
authored
Merge pull request #16897 from CodaFi/taskmaster
2 parents 4c62c19 + da13d89 commit f0b5833

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

include/swift/Driver/Compilation.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ namespace opt {
4040

4141
namespace swift {
4242
class DiagnosticEngine;
43+
namespace sys {
44+
class TaskQueue;
45+
}
4346

4447
namespace driver {
4548
class Driver;
@@ -72,6 +75,7 @@ enum class PreserveOnSignal : bool {
7275

7376
class Compilation {
7477
friend class PerformJobsState;
78+
7579
public:
7680
/// The filelist threshold value to pass to ensure file lists are never used
7781
static const size_t NEVER_USE_FILELIST = SIZE_MAX;
@@ -145,17 +149,6 @@ class Compilation {
145149
/// If unknown, this will be some time in the past.
146150
llvm::sys::TimePoint<> LastBuildTime = llvm::sys::TimePoint<>::min();
147151

148-
/// The number of commands which this compilation should attempt to run in
149-
/// parallel.
150-
const unsigned NumberOfParallelCommands;
151-
152-
/// Indicates whether this Compilation should use skip execution of
153-
/// subtasks during performJobs() by using a dummy TaskQueue.
154-
///
155-
/// \note For testing purposes only; similar user-facing features should be
156-
/// implemented separately, as the dummy TaskQueue may provide faked output.
157-
const bool SkipTaskExecution;
158-
159152
/// Indicates whether this Compilation should continue execution of subtasks
160153
/// even if they returned an error status.
161154
bool ContinueBuildingAfterErrors = false;
@@ -232,12 +225,10 @@ class Compilation {
232225
StringRef ArgsHash, llvm::sys::TimePoint<> StartTime,
233226
llvm::sys::TimePoint<> LastBuildTime,
234227
size_t FilelistThreshold,
235-
unsigned NumberOfParallelCommands = 1,
236228
bool EnableIncrementalBuild = false,
237229
bool EnableBatchMode = false,
238230
unsigned BatchSeed = 0,
239231
bool ForceOneBatchRepartition = false,
240-
bool SkipTaskExecution = false,
241232
bool SaveTemps = false,
242233
bool ShowDriverTimeCompilation = false,
243234
std::unique_ptr<UnifiedStatsReporter> Stats = nullptr);
@@ -284,10 +275,6 @@ class Compilation {
284275
return DerivedOutputFileMap;
285276
}
286277

287-
unsigned getNumberOfParallelCommands() const {
288-
return NumberOfParallelCommands;
289-
}
290-
291278
bool getIncrementalBuildEnabled() const {
292279
return EnableIncrementalBuild;
293280
}
@@ -320,6 +307,10 @@ class Compilation {
320307
return FilelistThreshold;
321308
}
322309

310+
UnifiedStatsReporter *getStatsReporter() const {
311+
return Stats.get();
312+
}
313+
323314
/// Requests the path to a file containing all input source files. This can
324315
/// be shared across jobs.
325316
///
@@ -330,9 +321,12 @@ class Compilation {
330321
const char *getAllSourcesPath() const;
331322

332323
/// Asks the Compilation to perform the Jobs which it knows about.
324+
///
325+
/// \param TQ The TaskQueue used to schedule jobs for execution.
326+
///
333327
/// \returns result code for the Compilation's Jobs; 0 indicates success and
334328
/// -2 indicates that one of the Compilation's Jobs crashed during execution
335-
int performJobs();
329+
int performJobs(std::unique_ptr<sys::TaskQueue> &&TQ);
336330

337331
/// Returns whether the callee is permitted to pass -emit-loaded-module-trace
338332
/// to a frontend job.
@@ -355,10 +349,11 @@ class Compilation {
355349
///
356350
/// \param[out] abnormalExit Set to true if any job exits abnormally (i.e.
357351
/// crashes).
352+
/// \param TQ The task queue on which jobs will be scheduled.
358353
///
359354
/// \returns exit code of the first failed Job, or 0 on success. If a Job
360355
/// crashes during execution, a negative value will be returned.
361-
int performJobsImpl(bool &abnormalExit);
356+
int performJobsImpl(bool &abnormalExit, std::unique_ptr<sys::TaskQueue> &&TQ);
362357

363358
/// \brief Performs a single Job by executing in place, if possible.
364359
///

include/swift/Driver/Driver.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace opt {
4343
}
4444

4545
namespace swift {
46+
namespace sys {
47+
class TaskQueue;
48+
}
4649
class DiagnosticEngine;
4750
namespace driver {
4851
class Action;
@@ -213,6 +216,13 @@ class Driver {
213216
std::unique_ptr<ToolChain>
214217
buildToolChain(const llvm::opt::InputArgList &ArgList);
215218

219+
/// Compute the task queue for this compilation and command line argument
220+
/// vector.
221+
///
222+
/// \return A TaskQueue, or nullptr if an invalid number of parallel jobs is
223+
/// specified. This condition is signalled by a diagnostic.
224+
std::unique_ptr<sys::TaskQueue> buildTaskQueue(const Compilation &C);
225+
216226
/// Construct a compilation object for a given ToolChain and command line
217227
/// argument vector.
218228
///

lib/Driver/Compilation.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ Compilation::Compilation(DiagnosticEngine &Diags,
108108
llvm::sys::TimePoint<> StartTime,
109109
llvm::sys::TimePoint<> LastBuildTime,
110110
size_t FilelistThreshold,
111-
unsigned NumberOfParallelCommands,
112111
bool EnableIncrementalBuild,
113112
bool EnableBatchMode,
114113
unsigned BatchSeed,
115114
bool ForceOneBatchRepartition,
116-
bool SkipTaskExecution,
117115
bool SaveTemps,
118116
bool ShowDriverTimeCompilation,
119117
std::unique_ptr<UnifiedStatsReporter> StatsReporter)
@@ -127,8 +125,6 @@ Compilation::Compilation(DiagnosticEngine &Diags,
127125
ArgsHash(ArgsHash),
128126
BuildStartTime(StartTime),
129127
LastBuildTime(LastBuildTime),
130-
NumberOfParallelCommands(NumberOfParallelCommands),
131-
SkipTaskExecution(SkipTaskExecution),
132128
EnableIncrementalBuild(EnableIncrementalBuild),
133129
OutputCompilationRecordForModuleOnlyBuild(
134130
OutputCompilationRecordForModuleOnlyBuild),
@@ -619,14 +615,9 @@ namespace driver {
619615
}
620616

621617
public:
622-
PerformJobsState(Compilation &Comp)
623-
: Comp(Comp),
624-
ActualIncrementalTracer(Comp.Stats.get()) {
625-
if (Comp.SkipTaskExecution)
626-
TQ.reset(new DummyTaskQueue(Comp.NumberOfParallelCommands));
627-
else
628-
TQ.reset(new TaskQueue(Comp.NumberOfParallelCommands,
629-
Comp.Stats.get()));
618+
PerformJobsState(Compilation &Comp, std::unique_ptr<TaskQueue> &&TaskQueue)
619+
: Comp(Comp), ActualIncrementalTracer(Comp.Stats.get()),
620+
TQ(std::move(TaskQueue)) {
630621
if (Comp.ShowIncrementalBuildDecisions || Comp.Stats)
631622
IncrementalTracer = &ActualIncrementalTracer;
632623
}
@@ -889,7 +880,7 @@ namespace driver {
889880
return;
890881
}
891882

892-
size_t NumPartitions = Comp.NumberOfParallelCommands;
883+
size_t NumPartitions = TQ->getNumberOfParallelTasks();
893884
CommandSetVector Batchable, NonBatchable;
894885
std::vector<const Job *> Batches;
895886
bool PretendTheCommandLineIsTooLongOnce =
@@ -1220,16 +1211,17 @@ static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
12201211
return ok;
12211212
}
12221213

1223-
int Compilation::performJobsImpl(bool &abnormalExit) {
1224-
PerformJobsState State(*this);
1214+
int Compilation::performJobsImpl(bool &abnormalExit,
1215+
std::unique_ptr<TaskQueue> &&TQ) {
1216+
PerformJobsState State(*this, std::move(TQ));
12251217

12261218
State.scheduleInitialJobs();
12271219
State.scheduleAdditionalJobs();
12281220
State.formBatchJobsAndAddPendingJobsToTaskQueue();
12291221
State.runTaskQueueToCompletion();
12301222
State.checkUnfinishedJobs();
12311223

1232-
if (!CompilationRecordPath.empty() && !SkipTaskExecution) {
1224+
if (!CompilationRecordPath.empty()) {
12331225
InputInfoMap InputInfo;
12341226
State.populateInputInfoMap(InputInfo);
12351227
checkForOutOfDateInputs(Diags, InputInfo);
@@ -1320,7 +1312,7 @@ static bool writeAllSourcesFile(DiagnosticEngine &diags, StringRef path,
13201312
return true;
13211313
}
13221314

1323-
int Compilation::performJobs() {
1315+
int Compilation::performJobs(std::unique_ptr<TaskQueue> &&TQ) {
13241316
if (AllSourceFilesPath)
13251317
if (!writeAllSourcesFile(Diags, AllSourceFilesPath, getInputFiles()))
13261318
return EXIT_FAILURE;
@@ -1334,12 +1326,12 @@ int Compilation::performJobs() {
13341326
return performSingleCommand(Jobs.front().get());
13351327
}
13361328

1337-
if (!TaskQueue::supportsParallelExecution() && NumberOfParallelCommands > 1) {
1329+
if (!TaskQueue::supportsParallelExecution() && TQ->getNumberOfParallelTasks() > 1) {
13381330
Diags.diagnose(SourceLoc(), diag::warning_parallel_execution_not_supported);
13391331
}
13401332

13411333
bool abnormalExit;
1342-
int result = performJobsImpl(abnormalExit);
1334+
int result = performJobsImpl(abnormalExit, std::move(TQ));
13431335

13441336
if (!SaveTemps) {
13451337
for (const auto &pathPair : TempFilePaths) {

lib/Driver/Driver.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,28 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
285285
return nullptr;
286286
}
287287

288+
std::unique_ptr<sys::TaskQueue> Driver::buildTaskQueue(const Compilation &C) {
289+
const auto &ArgList = C.getArgs();
290+
unsigned NumberOfParallelCommands = 1;
291+
if (const Arg *A = ArgList.getLastArg(options::OPT_j)) {
292+
if (StringRef(A->getValue()).getAsInteger(10, NumberOfParallelCommands)) {
293+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
294+
A->getAsString(ArgList), A->getValue());
295+
return nullptr;
296+
}
297+
}
298+
299+
const bool DriverSkipExecution =
300+
ArgList.hasArg(options::OPT_driver_skip_execution,
301+
options::OPT_driver_print_jobs);
302+
if (DriverSkipExecution) {
303+
return llvm::make_unique<sys::DummyTaskQueue>(NumberOfParallelCommands);
304+
} else {
305+
return llvm::make_unique<sys::TaskQueue>(NumberOfParallelCommands,
306+
C.getStatsReporter());
307+
}
308+
}
309+
288310
static void computeArgsHash(SmallString<32> &out, const DerivedArgList &args) {
289311
SmallVector<const Arg *, 32> interestingArgs;
290312
interestingArgs.reserve(args.size());
@@ -637,9 +659,6 @@ Driver::buildCompilation(const ToolChain &TC,
637659
bool DriverPrintDerivedOutputFileMap =
638660
ArgList->hasArg(options::OPT_driver_print_derived_output_file_map);
639661
DriverPrintBindings = ArgList->hasArg(options::OPT_driver_print_bindings);
640-
bool DriverSkipExecution =
641-
ArgList->hasArg(options::OPT_driver_skip_execution,
642-
options::OPT_driver_print_jobs);
643662
bool ShowIncrementalBuildDecisions =
644663
ArgList->hasArg(options::OPT_driver_show_incremental);
645664
bool ShowJobLifecycle =
@@ -791,15 +810,6 @@ Driver::buildCompilation(const ToolChain &TC,
791810
}
792811
}
793812

794-
unsigned NumberOfParallelCommands = 1;
795-
if (const Arg *A = ArgList->getLastArg(options::OPT_j)) {
796-
if (StringRef(A->getValue()).getAsInteger(10, NumberOfParallelCommands)) {
797-
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
798-
A->getAsString(*ArgList), A->getValue());
799-
return nullptr;
800-
}
801-
}
802-
803813
size_t DriverFilelistThreshold;
804814
if (getFilelistThreshold(*TranslatedArgList, DriverFilelistThreshold, Diags))
805815
return nullptr;
@@ -829,12 +839,10 @@ Driver::buildCompilation(const ToolChain &TC,
829839
StartTime,
830840
LastBuildTime,
831841
DriverFilelistThreshold,
832-
NumberOfParallelCommands,
833842
Incremental,
834843
BatchMode,
835844
DriverBatchSeed,
836845
DriverForceOneBatchRepartition,
837-
DriverSkipExecution,
838846
SaveTemps,
839847
ShowDriverTimeCompilation,
840848
std::move(StatsReporter)));

tools/driver/driver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/DiagnosticEngine.h"
1818
#include "swift/Basic/LLVMInitialize.h"
1919
#include "swift/Basic/Program.h"
20+
#include "swift/Basic/TaskQueue.h"
2021
#include "swift/Basic/SourceManager.h"
2122
#include "swift/Driver/Compilation.h"
2223
#include "swift/Driver/Driver.h"
@@ -219,11 +220,13 @@ int main(int argc_, const char **argv_) {
219220

220221
std::unique_ptr<Compilation> C =
221222
TheDriver.buildCompilation(*TC, std::move(ArgList));
223+
222224
if (Diags.hadAnyError())
223225
return 1;
224226

225227
if (C) {
226-
return C->performJobs();
228+
std::unique_ptr<sys::TaskQueue> TQ = TheDriver.buildTaskQueue(*C);
229+
return C->performJobs(std::move(TQ));
227230
}
228231

229232
return 0;

0 commit comments

Comments
 (0)