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

Paulo Matos via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 5 07:04:53 PDT 2021


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

The currently implementation of funcrefs is broken since it is putting
the funcref itself on the stack before the call_indirect. Instead what
should be on the stack is the constant 0, which is the index at which
we store the funcref in __funcref_call_table.


Repository:
  rG LLVM Github Monorepo

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,17 @@
   if (IsIndirect) {
     auto FnPtr = CallParams.getOperand(0);
     CallParams.RemoveOperand(0);
-    CallParams.addOperand(FnPtr);
+
+    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.377225.patch
Type: text/x-patch
Size: 1371 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211005/bab00818/attachment.bin>


More information about the llvm-commits mailing list