Skip to content

Commit 4d1b3b7

Browse files
build: Add -threads variant of Wasm stdlib build
This patch adds a `-threads` variant of the Wasm stdlib build, which has completely different ABI and target triple. Now we build non-threaded and threaded variants when `--build-wasm-stdlib` is enabled.
1 parent 418dd95 commit 4d1b3b7

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,14 @@ def compute_product_pipelines(self):
676676
is_enabled=self.args.build_wasmstdlib)
677677
builder.add_product(products.WasmLLVMRuntimeLibs,
678678
is_enabled=self.args.build_wasmstdlib)
679+
builder.add_product(products.WasmThreadsLLVMRuntimeLibs,
680+
is_enabled=self.args.build_wasmstdlib)
679681
builder.add_product(products.WasmKit,
680682
is_enabled=self.args.build_wasmkit)
681683
builder.add_product(products.WasmStdlib,
682684
is_enabled=self.args.build_wasmstdlib)
685+
builder.add_product(products.WasmThreadsStdlib,
686+
is_enabled=self.args.build_wasmstdlib)
683687

684688
# Keep SwiftDriver at last.
685689
# swift-driver's integration with the build scripts is not fully

utils/swift_build_support/swift_build_support/products/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
from .swiftpm import SwiftPM
3838
from .swiftsyntax import SwiftSyntax
3939
from .tsan_libdispatch import TSanLibDispatch
40-
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs
40+
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs, WasmThreadsLLVMRuntimeLibs
4141
from .wasmkit import WasmKit
42-
from .wasmstdlib import WasmStdlib
42+
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
4343
from .xctest import XCTest
4444
from .zlib import Zlib
4545

@@ -76,5 +76,7 @@
7676
'WASILibc',
7777
'WasmLLVMRuntimeLibs',
7878
'WasmKit',
79-
'WasmStdlib'
79+
'WasmStdlib',
80+
'WasmThreadsLLVMRuntimeLibs',
81+
'WasmThreadsStdlib',
8082
]

utils/swift_build_support/swift_build_support/products/wasisysroot.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def should_install(self, host_target):
4444
return False
4545

4646
def build(self, host_target):
47+
self._build(host_target)
48+
self._build(host_target, thread_model='posix')
49+
50+
def _build(self, host_target, thread_model='single'):
4751
build_root = os.path.dirname(self.build_dir)
4852
llvm_build_bin_dir = os.path.join(
4953
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
@@ -66,12 +70,13 @@ def build(self, host_target):
6670
# https://github.com/llvm/llvm-project/commit/7dd387d2971d7759cadfffeb2082439f6c7ddd49
6771
'--old-file=check-symbols',
6872
'-C', self.source_dir,
69-
'OBJDIR=' + os.path.join(self.build_dir, 'obj'),
73+
'OBJDIR=' + os.path.join(self.build_dir, 'obj-' + thread_model),
7074
'SYSROOT=' + sysroot_build_dir,
7175
'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root),
7276
'CC=' + os.path.join(clang_tools_path, 'clang'),
7377
'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'),
7478
'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'),
79+
'THREAD_MODEL=' + thread_model,
7580
])
7681

7782
@classmethod
@@ -121,18 +126,26 @@ def should_install(self, host_target):
121126
return False
122127

123128
def build(self, host_target):
129+
self._build(host_target)
130+
131+
def _build(self, host_target, enable_wasi_threads=False):
124132
build_root = os.path.dirname(self.build_dir)
125133
llvm_build_bin_dir = os.path.join(
126134
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
127135
llvm_tools_path = self.args.native_llvm_tools_path or llvm_build_bin_dir
128136
clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir
129137

138+
cmake_has_threads = 'TRUE' if enable_wasi_threads else 'FALSE'
139+
130140
self.cmake_options.define('CMAKE_SYSROOT:PATH',
131141
WASILibc.sysroot_build_path(build_root, host_target))
132142
self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING',
133143
'libcxx;libcxxabi;compiler-rt')
134-
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', '/wasm32-wasi')
135-
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', '/wasm32-wasi')
144+
libdir_suffix = '/wasm32-wasi'
145+
if enable_wasi_threads:
146+
libdir_suffix = '/wasm32-wasi-threads'
147+
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', libdir_suffix)
148+
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', libdir_suffix)
136149
self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/')
137150

138151
self.cmake_options.define('COMPILER_RT_DEFAULT_TARGET_ARCH:STRING', 'wasm32')
@@ -157,19 +170,27 @@ def build(self, host_target):
157170
os.path.join(clang_tools_path, 'clang'))
158171
self.cmake_options.define('CMAKE_CXX_COMPILER:STRING',
159172
os.path.join(clang_tools_path, 'clang++'))
173+
174+
c_flags = []
160175
# Explicitly disable exceptions even though it's usually implicitly disabled by
161176
# LIBCXX_ENABLE_EXCEPTIONS because the CMake feature check fails to detect
162177
# -fno-exceptions support in clang due to missing compiler-rt while configuring
163178
# as mono project.
164-
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', '-fno-exceptions')
179+
cxx_flags = ['-fno-exceptions']
180+
if enable_wasi_threads:
181+
c_flags.append('-pthread')
182+
cxx_flags.append('-pthread')
183+
self.cmake_options.define('CMAKE_C_FLAGS:STRING', ' '.join(c_flags))
184+
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', ' '.join(cxx_flags))
165185

166-
self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', 'wasm32-wasi')
167-
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', 'wasm32-wasi')
186+
target_triple = 'wasm32-wasi-threads' if enable_wasi_threads else 'wasm32-wasi'
187+
self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', target_triple)
188+
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', target_triple)
168189

169190
self.cmake_options.define('CXX_SUPPORTS_CXX11:BOOL', 'TRUE')
170191

171-
self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', 'FALSE')
172-
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', 'FALSE')
192+
self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', cmake_has_threads)
193+
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', cmake_has_threads)
173194
self.cmake_options.define('LIBCXX_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
174195
self.cmake_options.define('LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL', 'FALSE')
175196
self.cmake_options.define('LIBCXX_HAS_WIN32_THREAD_API:BOOL', 'FALSE')
@@ -184,8 +205,8 @@ def build(self, host_target):
184205
self.cmake_options.define('LIBCXXABI_ENABLE_EXCEPTIONS:BOOL', 'FALSE')
185206
self.cmake_options.define('LIBCXXABI_ENABLE_SHARED:BOOL', 'FALSE')
186207
self.cmake_options.define('LIBCXXABI_SILENT_TERMINATE:BOOL', 'TRUE')
187-
self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', 'FALSE')
188-
self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', 'FALSE')
208+
self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', cmake_has_threads)
209+
self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', cmake_has_threads)
189210
self.cmake_options.define('LIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
190211
self.cmake_options.define('LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL',
191212
'FALSE')
@@ -201,3 +222,7 @@ def build(self, host_target):
201222
@classmethod
202223
def get_dependencies(cls):
203224
return [WASILibc, llvm.LLVM]
225+
226+
class WasmThreadsLLVMRuntimeLibs(WasmLLVMRuntimeLibs):
227+
def build(self, host_target):
228+
self._build(host_target, enable_wasi_threads=True)

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def build(self, host_target):
8585
self.cmake_options.define(
8686
'SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY:BOOL', 'TRUE')
8787
self.cmake_options.define('SWIFT_ENABLE_DISPATCH:BOOL', 'FALSE')
88-
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'none')
8988
self.cmake_options.define(
9089
'SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING:BOOL', 'FALSE')
9190
self.cmake_options.define('SWIFT_STDLIB_HAS_DLADDR:BOOL', 'FALSE')
@@ -98,6 +97,8 @@ def build(self, host_target):
9897
os.path.join(self.source_dir, '..',
9998
'swift-experimental-string-processing'))
10099

100+
self.add_extra_cmake_options()
101+
101102
# Test configuration
102103
self.cmake_options.define('SWIFT_INCLUDE_TESTS:BOOL', 'TRUE')
103104
self.cmake_options.define('SWIFT_ENABLE_SOURCEKIT_TESTS:BOOL', 'FALSE')
@@ -119,6 +120,9 @@ def build(self, host_target):
119120
self.build_with_cmake([], self._build_variant, [],
120121
prefer_native_toolchain=True)
121122

123+
def add_extra_cmake_options(self):
124+
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'none')
125+
122126
def test(self, host_target):
123127
build_root = os.path.dirname(self.build_dir)
124128
bin_paths = [
@@ -170,3 +174,12 @@ def get_dependencies(cls):
170174
wasisysroot.WasmLLVMRuntimeLibs,
171175
wasmkit.WasmKit,
172176
swift.Swift]
177+
178+
class WasmThreadsStdlib(WasmStdlib):
179+
def add_extra_cmake_options(self):
180+
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'pthreads')
181+
self.cmake_options.define('SWIFT_STDLIB_EXTRA_C_COMPILE_FLAGS:STRING',
182+
'-mthread-model;posix;-pthread;-ftls-model=local-exec')
183+
self.cmake_options.define('SWIFT_STDLIB_EXTRA_SWIFT_COMPILE_FLAGS:STRING',
184+
'-Xcc;-matomics;-Xcc;-mbulk-memory;-Xcc;-mthread-model;-Xcc;posix;-Xcc;-pthread;-Xcc;-ftls-model=local-exec')
185+
self.cmake_options.define('SWIFT_ENABLE_WASI_THREADS:BOOL', 'TRUE')

0 commit comments

Comments
 (0)