Skip to content

Commit 6264c06

Browse files
Add compiler and linker options for wasm32-wasip1-threads
1 parent 90287f1 commit 6264c06

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension SwiftSDKGenerator {
3737
}
3838

3939
var toolset = Toolset(rootPath: relativeToolchainBinDir.string)
40-
recipe.applyPlatformOptions(toolset: &toolset)
40+
recipe.applyPlatformOptions(toolset: &toolset, targetTriple: self.targetTriple)
4141
try writeFile(at: toolsetJSONPath, encoder.encode(toolset))
4242

4343
return toolsetJSONPath

Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public struct LinuxRecipe: SwiftSDKRecipe {
104104
self.versionsConfiguration = versionsConfiguration
105105
}
106106

107-
public func applyPlatformOptions(toolset: inout Toolset) {
107+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
108108
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-use-ld=lld", "-Xlinker", "-R/usr/lib/swift/linux/"])
109109
toolset.cxxCompiler = Toolset.ToolProperties(extraCLIOptions: ["-lstdc++"])
110110
toolset.linker = Toolset.ToolProperties(path: "ld.lld")

Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public struct SwiftSDKProduct {
2222
/// A protocol describing a set of platform specific instructions to make a Swift SDK
2323
public protocol SwiftSDKRecipe: Sendable {
2424
/// Update the given toolset with platform specific options
25-
func applyPlatformOptions(toolset: inout Toolset)
25+
func applyPlatformOptions(
26+
toolset: inout Toolset, targetTriple: Triple
27+
)
2628
func applyPlatformOptions(
2729
metadata: inout SwiftSDKMetadataV4.TripleProperties,
2830
paths: PathsConfiguration,
@@ -37,7 +39,7 @@ public protocol SwiftSDKRecipe: Sendable {
3739
}
3840

3941
extension SwiftSDKRecipe {
40-
public func applyPlatformOptions(toolset: inout Toolset) {}
42+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {}
4143
public func applyPlatformOptions(
4244
metadata: inout SwiftSDKMetadataV4.TripleProperties,
4345
paths: PathsConfiguration,

Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,34 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe {
4545
"\(self.swiftVersion)_wasm"
4646
}
4747

48-
public func applyPlatformOptions(toolset: inout Toolset) {
48+
public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) {
4949
// We only support static linking for WebAssembly for now, so make it the default.
5050
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-static-stdlib"])
51+
if targetTriple.environmentName == "threads" {
52+
// Enable features required for threading support
53+
let ccOptions = [
54+
"-matomics", "-mbulk-memory", "-mthread-model", "posix",
55+
"-pthread", "-ftls-model=local-exec"
56+
]
57+
// Tell LLVM codegen in swiftc to enable those features via clang options
58+
toolset.swiftCompiler?.extraCLIOptions?.append(contentsOf: ccOptions.flatMap {
59+
["-Xcc", $0]
60+
})
61+
// Tell the C and C++ compilers to enable those features
62+
toolset.cCompiler = Toolset.ToolProperties(extraCLIOptions: ccOptions)
63+
toolset.cxxCompiler = Toolset.ToolProperties(extraCLIOptions: ccOptions)
64+
65+
let linkerOptions = [
66+
// Shared memory is required for WASI threads ABI
67+
// See https://github.com/WebAssembly/wasi-threads for more information.
68+
"--import-memory", "--export-memory", "--shared-memory",
69+
// Set the maximum memory size to 1GB because shared memory must specify
70+
// a maximum size. 1GB is chosen as a conservative default, but it can be
71+
// overridden by the user-provided --max-memory linker option.
72+
"--max-memory=1073741824",
73+
]
74+
toolset.linker = Toolset.ToolProperties(extraCLIOptions: linkerOptions)
75+
}
5176
}
5277

5378
public func applyPlatformOptions(
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import XCTest
14+
15+
@testable import SwiftSDKGenerator
16+
17+
final class WebAssemblyRecipeTests: XCTestCase {
18+
func createRecipe() -> WebAssemblyRecipe {
19+
return WebAssemblyRecipe(
20+
hostSwiftPackage: nil,
21+
targetSwiftPackagePath: "./target-toolchain",
22+
wasiSysroot: "./wasi-sysroot",
23+
swiftVersion: "5.10"
24+
)
25+
}
26+
27+
func testToolOptions() {
28+
let recipe = createRecipe()
29+
var toolset = Toolset(rootPath: nil)
30+
recipe.applyPlatformOptions(
31+
toolset: &toolset, targetTriple: Triple("wasm32-unknown-wasi")
32+
)
33+
XCTAssertEqual(toolset.swiftCompiler?.extraCLIOptions, ["-static-stdlib"])
34+
XCTAssertNil(toolset.cCompiler)
35+
XCTAssertNil(toolset.cxxCompiler)
36+
XCTAssertNil(toolset.linker)
37+
}
38+
39+
func testToolOptionsWithThreads() {
40+
let recipe = createRecipe()
41+
var toolset = Toolset(rootPath: nil)
42+
recipe.applyPlatformOptions(
43+
toolset: &toolset, targetTriple: Triple("wasm32-unknown-wasip1-threads")
44+
)
45+
XCTAssertEqual(
46+
toolset.swiftCompiler?.extraCLIOptions,
47+
[
48+
"-static-stdlib",
49+
"-Xcc", "-matomics",
50+
"-Xcc", "-mbulk-memory",
51+
"-Xcc", "-mthread-model",
52+
"-Xcc", "posix",
53+
"-Xcc", "-pthread",
54+
"-Xcc", "-ftls-model=local-exec"
55+
]
56+
)
57+
58+
let ccOptions = [
59+
"-matomics", "-mbulk-memory", "-mthread-model", "posix",
60+
"-pthread", "-ftls-model=local-exec"
61+
]
62+
XCTAssertEqual(toolset.cCompiler?.extraCLIOptions, ccOptions)
63+
XCTAssertEqual(toolset.cxxCompiler?.extraCLIOptions, ccOptions)
64+
XCTAssertEqual(toolset.linker?.extraCLIOptions, [
65+
"--import-memory", "--export-memory", "--shared-memory", "--max-memory=1073741824"
66+
])
67+
}
68+
}

0 commit comments

Comments
 (0)