Skip to content

Commit 0b4e7db

Browse files
committed
[BatchMode] Consolidate reasoning about -{enable,disable}-batch-mode flag.
Previous to this change, the initial inspection of the -{enable,disable}-batch-mode flag was made separate from the subsequent overriding by -wmo; this in turn meant that the driver might decide it's "in batch mode" in one place (in particular, when judging whether to ignore the -num-threads flag) and "in wmo mode" elsewhere (when judging whether to emit one or multiple outputs, which depends on the -num-threads flag). Divergence between these two views caused the driver to effectively drop the -num-threads flag even when overriding batch mode with wmo; since xcode assumes that passing -wmo -num-threads _will_ cause multiple outputs, this in turn caused linking to fail since the expected output files were not found.
1 parent 6df8732 commit 0b4e7db

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

include/swift/Driver/Driver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,11 @@ class Driver {
389389
/// there is an actual conflict.
390390
/// \param Args The input arguments.
391391
/// \param Inputs The inputs to the driver.
392+
/// \param BatchModeOut An out-parameter flag that indicates whether to
393+
/// batch the jobs of the resulting \c Mode::StandardCompile compilation.
392394
OutputInfo::Mode computeCompilerMode(const llvm::opt::DerivedArgList &Args,
393-
const InputFileList &Inputs) const;
395+
const InputFileList &Inputs,
396+
bool &BatchModeOut) const;
394397
};
395398

396399
} // end namespace driver

lib/Driver/Driver.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,6 @@ Driver::buildCompilation(const ToolChain &TC,
594594
Incremental = false;
595595
}
596596

597-
bool BatchMode = ArgList->hasFlag(options::OPT_enable_batch_mode,
598-
options::OPT_disable_batch_mode,
599-
false);
600-
601597
bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
602598
bool ContinueBuildingAfterErrors =
603599
ArgList->hasArg(options::OPT_continue_building_after_errors);
@@ -631,6 +627,8 @@ Driver::buildCompilation(const ToolChain &TC,
631627

632628
// Determine the OutputInfo for the driver.
633629
OutputInfo OI;
630+
bool BatchMode = false;
631+
OI.CompilerMode = computeCompilerMode(*TranslatedArgList, Inputs, BatchMode);
634632
buildOutputInfo(TC, *TranslatedArgList, BatchMode, Inputs, OI);
635633

636634
if (Diags.hadAnyError())
@@ -1122,8 +1120,6 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
11221120
? file_types::TY_Nothing
11231121
: file_types::TY_Object;
11241122

1125-
OI.CompilerMode = computeCompilerMode(Args, Inputs);
1126-
11271123
if (const Arg *A = Args.getLastArg(options::OPT_num_threads)) {
11281124
if (BatchMode) {
11291125
Diags.diagnose(SourceLoc(), diag::warning_cannot_multithread_batch_mode);
@@ -1393,27 +1389,33 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
13931389

13941390
OutputInfo::Mode
13951391
Driver::computeCompilerMode(const DerivedArgList &Args,
1396-
const InputFileList &Inputs) const {
1392+
const InputFileList &Inputs,
1393+
bool &BatchModeOut) const {
13971394

13981395
if (driverKind == Driver::DriverKind::Interactive)
13991396
return Inputs.empty() ? OutputInfo::Mode::REPL
14001397
: OutputInfo::Mode::Immediate;
14011398

1402-
const Arg *ArgRequiringWMO = Args.getLastArg(
1399+
const Arg *ArgRequiringSingleCompile = Args.getLastArg(
14031400
options::OPT_whole_module_optimization, options::OPT_index_file);
14041401

1405-
if (!ArgRequiringWMO)
1406-
return OutputInfo::Mode::StandardCompile;
1407-
1408-
// Test for -enable-batch-mode, rather than the BatchMode flag that is
1409-
// passed into the caller because the diagnostic is intended to warn against
1410-
// overriding *explicit* batch mode. No warning should be given if in batch
1411-
// mode by default.
1412-
if (Args.hasArg(options::OPT_enable_batch_mode))
1413-
Diags.diagnose(SourceLoc(), diag::warn_ignoring_batch_mode,
1414-
ArgRequiringWMO->getOption().getPrefixedName());
1402+
BatchModeOut = Args.hasFlag(options::OPT_enable_batch_mode,
1403+
options::OPT_disable_batch_mode,
1404+
false);
1405+
1406+
if (ArgRequiringSingleCompile) {
1407+
// Override batch mode if given -wmo or -index-file.
1408+
if (BatchModeOut) {
1409+
BatchModeOut = false;
1410+
// Emit a warning about such overriding (FIXME: we might conditionalize
1411+
// this based on the user or xcode passing -disable-batch-mode).
1412+
Diags.diagnose(SourceLoc(), diag::warn_ignoring_batch_mode,
1413+
ArgRequiringSingleCompile->getOption().getPrefixedName());
1414+
}
1415+
return OutputInfo::Mode::SingleCompile;
1416+
}
14151417

1416-
return OutputInfo::Mode::SingleCompile;
1418+
return OutputInfo::Mode::StandardCompile;
14171419
}
14181420

14191421
void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,

test/Driver/batch_mode_with_WMO_or_index.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@
1212
// RUN: %FileCheck -check-prefix CHECK-INDEX %s <%t/stderr_index_batch
1313
// RUN: %FileCheck -check-prefix CHECK-INDEX %s <%t/stderr_batch_index
1414
// CHECK-INDEX: warning: ignoring '-enable-batch-mode' because '-index-file' was also specified
15+
//
16+
// This next one is a regression test for a specific failure in the past: wmo +
17+
// batch mode should not just result in wmo, but also preserve the num-threads
18+
// argument and (crucially) the resulting fact that the single wmo subprocess
19+
// generates multiple output files. The build system that invokes swiftc expects
20+
// multiple outputs.
21+
//
22+
// RUN: touch %t/a.swift %t/b.swift %t/c.swift
23+
// RUN: %swiftc_driver %t/a.swift %t/b.swift %t/c.swift -num-threads 4 -whole-module-optimization -enable-batch-mode -### >%t/stdout_mt_wmo 2>%t/stderr_mt_wmo
24+
// RUN: %FileCheck --check-prefix CHECK-WMO %s <%t/stderr_mt_wmo
25+
// RUN: %FileCheck --check-prefix CHECK-MULTITHREADED-WMO-ARGS %s <%t/stdout_mt_wmo
26+
// CHECK-MULTITHREADED-WMO-ARGS: -num-threads 4 {{.*}}-o {{.*}}/a-{{[a-z0-9]+}}.o -o {{.*}}/b-{{[a-z0-9]+}}.o -o {{.*}}/c-{{[a-z0-9]+}}.o

0 commit comments

Comments
 (0)