Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / WebAssembly / wasm-funcref.c
blobf01af0db321dd54ec685d3cb5175dedf49e9f465
1 // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2 // RUN: %clang_cc1 -triple wasm32 -target-feature +reference-types -o - -emit-llvm %s | FileCheck %s
4 typedef void (*__funcref funcref_t)();
5 typedef int (*__funcref fn_funcref_t)(int);
6 typedef int (*fn_t)(int);
8 // Null funcref builtin call
9 // CHECK-LABEL: @get_null(
10 // CHECK-NEXT: entry:
11 // CHECK-NEXT: [[TMP0:%.*]] = call ptr addrspace(20) @llvm.wasm.ref.null.func()
12 // CHECK-NEXT: ret ptr addrspace(20) [[TMP0]]
14 funcref_t get_null() {
15 return __builtin_wasm_ref_null_func();
18 // Call to null funcref builtin but requires cast since
19 // default return value for builtin is a funcref with function type () -> ().
20 // CHECK-LABEL: @get_null_ii(
21 // CHECK-NEXT: entry:
22 // CHECK-NEXT: [[TMP0:%.*]] = call ptr addrspace(20) @llvm.wasm.ref.null.func()
23 // CHECK-NEXT: ret ptr addrspace(20) [[TMP0]]
25 fn_funcref_t get_null_ii() {
26 return (fn_funcref_t) __builtin_wasm_ref_null_func();
29 // Identity function for funcref.
30 // CHECK-LABEL: @identity(
31 // CHECK-NEXT: entry:
32 // CHECK-NEXT: [[FN_ADDR:%.*]] = alloca ptr addrspace(20), align 4
33 // CHECK-NEXT: store ptr addrspace(20) [[FN:%.*]], ptr [[FN_ADDR]], align 4
34 // CHECK-NEXT: [[TMP0:%.*]] = load ptr addrspace(20), ptr [[FN_ADDR]], align 4
35 // CHECK-NEXT: ret ptr addrspace(20) [[TMP0]]
37 funcref_t identity(funcref_t fn) {
38 return fn;
41 void helper(funcref_t);
43 // Pass funcref ref as an argument to a helper function.
44 // CHECK-LABEL: @handle(
45 // CHECK-NEXT: entry:
46 // CHECK-NEXT: [[FN_ADDR:%.*]] = alloca ptr addrspace(20), align 4
47 // CHECK-NEXT: store ptr addrspace(20) [[FN:%.*]], ptr [[FN_ADDR]], align 4
48 // CHECK-NEXT: [[TMP0:%.*]] = load ptr addrspace(20), ptr [[FN_ADDR]], align 4
49 // CHECK-NEXT: call void @helper(ptr addrspace(20) noundef [[TMP0]])
50 // CHECK-NEXT: ret i32 0
52 int handle(funcref_t fn) {
53 helper(fn);
54 return 0;
57 // Return funcref from function pointer.
58 // CHECK-LABEL: @get_ref(
59 // CHECK-NEXT: entry:
60 // CHECK-NEXT: [[FNPTR_ADDR:%.*]] = alloca ptr, align 4
61 // CHECK-NEXT: store ptr [[FNPTR:%.*]], ptr [[FNPTR_ADDR]], align 4
62 // CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[FNPTR_ADDR]], align 4
63 // CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr [[TMP0]] to ptr addrspace(20)
64 // CHECK-NEXT: ret ptr addrspace(20) [[TMP1]]
66 fn_funcref_t get_ref(fn_t fnptr) {
67 return (fn_funcref_t) fnptr;
70 // Call funcref
71 // CHECK-LABEL: @call_fn(
72 // CHECK-NEXT: entry:
73 // CHECK-NEXT: [[REF_ADDR:%.*]] = alloca ptr addrspace(20), align 4
74 // CHECK-NEXT: [[X_ADDR:%.*]] = alloca i32, align 4
75 // CHECK-NEXT: store ptr addrspace(20) [[REF:%.*]], ptr [[REF_ADDR]], align 4
76 // CHECK-NEXT: store i32 [[X:%.*]], ptr [[X_ADDR]], align 4
77 // CHECK-NEXT: [[TMP0:%.*]] = load ptr addrspace(20), ptr [[REF_ADDR]], align 4
78 // CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[X_ADDR]], align 4
79 // CHECK-NEXT: [[CALL:%.*]] = call addrspace(20) i32 [[TMP0]](i32 noundef [[TMP1]])
80 // CHECK-NEXT: ret i32 [[CALL]]
82 int call_fn(fn_funcref_t ref, int x) {
83 return ref(x);
86 typedef fn_funcref_t (*builtin_refnull_t)();
88 // Calling ref.null through a function pointer.
89 // CHECK-LABEL: @get_null_fptr(
90 // CHECK-NEXT: entry:
91 // CHECK-NEXT: [[REFNULL_ADDR:%.*]] = alloca ptr, align 4
92 // CHECK-NEXT: store ptr [[REFNULL:%.*]], ptr [[REFNULL_ADDR]], align 4
93 // CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[REFNULL_ADDR]], align 4
94 // CHECK-NEXT: [[CALL:%.*]] = call ptr addrspace(20) [[TMP0]]()
95 // CHECK-NEXT: ret ptr addrspace(20) [[CALL]]
97 fn_funcref_t get_null_fptr(builtin_refnull_t refnull) {
98 return refnull();