Originally #71540 by Paolo Matos, I picked it up and finished it. Resolves https://github.com/llvm/llvm-project/issues/69894. Model WebAssembly externref and funcref as target("wasm.externref") / target("wasm.funcref") TargetExtTypes instead of pointers in non-integral address spaces 10 and 20. The entire WebAssemblyLowerRefTypesIntPtrConv can be removed. This breaks the GlobalISel handling for reference types, I just disabled GlobalISel handling for functions that use them. I added intrinsics for `wasm.ptr.to_funcref` and `wasm.funcref.to_ptr`. ptr.to_funcref does a table.get from the indirect function pointer table. As a special case, 0 is converted to the null funcref rather than doing table.get on 0. `wasm.funcref.to_ptr` is only handled when we call it immediately, otherwise it will fail to lower. We could dynamically put the funcref into the table to make it work but that would require a stack of spilled funcrefs and isn't worth the effort. In the process of looking into this, I noticed that clang used to allow casting from funcref to function pointer but if the cast result escaped it would hit `Cannot select: FUNCREF_TO_PTR` in the backend. I added a diagnostic that says "a funcref can only be converted to a pointer to be directly called" to make this a little cleaner. cc @QuantumSegfault --------- Co-authored-by: Paulo Matos <pmatos@igalia.com>
34 lines
1.2 KiB
LLVM
34 lines
1.2 KiB
LLVM
; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types | FileCheck %s
|
|
|
|
%funcref = type target("wasm.funcref")
|
|
|
|
@funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
|
|
|
|
; CHECK: .tabletype __funcref_call_table, funcref, 1
|
|
|
|
declare %funcref @llvm.wasm.table.get.funcref(ptr addrspace(1), i32) nounwind
|
|
declare ptr @llvm.wasm.funcref.to_ptr(%funcref) nounwind
|
|
|
|
define void @call_funcref_from_table(i32 %i) {
|
|
; CHECK-LABEL: call_funcref_from_table:
|
|
; CHECK-NEXT: .functype call_funcref_from_table (i32) -> ()
|
|
; CHECK-NEXT: i32.const 0
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.get funcref_table
|
|
; 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: end_function
|
|
%ref = call %funcref @llvm.wasm.table.get.funcref(ptr addrspace(1) @funcref_table, i32 %i)
|
|
%refptr = call ptr @llvm.wasm.funcref.to_ptr(%funcref %ref)
|
|
call void %refptr()
|
|
ret void
|
|
}
|
|
|
|
; CHECK: .tabletype funcref_table, funcref
|
|
; CHECK-LABEL: funcref_table:
|
|
|