Skip to content

Commit 594d3ad

Browse files
Merge pull request #39300 from kateinoigakukun/katei/wasm-least-valid-addr
[Wasm] Specify the least valid pointer for wasm32
2 parents 645d73f + 1d50072 commit 594d3ad

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
@@ -176,6 +176,13 @@ static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple,
176176
target.SwiftRetainIgnoresNegativeValues = true;
177177
}
178178

179+
/// Configures target-specific information for wasm32 platforms.
180+
static void configureWasm32(IRGenModule &IGM, const llvm::Triple &triple,
181+
SwiftTargetInfo &target) {
182+
target.LeastValidPointerValue =
183+
SWIFT_ABI_WASM32_LEAST_VALID_POINTER;
184+
}
185+
179186
/// Configure a default target.
180187
SwiftTargetInfo::SwiftTargetInfo(
181188
llvm::Triple::ObjectFormatType outputObjectFormat,
@@ -247,6 +254,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
247254
case llvm::Triple::systemz:
248255
configureSystemZ(IGM, triple, target);
249256
break;
257+
case llvm::Triple::wasm32:
258+
configureWasm32(IGM, triple, target);
259+
break;
250260

251261
default:
252262
// 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
@@ -200,6 +200,22 @@ static_assert(alignof(HeapObject) == alignof(void*),
200200
#define _swift_BridgeObject_TaggedPointerBits \
201201
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_64
202202

203+
#elif defined(__wasm32__)
204+
205+
#define _swift_abi_LeastValidPointerValue \
206+
(__swift_uintptr_t) SWIFT_ABI_WASM32_LEAST_VALID_POINTER
207+
208+
#define _swift_abi_SwiftSpareBitsMask \
209+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_SWIFT_SPARE_BITS_MASK
210+
211+
#define _swift_abi_ObjCReservedBitsMask \
212+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_OBJC_RESERVED_BITS_MASK
213+
#define _swift_abi_ObjCReservedLowBits \
214+
(unsigned) SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS
215+
216+
#define _swift_BridgeObject_TaggedPointerBits \
217+
(__swift_uintptr_t) SWIFT_ABI_DEFAULT_BRIDGEOBJECT_TAG_32
218+
203219
#else
204220

205221
#define _swift_abi_LeastValidPointerValue \

stdlib/public/SwiftShims/System.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,12 @@
216216
#define SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE \
217217
(1<<SWIFT_ABI_S390X_OBJC_NUM_RESERVED_LOW_BITS)
218218

219+
/*********************************** wasm32 ************************************/
220+
221+
// WebAssembly doesn't reserve low addresses. But without "extra inhabitants" of
222+
// the pointer representation, runtime performance and memory footprint are
223+
// worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to
224+
// reserve low 1KB.
225+
#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 4096
226+
219227
#endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H

0 commit comments

Comments
 (0)