Skip to content

Commit aa10174

Browse files
author
David Ungar
authored
Merge pull request #15520 from davidungar/PR-18-15-num-threads-fix
[Batch mode] Harden compiler against -num-threads or -import-objc-header and -enable-batch-mode.
2 parents 69099a8 + 4fa3001 commit aa10174

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ ERROR(error_unknown_arg,none,
8383
"unknown argument: '%0'", (StringRef))
8484
ERROR(error_invalid_arg_value,none,
8585
"invalid value '%1' in '%0'", (StringRef, StringRef))
86+
WARNING(warning_cannot_multithread_batch_mode,none,
87+
"ignoring -num-threads argument; cannot multithread batch mode", ())
8688
ERROR(error_unsupported_option_argument,none,
8789
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
8890
ERROR(error_immediate_mode_missing_stdlib,none,

include/swift/Driver/Driver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ class Driver {
242242
/// information.
243243
void buildOutputInfo(const ToolChain &TC,
244244
const llvm::opt::DerivedArgList &Args,
245-
const InputFileList &Inputs, OutputInfo &OI) const;
245+
const bool BatchMode, const InputFileList &Inputs,
246+
OutputInfo &OI) const;
246247

247248
/// Construct the list of Actions to perform for the given arguments,
248249
/// which are only done for a single architecture.

lib/Driver/Compilation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,9 @@ static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
11541154
else {
11551155
// The normal case for non-single-compile jobs.
11561156
for (const Action *A : job->getSource().getInputs()) {
1157+
// A could be a GeneratePCHJobAction
1158+
if (!isa<InputAction>(A))
1159+
continue;
11571160
const auto *IA = cast<InputAction>(A);
11581161
out << IA->getInputArg().getValue() << "\n";
11591162
}

lib/Driver/Driver.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ Driver::buildCompilation(const ToolChain &TC,
588588

589589
// Determine the OutputInfo for the driver.
590590
OutputInfo OI;
591-
buildOutputInfo(TC, *TranslatedArgList, Inputs, OI);
591+
buildOutputInfo(TC, *TranslatedArgList, BatchMode, Inputs, OI);
592592

593593
if (Diags.hadAnyError())
594594
return nullptr;
@@ -1093,7 +1093,7 @@ static bool isSDKTooOld(StringRef sdkPath, const llvm::Triple &target) {
10931093
}
10941094

10951095
void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
1096-
const InputFileList &Inputs,
1096+
const bool BatchMode, const InputFileList &Inputs,
10971097
OutputInfo &OI) const {
10981098
// By default, the driver does not link its output; this will be updated
10991099
// appropriately below if linking is required.
@@ -1113,7 +1113,9 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
11131113
}
11141114

11151115
if (const Arg *A = Args.getLastArg(options::OPT_num_threads)) {
1116-
if (StringRef(A->getValue()).getAsInteger(10, OI.numThreads)) {
1116+
if (BatchMode) {
1117+
Diags.diagnose(SourceLoc(), diag::warning_cannot_multithread_batch_mode);
1118+
} else if (StringRef(A->getValue()).getAsInteger(10, OI.numThreads)) {
11171119
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
11181120
A->getAsString(Args), A->getValue());
11191121
}
@@ -2304,6 +2306,15 @@ void Driver::computeMainOutput(
23042306
file_types::isAfterLLVM(JA->getType())) {
23052307
// Multi-threaded compilation: A single frontend command produces multiple
23062308
// output file: one for each input files.
2309+
2310+
// In batch mode, the driver will try to reserve multiple differing main
2311+
// outputs to a bridging header. Another assertion will trip, but the cause
2312+
// will be harder to track down. Since the driver now ignores -num-threads
2313+
// in batch mode, the user should never be able to falsify this assertion.
2314+
assert(!C.getBatchModeEnabled() && "Batch mode produces only one main "
2315+
"output per input action, cannot have "
2316+
"batch mode & num-threads");
2317+
23072318
auto OutputFunc = [&](StringRef Base, StringRef Primary) {
23082319
const TypeToPathMap *OMForInput = nullptr;
23092320
if (OFM)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo 'print("Hello, World!")' >%t/main.swift
3+
// RUN: touch %t/bridgingHeader.h
4+
//
5+
// Make sure the proper warning is emitted:
6+
//
7+
// RUN: %swiftc_driver -enable-batch-mode -num-threads 2 %t/main.swift -import-objc-header %t/bridgingHeader.h -### 2>&1 | %FileCheck %s
8+
//
9+
// CHECK: ignoring -num-threads argument; cannot multithread batch mode
10+
//
11+
// Make sure that it actually works. (The link step fails if -num-threads is not ignored.)
12+
//
13+
// RUN: %swiftc_driver -enable-batch-mode -num-threads 2 %t/main.swift -import-objc-header %t/bridgingHeader.h

test/Driver/batch_mode_with_pch.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo 'print("Hello, World!")' >%t/main.swift
3+
// RUN: touch %t/bridgingHeader.h
4+
//
5+
// RUN: %swiftc_driver -driver-use-filelists -enable-batch-mode -num-threads 2 %t/main.swift -import-objc-header %t/bridgingHeader.h

0 commit comments

Comments
 (0)