Skip to content

Commit 15a01df

Browse files
Swift Runtime on WebAssembly (#11)
* [WASM] Build ImageInspectionShared even on macOS ImageInspectionShared was built only if host OS is Linux. But it's necessary when primary sdk is WASM. * [WASM] Use llvm-ar instead of ranlib on macOS Specify ar binary through extra-cmake-options * [WASM] Install llvm through HomeBrew to use llvm-ar * [WASM] Use llvm-ranlib instead of ranlib of Xcode * [WASM] Read offset as pointer when target is wasm Because WASM doesn't support relative pointer, compiler emit direct address instead of offset from current address. * [WASM] Copy SwiftRT-ELF.cpp into SwiftRT-WASM.cpp * [WASM] Emit empty swift5 sections if there aren't * [WASM] Remove swift_end and swift_start files
1 parent cc0770b commit 15a01df

File tree

7 files changed

+101
-72
lines changed

7 files changed

+101
-72
lines changed

Diff for: .github/workflows/main.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
-DSWIFT_SDKS='WASM;LINUX' \
5050
-DSWIFT_BUILD_SOURCEKIT=FALSE \
5151
-DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \
52+
-DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \
53+
-DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \
5254
" \
5355
--build-stdlib-deployment-targets "wasm-wasm32" \
5456
--build-swift-static-stdlib \
@@ -73,7 +75,7 @@ jobs:
7375
- uses: actions/checkout@v1
7476
- name: Run a multi-line script
7577
run: |
76-
brew install cmake ninja
78+
brew install cmake ninja llvm
7779
./utils/update-checkout --clone --scheme wasm
7880
git checkout $GITHUB_SHA
7981
export sourcedir=$PWD/..
@@ -96,6 +98,8 @@ jobs:
9698
-DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \
9799
-DSWIFT_BUILD_SOURCEKIT=FALSE \
98100
-DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \
101+
-DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \
102+
-DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \
99103
" \
100104
--build-stdlib-deployment-targets "wasm-wasm32" \
101105
--build-swift-dynamic-sdk-overlay false \

Diff for: CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,6 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM")
829829
# message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android")
830830
#endif()
831831

832-
if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
833-
# WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld
834-
set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar")
835-
endif()
836-
837832
if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "")
838833
set(SWIFT_SDK_WASM_ARCHITECTURES wasm32)
839834
endif()

Diff for: lib/IRGen/MetadataRequest.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -2288,10 +2288,18 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type,
22882288
IGM.Int32Ty);
22892289
stringAddrOffset = subIGF.Builder.CreateSExtOrBitCast(stringAddrOffset,
22902290
IGM.SizeTy);
2291-
auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy);
2292-
if (IGM.getModule()->getDataLayout().isBigEndian()) {
2293-
stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase,
2294-
llvm::ConstantInt::get(IGM.SizeTy, 4));
2291+
llvm::Value *stringAddr;
2292+
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
2293+
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddrOffset, IGM.Int8PtrTy);
2294+
} else {
2295+
auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy);
2296+
if (IGM.getModule()->getDataLayout().isBigEndian()) {
2297+
stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase,
2298+
llvm::ConstantInt::get(IGM.SizeTy, 4));
2299+
}
2300+
stringAddr = subIGF.Builder.CreateAdd(stringAddrBase,
2301+
stringAddrOffset);
2302+
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy);
22952303
}
22962304
auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase,
22972305
stringAddrOffset);

Diff for: stdlib/public/runtime/CMakeLists.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
8989
list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS)
9090
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include)
9191

92-
set(sdk "${SWIFT_HOST_VARIANT_SDK}")
93-
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
92+
set(image_inspection_shared_sdk)
93+
if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX")
94+
set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}")
95+
elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASM")
96+
set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}")
97+
endif()
98+
99+
if(NOT "${image_inspection_shared_sdk}" STREQUAL "")
100+
set(sdk "${image_inspection_shared_sdk}")
94101
list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp)
95102
set(static_binary_lnk_file_list)
96103
string(TOLOWER "${sdk}" lowercase_sdk)

Diff for: stdlib/public/runtime/SwiftRT-WASM.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,78 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
13+
#include "ImageInspectionELF.h"
14+
15+
#include <cstddef>
16+
17+
// Create empty sections to ensure that the start/stop symbols are synthesized
18+
// by the linker. Otherwise, we may end up with undefined symbol references as
19+
// the linker table section was never constructed.
20+
21+
#define DECLARE_SWIFT_SECTION(name) \
22+
__attribute__((__used__,__section__(#name),__aligned__(1))) const char __dummy_##name = 0x00; \
23+
__attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \
24+
__attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name;
25+
26+
extern "C" {
27+
DECLARE_SWIFT_SECTION(swift5_protocols)
28+
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
29+
DECLARE_SWIFT_SECTION(swift5_type_metadata)
30+
31+
DECLARE_SWIFT_SECTION(swift5_typeref)
32+
DECLARE_SWIFT_SECTION(swift5_reflstr)
33+
DECLARE_SWIFT_SECTION(swift5_fieldmd)
34+
DECLARE_SWIFT_SECTION(swift5_assocty)
35+
DECLARE_SWIFT_SECTION(swift5_replace)
36+
DECLARE_SWIFT_SECTION(swift5_replac2)
37+
DECLARE_SWIFT_SECTION(swift5_builtin)
38+
DECLARE_SWIFT_SECTION(swift5_capture)
39+
}
40+
41+
#undef DECLARE_SWIFT_SECTION
42+
43+
namespace {
44+
static swift::MetadataSections sections{};
45+
}
46+
47+
__attribute__((__constructor__))
48+
static void swift_image_constructor() {
49+
#define SWIFT_SECTION_RANGE(name) \
50+
{ reinterpret_cast<uintptr_t>(&__start_##name), \
51+
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }
52+
53+
sections = {
54+
swift::CurrentSectionMetadataVersion,
55+
0,
56+
57+
nullptr,
58+
nullptr,
59+
60+
SWIFT_SECTION_RANGE(swift5_protocols),
61+
SWIFT_SECTION_RANGE(swift5_protocol_conformances),
62+
SWIFT_SECTION_RANGE(swift5_type_metadata),
63+
64+
SWIFT_SECTION_RANGE(swift5_typeref),
65+
SWIFT_SECTION_RANGE(swift5_reflstr),
66+
SWIFT_SECTION_RANGE(swift5_fieldmd),
67+
SWIFT_SECTION_RANGE(swift5_assocty),
68+
SWIFT_SECTION_RANGE(swift5_replace),
69+
SWIFT_SECTION_RANGE(swift5_replac2),
70+
SWIFT_SECTION_RANGE(swift5_builtin),
71+
SWIFT_SECTION_RANGE(swift5_capture),
72+
};
73+
74+
#undef SWIFT_SECTION_RANGE
75+
76+
swift_addNewDSOImage(&sections);
77+
}
78+
79+
static __attribute__((__used__))
80+
__attribute__((__section__(".note.swift_reflection_metadata")))
81+
__attribute__((__aligned__(1)))
82+
struct {
83+
const char MagicString[sizeof(SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING)];
84+
const swift::MetadataSections *Sections;
85+
} __attribute__((__packed__))
86+
Note = {SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING, &sections};

Diff for: swift_end.cpp

-30
This file was deleted.

Diff for: swift_start.cpp

-30
This file was deleted.

0 commit comments

Comments
 (0)