Skip to content

Commit d71de4e

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 1155492 commit d71de4e

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
@@ -118,6 +118,9 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
118118
const JobContext &context) const override;
119119
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
120120
const JobContext &context) const override;
121+
void validateArguments(DiagnosticEngine &diags,
122+
const llvm::opt::ArgList &args,
123+
StringRef defaultTarget) const override;
121124

122125
public:
123126
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"
@@ -192,6 +194,15 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
192194
Arguments.push_back("-v");
193195
}
194196

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

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