xref: /llvm-project/clang/test/CodeGenOpenCL/as_type.cl (revision 1228becf7df28c68579f2b9b390b74aa41149a0a)
1// RUN: %clang_cc1 %s -emit-llvm -triple spir-unknown-unknown -o - | FileCheck %s
2
3typedef __attribute__(( ext_vector_type(3) )) char char3;
4typedef __attribute__(( ext_vector_type(4) )) char char4;
5typedef __attribute__(( ext_vector_type(16) )) char char16;
6typedef __attribute__(( ext_vector_type(3) )) int int3;
7
8//CHECK: define{{.*}} spir_func <3 x i8> @f1(<4 x i8> noundef %[[x:.*]])
9//CHECK: %[[astype:.*]] = shufflevector <4 x i8> %[[x]], <4 x i8> poison, <3 x i32> <i32 0, i32 1, i32 2>
10//CHECK: ret <3 x i8> %[[astype]]
11char3 f1(char4 x) {
12  return  __builtin_astype(x, char3);
13}
14
15//CHECK: define{{.*}} spir_func <4 x i8> @f2(<3 x i8> noundef %[[x:.*]])
16//CHECK: %[[astype:.*]] = shufflevector <3 x i8> %[[x]], <3 x i8> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>
17//CHECK: ret <4 x i8> %[[astype]]
18char4 f2(char3 x) {
19  return __builtin_astype(x, char4);
20}
21
22//CHECK: define{{.*}} spir_func <3 x i8> @f3(i32 noundef %[[x:.*]])
23//CHECK: %[[cast:.*]] = bitcast i32 %[[x]] to <4 x i8>
24//CHECK: %[[astype:.*]] = shufflevector <4 x i8> %[[cast]], <4 x i8> poison, <3 x i32> <i32 0, i32 1, i32 2>
25//CHECK: ret <3 x i8> %[[astype]]
26char3 f3(int x) {
27  return __builtin_astype(x, char3);
28}
29
30//CHECK: define{{.*}} spir_func noundef <4 x i8> @f4(i32 noundef %[[x:.*]])
31//CHECK: %[[astype:.*]] = bitcast i32 %[[x]] to <4 x i8>
32//CHECK-NOT: shufflevector
33//CHECK: ret <4 x i8> %[[astype]]
34char4 f4(int x) {
35  return __builtin_astype(x, char4);
36}
37
38//CHECK: define{{.*}} spir_func i32 @f5(<3 x i8> noundef %[[x:.*]])
39//CHECK: %[[shuffle:.*]] = shufflevector <3 x i8> %[[x]], <3 x i8> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>
40//CHECK: %[[astype:.*]] = bitcast <4 x i8> %[[shuffle]] to i32
41//CHECK: ret i32 %[[astype]]
42int f5(char3 x) {
43  return __builtin_astype(x, int);
44}
45
46//CHECK: define{{.*}} spir_func noundef i32 @f6(<4 x i8> noundef %[[x:.*]])
47//CHECK: %[[astype:.*]] = bitcast <4 x i8> %[[x]] to i32
48//CHECK-NOT: shufflevector
49//CHECK: ret i32 %[[astype]]
50int f6(char4 x) {
51  return __builtin_astype(x, int);
52}
53
54//CHECK: define{{.*}} spir_func noundef <3 x i8> @f7(<3 x i8> noundef returned %[[x:.*]])
55//CHECK-NOT: bitcast
56//CHECK-NOT: shufflevector
57//CHECK: ret <3 x i8> %[[x]]
58char3 f7(char3 x) {
59  return __builtin_astype(x, char3);
60}
61
62//CHECK: define{{.*}} spir_func <3 x i32> @f8(<16 x i8> noundef %[[x:.*]])
63//CHECK: %[[cast:.*]] = bitcast <16 x i8> %[[x]] to <4 x i32>
64//CHECK: %[[astype:.*]] = shufflevector <4 x i32> %[[cast]], <4 x i32> poison, <3 x i32> <i32 0, i32 1, i32 2>
65//CHECK: ret <3 x i32> %[[astype]]
66int3 f8(char16 x) {
67  return __builtin_astype(x, int3);
68}
69
70//CHECK: define{{.*}} spir_func noundef ptr addrspace(1) @addr_cast(ptr noundef readnone %[[x:.*]])
71//CHECK: %[[cast:.*]] ={{.*}} addrspacecast ptr %[[x]] to ptr addrspace(1)
72//CHECK: ret ptr addrspace(1) %[[cast]]
73global int* addr_cast(int *x) {
74  return __builtin_astype(x, global int*);
75}
76
77//CHECK: define{{.*}} spir_func noundef ptr addrspace(1) @int_to_ptr(i32 noundef %[[x:.*]])
78//CHECK: %[[cast:.*]] = inttoptr i32 %[[x]] to ptr addrspace(1)
79//CHECK: ret ptr addrspace(1) %[[cast]]
80global int* int_to_ptr(int x) {
81  return __builtin_astype(x, global int*);
82}
83
84//CHECK: define{{.*}} spir_func noundef i32 @ptr_to_int(ptr noundef %[[x:.*]])
85//CHECK: %[[cast:.*]] = ptrtoint ptr %[[x]] to i32
86//CHECK: ret i32 %[[cast]]
87int ptr_to_int(int *x) {
88  return __builtin_astype(x, int);
89}
90
91//CHECK: define{{.*}} spir_func <3 x i8> @ptr_to_char3(ptr noundef %[[x:.*]])
92//CHECK: %[[cast1:.*]] = ptrtoint ptr %[[x]] to i32
93//CHECK: %[[cast2:.*]] = bitcast i32 %[[cast1]] to <4 x i8>
94//CHECK: %[[astype:.*]] = shufflevector <4 x i8> %[[cast2]], <4 x i8> poison, <3 x i32> <i32 0, i32 1, i32 2>
95//CHECK: ret <3 x i8> %[[astype]]
96char3 ptr_to_char3(int *x) {
97  return  __builtin_astype(x, char3);
98}
99
100//CHECK: define{{.*}} spir_func ptr @char3_to_ptr(<3 x i8> noundef %[[x:.*]])
101//CHECK: %[[astype:.*]] = shufflevector <3 x i8> %[[x]], <3 x i8> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>
102//CHECK: %[[cast1:.*]] = bitcast <4 x i8> %[[astype]] to i32
103//CHECK: %[[cast2:.*]] = inttoptr i32 %[[cast1]] to ptr
104//CHECK: ret ptr %[[cast2]]
105int* char3_to_ptr(char3 x) {
106  return __builtin_astype(x, int*);
107}
108