Skip to content

Commit 10caa6e

Browse files
Unlock -profile-generate for WebAssembly targets
We added support for `-profile-generate` in the latest LLVM, but it is explicitly banned for WebAssembly targets now. Unlock the feature by adding the necessary runtime library to the link command line as well as other platforms.
1 parent 9dd6657 commit 10caa6e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ extension WebAssemblyToolchain {
160160
throw Error.sanitizersUnsupportedForTarget(targetTriple.triple)
161161
}
162162

163-
guard !parsedOptions.hasArgument(.profileGenerate) else {
164-
throw Error.profilingUnsupportedForTarget(targetTriple.triple)
163+
if parsedOptions.hasArgument(.profileGenerate) {
164+
let libProfile = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
165+
.appending(components: "clang", "lib", targetTriple.osName,
166+
"libclang_rt.profile-\(targetTriple.archName).a")
167+
commandLine.appendPath(libProfile)
165168
}
166169

167170
if let lto = lto {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,6 +4463,27 @@ final class SwiftDriverTests: XCTestCase {
44634463
}
44644464
#endif
44654465

4466+
// -profile-generate should add libclang_rt.profile for WebAssembly targets
4467+
try withTemporaryDirectory { resourceDir in
4468+
try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) {
4469+
$0.send("garbage")
4470+
}
4471+
4472+
for triple in ["wasm32-unknown-wasi", "wasm32-unknown-wasip1-threads"] {
4473+
var driver = try Driver(args: [
4474+
"swiftc", "-profile-generate", "-target", triple, "test.swift",
4475+
"-resource-dir", resourceDir.pathString
4476+
])
4477+
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs()
4478+
4479+
XCTAssertEqual(plannedJobs.count, 2)
4480+
XCTAssertEqual(plannedJobs[0].kind, .compile)
4481+
4482+
XCTAssertEqual(plannedJobs[1].kind, .link)
4483+
XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile-wasm32.a"))
4484+
}
4485+
}
4486+
44664487
for explicitUseLd in [true, false] {
44674488
var args = ["swiftc", "-profile-generate", "-target", "x86_64-unknown-windows-msvc", "test.swift"]
44684489
if explicitUseLd {

0 commit comments

Comments
 (0)