Skip to content

Commit 78e3cee

Browse files
[WASM] Fix least valid pointer value
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 ad75aa3 commit 78e3cee

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

lib/Driver/ToolChains.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
121121
const JobContext &context) const override;
122122
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
123123
const JobContext &context) const override;
124+
void validateArguments(DiagnosticEngine &diags,
125+
const llvm::opt::ArgList &args,
126+
StringRef defaultTarget) const override;
124127

125128
public:
126129
WebAssembly(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}

lib/Driver/WebAssemblyToolChains.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "ToolChains.h"
1414

15+
#include "swift/ABI/System.h"
16+
#include "swift/AST/DiagnosticsDriver.h"
1517
#include "swift/Basic/Dwarf.h"
1618
#include "swift/Basic/LLVM.h"
1719
#include "swift/Basic/Platform.h"
@@ -193,6 +195,15 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
193195
Arguments.push_back("-v");
194196
}
195197

198+
// WebAssembly doesn't reserve low addresses But without "extra inhabitants"
199+
// of the pointer representation, runtime performance and memory footprint are
200+
// worse. So assume that compiler driver uses wasm-ld and --global-base=1024
201+
// to reserve low 1KB.
202+
Arguments.push_back("-Xlinker");
203+
Arguments.push_back(context.Args.MakeArgString(
204+
Twine("--global-base=") +
205+
std::to_string(SWIFT_ABI_WASM32_LEAST_VALID_POINTER)));
206+
196207
// These custom arguments should be right before the object file at the end.
197208
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
198209
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
@@ -209,6 +220,23 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
209220
return II;
210221
}
211222

223+
void validateLinkerArguments(DiagnosticEngine &diags,
224+
ArgStringList linkerArgs) {
225+
for (auto arg : linkerArgs) {
226+
if (StringRef(arg).startswith("--global-base=")) {
227+
diags.diagnose(SourceLoc(), diag::error_option_not_supported, arg,
228+
"wasm32");
229+
}
230+
}
231+
}
232+
void toolchains::WebAssembly::validateArguments(DiagnosticEngine &diags,
233+
const llvm::opt::ArgList &args,
234+
StringRef defaultTarget) const {
235+
ArgStringList linkerArgs;
236+
args.AddAllArgValues(linkerArgs, options::OPT_Xlinker);
237+
validateLinkerArguments(diags, linkerArgs);
238+
}
239+
212240
ToolChain::InvocationInfo
213241
toolchains::WebAssembly::constructInvocation(const StaticLinkJobAction &job,
214242
const JobContext &context) const {

lib/IRGen/SwiftTargetInfo.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ static void configureSystemZ(IRGenModule &IGM, const llvm::Triple &triple,
140140
target.SwiftRetainIgnoresNegativeValues = true;
141141
}
142142

143+
/// Configures target-specific information for wasm32 platforms.
144+
static void configureWasm32(IRGenModule &IGM, const llvm::Triple &triple,
145+
SwiftTargetInfo &target) {
146+
target.LeastValidPointerValue =
147+
SWIFT_ABI_WASM32_LEAST_VALID_POINTER;
148+
}
149+
143150
/// Configure a default target.
144151
SwiftTargetInfo::SwiftTargetInfo(
145152
llvm::Triple::ObjectFormatType outputObjectFormat,
@@ -196,7 +203,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
196203
case llvm::Triple::systemz:
197204
configureSystemZ(IGM, triple, target);
198205
break;
199-
206+
case llvm::Triple::wasm32:
207+
configureWasm32(IGM, triple, target);
208+
break;
200209
default:
201210
// FIXME: Complain here? Default target info is unlikely to be correct.
202211
break;

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+
extern unsigned char __global_base;
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
@@ -192,4 +192,12 @@
192192
#define SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE \
193193
(1<<SWIFT_ABI_S390X_OBJC_NUM_RESERVED_LOW_BITS)
194194

195+
/*********************************** wasm32 ************************************/
196+
197+
// WebAssembly doesn't reserve low addresses But without "extra inhabitants" of
198+
// the pointer representation, runtime performance and memory footprint are
199+
// worse. So assume that compiler driver uses wasm-ld and --global-base=1024 to
200+
// reserve low 1KB.
201+
#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 1024
202+
195203
#endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H

0 commit comments

Comments
 (0)