[PATCH] D120229: [WebAssembly] Update WebAssemblyAsmTypeCheck for table.get

かめのこにょこにょこ via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 20 21:58:53 PST 2022


nokotan created this revision.
nokotan added a reviewer: sbc100.
Herald added subscribers: ecnelises, mikhail.ramalho, sunfish, hiraditya, jgravelle-google, dschuff.
nokotan requested review of this revision.
Herald added subscribers: llvm-commits, aheejin.
Herald added a project: LLVM.

This patch is aimed to resolve GitHub Issue #53789 <https://github.com/llvm/llvm-project/issues/53789>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120229

Files:
  lld/test/wasm/funcref.s
  llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
  llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h


Index: llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
===================================================================
--- llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
+++ llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.h
@@ -44,6 +44,7 @@
   bool getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
                  const MCSymbolRefExpr *&SymRef);
   bool getGlobal(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
+  bool getTable(SMLoc ErrorLoc, const MCInst &Inst, wasm::ValType &Type);
 
 public:
   WebAssemblyAsmTypeCheck(MCAsmParser &Parser, const MCInstrInfo &MII, bool is64);
Index: llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
+++ llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp
@@ -182,6 +182,34 @@
   return false;
 }
 
+bool WebAssemblyAsmTypeCheck::getTable(SMLoc ErrorLoc, const MCInst &Inst,
+                                       wasm::ValType &Type) {
+  const MCSymbolRefExpr *SymRef;
+  if (getSymRef(ErrorLoc, Inst, SymRef))
+    return true;
+  auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
+  switch (WasmSym->getType().getValueOr(wasm::WASM_SYMBOL_TYPE_DATA)) {
+  case wasm::WASM_SYMBOL_TYPE_TABLE:
+    Type = static_cast<wasm::ValType>(WasmSym->getTableType().ElemType);
+    break;
+  case wasm::WASM_SYMBOL_TYPE_FUNCTION:
+  case wasm::WASM_SYMBOL_TYPE_DATA:
+    switch (SymRef->getKind()) {
+    case MCSymbolRefExpr::VK_GOT:
+    case MCSymbolRefExpr::VK_WASM_GOT_TLS:
+      Type = is64 ? wasm::ValType::I64 : wasm::ValType::I32;
+      return false;
+    default:
+      break;
+    }
+    LLVM_FALLTHROUGH;
+  default:
+    return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
+                                   " missing .tabletype");
+  }
+  return false;
+}
+
 bool WebAssemblyAsmTypeCheck::endOfFunction(SMLoc ErrorLoc) {
   // Check the return types.
   for (auto RVT : llvm::reverse(ReturnTypes)) {
@@ -225,6 +253,19 @@
       return true;
     if (popType(ErrorLoc, Type))
       return true;
+  } else if (Name == "table.get") {
+    if (getTable(ErrorLoc, Inst, Type))
+      return true;
+    if (popType(ErrorLoc, wasm::ValType::I32))
+      return true;
+    Stack.push_back(Type);
+  } else if (Name == "table.set") {
+    if (getTable(ErrorLoc, Inst, Type))
+      return true;
+    if (popType(ErrorLoc, Type))
+      return true;
+    if (popType(ErrorLoc, wasm::ValType::I32))
+      return true;
   } else if (Name == "drop") {
     if (popType(ErrorLoc, {}))
       return true;
Index: lld/test/wasm/funcref.s
===================================================================
--- /dev/null
+++ lld/test/wasm/funcref.s
@@ -0,0 +1,23 @@
+# RUN: llvm-mc -mattr=+reference-types -triple=wasm32-unknown-unknown -filetype=obj -o %t.o %s
+# RUN: wasm-ld --no-entry --export obtain_funcref_from_table_index %t.o -o %t.wasm
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+.globl __indirect_function_table
+.tabletype __indirect_function_table, funcref
+
+.globl obtain_funcref_from_table_index
+
+obtain_funcref_from_table_index:
+  .functype obtain_funcref_from_table_index(i32) -> (funcref)
+  local.get 0
+  table.get __indirect_function_table
+  end_function
+
+#      CHECK:  Sections:
+# CHECK-NEXT:    - Type:            TYPE
+# CHECK-NEXT:      Signatures:
+# CHECK-NEXT:        - Index:           0
+# CHECK-NEXT:          ParamTypes:
+# CHECK-NEXT:            - I32
+# CHECK-NEXT:          ReturnTypes:
+# CHECK-NEXT:            - FUNCREF


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120229.410214.patch
Type: text/x-patch
Size: 3658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220221/29e22468/attachment.bin>


More information about the llvm-commits mailing list