Skip to content

Commit 8be2c53

Browse files
committed
[BatchMode] Add -driver-batch-count to allow overriding batch count inferred by -j
1 parent b52ef8b commit 8be2c53

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

include/swift/Driver/Compilation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class Compilation {
174174
/// Provides a randomization seed to batch-mode partitioning, for debugging.
175175
const unsigned BatchSeed;
176176

177+
/// Overrides parallelism level as count of batches, if in batch-mode.
178+
const Optional<unsigned> BatchCount;
179+
177180
/// In order to test repartitioning, set to true if
178181
/// -driver-force-one-batch-repartition is present.
179182
const bool ForceOneBatchRepartition = false;
@@ -228,6 +231,7 @@ class Compilation {
228231
bool EnableIncrementalBuild = false,
229232
bool EnableBatchMode = false,
230233
unsigned BatchSeed = 0,
234+
Optional<unsigned> BatchCount = None,
231235
bool ForceOneBatchRepartition = false,
232236
bool SaveTemps = false,
233237
bool ShowDriverTimeCompilation = false,

include/swift/Driver/Driver.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ class Driver {
176176
/// Indicates whether the driver should check that the input files exist.
177177
bool CheckInputFilesExist = true;
178178

179-
/// Provides a randomization seed to batch-mode partitioning, for debugging.
180-
unsigned DriverBatchSeed = 0;
181-
182-
/// Forces a repartition for testing.
183-
bool DriverForceOneBatchRepartition = false;
184-
185179
public:
186180
Driver(StringRef DriverExecutable, StringRef Name,
187181
ArrayRef<const char *> Args, DiagnosticEngine &Diags);

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def driver_filelist_threshold_EQ : Joined<["-"], "driver-filelist-threshold=">,
110110
def driver_batch_seed : Separate<["-"], "driver-batch-seed">,
111111
InternalDebugOpt,
112112
HelpText<"Use the given seed value to randomize batch-mode partitions">;
113+
def driver_batch_count : Separate<["-"], "driver-batch-count">,
114+
InternalDebugOpt,
115+
HelpText<"Use the given number of batch-mode partitions, rather than default parallelism level">;
113116
def driver_force_one_batch_repartition : Flag<["-"], "driver-force-one-batch-repartition">,
114117
InternalDebugOpt,
115118
HelpText<"Force one batch repartitioning for testing">;

lib/Driver/Compilation.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Compilation::Compilation(DiagnosticEngine &Diags,
111111
bool EnableIncrementalBuild,
112112
bool EnableBatchMode,
113113
unsigned BatchSeed,
114+
Optional<unsigned> BatchCount,
114115
bool ForceOneBatchRepartition,
115116
bool SaveTemps,
116117
bool ShowDriverTimeCompilation,
@@ -130,6 +131,7 @@ Compilation::Compilation(DiagnosticEngine &Diags,
130131
OutputCompilationRecordForModuleOnlyBuild),
131132
EnableBatchMode(EnableBatchMode),
132133
BatchSeed(BatchSeed),
134+
BatchCount(BatchCount),
133135
ForceOneBatchRepartition(ForceOneBatchRepartition),
134136
SaveTemps(SaveTemps),
135137
ShowDriverTimeCompilation(ShowDriverTimeCompilation),
@@ -880,7 +882,9 @@ namespace driver {
880882
return;
881883
}
882884

883-
size_t NumPartitions = TQ->getNumberOfParallelTasks();
885+
size_t NumPartitions = (Comp.BatchCount.hasValue() ?
886+
Comp.BatchCount.getValue() :
887+
TQ->getNumberOfParallelTasks());
884888
CommandSetVector Batchable, NonBatchable;
885889
std::vector<const Job *> Batches;
886890
bool PretendTheCommandLineIsTooLongOnce =

lib/Driver/Driver.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,35 @@ static bool getFilelistThreshold(DerivedArgList &Args, size_t &FilelistThreshold
643643
return false;
644644
}
645645

646+
static unsigned
647+
getDriverBatchSeed(llvm::opt::InputArgList &ArgList,
648+
DiagnosticEngine &Diags) {
649+
unsigned DriverBatchSeed = 0;
650+
if (const Arg *A = ArgList.getLastArg(options::OPT_driver_batch_seed)) {
651+
if (StringRef(A->getValue()).getAsInteger(10, DriverBatchSeed)) {
652+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
653+
A->getAsString(ArgList), A->getValue());
654+
}
655+
}
656+
return DriverBatchSeed;
657+
}
658+
659+
static Optional<unsigned>
660+
getDriverBatchCount(llvm::opt::InputArgList &ArgList,
661+
DiagnosticEngine &Diags)
662+
{
663+
if (const Arg *A = ArgList.getLastArg(options::OPT_driver_batch_count)) {
664+
unsigned Count = 0;
665+
if (StringRef(A->getValue()).getAsInteger(10, Count)) {
666+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
667+
A->getAsString(ArgList), A->getValue());
668+
} else {
669+
return Count;
670+
}
671+
}
672+
return None;
673+
}
674+
646675
std::unique_ptr<Compilation>
647676
Driver::buildCompilation(const ToolChain &TC,
648677
std::unique_ptr<llvm::opt::InputArgList> ArgList) {
@@ -663,13 +692,9 @@ Driver::buildCompilation(const ToolChain &TC,
663692
ArgList->hasArg(options::OPT_driver_show_incremental);
664693
bool ShowJobLifecycle =
665694
ArgList->hasArg(options::OPT_driver_show_job_lifecycle);
666-
if (const Arg *A = ArgList->getLastArg(options::OPT_driver_batch_seed)) {
667-
if (StringRef(A->getValue()).getAsInteger(10, DriverBatchSeed)) {
668-
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
669-
A->getAsString(*ArgList), A->getValue());
670-
}
671-
}
672-
DriverForceOneBatchRepartition =
695+
unsigned DriverBatchSeed = getDriverBatchSeed(*ArgList, Diags);
696+
Optional<unsigned> DriverBatchCount = getDriverBatchCount(*ArgList, Diags);
697+
bool DriverForceOneBatchRepartition =
673698
ArgList->hasArg(options::OPT_driver_force_one_batch_repartition);
674699

675700
bool Incremental = ArgList->hasArg(options::OPT_incremental);
@@ -837,6 +862,7 @@ Driver::buildCompilation(const ToolChain &TC,
837862
Incremental,
838863
BatchMode,
839864
DriverBatchSeed,
865+
DriverBatchCount,
840866
DriverForceOneBatchRepartition,
841867
SaveTemps,
842868
ShowDriverTimeCompilation,

0 commit comments

Comments
 (0)