Skip to content

Commit a29e8ef

Browse files
authored
[WebAssembly] Add path to PIC mode for wasm tables (llvm#67545)
Currently tables cannot be shared between compilation units, therefore no special treatment is needed for tables. Fixes llvm#65191
1 parent f58d54a commit a29e8ef

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ void WebAssembly::wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
7171
// that is a reference type.
7272
wasm::ValType ValTy;
7373
bool IsTable = false;
74-
if (GlobalVT->isArrayTy() && WebAssembly::isWebAssemblyReferenceType(
75-
GlobalVT->getArrayElementType())) {
74+
if (WebAssembly::isWebAssemblyTableType(GlobalVT)) {
7675
IsTable = true;
7776
const Type *ElTy = GlobalVT->getArrayElementType();
7877
if (WebAssembly::isWebAssemblyExternrefType(ElTy))

llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,29 @@ namespace WebAssembly {
2828

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

3536
/// Return true if this is a WebAssembly Funcref Type.
3637
inline bool isWebAssemblyFuncrefType(const Type *Ty) {
37-
return Ty->getPointerAddressSpace() ==
38-
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
38+
return Ty->isPointerTy() &&
39+
Ty->getPointerAddressSpace() ==
40+
WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
3941
}
4042

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

48+
/// Return true if the table represents a WebAssembly table type.
49+
inline bool isWebAssemblyTableType(const Type *Ty) {
50+
return Ty->isArrayTy() &&
51+
isWebAssemblyReferenceType(Ty->getArrayElementType());
52+
}
53+
4654
// Convert StringRef to ValType / HealType / BlockType
4755

4856
MVT parseMVT(StringRef Type);

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,8 +1724,11 @@ SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op,
17241724
fail(DL, DAG, "Invalid address space for WebAssembly target");
17251725

17261726
unsigned OperandFlags = 0;
1727-
if (isPositionIndependent()) {
1728-
const GlobalValue *GV = GA->getGlobal();
1727+
const GlobalValue *GV = GA->getGlobal();
1728+
// Since WebAssembly tables cannot yet be shared accross modules, we don't
1729+
// need special treatment for tables in PIC mode.
1730+
if (isPositionIndependent() &&
1731+
!WebAssembly::isWebAssemblyTableType(GV->getValueType())) {
17291732
if (getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) {
17301733
MachineFunction &MF = DAG.getMachineFunction();
17311734
MVT PtrVT = getPointerTy(MF.getDataLayout());

llvm/test/MC/WebAssembly/reloc-pic.s

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj < %s | obj2yaml | FileCheck %s
2-
# RUN: llvm-mc -triple=wasm32-unknown-unknown -mattr=+reference-types -filetype=obj < %s | obj2yaml | FileCheck --check-prefix=REF %s
1+
# RUN: sed -e '/^REF-/d' %s | llvm-mc -triple=wasm32-unknown-unknown -filetype=obj | obj2yaml | FileCheck %s
2+
# RUN: sed -e 's/^REF-//g' %s | llvm-mc -triple=wasm32-unknown-unknown -mattr=+reference-types -filetype=obj | obj2yaml | FileCheck --check-prefix=REF %s
33

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

53+
REF-mytable:
54+
REF-.tabletype mytable, externref
55+
5356
# CHECK: --- !WASM
5457
# CHECK-NEXT: FileHeader:
5558
# CHECK-NEXT: Version: 0x1
@@ -209,6 +212,11 @@ hidden_func:
209212
# CHECK-NEXT: Function: 5
210213
# REF: - Index: 10
211214
# REF-NEXT: Kind: TABLE
215+
# REF-NEXT: Name: mytable
216+
# REF-NEXT: Flags: [ BINDING_LOCAL ]
217+
# REF-NEXT: Table: 1
218+
# REF-NEXT: - Index: 11
219+
# REF-NEXT: Kind: TABLE
212220
# REF-NEXT: Name: __indirect_function_table
213221
# REF-NEXT: Flags: [ UNDEFINED, NO_STRIP ]
214222
# REF-NEXT: Table: 0

0 commit comments

Comments
 (0)