Skip to content

[WebAssembly] Use absolute pointers #31676

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

Closed
Closed
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
14 changes: 14 additions & 0 deletions include/swift/Basic/RelativePointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
std::is_signed<Offset>::value,
"offset type should be signed integer");

#if defined(__WASM__)
// WebAssembly target uses only absolute pointers.
// See https://forums.swift.org/t/wasm-support/16087/17 for more details.
return (uintptr_t)(intptr_t)offset;
#endif

auto base = reinterpret_cast<uintptr_t>(basePtr);
// We want to do wrapping arithmetic, but with a sign-extended
// offset. To do this in C, we need to do signed promotion to get
Expand All @@ -165,6 +171,14 @@ static inline Offset measureRelativeOffset(A *referent, B *base) {
std::is_signed<Offset>::value,
"offset type should be signed integer");

#if defined(__WASM__)
// WebAssembly target uses only absolute pointers.
auto offset = (Offset)(uintptr_t)referent;
assert((intptr_t)offset == (intptr_t)(uintptr_t)referent
&& "pointer too large to fit in offset type");
return offset;
#endif

auto distance = (uintptr_t)referent - (uintptr_t)base;
// Truncate as unsigned, then wrap around to signed.
auto truncatedDistance =
Expand Down
9 changes: 9 additions & 0 deletions lib/IRGen/IRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ IRGenFunction::emitLoadOfRelativePointer(Address addr, bool isFar,
if (!isFar) {
value = Builder.CreateSExt(value, IGM.IntPtrTy);
}
// WebAssembly target uses only absolute pointers.
// See https://forums.swift.org/t/wasm-support/16087/17 for more details.
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
auto *uncastPointer = Builder.CreateIntToPtr(value, IGM.Int8PtrTy);
auto uncastPointerAddress = Address(uncastPointer, IGM.getPointerAlignment());
auto pointer = Builder.CreateBitCast(uncastPointerAddress, expectedType);
return pointer.getAddress();
}

auto *addrInt = Builder.CreatePtrToInt(addr.getAddress(), IGM.IntPtrTy);
auto *uncastPointerInt = Builder.CreateAdd(addrInt, value);
auto *uncastPointer = Builder.CreateIntToPtr(uncastPointerInt, IGM.Int8PtrTy);
Expand Down