From 6fd3ae7812b21133e4a40434a719d1edbca04e20 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 14 Sep 2021 19:38:46 +0900 Subject: [PATCH 1/2] [Wasm] Specify the least valid pointer for wasm32 WebAssembly doesn't reserve low addresses but without "extra inhabitants" of the pointer representation, runtime performance and memory footprint are worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to reserve low 1KB. --- lib/Driver/WebAssemblyToolChains.cpp | 6 ++++-- lib/IRGen/SwiftTargetInfo.cpp | 10 ++++++++++ stdlib/public/SwiftShims/HeapObject.h | 16 ++++++++++++++++ stdlib/public/SwiftShims/System.h | 8 ++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/Driver/WebAssemblyToolChains.cpp b/lib/Driver/WebAssemblyToolChains.cpp index b4095ce61f36b..52c9a2cb06dc2 100644 --- a/lib/Driver/WebAssemblyToolChains.cpp +++ b/lib/Driver/WebAssemblyToolChains.cpp @@ -1,5 +1,4 @@ -//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) -//------===// +//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) ------===// // // This source file is part of the Swift.org open source project // @@ -189,6 +188,9 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, // worse. So assume that compiler driver uses wasm-ld and --global-base=1024 // to reserve low 1KB. Arguments.push_back("-Xlinker"); + Arguments.push_back(context.Args.MakeArgString( + Twine("--global-base=") + + std::to_string(SWIFT_ABI_WASM32_LEAST_VALID_POINTER))); // These custom arguments should be right before the object file at the end. context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group); diff --git a/lib/IRGen/SwiftTargetInfo.cpp b/lib/IRGen/SwiftTargetInfo.cpp index 97b70d6d75c0a..1832031cbf261 100644 --- a/lib/IRGen/SwiftTargetInfo.cpp +++ b/lib/IRGen/SwiftTargetInfo.cpp @@ -169,6 +169,13 @@ static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple, target.SwiftRetainIgnoresNegativeValues = true; } +/// Configures target-specific information for wasm32 platforms. +static void configureWasm32(IRGenModule &IGM, const llvm::Triple &triple, + SwiftTargetInfo &target) { + target.LeastValidPointerValue = + SWIFT_ABI_WASM32_LEAST_VALID_POINTER; +} + /// Configure a default target. SwiftTargetInfo::SwiftTargetInfo( llvm::Triple::ObjectFormatType outputObjectFormat, @@ -240,6 +247,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) { case llvm::Triple::systemz: configureSystemZ(IGM, triple, target); break; + case llvm::Triple::wasm32: + configureWasm32(IGM, triple, target); + break; default: // FIXME: Complain here? Default target info is unlikely to be correct. diff --git a/stdlib/public/SwiftShims/HeapObject.h b/stdlib/public/SwiftShims/HeapObject.h index 3933b0b8d40e2..3d4f12469f949 100644 --- a/stdlib/public/SwiftShims/HeapObject.h +++ b/stdlib/public/SwiftShims/HeapObject.h @@ -190,6 +190,22 @@ static_assert(alignof(HeapObject) == alignof(void*), #define _swift_BridgeObject_TaggedPointerBits \ (__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_64 +#elif defined(__wasm32__) + +#define _swift_abi_LeastValidPointerValue \ + (__swift_uintptr_t) SWIFT_ABI_WASM32_LEAST_VALID_POINTER + +#define _swift_abi_SwiftSpareBitsMask \ + (__swift_uintptr_t) SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK + +#define _swift_abi_ObjCReservedBitsMask \ + (__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK +#define _swift_abi_ObjCReservedLowBits \ + (unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS + +#define _swift_BridgeObject_TaggedPointerBits \ + (__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_32 + #else #define _swift_abi_LeastValidPointerValue \ diff --git a/stdlib/public/SwiftShims/System.h b/stdlib/public/SwiftShims/System.h index 978ec41f1eafb..2acfdd417ee03 100644 --- a/stdlib/public/SwiftShims/System.h +++ b/stdlib/public/SwiftShims/System.h @@ -207,4 +207,12 @@ #define SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE \ (1< Date: Tue, 24 May 2022 18:59:31 +0100 Subject: [PATCH 2/2] Set `SWIFT_ABI_WASM32_LEAST_VALID_POINTER` to 4096 --- stdlib/public/SwiftShims/System.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/SwiftShims/System.h b/stdlib/public/SwiftShims/System.h index 2acfdd417ee03..34174bebe8963 100644 --- a/stdlib/public/SwiftShims/System.h +++ b/stdlib/public/SwiftShims/System.h @@ -213,6 +213,6 @@ // the pointer representation, runtime performance and memory footprint are // worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to // reserve low 1KB. -#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 1024 +#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 4096 #endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H