Skip to content

Fix least valid pointer value for 5.3 branch #2246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
const JobContext &context) const override;
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
const JobContext &context) const override;
void validateArguments(DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
StringRef defaultTarget) const override;

public:
WebAssembly(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
Expand Down
28 changes: 28 additions & 0 deletions lib/Driver/WebAssemblyToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "ToolChains.h"

#include "swift/ABI/System.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
Expand Down Expand Up @@ -192,6 +194,15 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
Arguments.push_back("-v");
}

// 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.
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);
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
Expand All @@ -208,6 +219,23 @@ toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
return II;
}

void validateLinkerArguments(DiagnosticEngine &diags,
ArgStringList linkerArgs) {
for (auto arg : linkerArgs) {
if (StringRef(arg).startswith("--global-base=")) {
diags.diagnose(SourceLoc(), diag::error_option_not_supported, arg,
"wasm32");
}
}
}
void toolchains::WebAssembly::validateArguments(DiagnosticEngine &diags,
const llvm::opt::ArgList &args,
StringRef defaultTarget) const {
ArgStringList linkerArgs;
args.AddAllArgValues(linkerArgs, options::OPT_Xlinker);
validateLinkerArguments(diags, linkerArgs);
}

ToolChain::InvocationInfo
toolchains::WebAssembly::constructInvocation(const StaticLinkJobAction &job,
const JobContext &context) const {
Expand Down
11 changes: 10 additions & 1 deletion lib/IRGen/SwiftTargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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,
Expand Down Expand Up @@ -196,7 +203,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.
break;
Expand Down
16 changes: 16 additions & 0 deletions stdlib/public/SwiftShims/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
extern unsigned char __global_base;
#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 \
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/SwiftShims/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,12 @@
#define SWIFT_ABI_S390X_OBJC_WEAK_REFERENCE_MARKER_VALUE \
(1<<SWIFT_ABI_S390X_OBJC_NUM_RESERVED_LOW_BITS)

/*********************************** 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.
#define SWIFT_ABI_WASM32_LEAST_VALID_POINTER 1024

#endif // SWIFT_STDLIB_SHIMS_ABI_SYSTEM_H