Skip to content

Commit ec7f3f7

Browse files
committed
Add -no-whole-module-optimization flag
This mirrors this change swiftlang/swift#29362
1 parent 90bc104 commit ec7f3f7

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ extension Driver {
721721
// Some output flags affect the compiler mode.
722722
if let outputOption = parsedOptions.getLast(in: .modes) {
723723
switch outputOption.option {
724-
case .emitPch, .emitImportedModules, .indexFile:
724+
case .emitPch, .emitImportedModules:
725725
return .singleCompile
726726

727727
case .repl, .deprecatedIntegratedRepl, .lldbRepl:
@@ -740,13 +740,13 @@ extension Driver {
740740
return parsedOptions.hasAnyInput ? .immediate : .repl
741741
}
742742

743-
let requiresSingleCompile = parsedOptions.hasArgument(.wholeModuleOptimization, .indexFile)
744-
743+
let useWMO = parsedOptions.hasFlag(positive: .wholeModuleOptimization, negative: .noWholeModuleOptimization, default: false)
744+
let hasIndexFile = parsedOptions.hasArgument(.indexFile)
745745
let wantBatchMode = parsedOptions.hasFlag(positive: .enableBatchMode, negative: .disableBatchMode, default: false)
746746

747-
if requiresSingleCompile {
747+
if useWMO || hasIndexFile {
748748
if wantBatchMode {
749-
let disablingOption: Option = parsedOptions.hasArgument(.wholeModuleOptimization) ? .wholeModuleOptimization : .indexFile
749+
let disablingOption: Option = useWMO ? .wholeModuleOptimization : .indexFile
750750
diagnosticsEngine.emit(.warn_ignoring_batch_mode(disablingOption))
751751
}
752752

Sources/SwiftDriver/Options/Options.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ extension Option {
428428
public static let warnSwift3ObjcInference: Option = Option("-warn-swift3-objc-inference", .flag, alias: Option.warnSwift3ObjcInferenceComplete, attributes: [.helpHidden, .frontend, .doesNotAffectIncrementalBuild])
429429
public static let warningsAsErrors: Option = Option("-warnings-as-errors", .flag, attributes: [.frontend], helpText: "Treat warnings as errors")
430430
public static let wholeModuleOptimization: Option = Option("-whole-module-optimization", .flag, attributes: [.frontend, .noInteractive], helpText: "Optimize input files together instead of individually")
431+
public static let noWholeModuleOptimization: Option = Option("-no-whole-module-optimization", .flag, attributes: [.frontend, .noInteractive], helpText: "Disable optimizing input files together instead of individually")
431432
public static let wmo: Option = Option("-wmo", .flag, alias: Option.wholeModuleOptimization, attributes: [.helpHidden, .frontend, .noInteractive])
432433
public static let workingDirectoryEQ: Option = Option("-working-directory=", .joined, alias: Option.workingDirectory)
433434
public static let workingDirectory: Option = Option("-working-directory", .separate, metaVar: "<path>", helpText: "Resolve file paths relative to the specified directory")
@@ -723,6 +724,7 @@ extension Option {
723724
Option.noStdlibRpath,
724725
Option.noToolchainStdlibRpath,
725726
Option.nostdimport,
727+
Option.noWholeModuleOptimization,
726728
Option.numThreads,
727729
Option.Onone,
728730
Option.Oplayground,

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,44 @@ final class SwiftDriverTests: XCTestCase {
152152
let driver1 = try Driver(args: ["swiftc", "main.swift", "-whole-module-optimization"])
153153
XCTAssertEqual(driver1.compilerMode, .singleCompile)
154154

155-
let driver2 = try Driver(args: ["swiftc", "main.swift", "-g"])
155+
let driver2 = try Driver(args: ["swiftc", "main.swift", "-whole-module-optimization", "-no-whole-module-optimization"])
156156
XCTAssertEqual(driver2.compilerMode, .standardCompile)
157+
158+
let driver3 = try Driver(args: ["swiftc", "main.swift", "-g"])
159+
XCTAssertEqual(driver3.compilerMode, .standardCompile)
157160
}
158161
}
159162

163+
func testBatchModeDiagnostics() throws {
164+
try assertNoDriverDiagnostics(args: "swiftc", "-enable-batch-mode") { driver in
165+
switch driver.compilerMode {
166+
case .batchCompile:
167+
break
168+
default:
169+
XCTFail("Expected batch compile, got \(driver.compilerMode)")
170+
}
171+
}
172+
173+
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization") { driver, diagnostics in
174+
XCTAssertEqual(driver.compilerMode, .singleCompile)
175+
diagnostics.expect(.warning("ignoring '-enable-batch-mode' because '-whole-module-optimization' was also specified"))
176+
}
177+
178+
try assertDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization", "-no-whole-module-optimization", "-index-file", "-module-name", "foo") { driver, diagnostics in
179+
XCTAssertEqual(driver.compilerMode, .singleCompile)
180+
diagnostics.expect(.warning("ignoring '-enable-batch-mode' because '-index-file' was also specified"))
181+
}
182+
183+
try assertNoDriverDiagnostics(args: "swiftc", "-enable-batch-mode", "-whole-module-optimization", "-no-whole-module-optimization") { driver in
184+
switch driver.compilerMode {
185+
case .batchCompile:
186+
break
187+
default:
188+
XCTFail("Expected batch compile, got \(driver.compilerMode)")
189+
}
190+
}
191+
}
192+
160193
func testInputFiles() throws {
161194
let driver1 = try Driver(args: ["swiftc", "a.swift", "/tmp/b.swift"])
162195
XCTAssertEqual(driver1.inputFiles,

0 commit comments

Comments
 (0)