mirror-llvm-project/llvm/test/CodeGen/WebAssembly/ref-test-func.ll
Hood Chatham 765f0d14fe
[WebAssembly] Represent reference types as TargetExtType (#203165)
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>
2026-06-24 22:07:33 -07:00

161 lines
6.3 KiB
LLVM

; RUN: llc < %s --mtriple=wasm32-unknown-unknown -mcpu=mvp -mattr=+reference-types -mattr=+gc -verify-machineinstrs | FileCheck --check-prefixes CHECK,CHK32 %s
; RUN: llc < %s --mtriple=wasm64-unknown-unknown -mcpu=mvp -mattr=+reference-types -mattr=+gc -verify-machineinstrs | FileCheck --check-prefixes CHECK,CHK64 %s
define void @test_fpsig_void_void(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_void_void:
; CHK32: .functype test_fpsig_void_void (i32) -> ()
; CHK64: .functype test_fpsig_void_void (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test () -> ()
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_return_i32(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_return_i32:
; CHK32: .functype test_fpsig_return_i32 (i32) -> ()
; CHK64: .functype test_fpsig_return_i32 (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test () -> (i32)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, i32 poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_return_i64(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_return_i64:
; CHK32: .functype test_fpsig_return_i64 (i32) -> ()
; CHK64: .functype test_fpsig_return_i64 (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test () -> (i64)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, i64 poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_return_f32(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_return_f32:
; CHK32: .functype test_fpsig_return_f32 (i32) -> ()
; CHK64: .functype test_fpsig_return_f32 (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test () -> (f32)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, float poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_return_f64(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_return_f64:
; CHK32: .functype test_fpsig_return_f64 (i32) -> ()
; CHK64: .functype test_fpsig_return_f64 (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test () -> (f64)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, double poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_param_i32(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_param_i32:
; CHK32: .functype test_fpsig_param_i32 (i32) -> ()
; CHK64: .functype test_fpsig_param_i32 (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test (f64) -> ()
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, double poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_multiple_params_and_returns(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_multiple_params_and_returns:
; CHK32: .functype test_fpsig_multiple_params_and_returns (i32) -> ()
; CHK64: .functype test_fpsig_multiple_params_and_returns (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test (i64, f32, i64) -> (i32, i64, f32, f64)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, i32 poison, i64 poison, float poison, double poison, token poison, i64 poison, float poison, i64 poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_fpsig_ptrs(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_fpsig_ptrs:
; CHK32: .functype test_fpsig_ptrs (i32) -> ()
; CHK64: .functype test_fpsig_ptrs (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHK32-NEXT: ref.test (i32, i32) -> (i32)
; CHK64-NEXT: ref.test (i64, i64) -> (i64)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, ptr poison, token poison, ptr poison, ptr poison)
tail call void @use(i32 noundef %res) #3
ret void
}
define void @test_reference_types(ptr noundef %func) local_unnamed_addr #0 {
; CHECK-LABEL: test_reference_types:
; CHK32: .functype test_reference_types (i32) -> ()
; CHK64: .functype test_reference_types (i64) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: local.get 0
; CHK64-NEXT: i32.wrap_i64
; CHECK-NEXT: table.get __indirect_function_table
; CHECK-NEXT: ref.test (funcref, externref) -> (externref)
; CHECK-NEXT: call use
; CHECK-NEXT: # fallthrough-return
entry:
%res = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, target("wasm.externref") poison, token poison, target("wasm.funcref") poison, target("wasm.externref") poison)
tail call void @use(i32 noundef %res) #3
ret void
}
declare void @use(i32 noundef) local_unnamed_addr #1