Skip to content

Commit b9c4f02

Browse files
committed
Fix optimization record path handling in primary file compilation mode
When using -save-optimization-record-path in primary file mode, the user provided path was being ignored and used a derived path instead. -save-optimization-record-path was working correctly in WMO mode due to taking a different code path.
1 parent 91ce741 commit b9c4f02

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,13 @@ extension Driver {
691691
if let outputFileMapPath = try outputFileMap?.existingOutput(inputFile: input.fileHandle, outputType: outputType) {
692692
outputPath = outputFileMapPath
693693
} else if let output = inputOutputMap[input]?.first, output.file != .standardOutput, compilerOutputType != nil {
694-
// Alongside primary output
695-
outputPath = try output.file.replacingExtension(with: outputType).intern()
694+
// For optimization records, use finalOutputPath over derived path
695+
if outputType == .yamlOptimizationRecord || outputType == .bitstreamOptimizationRecord {
696+
outputPath = finalOutputPath
697+
} else {
698+
// Alongside primary output for other types
699+
outputPath = try output.file.replacingExtension(with: outputType).intern()
700+
}
696701
} else {
697702
outputPath = try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: input.file.basenameWithoutExt.appendingFileTypeExtension(outputType))).intern()
698703
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,49 @@ final class SwiftDriverTests: XCTestCase {
35423542
try checkSupplementaryOutputFileMap(format: "bitstream", .bitstreamOptimizationRecord)
35433543
}
35443544

3545+
func testOptimizationRecordPathUserProvidedPath() throws {
3546+
3547+
do {
3548+
var driver = try Driver(args: [
3549+
"swiftc", "-save-optimization-record", "-save-optimization-record-path", "/tmp/test.opt.yaml",
3550+
"-c", "test.swift"
3551+
])
3552+
let plannedJobs = try driver.planBuild()
3553+
let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile })
3554+
3555+
XCTAssertTrue(compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/test.opt.yaml")))))
3556+
XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path")))
3557+
}
3558+
3559+
do {
3560+
var driver = try Driver(args: [
3561+
"swiftc", "-wmo", "-save-optimization-record", "-save-optimization-record-path", "/tmp/wmo.opt.yaml",
3562+
"-c", "test.swift"
3563+
])
3564+
let plannedJobs = try driver.planBuild()
3565+
let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile })
3566+
3567+
XCTAssertTrue(compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/wmo.opt.yaml")))))
3568+
XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path")))
3569+
}
3570+
3571+
do {
3572+
var driver = try Driver(args: [
3573+
"swiftc", "-wmo", "-num-threads", "4", "-save-optimization-record",
3574+
"-save-optimization-record-path", "/tmp/mt1.opt.yaml",
3575+
"-save-optimization-record-path", "/tmp/mt2.opt.yaml",
3576+
"-c", "test1.swift", "test2.swift"
3577+
])
3578+
let plannedJobs = try driver.planBuild()
3579+
let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile })
3580+
3581+
XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path")))
3582+
let hasFirstPath = compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/mt1.opt.yaml"))))
3583+
let hasSecondPath = compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/mt2.opt.yaml"))))
3584+
XCTAssertTrue(hasFirstPath || hasSecondPath, "Should contain at least one user-provided optimization record path")
3585+
}
3586+
}
3587+
35453588
func testUpdateCode() throws {
35463589
do {
35473590
var driver = try Driver(args: [

0 commit comments

Comments
 (0)