mirror-llvm-project/llvm/test/CodeGen/WebAssembly/funcref-call.ll
Demetrius Kanios d79df25b12
[WebAssembly] Define __funcref_call_table in generated asm and objects (#180900)
Currently, calls into funcref (`call addrspace(20)`) are lowered into a
store of the ref into a special single-element `__funcref_call_table`, a
call_indirect into said table, then a store of null into it.

It functions just fine when `-filetype=asm` (default), but when emitting
an object file directly from llc it fails with `LLVM ERROR: undefined
table symbol cannot be weak`

This fixes said error by "defining" it during `WebAssemblyAsmPrinter`,
and ensuring it is definitely emitted as weak (so duplicate tables in
multiple objects are merged into a single one).
2026-06-30 00:26:14 -07:00

54 lines
2 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -fast-isel=0 -mattr=+reference-types | FileCheck %s
; RUN: llc < %s --mtriple=wasm32-unknown-unknown --filetype=obj
; Use -fast-isel-abort=3 (never fall back to SelectionDAG) to make sure that the
; funcref call is selected by FastISel
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -fast-isel=1 -fast-isel-abort=3 -mattr=+reference-types | FileCheck %s
%funcref = type target("wasm.funcref")
declare ptr @llvm.wasm.funcref.to_ptr(%funcref) nounwind
; CHECK: .tabletype __funcref_call_table, funcref, 1
define void @call_funcref(%funcref %ref) {
; CHECK-LABEL: call_funcref:
; CHECK: .functype call_funcref (funcref) -> ()
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: local.get 0
; CHECK-NEXT: table.set __funcref_call_table
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: call_indirect __funcref_call_table, () -> ()
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: ref.null_func
; CHECK-NEXT: table.set __funcref_call_table
; CHECK-NEXT: # fallthrough-return
%refptr = call ptr @llvm.wasm.funcref.to_ptr(%funcref %ref)
call void %refptr()
ret void
}
define float @call_funcref_with_args(%funcref %ref) {
; CHECK-LABEL: call_funcref_with_args:
; CHECK: .functype call_funcref_with_args (funcref) -> (f32)
; CHECK-NEXT: .local f32
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: local.get 0
; CHECK-NEXT: table.set __funcref_call_table
; CHECK-NEXT: f64.const 0x1p0
; CHECK-NEXT: i32.const 2
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: call_indirect __funcref_call_table, (f64, i32) -> (f32)
; CHECK-NEXT: local.set 1
; CHECK-NEXT: i32.const 0
; CHECK-NEXT: ref.null_func
; CHECK-NEXT: table.set __funcref_call_table
; CHECK-NEXT: local.get 1
; CHECK-NEXT: # fallthrough-return
%refptr = call ptr @llvm.wasm.funcref.to_ptr(%funcref %ref)
%ret = call float %refptr(double 1.0, i32 2)
ret float %ret
}