Skip to content

Commit 6aaa811

Browse files
authored
Merge pull request swiftlang#1287 from artemcm/OptimizationLevelOnLinkerInvocations
Pass down an optimization level to the Clang linker invocation
2 parents b72c1f0 + 7cda054 commit 6aaa811

6 files changed

+54
-5
lines changed

Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ extension DarwinToolchain {
191191
commandLine.appendFlag("-fapplication-extension")
192192
}
193193

194+
// Pass down an optimization level
195+
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
196+
commandLine.appendFlag(optArg)
197+
}
198+
194199
// Linking sanitizers will add rpaths, which might negatively interact when
195200
// other rpaths are involved, so we should make sure we add the rpaths after
196201
// all user-specified rpaths.

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ extension GenericUnixToolchain {
253253
commandLine.append(.responseFilePath(linkFile))
254254
}
255255

256+
// Pass down an optimization level
257+
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
258+
commandLine.appendFlag(optArg)
259+
}
260+
256261
// Explicitly pass the target to the linker
257262
commandLine.appendFlag("--target=\(targetTriple.triple)")
258263

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ extension WebAssemblyToolchain {
125125
}
126126
commandLine.append(.responseFilePath(linkFilePath))
127127

128+
// Pass down an optimization level
129+
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
130+
commandLine.appendFlag(optArg)
131+
}
132+
128133
// Explicitly pass the target to the linker
129134
commandLine.appendFlag("--target=\(targetTriple.triple)")
130135

Sources/SwiftDriver/Jobs/WindowsToolchain+LinkerSupport.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ extension WindowsToolchain {
172172
commandLine.appendFlag("-stdlib=\(stdlib.asSingle)")
173173
}
174174

175+
// Pass down an optimization level
176+
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {
177+
commandLine.appendFlag(optArg)
178+
}
179+
175180
// FIXME(compnerd) render asan/ubsan runtime link for executables
176181

177182
if parsedOptions.contains(.profileGenerate) {

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,30 @@ extension Toolchain {
325325
supportsResponseFiles: tool.supportsResponseFiles(in: self)
326326
)
327327
}
328+
329+
/// Maps an optimization level swiftc arg to a corresponding flag for the Clang linker driver invocation
330+
internal func mapOptimizationLevelToClangArg(from parsedOptions: inout ParsedOptions) -> String? {
331+
guard let opt = parsedOptions.getLast(in: .O) else {
332+
return nil
333+
}
334+
let clangArg: String?
335+
switch opt.option {
336+
case .Oplayground:
337+
fallthrough
338+
case .Onone:
339+
clangArg = "-O0"
340+
case .O:
341+
fallthrough
342+
case .Ounchecked:
343+
clangArg = "-O3"
344+
case .Osize:
345+
clangArg = "-Os"
346+
default:
347+
clangArg = nil
348+
assert(false, "Unhandled Optimization Mode: \(opt.description)")
349+
}
350+
return clangArg
351+
}
328352
}
329353

330354
@_spi(Testing) public enum ToolchainError: Swift.Error {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ final class SwiftDriverTests: XCTestCase {
16661666

16671667
do {
16681668
// macOS target
1669-
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15", "-use-ld=foo"], env: env)
1669+
var driver = try Driver(args: commonArgs + ["-emit-library", "-target", "x86_64-apple-macosx10.15", "-Onone", "-use-ld=foo"], env: env)
16701670
let plannedJobs = try driver.planBuild()
16711671

16721672
XCTAssertEqual(3, plannedJobs.count)
@@ -1685,6 +1685,7 @@ final class SwiftDriverTests: XCTestCase {
16851685
XCTAssertFalse(cmd.contains(.flag("-shared")))
16861686
// Handling of '-lobjc' is now in the Clang linker driver.
16871687
XCTAssertFalse(cmd.contains(.flag("-lobjc")))
1688+
XCTAssertTrue(cmd.contains(.flag("-O0")))
16881689
}
16891690

16901691
do {
@@ -1952,12 +1953,13 @@ final class SwiftDriverTests: XCTestCase {
19521953
XCTAssertTrue(linkJob1?.commandLine.contains(.flag("-flto=thin")))
19531954
#endif
19541955

1955-
var driver2 = try Driver(args: commonArgs + ["-emit-executable", "-target", "x86_64-unknown-linux", "-lto=llvm-thin"], env: env)
1956+
var driver2 = try Driver(args: commonArgs + ["-emit-executable", "-O", "-target", "x86_64-unknown-linux", "-lto=llvm-thin"], env: env)
19561957
let plannedJobs2 = try driver2.planBuild()
19571958
XCTAssertFalse(plannedJobs2.contains(where: { $0.kind == .autolinkExtract }))
19581959
let linkJob2 = plannedJobs2.first(where: { $0.kind == .link })
19591960
XCTAssertTrue(linkJob2?.tool.name.contains("clang"))
19601961
XCTAssertTrue(linkJob2?.commandLine.contains(.flag("-flto=thin")))
1962+
XCTAssertTrue(linkJob2?.commandLine.contains(.flag("-O3")))
19611963

19621964
var driver3 = try Driver(args: commonArgs + ["-emit-executable", "-target", "x86_64-unknown-linux", "-lto=llvm-full"], env: env)
19631965
let plannedJobs3 = try driver3.planBuild()
@@ -1972,7 +1974,7 @@ final class SwiftDriverTests: XCTestCase {
19721974
}
19731975

19741976
do {
1975-
var driver = try Driver(args: commonArgs + ["-emit-executable", "-emit-module", "-g", "-target", "x86_64-apple-macosx10.15"], env: env)
1977+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-Onone", "-emit-module", "-g", "-target", "x86_64-apple-macosx10.15"], env: env)
19761978
let plannedJobs = try driver.planBuild()
19771979
XCTAssertEqual(5, plannedJobs.count)
19781980
XCTAssertEqual(plannedJobs.map(\.kind), [.emitModule, .compile, .compile, .link, .generateDSYM])
@@ -1985,6 +1987,7 @@ final class SwiftDriverTests: XCTestCase {
19851987
XCTAssertTrue(commandContainsTemporaryPath(cmd, "foo.o"))
19861988
XCTAssertTrue(commandContainsTemporaryPath(cmd, "bar.o"))
19871989
XCTAssertTrue(cmd.contains(.joinedOptionAndPath("-Wl,-add_ast_path,", .relative(.init("Test.swiftmodule")))))
1990+
XCTAssertTrue(cmd.contains(.flag("-O0")))
19881991
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))
19891992

19901993
XCTAssertFalse(cmd.contains(.flag("-static")))
@@ -2057,7 +2060,7 @@ final class SwiftDriverTests: XCTestCase {
20572060
#if os(Linux)
20582061
do {
20592062
// executable linking linux static stdlib
2060-
var driver = try Driver(args: commonArgs + ["-emit-executable", "-static-stdlib", "-target", "x86_64-unknown-linux"], env: env)
2063+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-Osize", "-static-stdlib", "-target", "x86_64-unknown-linux"], env: env)
20612064
let plannedJobs = try driver.planBuild()
20622065

20632066
XCTAssertEqual(plannedJobs.count, 4)
@@ -2077,6 +2080,7 @@ final class SwiftDriverTests: XCTestCase {
20772080
XCTAssertTrue(commandContainsTemporaryPath(cmd, "bar.o"))
20782081
XCTAssertTrue(cmd.contains(.flag("--start-group")))
20792082
XCTAssertTrue(cmd.contains(.flag("--end-group")))
2083+
XCTAssertTrue(cmd.contains(.flag("-Os")))
20802084
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))
20812085

20822086
XCTAssertFalse(cmd.contains(.flag("-static")))
@@ -2119,7 +2123,7 @@ final class SwiftDriverTests: XCTestCase {
21192123
try localFileSystem.writeFileContents(
21202124
path.appending(components: "wasi", "static-executable-args.lnk")) { $0 <<< "garbage" }
21212125
// WASM executable linking
2122-
var driver = try Driver(args: commonArgs + ["-emit-executable",
2126+
var driver = try Driver(args: commonArgs + ["-emit-executable", "-Ounchecked",
21232127
"-target", "wasm32-unknown-wasi",
21242128
"-resource-dir", path.pathString,
21252129
"-sdk", "/sdk/path"], env: env)
@@ -2144,6 +2148,7 @@ final class SwiftDriverTests: XCTestCase {
21442148
XCTAssertTrue(commandContainsTemporaryPath(cmd, "bar.o"))
21452149
XCTAssertTrue(commandContainsTemporaryResponsePath(cmd, "Test.autolink"))
21462150
XCTAssertTrue(cmd.contains(.responseFilePath(.absolute(path.appending(components: "wasi", "static-executable-args.lnk")))))
2151+
XCTAssertTrue(cmd.contains(.flag("-O3")))
21472152
XCTAssertEqual(linkJob.outputs[0].file, try VirtualPath(path: "Test"))
21482153

21492154
XCTAssertFalse(cmd.contains(.flag("-dylib")))

0 commit comments

Comments
 (0)