Skip to content

Commit 0a51091

Browse files
authored
Merge branch 'swiftwasm' into master
2 parents 5243d3c + eef11bc commit 0a51091

File tree

154 files changed

+2439
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+2439
-140
lines changed

.github/pull.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "1"
2+
rules:
3+
- base: swiftwasm
4+
upstream: master
5+
mergeMethod: merge
6+
- base: master
7+
upstream: apple:master
8+
mergeMethod: hardreset
9+
label: ":arrow_heading_down: Upstream Tracking"

.github/workflows/main.yml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- swiftwasm
7+
pull_request:
8+
branches:
9+
- swiftwasm
10+
11+
jobs:
12+
linux_build:
13+
timeout-minutes: 0
14+
runs-on: ubuntu-18.04
15+
16+
steps:
17+
- name: Free disk space
18+
run: |
19+
df -h
20+
sudo swapoff -a
21+
sudo rm -f /swapfile
22+
sudo apt clean
23+
docker rmi $(docker image ls -aq)
24+
df -h
25+
- uses: actions/checkout@v1
26+
with:
27+
path: swift
28+
- uses: actions/cache@v1
29+
with:
30+
path: ../build-cache
31+
key: ${{ runner.os }}-sccache-v4
32+
- name: Build Linux installable archive
33+
run: |
34+
./utils/webassembly/ci.sh
35+
echo "Cleanup build directory to free disk space"
36+
rm -rf ../build
37+
- name: Upload Linux installable archive
38+
uses: actions/upload-artifact@v1
39+
with:
40+
name: linux-installable
41+
path: ../swift-wasm-DEVELOPMENT-SNAPSHOT-linux.tar.gz
42+
# - name: Pack test results
43+
# run: tar cJf swift-test-results.tar.gz ../build/*/swift-linux-x86_64/swift-test-results
44+
# - name: Upload test results
45+
# uses: actions/upload-artifact@v1
46+
# with:
47+
# name: linux-test-results
48+
# path: ./swift-test-results.tar.gz
49+
50+
macos_build:
51+
timeout-minutes: 0
52+
runs-on: macos-latest
53+
54+
steps:
55+
- uses: actions/checkout@v1
56+
with:
57+
path: swift
58+
- uses: actions/cache@v1
59+
with:
60+
path: ../build-cache
61+
key: ${{ runner.os }}-sccache-v4
62+
- name: Build macOS installable archive
63+
run: ./utils/webassembly/ci.sh
64+
- name: Upload macOS installable archive
65+
uses: actions/upload-artifact@v1
66+
with:
67+
name: macos-installable
68+
path: ../swift-wasm-DEVELOPMENT-SNAPSHOT-osx.tar.gz
69+
- name: Upload packaging scripts
70+
uses: actions/upload-artifact@v1
71+
with:
72+
name: packaging-scripts
73+
path: utils/webassembly
74+
- name: Pack test results
75+
run: tar cJf swift-test-results.tar.gz ../build/*/swift-macosx-x86_64/swift-test-results
76+
- name: Upload test results
77+
uses: actions/upload-artifact@v1
78+
with:
79+
name: macos-test-results
80+
path: ./swift-test-results.tar.gz
81+
macos_smoke_test:
82+
name: Compile hello.swift on macOS
83+
runs-on: macos-latest
84+
needs: macos_build
85+
steps:
86+
- name: Download installable macOS archive
87+
uses: actions/download-artifact@v1
88+
with:
89+
name: macos-installable
90+
- name: Build hello.wasm
91+
shell: bash
92+
run: |
93+
set -x
94+
tar xf $(find . -name "swift-wasm-*.tar.gz" -type f)
95+
TOOLCHAIN_PATH=$(find . -name "swift-wasm-*" -type d)
96+
echo 'print("Hello, world!")' > hello.swift
97+
$TOOLCHAIN_PATH/usr/bin/swiftc \
98+
-target wasm32-unknown-wasi \
99+
-sdk $TOOLCHAIN_PATH/usr/share/wasi-sysroot \
100+
hello.swift -o hello.wasm && \
101+
echo "Successfully linked hello.wasm"
102+
103+
- name: Upload hello.wasm compiled with macOS package
104+
uses: actions/upload-artifact@v1
105+
with:
106+
name: macos-hello.wasm
107+
path: hello.wasm
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Nightly distribution
2+
on:
3+
schedule:
4+
- cron: '0 0 * * *'
5+
jobs:
6+
nightly-distribution:
7+
runs-on: macos-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
- run: ./utils/webassembly/distribute-latest-toolchain.sh
11+
env:
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
if: github.ref == 'refs/heads/swiftwasm'

CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING
265265
"Path on an Android device where build products will be pushed. These are used when running the test suite against the device")
266266

267267
#
268-
# User-configurable ICU specific options for Android, FreeBSD, Linux and Haiku.
268+
# User-configurable ICU specific options for Android, FreeBSD, Linux, Haiku, and WebAssembly.
269269
#
270270

271-
foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU)
272-
foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;x86_64)
271+
foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASI)
272+
foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64)
273273
set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING
274274
"Path to a directory containing the icuuc library for ${sdk}")
275275
set(SWIFT_${sdk}_${arch}_ICU_UC_INCLUDE "" CACHE STRING
@@ -601,6 +601,8 @@ else()
601601
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
602602
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
603603
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
604+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
605+
set(SWIFT_HOST_VARIANT_ARCH_default "wasm32")
604606
# FIXME: Only matches v6l/v7l - by far the most common variants
605607
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
606608
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
@@ -794,6 +796,18 @@ if(swift_build_windows AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
794796
configure_sdk_windows("Windows" "msvc" "${SWIFT_SDK_WINDOWS_ARCHITECTURES}")
795797
endif()
796798

799+
# Should we cross-compile the standard library for WebAssembly (WASI)?
800+
is_sdk_requested(WASI swift_build_wasm)
801+
if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI")
802+
if(SWIFT_BUILD_DYNAMIC_SDK_OVERLAY OR SWIFT_BUILD_DYNAMIC_STDLIB)
803+
message(FATAL_ERROR "Unable to build dynamic overlay/stdlib for WASI. WASM does not support dynamic linking.")
804+
endif()
805+
if("${SWIFT_SDK_WASI_ARCHITECTURES}" STREQUAL "")
806+
set(SWIFT_SDK_WASI_ARCHITECTURES wasm32)
807+
endif()
808+
configure_sdk_unix("WASI" "${SWIFT_SDK_WASI_ARCHITECTURES}")
809+
endif()
810+
797811
if("${SWIFT_SDKS}" STREQUAL "")
798812
set(SWIFT_SDKS "${SWIFT_CONFIGURED_SDKS}")
799813
endif()

cmake/modules/AddSwift.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ function(_add_host_variant_c_compile_flags)
351351
list(APPEND result "SHELL:${CMAKE_INCLUDE_SYSTEM_FLAG_C}${path}")
352352
endforeach()
353353
list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}")
354+
elseif("${CFLAGS_SDK}" STREQUAL "WASI")
355+
list(APPEND result "-D_WASI_EMULATED_MMAN")
354356
endif()
355357

356358
if("${CFLAGS_SDK}" STREQUAL "LINUX")
@@ -437,6 +439,8 @@ function(_add_host_variant_link_flags)
437439
foreach(path IN LISTS ${LFLAGS_ARCH}_LIB)
438440
list(APPEND library_search_directories ${path})
439441
endforeach()
442+
elseif("${LFLAGS_SDK}" STREQUAL "WASI")
443+
list(APPEND result "-Wl,wasi-emulated-mman")
440444
else()
441445
# If lto is enabled, we need to add the object path flag so that the LTO code
442446
# generator leaves the intermediate object file in a place where it will not
@@ -669,6 +673,12 @@ function(_add_swift_host_library_single target)
669673
list(APPEND c_compile_flags -D_WINDLL)
670674
endif()
671675
endif()
676+
# Double-check that we're not trying to build a dynamic library for WASM.
677+
if(SWIFTLIB_SINGLE_SDK MATCHES WASM)
678+
if(libkind STREQUAL SHARED)
679+
message(FATAL_ERROR "WASM does not support shared libraries.")
680+
endif()
681+
endif()
672682
_add_host_variant_link_flags(
673683
SDK "${SWIFT_HOST_VARIANT_SDK}"
674684
ARCH "${SWIFT_HOST_VARIANT_ARCH}"

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ macro(configure_sdk_unix name architectures)
229229
set(SWIFT_SDK_${prefix}_ARCHITECTURES "${architectures}")
230230
if("${prefix}" STREQUAL "CYGWIN")
231231
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "COFF")
232+
elseif("${prefix}" STREQUAL "WASI")
233+
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "WASM")
232234
else()
233235
set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "ELF")
234236
endif()
@@ -354,6 +356,14 @@ macro(configure_sdk_unix name architectures)
354356
message(FATAL_ERROR "unsupported arch for Haiku: ${arch}")
355357
endif()
356358
set(SWIFT_SDK_HAIKU_ARCH_x86_64_TRIPLE "x86_64-unknown-haiku")
359+
elseif("${prefix}" STREQUAL "WASI")
360+
if(NOT arch STREQUAL wasm32)
361+
message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}")
362+
endif()
363+
set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot")
364+
set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi")
365+
set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot/include")
366+
set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot/include")
357367
else()
358368
message(FATAL_ERROR "unknown Unix OS: ${prefix}")
359369
endif()

cmake/modules/SwiftSetIfArchBitness.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function(set_if_arch_bitness var_name)
1212
"${SIA_ARCH}" STREQUAL "armv6" OR
1313
"${SIA_ARCH}" STREQUAL "armv7" OR
1414
"${SIA_ARCH}" STREQUAL "armv7k" OR
15-
"${SIA_ARCH}" STREQUAL "armv7s")
15+
"${SIA_ARCH}" STREQUAL "armv7s" OR
16+
"${SIA_ARCH}" STREQUAL "wasm32")
1617
set("${var_name}" "${SIA_CASE_32_BIT}" PARENT_SCOPE)
1718
elseif("${SIA_ARCH}" STREQUAL "x86_64" OR
1819
"${SIA_ARCH}" STREQUAL "amd64" OR

include/swift/Basic/Lazy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
#include "swift/Basic/Malloc.h"
2525
#include "swift/Basic/type_traits.h"
2626

27+
#ifdef __wasi__
28+
inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) {
29+
switch (*flag) {
30+
case 0:
31+
*flag = 1;
32+
func(context);
33+
return;
34+
case 1:
35+
return;
36+
default:
37+
assert(false && "wasi_call_once got invalid flag");
38+
abort();
39+
}
40+
}
41+
42+
#endif
43+
2744
namespace swift {
2845

2946
#ifdef __APPLE__
@@ -38,6 +55,10 @@ namespace swift {
3855
using OnceToken_t = unsigned long;
3956
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
4057
_swift_once_f(&TOKEN, CONTEXT, FUNC)
58+
#elif defined(__wasi__)
59+
using OnceToken_t = int;
60+
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
61+
::wasi_call_once(&TOKEN, CONTEXT, FUNC)
4162
#else
4263
using OnceToken_t = std::once_flag;
4364
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \

include/swift/Basic/RelativePointer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
146146
std::is_signed<Offset>::value,
147147
"offset type should be signed integer");
148148

149+
#ifdef __wasm__
150+
// WebAssembly: hack: disable relative pointers
151+
return (uintptr_t)(intptr_t)offset;
152+
#endif
153+
149154
auto base = reinterpret_cast<uintptr_t>(basePtr);
150155
// We want to do wrapping arithmetic, but with a sign-extended
151156
// offset. To do this in C, we need to do signed promotion to get
@@ -164,6 +169,13 @@ static inline Offset measureRelativeOffset(A *referent, B *base) {
164169
static_assert(std::is_integral<Offset>::value &&
165170
std::is_signed<Offset>::value,
166171
"offset type should be signed integer");
172+
#ifdef __wasm__
173+
// WebAssembly: hack: disable relative pointers
174+
auto offset = (Offset)(uintptr_t)referent;
175+
assert((intptr_t)offset == (intptr_t)(uintptr_t)referent
176+
&& "pointer too large to fit in offset type");
177+
return offset;
178+
#endif
167179

168180
auto distance = (uintptr_t)referent - (uintptr_t)base;
169181
// Truncate as unsigned, then wrap around to signed.

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
637637
//// define what these will be.
638638
/// \returns the demangled name. Returns nullptr if the input String is not a
639639
/// Swift mangled name.
640+
SWIFT_CC(swift)
640641
SWIFT_RUNTIME_EXPORT
641642
char *swift_demangle(const char *mangledName,
642643
size_t mangledNameLength,

include/swift/IRGen/Linking.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,15 @@ class ApplyIRLinkage {
11891189
// TODO: BFD and gold do not handle COMDATs properly
11901190
if (Triple.isOSBinFormatELF())
11911191
return;
1192+
// WebAssembly: hack: comdat + custom section = explosion
1193+
// the comdat code assumes section name would be unique for each comdat
1194+
// this doesn't happen for metadata.
1195+
// And avoid to create GOT because wasm32 doesn't support
1196+
// dynamic linking yet
1197+
if (Triple.isOSBinFormatWasm()) {
1198+
GV->setDSOLocal(true);
1199+
return;
1200+
}
11921201

11931202
if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage ||
11941203
IRL.Linkage == llvm::GlobalValue::WeakODRLinkage)

include/swift/Runtime/HeapObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n);
205205

206206
// Refcounting observation hooks for memory tools. Don't use these.
207207
SWIFT_RUNTIME_EXPORT
208+
SWIFT_CC(swift)
208209
size_t swift_retainCount(HeapObject *object);
209210
SWIFT_RUNTIME_EXPORT
211+
SWIFT_CC(swift)
210212
size_t swift_unownedRetainCount(HeapObject *object);
211213
SWIFT_RUNTIME_EXPORT
214+
SWIFT_CC(swift)
212215
size_t swift_weakRetainCount(HeapObject *object);
213216

214217
/// Is this pointer a non-null unique reference to an object

include/swift/Runtime/MutexPThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef pthread_cond_t ConditionHandle;
2626
typedef pthread_mutex_t MutexHandle;
2727
typedef pthread_rwlock_t ReadWriteLockHandle;
2828

29-
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__)
29+
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__wasi__)
3030
// At the moment CYGWIN pthreads implementation doesn't support the use of
3131
// constexpr for static allocation versions. The way they define things
3232
// results in a reinterpret_cast which violates constexpr. Similarly, Android's

include/swift/Runtime/Once.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ typedef std::once_flag swift_once_t;
4444
/// extent of type swift_once_t.
4545
SWIFT_RUNTIME_EXPORT
4646
void swift_once(swift_once_t *predicate, void (*fn)(void *), void *context);
47+
#ifdef __wasm__
48+
// WebAssembly: hack
49+
void swift_once_real(swift_once_t *predicate, void (*fn)(void *), void *context);
50+
#endif
4751

4852
}
4953

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
599599
});
600600
}
601601

602+
if (triple.isOSWASI()) {
603+
invocationArgStrs.insert(invocationArgStrs.end(), {"-D_WASI_EMULATED_MMAN"});
604+
}
605+
602606
if (triple.isOSWindows()) {
603607
switch (triple.getArch()) {
604608
default: llvm_unreachable("unsupported Windows architecture");

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ namespace {
709709
}
710710

711711
static const llvm::StringLiteral vaListNames[] = {
712-
"va_list", "__gnuc_va_list", "__va_list"
712+
"va_list", "__gnuc_va_list", "__isoc_va_list", "__va_list"
713713
};
714714

715715
ImportHint hint = ImportHint::None;

lib/ClangImporter/MappedTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ MAP_STDLIB_TYPE("u_int64_t", UnsignedInt, 64, "UInt64", false, DoNothing)
128128
// There's an explicit workaround in ImportType.cpp's VisitDecayedType for that.
129129
MAP_STDLIB_TYPE("va_list", VaList, 0, "CVaListPointer", false, DoNothing)
130130
MAP_STDLIB_TYPE("__gnuc_va_list", VaList, 0, "CVaListPointer", false, DoNothing)
131+
MAP_STDLIB_TYPE("__isoc_va_list", VaList, 0, "CVaListPointer", false, DoNothing)
131132
MAP_STDLIB_TYPE("__va_list", VaList, 0, "CVaListPointer", false, DoNothing)
132133

133134
// libkern/OSTypes.h types.

0 commit comments

Comments
 (0)