Skip to content

Commit 6fd3ae7

Browse files
[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.
1 parent ecf8a35 commit 6fd3ae7

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

lib/Driver/WebAssemblyToolChains.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific)
2-
//------===//
1+
//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) ------===//
32
//
43
// This source file is part of the Swift.org open source project
54
//
@@ -189,6 +188,9 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
189188
// worse. So assume that compiler driver uses wasm-ld and --global-base=1024
190189
// to reserve low 1KB.
191190
Arguments.push_back("-Xlinker");
191+
Arguments.push_back(context.Args.MakeArgString(
192+
Twine("--global-base=") +
193+
std::to_string(SWIFT_ABI_WASM32_LEAST_VALID_POINTER)));
192194

193195
// These custom arguments should be right before the object file at the end.
194196
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);

lib/IRGen/SwiftTargetInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple,
169169
target.SwiftRetainIgnoresNegativeValues = true;
170170
}
171171

172+
/// Configures target-specific information for wasm32 platforms.
173+
static void configureWasm32(IRGenModule &IGM, const llvm::Triple &triple,
174+
SwiftTargetInfo &target) {
175+
target.LeastValidPointerValue =
176+
SWIFT_ABI_WASM32_LEAST_VALID_POINTER;
177+
}
178+
172179
/// Configure a default target.
173180
SwiftTargetInfo::SwiftTargetInfo(
174181
llvm::Triple::ObjectFormatType outputObjectFormat,
@@ -240,6 +247,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
240247
case llvm::Triple::systemz:
241248
configureSystemZ(IGM, triple, target);
242249
break;
250+
case llvm::Triple::wasm32:
251+
configureWasm32(IGM, triple, target);
252+
break;
243253

244254
default:
245255
// FIXME: Complain here? Default target info is unlikely to be correct.

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ static_assert(alignof(HeapObject) == alignof(void*),
190190
#define _swift_BridgeObject_TaggedPointerBits \
191191
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_64
192192

193+
#elif defined(__wasm32__)
194+
195+
#define _swift_abi_LeastValidPointerValue \
196+
(__swift_uintptr_t) SWIFT_ABI_WASM32_LEAST_VALID_POINTER
197+
198+
#define _swift_abi_SwiftSpareBitsMask \
199+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK
200+
201+
#define _swift_abi_ObjCReservedBitsMask \
202+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK
203+
#define _swift_abi_ObjCReservedLowBits \
204+
(unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS
205+
206+
#define _swift_BridgeObject_TaggedPointerBits \
207+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_32
208+
193209
#else
194210

195211
#define _swift_abi_LeastValidPointerValue \

stdlib/public/SwiftShims/System.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,12 @@
207207
#define SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE \
208208
(1<<SWIFT_ABI_S390X_OBJC_NUM_RESERVED_LOW_BITS)
209209

210+
/*********************************** wasm32 ************************************/
211+
212+
// WebAssembly doesn't reserve low addresses. But without "extra inhabitants" of
213+
// the pointer representation, runtime performance and memory footprint are
214+
// worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to
215+
// reserve low 1KB.
216+
#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 1024
217+
210218
#endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H

0 commit comments

Comments
 (0)