Skip to content

Enable _Concurrency for Embedded Swift with WASI #79292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
32da3a5
Enable `_Concurrency` for Wasm in Embedded Swift
MaxDesiatov Feb 11, 2025
55f047b
Enable embedded concurrency for Wasm on Linux hosts
MaxDesiatov Feb 12, 2025
d42a8b8
Merge branch 'main' of github.com:apple/swift into maxd/embedded-wasm…
MaxDesiatov Mar 5, 2025
635ef6a
Merge branch 'main' of github.com:apple/swift into maxd/embedded-wasm…
MaxDesiatov Mar 11, 2025
4336eb6
Merge branch 'main' of github.com:apple/swift into maxd/embedded-wasm…
MaxDesiatov Mar 12, 2025
375187d
Build embedded stdlib for `wasm32-unknown-wasip1-wasm` triple
MaxDesiatov Mar 12, 2025
5a3626b
Pass `SWIFT_WASI_SYSROOT_PATH` in `Concurrency/CMakeLists.txt`
MaxDesiatov Mar 12, 2025
8a11cda
Pass `SWIFT_WASI_SYSROOT_PATH` to `swift_build_support/products/swift…
MaxDesiatov Mar 13, 2025
e178bf8
Merge branch 'main' of github.com:apple/swift into maxd/embedded-wasm…
MaxDesiatov Mar 13, 2025
431063b
Swift SDK build work in progress
MaxDesiatov Mar 14, 2025
63d57f4
Fix `Python/python_lint.swift` test
MaxDesiatov Mar 17, 2025
a3e218d
Remove unnecessary diff in `products/swift.py`
MaxDesiatov Mar 17, 2025
ee5d8d0
Revert changes to `build-presets.ini`
MaxDesiatov Mar 17, 2025
f1b616f
Revert changes to `update-checkout-config.json`
MaxDesiatov Mar 17, 2025
a05560b
Remove unnecessary CMake `message(STATUS)` logging
MaxDesiatov Mar 17, 2025
b9de71b
Remove unreachable CMake code change from stdlib/public/Concurrency/C…
kateinoigakukun Mar 17, 2025
59fbab8
CMake: Stop using `-I` for sysroot paths in the WASI stdlib
kateinoigakukun Mar 17, 2025
220ab30
build: Alias wasm32-wasip1 to wasm32-wasi in the sysroot for Embedded…
kateinoigakukun Mar 17, 2025
5973bc8
Merge pull request #80274 from kateinoigakukun/maxd/embedded-wasm-con…
MaxDesiatov Mar 25, 2025
cef01c1
Merge branch 'main' of github.com:apple/swift into maxd/embedded-wasm…
MaxDesiatov Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ set(SWIFT_NATIVE_CLANG_TOOLS_PATH "" CACHE STRING
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "" CACHE STRING
"Path to the directory that contains Swift tools that are executable on the build machine")

set(SWIFT_WASI_SYSROOT_PATH "" CACHE PATH
"Path to the directory that contains WASI Sysroot")

option(SWIFT_STDLIB_ENABLE_SIB_TARGETS
"Should we generate sib targets for the stdlib or not?"
FALSE)
Expand Down
123 changes: 67 additions & 56 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ option(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING
set(SWIFT_EMBEDDED_STDLIB_EXTRA_TARGET_TRIPLES "" CACHE STRING
"List of extra target triples to build the embedded Swift standard library for")

if((NOT SWIFT_HOST_VARIANT STREQUAL "macosx") AND (NOT SWIFT_HOST_VARIANT STREQUAL "linux") AND (NOT SWIFT_HOST_VARIANT STREQUAL "windows"))
if((NOT SWIFT_HOST_VARIANT STREQUAL "macosx") AND
(NOT SWIFT_HOST_VARIANT STREQUAL "linux") AND
(NOT SWIFT_HOST_VARIANT STREQUAL "wasi") AND
(NOT SWIFT_HOST_VARIANT STREQUAL "windows"))
set(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB FALSE)
elseif(BOOTSTRAPPING_MODE STREQUAL "OFF")
set(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB FALSE)
Expand All @@ -168,68 +171,76 @@ endif()
set(EMBEDDED_STDLIB_TARGET_TRIPLES)

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING)
if("ARM" IN_LIST LLVM_TARGETS_TO_BUILD)
if(SWIFT_WASI_SYSROOT_PATH)
# Don't attempt to build any other Embedded Swift stdlib triples
# when building for WASI.
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
# arch module_name target triple
"armv6 armv6-apple-none-macho armv6-apple-none-macho"
"armv6m armv6m-apple-none-macho armv6m-apple-none-macho"
"armv7 armv7-apple-none-macho armv7-apple-none-macho"
"armv7m armv7m-apple-none-macho armv7m-apple-none-macho"
"armv7em armv7em-apple-none-macho armv7em-apple-none-macho"
"wasm32 wasm32-unknown-wasip1-wasm wasm32-unknown-wasip1-wasm"
)
endif()
if("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"arm64 arm64-apple-none-macho arm64-apple-none-macho"
"arm64e arm64e-apple-none-macho arm64e-apple-none-macho"
"arm64 arm64-apple-ios arm64-apple-ios18"
"arm64e arm64e-apple-ios arm64e-apple-ios18"
)
endif()
else()
if("ARM" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
# arch module_name target triple
"armv6 armv6-apple-none-macho armv6-apple-none-macho"
"armv6m armv6m-apple-none-macho armv6m-apple-none-macho"
"armv7 armv7-apple-none-macho armv7-apple-none-macho"
"armv7m armv7m-apple-none-macho armv7m-apple-none-macho"
"armv7em armv7em-apple-none-macho armv7em-apple-none-macho"
)
endif()
if("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"arm64 arm64-apple-none-macho arm64-apple-none-macho"
"arm64e arm64e-apple-none-macho arm64e-apple-none-macho"
"arm64 arm64-apple-ios arm64-apple-ios18"
"arm64e arm64e-apple-ios arm64e-apple-ios18"
)
endif()

# the following are all ELF targets
if("ARM" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"armv4t armv4t-none-none-eabi armv4t-none-none-eabi"
"armv6 armv6-none-none-eabi armv6-none-none-eabi"
"armv6m armv6m-none-none-eabi armv6m-none-none-eabi"
"armv7 armv7-none-none-eabi armv7-none-none-eabi"
"armv7em armv7em-none-none-eabi armv7em-none-none-eabi"
)
endif()
if("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"aarch64 aarch64-none-none-elf aarch64-none-none-elf"
)
endif()
if("RISCV" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"riscv32 riscv32-none-none-eabi riscv32-none-none-eabi"
"riscv64 riscv64-none-none-eabi riscv64-none-none-eabi"
)
endif()
if("X86" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"i686 i686-unknown-none-elf i686-unknown-none-elf"
"x86_64 x86_64-unknown-none-elf x86_64-unknown-none-elf"
)
endif()

# the following are all ELF targets
if("ARM" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"armv4t armv4t-none-none-eabi armv4t-none-none-eabi"
"armv6 armv6-none-none-eabi armv6-none-none-eabi"
"armv6m armv6m-none-none-eabi armv6m-none-none-eabi"
"armv7 armv7-none-none-eabi armv7-none-none-eabi"
"armv7em armv7em-none-none-eabi armv7em-none-none-eabi"
)
endif()
if("AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"aarch64 aarch64-none-none-elf aarch64-none-none-elf"
)
endif()
if("RISCV" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"riscv32 riscv32-none-none-eabi riscv32-none-none-eabi"
"riscv64 riscv64-none-none-eabi riscv64-none-none-eabi"
)
endif()
if("X86" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"i686 i686-unknown-none-elf i686-unknown-none-elf"
"x86_64 x86_64-unknown-none-elf x86_64-unknown-none-elf"
)
endif()
if("WebAssembly" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"wasm32 wasm32-unknown-none-wasm wasm32-unknown-none-wasm"
"wasm64 wasm64-unknown-none-wasm wasm64-unknown-none-wasm"
)
endif()

if("WebAssembly" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"wasm32 wasm32-unknown-none-wasm wasm32-unknown-none-wasm"
"wasm64 wasm64-unknown-none-wasm wasm64-unknown-none-wasm"
)
if("AVR" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"avr avr-none-none-elf avr-none-none-elf"
)
endif()
endif()

if("AVR" IN_LIST LLVM_TARGETS_TO_BUILD)
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"avr avr-none-none-elf avr-none-none-elf"
)
endif()
endif()

if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND NOT SWIFT_WASI_SYSROOT_PATH)
if (SWIFT_HOST_VARIANT STREQUAL "linux")
list(APPEND EMBEDDED_STDLIB_TARGET_TRIPLES
"${SWIFT_HOST_VARIANT_ARCH} ${SWIFT_HOST_VARIANT_ARCH}-unknown-linux-gnu ${SWIFT_HOST_VARIANT_ARCH}-unknown-linux-gnu"
Expand Down
26 changes: 21 additions & 5 deletions stdlib/public/Concurrency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
list(GET list 2 triple)

if (SWIFT_HOST_VARIANT STREQUAL "linux")
if(NOT "${mod}" MATCHES "-linux-gnu$")
if((NOT "${mod}" MATCHES "-linux-gnu$") AND (NOT "${mod}" MATCHES "-wasip1"))
continue()
endif()
set(extra_c_compile_flags)
set(extra_swift_compile_flags)
elseif (SWIFT_HOST_VARIANT STREQUAL "macosx")
if(NOT "${mod}" MATCHES "-macos$")
if((NOT "${mod}" MATCHES "-macos$") AND (NOT "${mod}" MATCHES "-wasip1"))
continue()
endif()
if("${mod}" MATCHES "riscv")
Expand All @@ -260,8 +260,21 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
if("${mod}" MATCHES "armv6m")
continue()
endif()
set(extra_c_compile_flags -D__MACH__ -D__APPLE__ -ffreestanding -stdlib=libc++)
set(extra_swift_compile_flags -Xcc -D__MACH__ -Xcc -D__APPLE__ -Xcc -ffreestanding)

if("${mod}" MATCHES "-macos$")
set(extra_c_compile_flags -D__MACH__ -D__APPLE__ -ffreestanding -stdlib=libc++)
set(extra_swift_compile_flags -Xcc -D__MACH__ -Xcc -D__APPLE__ -Xcc -ffreestanding)
Comment on lines +265 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are -D__MACH__ -D__APPLE__ needed? those should be present with the apple vendor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's unclear to me. Keeping as is, changing it doesn't seem relevant to this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are needed for -apple-none-macho but shouldn't be needed otherwise. the __MACH__ one should only be needed for os ~= darwin but sadly __MACH__ is abused to also mean __MACHO__

endif()

elseif (SWIFT_HOST_VARIANT STREQUAL "wasi")
if("${mod}" MATCHES "-wasip1")
set(extra_c_compile_flags "-I${SWIFT_WASI_SYSROOT_PATH}/include/c++/v1")
if(${SWIFT_WASI_SYSROOT_PATH} MATCHES "threads")
list(APPEND extra_c_compile_flags "-I${SWIFT_WASI_SYSROOT_PATH}/include/wasm32-wasip1-threads")
else()
list(APPEND extra_c_compile_flags "-I${SWIFT_WASI_SYSROOT_PATH}/include/wasm32-wasi")
endif()
endif()
endif()

set(SWIFT_SDK_embedded_THREADING_PACKAGE none)
Expand Down Expand Up @@ -304,7 +317,10 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
COMPONENT "stdlib"
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
set_property(TARGET embedded-concurrency-${mod} PROPERTY OSX_ARCHITECTURES "${arch}")

if(NOT "${arch}" MATCHES "wasm32")
set_property(TARGET embedded-concurrency-${mod} PROPERTY OSX_ARCHITECTURES "${arch}")
endif()
add_dependencies(embedded-concurrency embedded-concurrency-${mod})

# lib/swift/embedded/<triple>/libswift_ConcurrencyDefaultExecutor.a
Expand Down
9 changes: 1 addition & 8 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -1639,13 +1639,6 @@ for host in "${ALL_HOSTS[@]}"; do
)
fi

if [[ ! "${SKIP_BUILD_WASM}" ]]; then
cmake_options=(
"${cmake_options[@]}"
-DSWIFT_WASI_SYSROOT_PATH:STRING="${WASI_SYSROOT}"
)
fi

if [[ $(is_cross_tools_host ${host}) && "${host}" == "openbsd-"* && -n "${OPENBSD_USE_TOOLCHAIN_FILE}" ]]; then
cmake_options=(
"${cmake_options[@]}"
Expand Down Expand Up @@ -2503,7 +2496,7 @@ for host in "${ALL_HOSTS[@]}"; do
cmake_options=(
-DENABLE_SWIFT=YES
${cmake_options[@]}

-DCMAKE_BUILD_TYPE:STRING="${LIBDISPATCH_BUILD_TYPE}"
-DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
-DCMAKE_CXX_COMPILER:PATH="${CLANG_BIN}/clang++"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ def _build_stdlib(self, host_target, target_triple, llvm_cmake_dir):
self.cmake_options.define('SWIFT_STDLIB_TRACING:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_ASLR:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_CONCURRENCY_TRACING:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB:BOOL', 'FALSE')
self.cmake_options.define(
'SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_RUNTIME_CRASH_REPORTER_CLIENT:BOOL', 'FALSE')
Expand All @@ -165,6 +164,10 @@ def _build_stdlib(self, host_target, target_triple, llvm_cmake_dir):
self.cmake_options.define('SWIFT_ENABLE_VOLATILE:BOOL', 'TRUE')
self.cmake_options.define('SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION:BOOL', 'TRUE')

self.cmake_options.define('SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB:BOOL', 'TRUE')
self.cmake_options.define(
'SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB_CROSS_COMPILING', 'TRUE')

self.add_extra_cmake_options()

# Test configuration
Expand Down