diff --git a/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift b/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift index 2ac5cace7..a4ca6e9d6 100644 --- a/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift +++ b/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift @@ -691,8 +691,13 @@ extension Driver { if let outputFileMapPath = try outputFileMap?.existingOutput(inputFile: input.fileHandle, outputType: outputType) { outputPath = outputFileMapPath } else if let output = inputOutputMap[input]?.first, output.file != .standardOutput, compilerOutputType != nil { - // Alongside primary output - outputPath = try output.file.replacingExtension(with: outputType).intern() + // For optimization records, use finalOutputPath over derived path + if outputType == .yamlOptimizationRecord || outputType == .bitstreamOptimizationRecord { + outputPath = finalOutputPath + } else { + // Alongside primary output for other types + outputPath = try output.file.replacingExtension(with: outputType).intern() + } } else { outputPath = try VirtualPath.createUniqueTemporaryFile(RelativePath(validating: input.file.basenameWithoutExt.appendingFileTypeExtension(outputType))).intern() } diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 835649acc..39d2e2570 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -3542,6 +3542,49 @@ final class SwiftDriverTests: XCTestCase { try checkSupplementaryOutputFileMap(format: "bitstream", .bitstreamOptimizationRecord) } + func testOptimizationRecordPathUserProvidedPath() throws { + + do { + var driver = try Driver(args: [ + "swiftc", "-save-optimization-record", "-save-optimization-record-path", "/tmp/test.opt.yaml", + "-c", "test.swift" + ]) + let plannedJobs = try driver.planBuild() + let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile }) + + XCTAssertTrue(compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/test.opt.yaml"))))) + XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path"))) + } + + do { + var driver = try Driver(args: [ + "swiftc", "-wmo", "-save-optimization-record", "-save-optimization-record-path", "/tmp/wmo.opt.yaml", + "-c", "test.swift" + ]) + let plannedJobs = try driver.planBuild() + let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile }) + + XCTAssertTrue(compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/wmo.opt.yaml"))))) + XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path"))) + } + + do { + var driver = try Driver(args: [ + "swiftc", "-wmo", "-num-threads", "4", "-save-optimization-record", + "-save-optimization-record-path", "/tmp/mt1.opt.yaml", + "-save-optimization-record-path", "/tmp/mt2.opt.yaml", + "-c", "test1.swift", "test2.swift" + ]) + let plannedJobs = try driver.planBuild() + let compileJob = try XCTUnwrap(plannedJobs.first { $0.kind == .compile }) + + XCTAssertTrue(compileJob.commandLine.contains(.flag("-save-optimization-record-path"))) + let hasFirstPath = compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/mt1.opt.yaml")))) + let hasSecondPath = compileJob.commandLine.contains(.path(VirtualPath.absolute(try AbsolutePath(validating: "/tmp/mt2.opt.yaml")))) + XCTAssertTrue(hasFirstPath || hasSecondPath, "Should contain at least one user-provided optimization record path") + } + } + func testUpdateCode() throws { do { var driver = try Driver(args: [