Skip to content

Commit d7493dd

Browse files
committed
[NFC] Expose the TaskQueue as a Compilation Parameter
Shuffle the responsibility for creating the TaskQueue out of the Compilation's internal job state object and into the driver. Expose a builder convenience function that handles the argument parsing.
1 parent bfbce36 commit d7493dd

File tree

5 files changed

+61
-38
lines changed

5 files changed

+61
-38
lines changed

include/swift/Driver/Compilation.h

Lines changed: 18 additions & 11 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,10 +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-
152152
/// Indicates whether this Compilation should use skip execution of
153153
/// subtasks during performJobs() by using a dummy TaskQueue.
154154
///
@@ -232,7 +232,6 @@ class Compilation {
232232
StringRef ArgsHash, llvm::sys::TimePoint<> StartTime,
233233
llvm::sys::TimePoint<> LastBuildTime,
234234
size_t FilelistThreshold,
235-
unsigned NumberOfParallelCommands = 1,
236235
bool EnableIncrementalBuild = false,
237236
bool EnableBatchMode = false,
238237
unsigned BatchSeed = 0,
@@ -284,10 +283,6 @@ class Compilation {
284283
return DerivedOutputFileMap;
285284
}
286285

287-
unsigned getNumberOfParallelCommands() const {
288-
return NumberOfParallelCommands;
289-
}
290-
291286
bool getIncrementalBuildEnabled() const {
292287
return EnableIncrementalBuild;
293288
}
@@ -301,6 +296,10 @@ class Compilation {
301296

302297
bool getForceOneBatchRepartition() const { return ForceOneBatchRepartition; }
303298

299+
bool wantsDummyQueue() const {
300+
return SkipTaskExecution;
301+
}
302+
304303
bool getContinueBuildingAfterErrors() const {
305304
return ContinueBuildingAfterErrors;
306305
}
@@ -320,6 +319,10 @@ class Compilation {
320319
return FilelistThreshold;
321320
}
322321

322+
UnifiedStatsReporter *getStatsReporter() const {
323+
return Stats.get();
324+
}
325+
323326
/// Requests the path to a file containing all input source files. This can
324327
/// be shared across jobs.
325328
///
@@ -330,9 +333,12 @@ class Compilation {
330333
const char *getAllSourcesPath() const;
331334

332335
/// Asks the Compilation to perform the Jobs which it knows about.
336+
///
337+
/// \param TQ The TaskQueue used to schedule jobs for execution.
338+
///
333339
/// \returns result code for the Compilation's Jobs; 0 indicates success and
334340
/// -2 indicates that one of the Compilation's Jobs crashed during execution
335-
int performJobs();
341+
int performJobs(std::unique_ptr<sys::TaskQueue> &&TQ);
336342

337343
/// Returns whether the callee is permitted to pass -emit-loaded-module-trace
338344
/// to a frontend job.
@@ -355,10 +361,11 @@ class Compilation {
355361
///
356362
/// \param[out] abnormalExit Set to true if any job exits abnormally (i.e.
357363
/// crashes).
364+
/// \param TQ The task queue on which jobs will be scheduled.
358365
///
359366
/// \returns exit code of the first failed Job, or 0 on success. If a Job
360367
/// crashes during execution, a negative value will be returned.
361-
int performJobsImpl(bool &abnormalExit);
368+
int performJobsImpl(bool &abnormalExit, std::unique_ptr<sys::TaskQueue> &&TQ);
362369

363370
/// \brief Performs a single Job by executing in place, if possible.
364371
///

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: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ 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,
@@ -127,7 +126,6 @@ Compilation::Compilation(DiagnosticEngine &Diags,
127126
ArgsHash(ArgsHash),
128127
BuildStartTime(StartTime),
129128
LastBuildTime(LastBuildTime),
130-
NumberOfParallelCommands(NumberOfParallelCommands),
131129
SkipTaskExecution(SkipTaskExecution),
132130
EnableIncrementalBuild(EnableIncrementalBuild),
133131
OutputCompilationRecordForModuleOnlyBuild(
@@ -619,14 +617,9 @@ namespace driver {
619617
}
620618

621619
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()));
620+
PerformJobsState(Compilation &Comp, std::unique_ptr<TaskQueue> &&TaskQueue)
621+
: Comp(Comp), ActualIncrementalTracer(Comp.Stats.get()),
622+
TQ(std::move(TaskQueue)) {
630623
if (Comp.ShowIncrementalBuildDecisions || Comp.Stats)
631624
IncrementalTracer = &ActualIncrementalTracer;
632625
}
@@ -889,7 +882,7 @@ namespace driver {
889882
return;
890883
}
891884

892-
size_t NumPartitions = Comp.NumberOfParallelCommands;
885+
size_t NumPartitions = TQ->getNumberOfParallelTasks();
893886
CommandSetVector Batchable, NonBatchable;
894887
std::vector<const Job *> Batches;
895888
bool PretendTheCommandLineIsTooLongOnce =
@@ -1220,8 +1213,9 @@ static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
12201213
return ok;
12211214
}
12221215

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

12261220
State.scheduleInitialJobs();
12271221
State.scheduleAdditionalJobs();
@@ -1320,7 +1314,7 @@ static bool writeAllSourcesFile(DiagnosticEngine &diags, StringRef path,
13201314
return true;
13211315
}
13221316

1323-
int Compilation::performJobs() {
1317+
int Compilation::performJobs(std::unique_ptr<TaskQueue> &&TQ) {
13241318
if (AllSourceFilesPath)
13251319
if (!writeAllSourcesFile(Diags, AllSourceFilesPath, getInputFiles()))
13261320
return EXIT_FAILURE;
@@ -1334,12 +1328,12 @@ int Compilation::performJobs() {
13341328
return performSingleCommand(Jobs.front().get());
13351329
}
13361330

1337-
if (!TaskQueue::supportsParallelExecution() && NumberOfParallelCommands > 1) {
1331+
if (!TaskQueue::supportsParallelExecution() && TQ->getNumberOfParallelTasks() > 1) {
13381332
Diags.diagnose(SourceLoc(), diag::warning_parallel_execution_not_supported);
13391333
}
13401334

13411335
bool abnormalExit;
1342-
int result = performJobsImpl(abnormalExit);
1336+
int result = performJobsImpl(abnormalExit, std::move(TQ));
13431337

13441338
if (!SaveTemps) {
13451339
for (const auto &pathPair : TempFilePaths) {

lib/Driver/Driver.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,25 @@ 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+
if (C.wantsDummyQueue()) {
300+
return llvm::make_unique<sys::DummyTaskQueue>(NumberOfParallelCommands);
301+
} else {
302+
return llvm::make_unique<sys::TaskQueue>(NumberOfParallelCommands,
303+
C.getStatsReporter());
304+
}
305+
}
306+
288307
static void computeArgsHash(SmallString<32> &out, const DerivedArgList &args) {
289308
SmallVector<const Arg *, 32> interestingArgs;
290309
interestingArgs.reserve(args.size());
@@ -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,7 +839,6 @@ Driver::buildCompilation(const ToolChain &TC,
829839
StartTime,
830840
LastBuildTime,
831841
DriverFilelistThreshold,
832-
NumberOfParallelCommands,
833842
Incremental,
834843
BatchMode,
835844
DriverBatchSeed,

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)