Skip to content

Fix LTO support for WebAssembly targets #1710

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 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ extension WebAssemblyToolchain {
return .responseFilePath(input.file)
} else if input.type == .object {
return .path(input.file)
} else if lto != nil && input.type == .llvmBitcode {
return .path(input.file)
} else {
return nil
}
Expand Down Expand Up @@ -162,6 +164,15 @@ extension WebAssemblyToolchain {
throw Error.profilingUnsupportedForTarget(targetTriple.triple)
}

if let lto = lto {
switch lto {
case .llvmFull:
commandLine.appendFlag("-flto=full")
case .llvmThin:
commandLine.appendFlag("-flto=thin")
}
}

// Run clang++ in verbose mode if "-v" is set
try commandLine.appendLast(.v, from: &parsedOptions)

Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,27 @@ final class SwiftDriverTests: XCTestCase {
let linkJob3 = try plannedJobs3.findJob(.link)
XCTAssertTrue(linkJob3.tool.name.contains("clang"))
XCTAssertTrue(linkJob3.commandLine.contains(.flag("-flto=full")))

try withTemporaryDirectory { path in
try localFileSystem.writeFileContents(path.appending(components: "wasi", "static-executable-args.lnk")) {
$0.send("garbage")
}
var driver4 = try Driver(args: commonArgs + [
"-emit-executable", "-target", "wasm32-unknown-wasi", "-lto=llvm-thin", "baz.bc",
"-resource-dir", path.pathString
], env: env)
let plannedJobs4 = try driver4.planBuild()
XCTAssertFalse(plannedJobs4.containsJob(.autolinkExtract))
let linkJob4 = try plannedJobs4.findJob(.link)
XCTAssertTrue(linkJob4.tool.name.contains("clang"))
XCTAssertTrue(linkJob4.commandLine.contains(.flag("-flto=thin")))
for linkBcInput in ["foo", "bar", "baz.bc"] {
XCTAssertTrue(
linkJob4.inputs.contains { $0.file.basename.hasPrefix(linkBcInput) && $0.type == .llvmBitcode },
"Missing input \(linkBcInput)"
)
}
}
}

do {
Expand Down