Skip to content

Commit 61d6215

Browse files
Enable --gc-sections for WebAssembly targets (#7356)
Enable `--gc-sections` back for WebAssembly targets ### Motivation: We disabled `--gc-sections` for Wasm targets due to a lack of features in the object file format. However, we recently added the missing piece in wasm object file format, so we no longer have any reason to disable it. ### Modifications: This effectively reverts 3a366cc. After swiftlang/swift#71768, we don't have any reason to disable `--gc-sections` for WebAssembly targets. Note that the new wasm segment flags are only supported by the latest LLVM and wasm-ld, but we can assume that toolchains or Swift SDKs for WebAssembly have wasm-ld built from the latest LLVM. ### Result: Allow dead code stripping at link time for WebAssembly targets.
1 parent 98747d5 commit 61d6215

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
122122
return ["-Xlinker", "-dead_strip"]
123123
} else if triple.isWindows() {
124124
return ["-Xlinker", "/OPT:REF"]
125-
} else if triple.arch == .wasm32 {
126-
// FIXME: wasm-ld strips data segments referenced through __start/__stop symbols
127-
// during GC, and it removes Swift metadata sections like swift5_protocols
128-
// We should add support of SHF_GNU_RETAIN-like flag for __attribute__((retain))
129-
// to LLVM and wasm-ld
130-
// This workaround is required for not only WASI but also all WebAssembly triples
131-
// using wasm-ld (e.g. wasm32-unknown-unknown). So this branch is conditioned by
132-
// arch == .wasm32
133-
return []
134125
} else {
135126
return ["-Xlinker", "--gc-sections"]
136127
}

Tests/BuildTests/CrossCompilationBuildPlanTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,39 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
7070
)
7171
}
7272

73+
func testWasmTargetRelease() throws {
74+
let pkgPath = AbsolutePath("/Pkg")
75+
76+
let (graph, fs, observabilityScope) = try trivialPackageGraph(pkgRootPath: pkgPath)
77+
78+
var parameters = mockBuildParameters(
79+
config: .release, targetTriple: .wasi, linkerDeadStrip: true
80+
)
81+
parameters.linkingParameters.shouldLinkStaticSwiftStdlib = true
82+
let result = try BuildPlanResult(plan: BuildPlan(
83+
buildParameters: parameters,
84+
graph: graph,
85+
fileSystem: fs,
86+
observabilityScope: observabilityScope
87+
))
88+
let buildPath = result.plan.productsBuildPath
89+
90+
let appBuildDescription = try result.buildProduct(for: "app")
91+
XCTAssertEqual(
92+
try appBuildDescription.linkArguments(),
93+
[
94+
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
95+
"-L", buildPath.pathString,
96+
"-o", buildPath.appending(components: "app.wasm").pathString,
97+
"-module-name", "app", "-static-stdlib", "-emit-executable",
98+
"-Xlinker", "--gc-sections",
99+
"@\(buildPath.appending(components: "app.product", "Objects.LinkFileList"))",
100+
"-target", "wasm32-unknown-wasi",
101+
"-g",
102+
]
103+
)
104+
}
105+
73106
func testWASITarget() throws {
74107
let pkgPath = AbsolutePath("/Pkg")
75108

0 commit comments

Comments
 (0)