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>
79 lines
2.9 KiB
LLVM
79 lines
2.9 KiB
LLVM
; RUN: llc --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types < %s | FileCheck %s
|
|
|
|
%funcref = type target("wasm.funcref")
|
|
|
|
@funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
|
|
|
|
declare void @llvm.wasm.table.set.funcref(ptr addrspace(1), i32, %funcref) nounwind
|
|
|
|
define void @set_funcref_table(%funcref %g, i32 %i) {
|
|
; CHECK-LABEL: set_funcref_table:
|
|
; CHECK-NEXT: .functype set_funcref_table (funcref, i32) -> ()
|
|
; CHECK-NEXT: local.get 1
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.set funcref_table
|
|
; CHECK-NEXT: end_function
|
|
|
|
;; this generates a table.set of @funcref_table
|
|
call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 %i, %funcref %g)
|
|
ret void
|
|
}
|
|
|
|
define void @set_funcref_table_const(%funcref %g) {
|
|
; CHECK-LABEL: set_funcref_table_const:
|
|
; CHECK-NEXT: .functype set_funcref_table_const (funcref) -> ()
|
|
; CHECK-NEXT: i32.const 0
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.set funcref_table
|
|
; CHECK-NEXT: end_function
|
|
call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 0, %funcref %g)
|
|
ret void
|
|
}
|
|
|
|
define void @set_funcref_table_with_offset(%funcref %g, i32 %i) {
|
|
; CHECK-LABEL: set_funcref_table_with_offset:
|
|
; CHECK-NEXT: .functype set_funcref_table_with_offset (funcref, i32) -> ()
|
|
; CHECK-NEXT: local.get 1
|
|
; CHECK-NEXT: i32.const 2
|
|
; CHECK-NEXT: i32.add
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.set funcref_table
|
|
; CHECK-NEXT: end_function
|
|
%off = add nsw i32 %i, 2
|
|
call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 %off, %funcref %g)
|
|
ret void
|
|
}
|
|
|
|
define void @set_funcref_table_with_var_offset(%funcref %g, i32 %i, i32 %j) {
|
|
; CHECK-LABEL: set_funcref_table_with_var_offset:
|
|
; CHECK-NEXT: .functype set_funcref_table_with_var_offset (funcref, i32, i32) -> ()
|
|
; CHECK-NEXT: local.get 1
|
|
; CHECK-NEXT: local.get 2
|
|
; CHECK-NEXT: i32.add
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.set funcref_table
|
|
; CHECK-NEXT: end_function
|
|
%off = add nsw i32 %i, %j
|
|
call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 %off, %funcref %g)
|
|
ret void
|
|
}
|
|
|
|
declare i32 @set_offset()
|
|
|
|
define void @set_funcref_table_with_var_offset2(%funcref %g, i32 %i) {
|
|
; CHECK-LABEL: set_funcref_table_with_var_offset2:
|
|
; CHECK-NEXT: .functype set_funcref_table_with_var_offset2 (funcref, i32) -> ()
|
|
; CHECK-NEXT: local.get 1
|
|
; CHECK-NEXT: call set_offset
|
|
; CHECK-NEXT: i32.add
|
|
; CHECK-NEXT: local.get 0
|
|
; CHECK-NEXT: table.set funcref_table
|
|
; CHECK-NEXT: end_function
|
|
%j = call i32 @set_offset()
|
|
%off = add nsw i32 %i, %j
|
|
call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 %off, %funcref %g)
|
|
ret void
|
|
}
|
|
|
|
; CHECK: .tabletype funcref_table, funcref
|
|
; CHECK-LABEL: funcref_table:
|