Skip to content

Commit

Permalink
build: Add -threads variant of Wasm stdlib build
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kateinoigakukun committed Mar 28, 2024
1 parent 418dd95 commit 4d1b3b7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,14 @@ def compute_product_pipelines(self):
is_enabled=self.args.build_wasmstdlib)
builder.add_product(products.WasmLLVMRuntimeLibs,
is_enabled=self.args.build_wasmstdlib)
builder.add_product(products.WasmThreadsLLVMRuntimeLibs,
is_enabled=self.args.build_wasmstdlib)
builder.add_product(products.WasmKit,
is_enabled=self.args.build_wasmkit)
builder.add_product(products.WasmStdlib,
is_enabled=self.args.build_wasmstdlib)
builder.add_product(products.WasmThreadsStdlib,
is_enabled=self.args.build_wasmstdlib)

# Keep SwiftDriver at last.
# swift-driver's integration with the build scripts is not fully
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
from .swiftpm import SwiftPM
from .swiftsyntax import SwiftSyntax
from .tsan_libdispatch import TSanLibDispatch
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs
from .wasisysroot import WASILibc, WasmLLVMRuntimeLibs, WasmThreadsLLVMRuntimeLibs
from .wasmkit import WasmKit
from .wasmstdlib import WasmStdlib
from .wasmstdlib import WasmStdlib, WasmThreadsStdlib
from .xctest import XCTest
from .zlib import Zlib

Expand Down Expand Up @@ -76,5 +76,7 @@
'WASILibc',
'WasmLLVMRuntimeLibs',
'WasmKit',
'WasmStdlib'
'WasmStdlib',
'WasmThreadsLLVMRuntimeLibs',
'WasmThreadsStdlib',
]
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def should_install(self, host_target):
return False

def build(self, host_target):
self._build(host_target)
self._build(host_target, thread_model='posix')

def _build(self, host_target, thread_model='single'):
build_root = os.path.dirname(self.build_dir)
llvm_build_bin_dir = os.path.join(
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
Expand All @@ -66,12 +70,13 @@ def build(self, host_target):
# https://github.com/llvm/llvm-project/commit/7dd387d2971d7759cadfffeb2082439f6c7ddd49
'--old-file=check-symbols',
'-C', self.source_dir,
'OBJDIR=' + os.path.join(self.build_dir, 'obj'),
'OBJDIR=' + os.path.join(self.build_dir, 'obj-' + thread_model),
'SYSROOT=' + sysroot_build_dir,
'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root),
'CC=' + os.path.join(clang_tools_path, 'clang'),
'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'),
'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'),
'THREAD_MODEL=' + thread_model,
])

@classmethod
Expand Down Expand Up @@ -121,18 +126,26 @@ def should_install(self, host_target):
return False

def build(self, host_target):
self._build(host_target)

def _build(self, host_target, enable_wasi_threads=False):
build_root = os.path.dirname(self.build_dir)
llvm_build_bin_dir = os.path.join(
'..', build_root, '%s-%s' % ('llvm', host_target), 'bin')
llvm_tools_path = self.args.native_llvm_tools_path or llvm_build_bin_dir
clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir

cmake_has_threads = 'TRUE' if enable_wasi_threads else 'FALSE'

self.cmake_options.define('CMAKE_SYSROOT:PATH',
WASILibc.sysroot_build_path(build_root, host_target))
self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING',
'libcxx;libcxxabi;compiler-rt')
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', '/wasm32-wasi')
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', '/wasm32-wasi')
libdir_suffix = '/wasm32-wasi'
if enable_wasi_threads:
libdir_suffix = '/wasm32-wasi-threads'
self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', libdir_suffix)
self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', libdir_suffix)
self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/')

self.cmake_options.define('COMPILER_RT_DEFAULT_TARGET_ARCH:STRING', 'wasm32')
Expand All @@ -157,19 +170,27 @@ def build(self, host_target):
os.path.join(clang_tools_path, 'clang'))
self.cmake_options.define('CMAKE_CXX_COMPILER:STRING',
os.path.join(clang_tools_path, 'clang++'))

c_flags = []
# Explicitly disable exceptions even though it's usually implicitly disabled by
# LIBCXX_ENABLE_EXCEPTIONS because the CMake feature check fails to detect
# -fno-exceptions support in clang due to missing compiler-rt while configuring
# as mono project.
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', '-fno-exceptions')
cxx_flags = ['-fno-exceptions']
if enable_wasi_threads:
c_flags.append('-pthread')
cxx_flags.append('-pthread')
self.cmake_options.define('CMAKE_C_FLAGS:STRING', ' '.join(c_flags))
self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', ' '.join(cxx_flags))

self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', 'wasm32-wasi')
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', 'wasm32-wasi')
target_triple = 'wasm32-wasi-threads' if enable_wasi_threads else 'wasm32-wasi'
self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', target_triple)
self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', target_triple)

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

self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', 'FALSE')
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', 'FALSE')
self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', cmake_has_threads)
self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', cmake_has_threads)
self.cmake_options.define('LIBCXX_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
self.cmake_options.define('LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL', 'FALSE')
self.cmake_options.define('LIBCXX_HAS_WIN32_THREAD_API:BOOL', 'FALSE')
Expand All @@ -184,8 +205,8 @@ def build(self, host_target):
self.cmake_options.define('LIBCXXABI_ENABLE_EXCEPTIONS:BOOL', 'FALSE')
self.cmake_options.define('LIBCXXABI_ENABLE_SHARED:BOOL', 'FALSE')
self.cmake_options.define('LIBCXXABI_SILENT_TERMINATE:BOOL', 'TRUE')
self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', 'FALSE')
self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', 'FALSE')
self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', cmake_has_threads)
self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', cmake_has_threads)
self.cmake_options.define('LIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE')
self.cmake_options.define('LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL',
'FALSE')
Expand All @@ -201,3 +222,7 @@ def build(self, host_target):
@classmethod
def get_dependencies(cls):
return [WASILibc, llvm.LLVM]

class WasmThreadsLLVMRuntimeLibs(WasmLLVMRuntimeLibs):
def build(self, host_target):
self._build(host_target, enable_wasi_threads=True)
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def build(self, host_target):
self.cmake_options.define(
'SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY:BOOL', 'TRUE')
self.cmake_options.define('SWIFT_ENABLE_DISPATCH:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'none')
self.cmake_options.define(
'SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_DLADDR:BOOL', 'FALSE')
Expand All @@ -98,6 +97,8 @@ def build(self, host_target):
os.path.join(self.source_dir, '..',
'swift-experimental-string-processing'))

self.add_extra_cmake_options()

# Test configuration
self.cmake_options.define('SWIFT_INCLUDE_TESTS:BOOL', 'TRUE')
self.cmake_options.define('SWIFT_ENABLE_SOURCEKIT_TESTS:BOOL', 'FALSE')
Expand All @@ -119,6 +120,9 @@ def build(self, host_target):
self.build_with_cmake([], self._build_variant, [],
prefer_native_toolchain=True)

def add_extra_cmake_options(self):
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'none')

def test(self, host_target):
build_root = os.path.dirname(self.build_dir)
bin_paths = [
Expand Down Expand Up @@ -170,3 +174,12 @@ def get_dependencies(cls):
wasisysroot.WasmLLVMRuntimeLibs,
wasmkit.WasmKit,
swift.Swift]

class WasmThreadsStdlib(WasmStdlib):
def add_extra_cmake_options(self):
self.cmake_options.define('SWIFT_THREADING_PACKAGE:STRING', 'pthreads')
self.cmake_options.define('SWIFT_STDLIB_EXTRA_C_COMPILE_FLAGS:STRING',
'-mthread-model;posix;-pthread;-ftls-model=local-exec')
self.cmake_options.define('SWIFT_STDLIB_EXTRA_SWIFT_COMPILE_FLAGS:STRING',
'-Xcc;-matomics;-Xcc;-mbulk-memory;-Xcc;-mthread-model;-Xcc;posix;-Xcc;-pthread;-Xcc;-ftls-model=local-exec')
self.cmake_options.define('SWIFT_ENABLE_WASI_THREADS:BOOL', 'TRUE')

0 comments on commit 4d1b3b7

Please sign in to comment.