[PATCH] D111152: [WebAssembly] Fix call_indirect on funcrefs

Paulo Matos via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 6 01:25:45 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG0c7495848a02: [WebAssembly] Fix call_indirect on funcrefs (authored by pmatos).

Changed prior to commit:
  https://reviews.llvm.org/D111152?vs=377225&id=377461#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111152/new/

https://reviews.llvm.org/D111152

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
  llvm/test/CodeGen/WebAssembly/funcref-call.ll


Index: llvm/test/CodeGen/WebAssembly/funcref-call.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/funcref-call.ll
+++ llvm/test/CodeGen/WebAssembly/funcref-call.ll
@@ -15,7 +15,7 @@
 ; CHECK-NEXT: i32.const 0
 ; CHECK-NEXT: local.get 0
 ; CHECK-NEXT: table.set __funcref_call_table
-; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i32.const 0
 ; CHECK-NEXT: call_indirect __funcref_call_table, () -> ()
 ; CHECK-NEXT: i32.const 0
 ; CHECK-NEXT: ref.null func
Index: llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -567,7 +567,21 @@
   if (IsIndirect) {
     auto FnPtr = CallParams.getOperand(0);
     CallParams.RemoveOperand(0);
-    CallParams.addOperand(FnPtr);
+
+    // For funcrefs, call_indirect is done through __funcref_call_table and the
+    // funcref is always installed in slot 0 of the table, therefore instead of having
+    // the function pointer added at the end of the params list, a zero (the index in
+    // __funcref_call_table is added).
+    if (IsFuncrefCall) {
+      Register RegZero =
+          MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
+      MachineInstrBuilder MIBC0 =
+          BuildMI(MF, DL, TII.get(WebAssembly::CONST_I32), RegZero).addImm(0);
+
+      BB->insert(CallResults.getIterator(), MIBC0);
+      MachineInstrBuilder(MF, CallParams).addReg(RegZero);
+    } else
+      CallParams.addOperand(FnPtr);
   }
 
   for (auto Def : CallResults.defs())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111152.377461.patch
Type: text/x-patch
Size: 1667 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211006/719e0b86/attachment.bin>


More information about the llvm-commits mailing list