Skip to content

Building object files using -c with -lto should respect the output file path (-o) if present #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2021

Conversation

kubamracek
Copy link
Contributor

Currently, when combining -c + -lto + -o, the driver will produce a ".bc" file on disk, even if the -o argument said ".o". This PR fixes that and matches the behavior of the C++ driver in the swift repo.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @kubamracek.

@kubamracek kubamracek force-pushed the mracek/lto-should-respect-dash-o-arg branch from 5bf5968 to f64c512 Compare September 30, 2021 01:11
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@artemcm @compnerd could I ask for another review? I had to fix up some existing tests, and I am not super sure the change is actually correct now :)

@@ -79,6 +79,10 @@ extension Driver {
case .object:
return (linkerOutputType == nil)
case .llvmBitcode:
if linkerOutputType == nil {
return true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this override for all cases or only for lto? There is some lto checks below:

// When -lto is set, .bc will be used for linking. Otherwise, .bc is
// top-level output (-emit-bc)
return lto == nil

Maybe?

lto == nil || linkerOutputType == nil

Looking at the tests it seems like bc is a top level output in cases where maybe it shouldn't be.
Ex: "-embed-bitcode", "-c" .bc should be intermediary output that is passed to the "backend" job to become .o. We don't need to output it next to the final outputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I think that's the part I was missing. This makes sense, let me adjust the PR.

Comment on lines 317 to 334
var driver1 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver1.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver1.linkerOutputType, nil)
let jobs1 = try driver1.planBuild()
XCTAssertEqual(jobs1.count, 1)
XCTAssertEqual(jobs1[0].outputs.count, 1)
XCTAssertEqual(jobs1[0].outputs[0].file.basename, "foo.bc")

var driver2 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9",
"-o", "foo.o"])
XCTAssertEqual(driver2.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver2.linkerOutputType, nil)
let jobs2 = try driver2.planBuild()
XCTAssertEqual(jobs2.count, 1)
XCTAssertEqual(jobs2[0].outputs.count, 1)
XCTAssertEqual(jobs2[0].outputs[0].file.basename, "foo.o")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can wrap these in do blocks to namespace the variables and not need to number everything:

Suggested change
var driver1 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver1.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver1.linkerOutputType, nil)
let jobs1 = try driver1.planBuild()
XCTAssertEqual(jobs1.count, 1)
XCTAssertEqual(jobs1[0].outputs.count, 1)
XCTAssertEqual(jobs1[0].outputs[0].file.basename, "foo.bc")
var driver2 = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9",
"-o", "foo.o"])
XCTAssertEqual(driver2.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver2.linkerOutputType, nil)
let jobs2 = try driver2.planBuild()
XCTAssertEqual(jobs2.count, 1)
XCTAssertEqual(jobs2[0].outputs.count, 1)
XCTAssertEqual(jobs2[0].outputs[0].file.basename, "foo.o")
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9"])
XCTAssertEqual(driver.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver.linkerOutputType, nil)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.count, 1)
XCTAssertEqual(jobs[0].outputs.count, 1)
XCTAssertEqual(jobs[0].outputs[0].file.basename, "foo.bc")
}
do {
var driver = try Driver(args: ["swiftc", "foo.swift", "-lto=llvm-full",
"-c", "-target", "x86_64-apple-macosx10.9",
"-o", "foo.o"])
XCTAssertEqual(driver.compilerOutputType, .llvmBitcode)
XCTAssertEqual(driver.linkerOutputType, nil)
let jobs = try driver.planBuild()
XCTAssertEqual(jobs.count, 1)
XCTAssertEqual(jobs[0].outputs.count, 1)
XCTAssertEqual(jobs[0].outputs[0].file.basename, "foo.o")
}

@kubamracek kubamracek force-pushed the mracek/lto-should-respect-dash-o-arg branch from f64c512 to 5573c5c Compare September 30, 2021 15:38
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek force-pushed the mracek/lto-should-respect-dash-o-arg branch from 5573c5c to 2c1a43b Compare October 1, 2021 04:50
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek merged commit 607df61 into main Oct 1, 2021
@kubamracek kubamracek deleted the mracek/lto-should-respect-dash-o-arg branch October 1, 2021 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants