From 325d95176dadb391ad21da0358bc79f8a9786661 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 3 Nov 2019 10:13:17 +0900 Subject: [PATCH 1/8] [WASM] Build ImageInspectionShared even on macOS ImageInspectionShared was built only if host OS is Linux. But it's necessary when primary sdk is WASM. --- stdlib/public/runtime/CMakeLists.txt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index a89665ac1d210..5c88a2a0a5e89 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -88,8 +88,15 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags}) list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS) list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include) -set(sdk "${SWIFT_HOST_VARIANT_SDK}") -if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") +set(image_inspection_shared_sdk) +if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX") + set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}") +elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASM") + set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}") +endif() + +if(NOT "${image_inspection_shared_sdk}" STREQUAL "") + set(sdk "${image_inspection_shared_sdk}") list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp) set(static_binary_lnk_file_list) string(TOLOWER "${sdk}" lowercase_sdk) From ade0e725d714c57dfb3bad7b760e13e1ba7d8368 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 3 Nov 2019 10:17:17 +0900 Subject: [PATCH 2/8] [WASM] Use llvm-ar instead of ranlib on macOS Specify ar binary through extra-cmake-options --- .github/workflows/main.yml | 2 ++ CMakeLists.txt | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ba665370d2b70..ced31ef4f69ec 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,6 +49,7 @@ jobs: -DSWIFT_SDKS='WASM;LINUX' \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-static-stdlib \ @@ -96,6 +97,7 @@ jobs: -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='$sourcedir/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/bin/llvm-ar' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-dynamic-sdk-overlay false \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 20d7da25359b0..7ced37522b3fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -850,11 +850,6 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") #endif() - if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") - # WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld - set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar") - endif() - if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "") set(SWIFT_SDK_WASM_ARCHITECTURES wasm32) endif() From b15f195a264d53e9b459ed07afc7109407fc72a6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 3 Nov 2019 10:27:58 +0900 Subject: [PATCH 3/8] [WASM] Install llvm through HomeBrew to use llvm-ar --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ced31ef4f69ec..098fe90e81013 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -74,7 +74,7 @@ jobs: - uses: actions/checkout@v1 - name: Run a multi-line script run: | - brew install cmake ninja + brew install cmake ninja llvm ./utils/update-checkout --clone --scheme wasm git checkout $GITHUB_SHA export sourcedir=$PWD/.. @@ -97,7 +97,7 @@ jobs: -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ - -DCMAKE_AR='$sourcedir/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/bin/llvm-ar' \ + -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-dynamic-sdk-overlay false \ From b0cad398963b50a73d68fd8c3b4f1ed7c996cf8f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 3 Nov 2019 15:57:02 +0900 Subject: [PATCH 4/8] [WASM] Use llvm-ranlib instead of ranlib of Xcode --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 098fe90e81013..0950dff14adfa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -50,6 +50,7 @@ jobs: -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ + -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-static-stdlib \ @@ -98,6 +99,7 @@ jobs: -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ + -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-dynamic-sdk-overlay false \ From f75e825a2fdb39a50288f9bcc95f32aa1218103e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 4 Nov 2019 02:03:15 +0900 Subject: [PATCH 5/8] [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. --- lib/IRGen/MetadataRequest.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/IRGen/MetadataRequest.cpp b/lib/IRGen/MetadataRequest.cpp index 3627f493cf16a..5387de0db3eef 100644 --- a/lib/IRGen/MetadataRequest.cpp +++ b/lib/IRGen/MetadataRequest.cpp @@ -2268,14 +2268,19 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, IGM.Int32Ty); stringAddrOffset = subIGF.Builder.CreateSExtOrBitCast(stringAddrOffset, IGM.SizeTy); - auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy); - if (IGM.getModule()->getDataLayout().isBigEndian()) { - stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase, - llvm::ConstantInt::get(IGM.SizeTy, 4)); + llvm::Value *stringAddr; + if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + stringAddr = subIGF.Builder.CreateIntToPtr(stringAddrOffset, IGM.Int8PtrTy); + } else { + auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy); + if (IGM.getModule()->getDataLayout().isBigEndian()) { + stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase, + llvm::ConstantInt::get(IGM.SizeTy, 4)); + } + stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, + stringAddrOffset); + stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); } - auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, - stringAddrOffset); - stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); auto call = subIGF.Builder.CreateCall(IGM.getGetTypeByMangledNameInContextFn(), From 86218238d725887e474eb8b3517d963381eee976 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 8 Nov 2019 21:52:21 +0900 Subject: [PATCH 6/8] [WASM] Copy SwiftRT-ELF.cpp into SwiftRT-WASM.cpp --- stdlib/public/runtime/SwiftRT-WASM.cpp | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp index 6812763e9b1e7..92eb7d150e805 100644 --- a/stdlib/public/runtime/SwiftRT-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-WASM.cpp @@ -9,3 +9,77 @@ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// + +#include "ImageInspectionELF.h" + +#include + +// Create empty sections to ensure that the start/stop symbols are synthesized +// by the linker. Otherwise, we may end up with undefined symbol references as +// the linker table section was never constructed. + +#define DECLARE_SWIFT_SECTION(name) \ + __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ + __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; + +extern "C" { +DECLARE_SWIFT_SECTION(swift5_protocols) +DECLARE_SWIFT_SECTION(swift5_protocol_conformances) +DECLARE_SWIFT_SECTION(swift5_type_metadata) + +DECLARE_SWIFT_SECTION(swift5_typeref) +DECLARE_SWIFT_SECTION(swift5_reflstr) +DECLARE_SWIFT_SECTION(swift5_fieldmd) +DECLARE_SWIFT_SECTION(swift5_assocty) +DECLARE_SWIFT_SECTION(swift5_replace) +DECLARE_SWIFT_SECTION(swift5_replac2) +DECLARE_SWIFT_SECTION(swift5_builtin) +DECLARE_SWIFT_SECTION(swift5_capture) +} + +#undef DECLARE_SWIFT_SECTION + +namespace { +static swift::MetadataSections sections{}; +} + +__attribute__((__constructor__)) +static void swift_image_constructor() { +#define SWIFT_SECTION_RANGE(name) \ + { reinterpret_cast(&__start_##name), \ + static_cast(&__stop_##name - &__start_##name) } + + sections = { + swift::CurrentSectionMetadataVersion, + 0, + + nullptr, + nullptr, + + SWIFT_SECTION_RANGE(swift5_protocols), + SWIFT_SECTION_RANGE(swift5_protocol_conformances), + SWIFT_SECTION_RANGE(swift5_type_metadata), + + SWIFT_SECTION_RANGE(swift5_typeref), + SWIFT_SECTION_RANGE(swift5_reflstr), + SWIFT_SECTION_RANGE(swift5_fieldmd), + SWIFT_SECTION_RANGE(swift5_assocty), + SWIFT_SECTION_RANGE(swift5_replace), + SWIFT_SECTION_RANGE(swift5_replac2), + SWIFT_SECTION_RANGE(swift5_builtin), + SWIFT_SECTION_RANGE(swift5_capture), + }; + +#undef SWIFT_SECTION_RANGE + + swift_addNewDSOImage(§ions); +} + +static __attribute__((__used__)) +__attribute__((__section__(".note.swift_reflection_metadata"))) +__attribute__((__aligned__(1))) +struct { + const char MagicString[sizeof(SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING)]; + const swift::MetadataSections *Sections; +} __attribute__((__packed__)) +Note = {SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING, §ions}; From b7e1e39db334a921ed6830fcd9d6e88f0696e7aa Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 8 Nov 2019 21:23:42 +0900 Subject: [PATCH 7/8] [WASM] Emit empty swift5 sections if there aren't --- stdlib/public/runtime/SwiftRT-WASM.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp index 92eb7d150e805..1b4c84f3f2ae9 100644 --- a/stdlib/public/runtime/SwiftRT-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-WASM.cpp @@ -18,8 +18,9 @@ // by the linker. Otherwise, we may end up with undefined symbol references as // the linker table section was never constructed. -#define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ +#define DECLARE_SWIFT_SECTION(name) \ + __attribute__((__used__,__section__(#name),__aligned__(1))) const char __dummy_##name = 0x00; \ + __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; extern "C" { From ca8e6a81c1ba8f28b8da41593acb2970dab79d70 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 8 Nov 2019 21:24:26 +0900 Subject: [PATCH 8/8] [WASM] Remove swift_end and swift_start files --- swift_end.cpp | 30 ------------------------------ swift_start.cpp | 30 ------------------------------ 2 files changed, 60 deletions(-) delete mode 100644 swift_end.cpp delete mode 100644 swift_start.cpp diff --git a/swift_end.cpp b/swift_end.cpp deleted file mode 100644 index b3d476ac41305..0000000000000 --- a/swift_end.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===--- SwiftRT-ELF.cpp --------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include - -// We synthesize the start/stop symbols ourselves. -#define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __stop_##name = (void*)0xfacefeed; \ - -extern "C" { -DECLARE_SWIFT_SECTION(swift5_protocols) -DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -DECLARE_SWIFT_SECTION(swift5_type_metadata) - -DECLARE_SWIFT_SECTION(swift5_typeref) -DECLARE_SWIFT_SECTION(swift5_reflstr) -DECLARE_SWIFT_SECTION(swift5_fieldmd) -DECLARE_SWIFT_SECTION(swift5_assocty) -DECLARE_SWIFT_SECTION(swift5_replace) -DECLARE_SWIFT_SECTION(swift5_replac2) -} diff --git a/swift_start.cpp b/swift_start.cpp deleted file mode 100644 index a9cf1faa390b3..0000000000000 --- a/swift_start.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===--- SwiftRT-ELF.cpp --------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include - -// We synthesize the start/stop symbols ourselves. -#define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __start_##name = (void*)0xdeadbeef; \ - -extern "C" { -DECLARE_SWIFT_SECTION(swift5_protocols) -DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -DECLARE_SWIFT_SECTION(swift5_type_metadata) - -DECLARE_SWIFT_SECTION(swift5_typeref) -DECLARE_SWIFT_SECTION(swift5_reflstr) -DECLARE_SWIFT_SECTION(swift5_fieldmd) -DECLARE_SWIFT_SECTION(swift5_assocty) -DECLARE_SWIFT_SECTION(swift5_replace) -DECLARE_SWIFT_SECTION(swift5_replac2) -}