From 10caa6ecedb5e8c12da0a0b9fb94bd8af383b24e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 15 Oct 2024 13:29:18 +0000 Subject: [PATCH] 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. --- .../WebAssemblyToolchain+LinkerSupport.swift | 7 +++++-- Tests/SwiftDriverTests/SwiftDriverTests.swift | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift index a0c0a02a0..085f27f8f 100644 --- a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift @@ -160,8 +160,11 @@ extension WebAssemblyToolchain { throw Error.sanitizersUnsupportedForTarget(targetTriple.triple) } - guard !parsedOptions.hasArgument(.profileGenerate) else { - throw Error.profilingUnsupportedForTarget(targetTriple.triple) + if parsedOptions.hasArgument(.profileGenerate) { + let libProfile = VirtualPath.lookup(targetInfo.runtimeResourcePath.path) + .appending(components: "clang", "lib", targetTriple.osName, + "libclang_rt.profile-\(targetTriple.archName).a") + commandLine.appendPath(libProfile) } if let lto = lto { diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 1fc27396b..118a9830e 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -4463,6 +4463,27 @@ final class SwiftDriverTests: XCTestCase { } #endif + // -profile-generate should add libclang_rt.profile for WebAssembly targets + try withTemporaryDirectory { resourceDir in + try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) { + $0.send("garbage") + } + + for triple in ["wasm32-unknown-wasi", "wasm32-unknown-wasip1-threads"] { + var driver = try Driver(args: [ + "swiftc", "-profile-generate", "-target", triple, "test.swift", + "-resource-dir", resourceDir.pathString + ]) + let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() + + XCTAssertEqual(plannedJobs.count, 2) + XCTAssertEqual(plannedJobs[0].kind, .compile) + + XCTAssertEqual(plannedJobs[1].kind, .link) + XCTAssert(plannedJobs[1].commandLine.containsPathWithBasename("libclang_rt.profile-wasm32.a")) + } + } + for explicitUseLd in [true, false] { var args = ["swiftc", "-profile-generate", "-target", "x86_64-unknown-windows-msvc", "test.swift"] if explicitUseLd {