Skip to content

Commit 15e8441

Browse files
authored
Merge pull request #14525 from graydon/batch-mode-driver-work
Batch mode driver work
2 parents 3956205 + 7686a5a commit 15e8441

File tree

8 files changed

+514
-21
lines changed

8 files changed

+514
-21
lines changed

include/swift/Basic/TaskQueue.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class TaskQueue {
150150

151151
/// Returns true if there are any tasks that have been queued but have not
152152
/// yet been executed.
153-
bool hasRemainingTasks() {
153+
virtual bool hasRemainingTasks() {
154154
return !QueuedTasks.empty();
155155
}
156156
};
@@ -180,14 +180,20 @@ class DummyTaskQueue : public TaskQueue {
180180
DummyTaskQueue(unsigned NumberOfParallelTasks = 0);
181181
virtual ~DummyTaskQueue();
182182

183-
virtual void addTask(const char *ExecPath, ArrayRef<const char *> Args,
184-
ArrayRef<const char *> Env = llvm::None,
185-
void *Context = nullptr, bool SeparateErrors = false);
183+
void addTask(const char *ExecPath, ArrayRef<const char *> Args,
184+
ArrayRef<const char *> Env = llvm::None,
185+
void *Context = nullptr, bool SeparateErrors = false) override;
186186

187-
virtual bool
187+
bool
188188
execute(TaskBeganCallback Began = TaskBeganCallback(),
189189
TaskFinishedCallback Finished = TaskFinishedCallback(),
190-
TaskSignalledCallback Signalled = TaskSignalledCallback());
190+
TaskSignalledCallback Signalled = TaskSignalledCallback()) override;
191+
192+
bool hasRemainingTasks() override {
193+
// Need to override here because QueuedTasks is redeclared.
194+
return !QueuedTasks.empty();
195+
}
196+
191197
};
192198

193199
} // end namespace sys

include/swift/Driver/Compilation.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_DRIVER_COMPILATION_H
1818
#define SWIFT_DRIVER_COMPILATION_H
1919

20+
#include "swift/Driver/Driver.h"
2021
#include "swift/Driver/Job.h"
2122
#include "swift/Driver/Util.h"
2223
#include "swift/Basic/ArrayRefView.h"
@@ -42,6 +43,7 @@ namespace swift {
4243
namespace driver {
4344
class Driver;
4445
class ToolChain;
46+
class OutputInfo;
4547
class PerformJobsState;
4648

4749
/// An enum providing different levels of output which should be produced
@@ -72,7 +74,12 @@ class Compilation {
7274

7375
/// The ToolChain this Compilation was built with, that it may reuse to build
7476
/// subsequent BatchJobs.
75-
LLVM_ATTRIBUTE_UNUSED const ToolChain &TheToolChain;
77+
const ToolChain &TheToolChain;
78+
79+
/// The OutputInfo, which the Compilation stores a copy of upon
80+
/// construction, and which it may use to build subsequent batch
81+
/// jobs itself.
82+
OutputInfo TheOutputInfo;
7683

7784
/// The OutputLevel at which this Compilation should generate output.
7885
OutputLevel Level;
@@ -149,6 +156,11 @@ class Compilation {
149156
/// of date.
150157
bool EnableIncrementalBuild;
151158

159+
/// Indicates whether groups of parallel frontend jobs should be merged
160+
/// together and run in composite "batch jobs" when possible, to reduce
161+
/// redundant work.
162+
bool EnableBatchMode;
163+
152164
/// True if temporary files should not be deleted.
153165
bool SaveTemps;
154166

@@ -182,19 +194,29 @@ class Compilation {
182194

183195
public:
184196
Compilation(DiagnosticEngine &Diags, const ToolChain &TC,
197+
OutputInfo const &OI,
185198
OutputLevel Level,
186199
std::unique_ptr<llvm::opt::InputArgList> InputArgs,
187200
std::unique_ptr<llvm::opt::DerivedArgList> TranslatedArgs,
188201
InputFileList InputsWithTypes,
189202
StringRef ArgsHash, llvm::sys::TimePoint<> StartTime,
190203
unsigned NumberOfParallelCommands = 1,
191204
bool EnableIncrementalBuild = false,
205+
bool EnableBatchMode = false,
192206
bool SkipTaskExecution = false,
193207
bool SaveTemps = false,
194208
bool ShowDriverTimeCompilation = false,
195209
std::unique_ptr<UnifiedStatsReporter> Stats = nullptr);
196210
~Compilation();
197211

212+
ToolChain const &getToolChain() const {
213+
return TheToolChain;
214+
}
215+
216+
OutputInfo const &getOutputInfo() const {
217+
return TheOutputInfo;
218+
}
219+
198220
UnwrappedArrayView<const Action> getActions() const {
199221
return llvm::makeArrayRef(Actions);
200222
}
@@ -238,7 +260,11 @@ class Compilation {
238260
void disableIncrementalBuild() {
239261
EnableIncrementalBuild = false;
240262
}
241-
263+
264+
bool getBatchModeEnabled() const {
265+
return EnableBatchMode;
266+
}
267+
242268
bool getContinueBuildingAfterErrors() const {
243269
return ContinueBuildingAfterErrors;
244270
}

include/swift/Driver/Job.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ class CommandOutput {
147147
/// with the provided \p Input pair of Base and Primary inputs.
148148
void addPrimaryOutput(CommandInputPair Input, StringRef PrimaryOutputFile);
149149

150+
/// Return true iff the set of additional output types in \c this is
151+
/// identical to the set of additional output types in \p other.
152+
bool hasSameAdditionalOutputTypes(CommandOutput const &other) const;
153+
154+
/// Copy all the input pairs from \p other to \c this. Assumes (and asserts)
155+
/// that \p other shares output file map and PrimaryOutputType with \c this
156+
/// already, as well as AdditionalOutputTypes if \c this has any.
157+
void addOutputs(CommandOutput const &other);
158+
150159
/// Assuming (and asserting) that there is only one input pair, return the
151160
/// primary output file associated with it. Note that the returned StringRef
152161
/// may be invalidated by subsequent mutations to the \c CommandOutput.
@@ -236,6 +245,8 @@ class Job {
236245
ExtraEnvironment(std::move(ExtraEnvironment)),
237246
FilelistFileInfos(std::move(Infos)) {}
238247

248+
virtual ~Job();
249+
239250
const JobAction &getSource() const {
240251
return *SourceAndCondition.getPointer();
241252
}

include/swift/Driver/ToolChain.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ class ToolChain {
166166
std::unique_ptr<CommandOutput> output,
167167
const OutputInfo &OI) const;
168168

169+
/// Return true iff the input \c Job \p A is an acceptable candidate for
170+
/// batching together into a BatchJob, via a call to \c
171+
/// constructBatchJob. This is true when the \c Job is a built from a \c
172+
/// CompileJobAction in a \c Compilation \p C running in \c
173+
/// OutputInfo::Mode::StandardCompile output mode, with a single \c TY_Swift
174+
/// \c InputAction.
175+
bool jobIsBatchable(const Compilation &C, const Job *A) const;
176+
177+
/// Equivalence relation that holds iff the two input Jobs \p A and \p B are
178+
/// acceptable candidates for combining together into a \c BatchJob, via a
179+
/// call to \c constructBatchJob. This is true when each job independently
180+
/// satisfies \c jobIsBatchable, and the two jobs have identical executables,
181+
/// output types and environments (i.e. they are identical aside from their
182+
/// inputs).
183+
bool jobsAreBatchCombinable(const Compilation &C, const Job *A,
184+
const Job *B) const;
185+
186+
/// Construct a \c BatchJob that subsumes the work of a set of Jobs. Any pair
187+
/// of elements in \p Jobs are assumed to satisfy the equivalence relation \c
188+
/// jobsAreBatchCombinable, i.e. they should all be "the same" job in in all
189+
/// ways other than their choices of inputs.
190+
std::unique_ptr<Job> constructBatchJob(ArrayRef<const Job *> Jobs,
191+
Compilation &C) const;
192+
169193
/// Return the default language type to use for the given extension.
170194
/// If the extension is empty or is otherwise not recognized, return
171195
/// the invalid type \c TY_INVALID.

0 commit comments

Comments
 (0)