Skip to content

Commit cb12b0c

Browse files
Merge pull request #72650 from kateinoigakukun/pr-92b3c15ca0f4d772ecfcfa10876535d529bb69b7
build: Add -threads variant of Wasm stdlib build
2 parents 4ab27ff + e73bc12 commit cb12b0c

File tree

6 files changed

+97
-18
lines changed

6 files changed

+97
-18
lines changed

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ macro(configure_sdk_unix name architectures)
423423
endif()
424424
set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SYSROOT_PATH}")
425425
if(SWIFT_ENABLE_WASI_THREADS)
426-
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi-threads")
426+
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasip1-threads")
427427
else()
428428
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi")
429429
endif()

test/lit.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ if run_vers.endswith('-simulator'):
400400
else:
401401
run_environment=''
402402

403+
# Don't distinguish between wasi, wasip1, and so on to use OS=wasi condition in
404+
# the test suites.
405+
if run_os.startswith('wasi'):
406+
run_os = 'wasi'
407+
403408
# Parse the host triple
404409
(host_cpu, host_vendor, host_os, host_vers) = re.match('([^-]+)-([^-]+)-([^0-9-]+)(.*)', config.host_triple).groups()
405410

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: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ 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+
target_triple='wasm32-wasip1-threads')
50+
51+
def _build(self, host_target, thread_model='single', target_triple='wasm32-wasi'):
4752
build_root = os.path.dirname(self.build_dir)
4853
llvm_build_bin_dir = os.path.join(
4954
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
@@ -66,12 +71,14 @@ def build(self, host_target):
6671
# https://github.com/llvm/llvm-project/commit/7dd387d2971d7759cadfffeb2082439f6c7ddd49
6772
'--old-file=check-symbols',
6873
'-C', self.source_dir,
69-
'OBJDIR=' + os.path.join(self.build_dir, 'obj'),
74+
'OBJDIR=' + os.path.join(self.build_dir, 'obj-' + thread_model),
7075
'SYSROOT=' + sysroot_build_dir,
7176
'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root),
7277
'CC=' + os.path.join(clang_tools_path, 'clang'),
7378
'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'),
7479
'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'),
80+
'THREAD_MODEL=' + thread_model,
81+
'TARGET_TRIPLE=' + target_triple,
7582
])
7683

7784
@classmethod
@@ -121,18 +128,31 @@ def should_install(self, host_target):
121128
return False
122129

123130
def build(self, host_target):
131+
self._build(host_target)
132+
133+
def _build(self, host_target, enable_wasi_threads=False):
124134
build_root = os.path.dirname(self.build_dir)
125135
llvm_build_bin_dir = os.path.join(
126136
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
127137
llvm_tools_path = self.args.native_llvm_tools_path or llvm_build_bin_dir
128138
clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir
129139

140+
cmake_has_threads = 'TRUE' if enable_wasi_threads else 'FALSE'
141+
130142
self.cmake_options.define('CMAKE_SYSROOT:PATH',
131143
WASILibc.sysroot_build_path(build_root, host_target))
144+
enable_runtimes = ['libcxx', 'libcxxabi']
145+
if not enable_wasi_threads:
146+
# compiler-rt can be shared between wasi and wasip1-threads
147+
enable_runtimes.append('compiler-rt')
132148
self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING',
133-
'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')
149+
';'.join(enable_runtimes))
150+
151+
libdir_suffix = '/wasm32-wasi'
152+
if enable_wasi_threads:
153+
libdir_suffix = '/wasm32-wasip1-threads'
154+
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', libdir_suffix)
155+
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', libdir_suffix)
136156
self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/')
137157

138158
self.cmake_options.define('COMPILER_RT_DEFAULT_TARGET_ARCH:STRING', 'wasm32')
@@ -157,19 +177,30 @@ def build(self, host_target):
157177
os.path.join(clang_tools_path, 'clang'))
158178
self.cmake_options.define('CMAKE_CXX_COMPILER:STRING',
159179
os.path.join(clang_tools_path, 'clang++'))
180+
181+
c_flags = []
160182
# Explicitly disable exceptions even though it's usually implicitly disabled by
161183
# LIBCXX_ENABLE_EXCEPTIONS because the CMake feature check fails to detect
162184
# -fno-exceptions support in clang due to missing compiler-rt while configuring
163185
# as mono project.
164-
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', '-fno-exceptions')
165-
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+
cxx_flags = ['-fno-exceptions']
187+
if enable_wasi_threads:
188+
c_flags.append('-pthread')
189+
cxx_flags.append('-pthread')
190+
self.cmake_options.define('CMAKE_C_FLAGS:STRING', ' '.join(c_flags))
191+
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', ' '.join(cxx_flags))
192+
193+
if enable_wasi_threads:
194+
target_triple = 'wasm32-wasip1-threads'
195+
else:
196+
target_triple = 'wasm32-wasi'
197+
self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', target_triple)
198+
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', target_triple)
168199

169200
self.cmake_options.define('CXX_SUPPORTS_CXX11:BOOL', 'TRUE')
170201

171-
self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', 'FALSE')
172-
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', 'FALSE')
202+
self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', cmake_has_threads)
203+
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', cmake_has_threads)
173204
self.cmake_options.define('LIBCXX_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
174205
self.cmake_options.define('LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL', 'FALSE')
175206
self.cmake_options.define('LIBCXX_HAS_WIN32_THREAD_API:BOOL', 'FALSE')
@@ -184,8 +215,8 @@ def build(self, host_target):
184215
self.cmake_options.define('LIBCXXABI_ENABLE_EXCEPTIONS:BOOL', 'FALSE')
185216
self.cmake_options.define('LIBCXXABI_ENABLE_SHARED:BOOL', 'FALSE')
186217
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')
218+
self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', cmake_has_threads)
219+
self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', cmake_has_threads)
189220
self.cmake_options.define('LIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
190221
self.cmake_options.define('LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL',
191222
'FALSE')
@@ -201,3 +232,16 @@ def build(self, host_target):
201232
@classmethod
202233
def get_dependencies(cls):
203234
return [WASILibc, llvm.LLVM]
235+
236+
237+
class WasmThreadsLLVMRuntimeLibs(WasmLLVMRuntimeLibs):
238+
def build(self, host_target):
239+
self._build(host_target, enable_wasi_threads=True)
240+
241+
build_root = os.path.dirname(self.build_dir)
242+
wasi_sysroot = WASILibc.sysroot_install_path(build_root)
243+
# Copy compiler-rt os dirs to the WASI variant
244+
os_dir = os.path.join(wasi_sysroot, 'lib', 'wasip1')
245+
if os.path.exists(os_dir):
246+
shell.rmtree(os_dir)
247+
shell.copytree(os.path.join(wasi_sysroot, 'lib', 'wasi'), os_dir)

utils/swift_build_support/swift_build_support/products/wasmstdlib.py

Lines changed: 26 additions & 2 deletions
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 = [
@@ -129,7 +133,7 @@ def test(self, host_target):
129133
wasmkit_build_path = os.path.join(
130134
build_root, '%s-%s' % ('wasmkit', host_target))
131135
wasmkit_bin_path = wasmkit.WasmKit.cli_file_path(wasmkit_build_path)
132-
if not os.path.exists(wasmkit_bin_path):
136+
if not os.path.exists(wasmkit_bin_path) or not self.should_test_executable():
133137
test_target = "check-swift-only_non_executable-wasi-wasm32-custom"
134138
else:
135139
test_target = "check-swift-wasi-wasm32-custom"
@@ -143,6 +147,9 @@ def test(self, host_target):
143147
}
144148
self.test_with_cmake(None, [test_target], self._build_variant, [], test_env=env)
145149

150+
def should_test_executable(self):
151+
return True
152+
146153
@property
147154
def _build_variant(self):
148155
return self.args.build_variant
@@ -170,3 +177,20 @@ def get_dependencies(cls):
170177
wasisysroot.WasmLLVMRuntimeLibs,
171178
wasmkit.WasmKit,
172179
swift.Swift]
180+
181+
182+
class WasmThreadsStdlib(WasmStdlib):
183+
def should_test_executable(self):
184+
# TODO(katei): Enable tests once WasmKit supports WASI threads
185+
return False
186+
187+
def add_extra_cmake_options(self):
188+
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'pthreads')
189+
self.cmake_options.define('SWIFT_STDLIB_EXTRA_C_COMPILE_FLAGS:STRING',
190+
'-mthread-model;posix;-pthread;'
191+
'-ftls-model=local-exec')
192+
self.cmake_options.define('SWIFT_STDLIB_EXTRA_SWIFT_COMPILE_FLAGS:STRING',
193+
'-Xcc;-matomics;-Xcc;-mbulk-memory;'
194+
'-Xcc;-mthread-model;-Xcc;posix;'
195+
'-Xcc;-pthread;-Xcc;-ftls-model=local-exec')
196+
self.cmake_options.define('SWIFT_ENABLE_WASI_THREADS:BOOL', 'TRUE')

0 commit comments

Comments
 (0)