Skip to content

Commit

Permalink
In WMO, make sure the dummy "primary" compilation input is a Swift so…
Browse files Browse the repository at this point in the history
…urce file

Otherwise, if the first argument is anything else (e.g. an object file) we will use it as a key and the compiler won't be too happy about that.

Resolves rdar://108741675
  • Loading branch information
artemcm committed May 2, 2023
1 parent 221b5ea commit 7bef081
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,10 @@ extension Driver {
// To match the legacy driver behavior, make sure we add the first input file
// to the output file map if compiling without primary inputs (WMO), even
// if there aren't any corresponding outputs.
entries[inputFiles[0].fileHandle] = [:]
guard let firstSourceInputHandle = inputFiles.first(where:{ $0.type == .swift })?.fileHandle else {
fatalError("Formulating swift-frontend invocation without any input .swift files")
}
entries[firstSourceInputHandle] = [:]
}

for flaggedPair in flaggedInputOutputPairs {
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,11 @@ extension Driver {
emitModuleTrace: Bool
) throws -> Job? {
guard case .singleCompile = compilerMode,
inputFiles.allSatisfy({ $0.type.isPartOfSwiftCompilation })
inputFiles.contains(where: { $0.type.isPartOfSwiftCompilation })
else { return nil }

if parsedOptions.hasArgument(.embedBitcode) {
if parsedOptions.hasArgument(.embedBitcode),
inputFiles.allSatisfy({ $0.type.isPartOfSwiftCompilation }) {
let compile = try compileJob(primaryInputs: [],
outputType: .llvmBitcode,
addJobOutputs: addJobOutputs,
Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2824,6 +2824,27 @@ final class SwiftDriverTests: XCTestCase {

}

func testWMOWithNonSourceInput() throws {
var driver1 = try Driver(args: [
"swiftc", "-whole-module-optimization", "danger.o", "foo.swift", "bar.swift", "wibble.swift", "-module-name", "Test",
"-driver-filelist-threshold=0"
])
let plannedJobs = try driver1.planBuild().removingAutolinkExtractJobs()
XCTAssertEqual(plannedJobs.count, 2)
let compileJob = plannedJobs[0]
XCTAssertEqual(compileJob.kind, .compile)
XCTAssert(compileJob.commandLine.contains(.flag("-supplementary-output-file-map")))
let argIdx = try XCTUnwrap(compileJob.commandLine.firstIndex(where: { $0 == .flag("-supplementary-output-file-map") }))
let supplOutputs = compileJob.commandLine[argIdx+1]
guard case let .path(path) = supplOutputs,
case let .fileList(_, fileList) = path,
case let .outputFileMap(outFileMap) = fileList else {
throw StringError("Unexpected argument for output file map")
}
let firstKey: String = try VirtualPath.lookup(XCTUnwrap(outFileMap.entries.keys.first)).description
XCTAssertEqual(firstKey, "foo.swift")
}

func testDashDashPassingDownInput() throws {
do {
var driver = try Driver(args: ["swiftc", "-module-name=ThisModule", "-wmo", "-num-threads", "4", "-emit-module", "-o", "test.swiftmodule", "--", "main.swift", "multi-threaded.swift"])
Expand Down

0 comments on commit 7bef081

Please sign in to comment.