Skip to content

Commit 643f540

Browse files
authored
Merge pull request #17452 from graydon/driver-batch-count-for-swift-4.2-branch
[4.2][BatchMode] Add -driver-batch-count to allow overriding batch count inferred by -j
2 parents 177b606 + e9c8696 commit 643f540

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
@@ -181,6 +181,9 @@ class Compilation {
181181
/// Provides a randomization seed to batch-mode partitioning, for debugging.
182182
const unsigned BatchSeed;
183183

184+
/// Overrides parallelism level as count of batches, if in batch-mode.
185+
const Optional<unsigned> BatchCount;
186+
184187
/// In order to test repartitioning, set to true if
185188
/// -driver-force-one-batch-repartition is present.
186189
const bool ForceOneBatchRepartition = false;
@@ -236,6 +239,7 @@ class Compilation {
236239
bool EnableIncrementalBuild = false,
237240
bool EnableBatchMode = false,
238241
unsigned BatchSeed = 0,
242+
Optional<unsigned> BatchCount = None,
239243
bool ForceOneBatchRepartition = false,
240244
bool SkipTaskExecution = false,
241245
bool SaveTemps = false,

include/swift/Driver/Driver.h

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

176-
/// Provides a randomization seed to batch-mode partitioning, for debugging.
177-
unsigned DriverBatchSeed = 0;
178-
179-
/// Forces a repartition for testing.
180-
bool DriverForceOneBatchRepartition = false;
181-
182176
public:
183177
Driver(StringRef DriverExecutable, StringRef Name,
184178
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
@@ -114,6 +114,7 @@ Compilation::Compilation(DiagnosticEngine &Diags,
114114
bool EnableIncrementalBuild,
115115
bool EnableBatchMode,
116116
unsigned BatchSeed,
117+
Optional<unsigned> BatchCount,
117118
bool ForceOneBatchRepartition,
118119
bool SkipTaskExecution,
119120
bool SaveTemps,
@@ -136,6 +137,7 @@ Compilation::Compilation(DiagnosticEngine &Diags,
136137
OutputCompilationRecordForModuleOnlyBuild),
137138
EnableBatchMode(EnableBatchMode),
138139
BatchSeed(BatchSeed),
140+
BatchCount(BatchCount),
139141
ForceOneBatchRepartition(ForceOneBatchRepartition),
140142
SaveTemps(SaveTemps),
141143
ShowDriverTimeCompilation(ShowDriverTimeCompilation),
@@ -931,7 +933,9 @@ namespace driver {
931933
return;
932934
}
933935

934-
size_t NumPartitions = Comp.NumberOfParallelCommands;
936+
size_t NumPartitions = (Comp.BatchCount.hasValue() ?
937+
Comp.BatchCount.getValue() :
938+
TQ->getNumberOfParallelTasks());
935939
CommandSetVector Batchable, NonBatchable;
936940
std::vector<const Job *> Batches;
937941
bool PretendTheCommandLineIsTooLongOnce =

lib/Driver/Driver.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,35 @@ static bool getFilelistThreshold(DerivedArgList &Args, size_t &FilelistThreshold
621621
return false;
622622
}
623623

624+
static unsigned
625+
getDriverBatchSeed(llvm::opt::InputArgList &ArgList,
626+
DiagnosticEngine &Diags) {
627+
unsigned DriverBatchSeed = 0;
628+
if (const Arg *A = ArgList.getLastArg(options::OPT_driver_batch_seed)) {
629+
if (StringRef(A->getValue()).getAsInteger(10, DriverBatchSeed)) {
630+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
631+
A->getAsString(ArgList), A->getValue());
632+
}
633+
}
634+
return DriverBatchSeed;
635+
}
636+
637+
static Optional<unsigned>
638+
getDriverBatchCount(llvm::opt::InputArgList &ArgList,
639+
DiagnosticEngine &Diags)
640+
{
641+
if (const Arg *A = ArgList.getLastArg(options::OPT_driver_batch_count)) {
642+
unsigned Count = 0;
643+
if (StringRef(A->getValue()).getAsInteger(10, Count)) {
644+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
645+
A->getAsString(ArgList), A->getValue());
646+
} else {
647+
return Count;
648+
}
649+
}
650+
return None;
651+
}
652+
624653
std::unique_ptr<Compilation>
625654
Driver::buildCompilation(const ToolChain &TC,
626655
std::unique_ptr<llvm::opt::InputArgList> ArgList) {
@@ -644,13 +673,9 @@ Driver::buildCompilation(const ToolChain &TC,
644673
ArgList->hasArg(options::OPT_driver_show_incremental);
645674
bool ShowJobLifecycle =
646675
ArgList->hasArg(options::OPT_driver_show_job_lifecycle);
647-
if (const Arg *A = ArgList->getLastArg(options::OPT_driver_batch_seed)) {
648-
if (StringRef(A->getValue()).getAsInteger(10, DriverBatchSeed)) {
649-
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
650-
A->getAsString(*ArgList), A->getValue());
651-
}
652-
}
653-
DriverForceOneBatchRepartition =
676+
unsigned DriverBatchSeed = getDriverBatchSeed(*ArgList, Diags);
677+
Optional<unsigned> DriverBatchCount = getDriverBatchCount(*ArgList, Diags);
678+
bool DriverForceOneBatchRepartition =
654679
ArgList->hasArg(options::OPT_driver_force_one_batch_repartition);
655680

656681
bool Incremental = ArgList->hasArg(options::OPT_incremental);
@@ -828,6 +853,7 @@ Driver::buildCompilation(const ToolChain &TC,
828853
Incremental,
829854
BatchMode,
830855
DriverBatchSeed,
856+
DriverBatchCount,
831857
DriverForceOneBatchRepartition,
832858
DriverSkipExecution,
833859
SaveTemps,

0 commit comments

Comments
 (0)