Skip to content

[4.2] [BatchMode] Consolidate reasoning about -{enable,disable}-batch-mode flag. #15754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ class OutputInfo {
/// A compilation using a single frontend invocation without -primary-file.
SingleCompile,

/// A single process that batches together multiple StandardCompile jobs.
/// A single process that batches together multiple StandardCompile Jobs.
///
/// Note: this is a transient value to use _only_ for the individual
/// BatchJobs that are the temporary containers for multiple StandardCompile
/// Jobs built by ToolChain::constructBatchJob.
///
/// In particular, the driver treats a batch-mode-enabled Compilation as
/// having OutputInfo::CompilerMode == StandardCompile, with the
/// Compilation::BatchModeEnabled flag set to true, _not_ as a
/// BatchModeCompile Compilation. The top-level OutputInfo::CompilerMode for
/// a Compilation should never be BatchModeCompile.
BatchModeCompile,

/// Invoke the REPL
Expand Down Expand Up @@ -389,8 +399,11 @@ class Driver {
/// there is an actual conflict.
/// \param Args The input arguments.
/// \param Inputs The inputs to the driver.
/// \param BatchModeOut An out-parameter flag that indicates whether to
/// batch the jobs of the resulting \c Mode::StandardCompile compilation.
OutputInfo::Mode computeCompilerMode(const llvm::opt::DerivedArgList &Args,
const InputFileList &Inputs) const;
const InputFileList &Inputs,
bool &BatchModeOut) const;
};

} // end namespace driver
Expand Down
33 changes: 17 additions & 16 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,6 @@ Driver::buildCompilation(const ToolChain &TC,
Incremental = false;
}

bool BatchMode = ArgList->hasFlag(options::OPT_enable_batch_mode,
options::OPT_disable_batch_mode,
false);

bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
bool ContinueBuildingAfterErrors =
ArgList->hasArg(options::OPT_continue_building_after_errors);
Expand Down Expand Up @@ -588,6 +584,8 @@ Driver::buildCompilation(const ToolChain &TC,

// Determine the OutputInfo for the driver.
OutputInfo OI;
bool BatchMode = false;
OI.CompilerMode = computeCompilerMode(*TranslatedArgList, Inputs, BatchMode);
buildOutputInfo(TC, *TranslatedArgList, BatchMode, Inputs, OI);

if (Diags.hadAnyError())
Expand Down Expand Up @@ -1102,8 +1100,6 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
? file_types::TY_Nothing
: file_types::TY_Object;

OI.CompilerMode = computeCompilerMode(Args, Inputs);

if (const Arg *A = Args.getLastArg(options::OPT_num_threads)) {
if (BatchMode) {
Diags.diagnose(SourceLoc(), diag::warning_cannot_multithread_batch_mode);
Expand Down Expand Up @@ -1373,26 +1369,31 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,

OutputInfo::Mode
Driver::computeCompilerMode(const DerivedArgList &Args,
const InputFileList &Inputs) const {
const InputFileList &Inputs,
bool &BatchModeOut) const {

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

const Arg *ArgRequiringWMO = Args.getLastArg(
const Arg *ArgRequiringSingleCompile = Args.getLastArg(
options::OPT_whole_module_optimization, options::OPT_index_file);

if (!ArgRequiringWMO)
BatchModeOut = Args.hasFlag(options::OPT_enable_batch_mode,
options::OPT_disable_batch_mode,
false);

if (!ArgRequiringSingleCompile)
return OutputInfo::Mode::StandardCompile;

// Test for -enable-batch-mode, rather than the BatchMode flag that is
// passed into the caller because the diagnostic is intended to warn against
// overriding *explicit* batch mode. No warning should be given if in batch
// mode by default.
if (Args.hasArg(options::OPT_enable_batch_mode))
// Override batch mode if given -wmo or -index-file.
if (BatchModeOut) {
BatchModeOut = false;
// Emit a warning about such overriding (FIXME: we might conditionalize
// this based on the user or xcode passing -disable-batch-mode).
Diags.diagnose(SourceLoc(), diag::warn_ignoring_batch_mode,
ArgRequiringWMO->getOption().getPrefixedName());

ArgRequiringSingleCompile->getOption().getPrefixedName());
}
return OutputInfo::Mode::SingleCompile;
}

Expand Down
12 changes: 12 additions & 0 deletions test/Driver/batch_mode_with_WMO_or_index.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@
// RUN: %FileCheck -check-prefix CHECK-INDEX %s <%t/stderr_index_batch
// RUN: %FileCheck -check-prefix CHECK-INDEX %s <%t/stderr_batch_index
// CHECK-INDEX: warning: ignoring '-enable-batch-mode' because '-index-file' was also specified
//
// This next one is a regression test for a specific failure in the past: wmo +
// batch mode should not just result in wmo, but also preserve the num-threads
// argument and (crucially) the resulting fact that the single wmo subprocess
// generates multiple output files. The build system that invokes swiftc expects
// multiple outputs.
//
// RUN: touch %t/a.swift %t/b.swift %t/c.swift
// 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
// RUN: %FileCheck --check-prefix CHECK-WMO %s <%t/stderr_mt_wmo
// RUN: %FileCheck --check-prefix CHECK-MULTITHREADED-WMO-ARGS %s <%t/stdout_mt_wmo
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not --crash %target-swift-frontend %s -emit-ir
// REQUIRES: rdar38932729
protocol A:A&CLong