Skip to content

Commit 19cfbba

Browse files
committed
Add tests for numThreads and numParallelJobs. Diagnose trying to multithread batch mode.
1 parent 2737f31 commit 19cfbba

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ extension Diagnostic.Message {
466466
.error("-driver-use-frontend-path requires a Swift compiler executable argument")
467467
}
468468

469-
static var warning_cannot_multithread_batch_mode: Diagnostic.Message {
469+
public static var warning_cannot_multithread_batch_mode: Diagnostic.Message {
470470
.warning("ignoring -num-threads argument; cannot multithread batch mode")
471471
}
472472
}
@@ -731,17 +731,14 @@ extension Driver {
731731

732732
// Make sure we have a non-negative integer value.
733733
guard let numThreads = Int(numThreadsArg.asSingle), numThreads >= 0 else {
734-
diagnosticsEngine.emit(Diagnostic.Message.error_invalid_arg_value(arg: .numThreads, value: numThreadsArg.asSingle))
734+
diagnosticsEngine.emit(.error_invalid_arg_value(arg: .numThreads, value: numThreadsArg.asSingle))
735735
return 0
736736
}
737737

738-
#if false
739-
// FIXME: Check for batch mode.
740-
if false {
738+
if case .batchCompile = compilerMode {
741739
diagnosticsEngine.emit(.warning_cannot_multithread_batch_mode)
742740
return 0
743741
}
744-
#endif
745742

746743
return numThreads
747744
}
@@ -771,7 +768,7 @@ extension Driver {
771768
}
772769

773770
extension Diagnostic.Message {
774-
static func remark_max_determinism_overriding(_ option: Option) -> Diagnostic.Message {
771+
public static func remark_max_determinism_overriding(_ option: Option) -> Diagnostic.Message {
775772
.remark("SWIFTC_MAXIMUM_DETERMINISM overriding \(option.spelling)")
776773
}
777774
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,46 @@ final class SwiftDriverTests: XCTestCase {
148148
}
149149
}
150150

151+
func testMultithreading() throws {
152+
153+
XCTAssertEqual(try Driver(args: ["swiftc"]).numThreads, 0)
154+
155+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "4"]).numThreads, 4)
156+
157+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "0"]).numThreads, 0)
158+
159+
XCTAssertEqual(try Driver(args: ["swiftc", "-num-threads", "-1"]).numThreads, 0)
160+
161+
XCTAssertEqual(try Driver(args: ["swiftc", "-enable-batch-mode", "-num-threads", "4"]).numThreads, 0)
162+
163+
XCTAssertNil(try Driver(args: ["swiftc"]).numParallelJobs)
164+
165+
XCTAssertEqual(try Driver(args: ["swiftc", "-j", "4"]).numParallelJobs, 4)
166+
167+
XCTAssertNil(try Driver(args: ["swiftc", "-j", "0"]).numParallelJobs)
168+
169+
XCTAssertEqual(try Driver(args: ["swiftc", "-j", "4"], env: ["SWIFTC_MAXIMUM_DETERMINISM": "1"]).numParallelJobs, 1)
170+
}
171+
172+
func testMultithreadingDiagnostics() throws {
173+
174+
try assertDriverDiagnostics(args: "swift", "-num-threads", "-1") {
175+
$1.expect(.error_invalid_arg_value(arg: .numThreads, value: "-1"))
176+
}
177+
178+
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-num-threads", "4") {
179+
$1.expect(.warning_cannot_multithread_batch_mode)
180+
}
181+
182+
try assertDriverDiagnostics(args: "swiftc", "-j", "0") {
183+
$1.expect(.error_invalid_arg_value(arg: .j, value: "0"))
184+
}
185+
186+
try assertDriverDiagnostics(args: "swiftc", "-j", "8", env: ["SWIFTC_MAXIMUM_DETERMINISM": "1"]) {
187+
$1.expect(.remark_max_determinism_overriding(.j))
188+
}
189+
}
190+
151191
func testDebugSettings() throws {
152192
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module") { driver in
153193
XCTAssertNil(driver.debugInfoLevel)

0 commit comments

Comments
 (0)