Skip to content

Commit

Permalink
[WebAssembly] Add path to PIC mode for wasm tables (llvm#67545)
Browse files Browse the repository at this point in the history
Currently tables cannot be shared between compilation units, therefore
no special treatment is needed for tables.

Fixes llvm#65191
  • Loading branch information
pmatos authored Oct 3, 2023
1 parent f58d54a commit a29e8ef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ void WebAssembly::wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
// that is a reference type.
wasm::ValType ValTy;
bool IsTable = false;
if (GlobalVT->isArrayTy() && WebAssembly::isWebAssemblyReferenceType(
GlobalVT->getArrayElementType())) {
if (WebAssembly::isWebAssemblyTableType(GlobalVT)) {
IsTable = true;
const Type *ElTy = GlobalVT->getArrayElementType();
if (WebAssembly::isWebAssemblyExternrefType(ElTy))
Expand Down
16 changes: 12 additions & 4 deletions llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@ namespace WebAssembly {

/// Return true if this is a WebAssembly Externref Type.
inline bool isWebAssemblyExternrefType(const Type *Ty) {
return Ty->getPointerAddressSpace() ==
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF;
return Ty->isPointerTy() &&
Ty->getPointerAddressSpace() ==
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF;
}

/// Return true if this is a WebAssembly Funcref Type.
inline bool isWebAssemblyFuncrefType(const Type *Ty) {
return Ty->getPointerAddressSpace() ==
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
return Ty->isPointerTy() &&
Ty->getPointerAddressSpace() ==
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
}

/// Return true if this is a WebAssembly Reference Type.
inline bool isWebAssemblyReferenceType(const Type *Ty) {
return isWebAssemblyExternrefType(Ty) || isWebAssemblyFuncrefType(Ty);
}

/// Return true if the table represents a WebAssembly table type.
inline bool isWebAssemblyTableType(const Type *Ty) {
return Ty->isArrayTy() &&
isWebAssemblyReferenceType(Ty->getArrayElementType());
}

// Convert StringRef to ValType / HealType / BlockType

MVT parseMVT(StringRef Type);
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,8 +1724,11 @@ SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op,
fail(DL, DAG, "Invalid address space for WebAssembly target");

unsigned OperandFlags = 0;
if (isPositionIndependent()) {
const GlobalValue *GV = GA->getGlobal();
const GlobalValue *GV = GA->getGlobal();
// Since WebAssembly tables cannot yet be shared accross modules, we don't
// need special treatment for tables in PIC mode.
if (isPositionIndependent() &&
!WebAssembly::isWebAssemblyTableType(GV->getValueType())) {
if (getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) {
MachineFunction &MF = DAG.getMachineFunction();
MVT PtrVT = getPointerTy(MF.getDataLayout());
Expand Down
12 changes: 10 additions & 2 deletions llvm/test/MC/WebAssembly/reloc-pic.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj < %s | obj2yaml | FileCheck %s
# RUN: llvm-mc -triple=wasm32-unknown-unknown -mattr=+reference-types -filetype=obj < %s | obj2yaml | FileCheck --check-prefix=REF %s
# RUN: sed -e '/^REF-/d' %s | llvm-mc -triple=wasm32-unknown-unknown -filetype=obj | obj2yaml | FileCheck %s
# RUN: sed -e 's/^REF-//g' %s | llvm-mc -triple=wasm32-unknown-unknown -mattr=+reference-types -filetype=obj | obj2yaml | FileCheck --check-prefix=REF %s

# Verify that @GOT relocation entryes result in R_WASM_GLOBAL_INDEX_LEB against
# against the corrsponding function or data symbol and that the corresponding
Expand Down Expand Up @@ -50,6 +50,9 @@ hidden_func:
#.hidden hidden_data
.size default_data, 4

REF-mytable:
REF-.tabletype mytable, externref

# CHECK: --- !WASM
# CHECK-NEXT: FileHeader:
# CHECK-NEXT: Version: 0x1
Expand Down Expand Up @@ -209,6 +212,11 @@ hidden_func:
# CHECK-NEXT: Function: 5
# REF: - Index: 10
# REF-NEXT: Kind: TABLE
# REF-NEXT: Name: mytable
# REF-NEXT: Flags: [ BINDING_LOCAL ]
# REF-NEXT: Table: 1
# REF-NEXT: - Index: 11
# REF-NEXT: Kind: TABLE
# REF-NEXT: Name: __indirect_function_table
# REF-NEXT: Flags: [ UNDEFINED, NO_STRIP ]
# REF-NEXT: Table: 0
Expand Down

0 comments on commit a29e8ef

Please sign in to comment.