1// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL -fdeclare-opencl-builtins -DNO_HEADER
2// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL -fdeclare-opencl-builtins -finclude-default-header
3// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL1.2 -fdeclare-opencl-builtins -DNO_HEADER
4// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL1.2 -fdeclare-opencl-builtins -finclude-default-header
5// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL2.0 -fdeclare-opencl-builtins -DNO_HEADER
6// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CL2.0 -fdeclare-opencl-builtins -finclude-default-header
7// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CLC++ -fdeclare-opencl-builtins -DNO_HEADER
8// RUN: %clang_cc1 %s -triple spir -verify -pedantic -Wconversion -Werror -fsyntax-only -cl-std=CLC++ -fdeclare-opencl-builtins -finclude-default-header
9
10// Test the -fdeclare-opencl-builtins option.
11
12#pragma OPENCL EXTENSION cl_khr_fp16 : enable
13#if __OPENCL_C_VERSION__ < CL_VERSION_1_2
14#pragma OPENCL EXTENSION cl_khr_fp64 : enable
15#endif
16
17// Provide typedefs when invoking clang without -finclude-default-header.
18#ifdef NO_HEADER
19typedef unsigned char uchar;
20typedef unsigned int uint;
21typedef unsigned long ulong;
22typedef unsigned short ushort;
23typedef __SIZE_TYPE__ size_t;
24typedef char char2 __attribute__((ext_vector_type(2)));
25typedef char char4 __attribute__((ext_vector_type(4)));
26typedef uchar uchar4 __attribute__((ext_vector_type(4)));
27typedef float float4 __attribute__((ext_vector_type(4)));
28typedef half half4 __attribute__((ext_vector_type(4)));
29typedef int int2 __attribute__((ext_vector_type(2)));
30typedef int int4 __attribute__((ext_vector_type(4)));
31typedef uint uint4 __attribute__((ext_vector_type(4)));
32typedef long long2 __attribute__((ext_vector_type(2)));
33#endif
34
35kernel void test_pointers(volatile global void *global_p, global const int4 *a) {
36  int i;
37  unsigned int ui;
38
39  prefetch(a, 2);
40
41  atom_add((volatile __global int *)global_p, i);
42#if !defined(__OPENCL_CPP_VERSION__) && __OPENCL_C_VERSION__ < CL_VERSION_1_1
43// expected-error@-2{{no matching function for call to 'atom_add'}}
44
45// There are two potential definitions of the function "atom_add", both are
46// currently disabled because the associated extension is disabled.
47// expected-note@-6{{candidate function not viable: cannot pass pointer to address space '__global' as a pointer to address space '__local' in 1st argument}}
48// expected-note@-7{{candidate function not viable: no known conversion}}
49// expected-note@-8{{candidate function not viable: no known conversion}}
50// expected-note@-9{{candidate function not viable: no known conversion}}
51// expected-note@-10{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
52// expected-note@-11{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
53// expected-note@-12{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
54// expected-note@-13{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
55#endif
56
57#if __OPENCL_C_VERSION__ < CL_VERSION_1_1
58#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
59#endif
60
61  atom_add((volatile __global int *)global_p, i);
62  atom_cmpxchg((volatile __global unsigned int *)global_p, ui, ui);
63}
64
65kernel void basic_conversion() {
66  double d;
67  float f;
68  char2 c2;
69  long2 l2;
70  float4 f4;
71  int4 i4;
72
73  f = convert_float(d);
74  d = convert_double_rtp(f);
75  l2 = convert_long2_rtz(c2);
76  i4 = convert_int4_sat(f4);
77}
78
79kernel void basic_conversion_neg() {
80  int i;
81  float f;
82
83  f = convert_float_sat(i);
84#if !defined(__OPENCL_CPP_VERSION__)
85  // expected-error@-2{{implicit declaration of function 'convert_float_sat' is invalid in OpenCL}}
86  // expected-error@-3{{implicit conversion from 'int' to 'float' may lose precision}}
87#else
88  // expected-error@-5{{use of undeclared identifier 'convert_float_sat'; did you mean 'convert_float'?}}
89  // expected-note@-6{{'convert_float' declared here}}
90#endif
91}
92
93char4 test_int(char c, char4 c4) {
94  char m = max(c, c);
95  char4 m4 = max(c4, c4);
96  uchar4 abs1 = abs(c4);
97  uchar4 abs2 = abs(abs1);
98  return max(c4, c);
99}
100
101kernel void basic_vector_misc(float4 a) {
102  float4 res;
103  uint4 mask = (uint4)(1, 2, 3, 4);
104
105  res = shuffle(a, mask);
106}
107
108kernel void basic_image_readonly(read_only image2d_t image_read_only_image2d) {
109  int2 i2;
110  sampler_t sampler;
111  half4 res;
112  float4 resf;
113
114  resf = read_imagef(image_read_only_image2d, i2);
115  res = read_imageh(image_read_only_image2d, i2);
116  res = read_imageh(image_read_only_image2d, sampler, i2);
117
118  int imgWidth = get_image_width(image_read_only_image2d);
119}
120
121#if __OPENCL_C_VERSION__ >= CL_VERSION_2_0
122kernel void basic_image_readwrite(read_write image3d_t image_read_write_image3d) {
123  half4 h4;
124  int4 i4;
125
126  write_imageh(image_read_write_image3d, i4, h4);
127
128  int imgDepth = get_image_depth(image_read_write_image3d);
129}
130#endif // __OPENCL_C_VERSION__ >= CL_VERSION_2_0
131
132kernel void basic_image_writeonly(write_only image1d_buffer_t image_write_only_image1d_buffer) {
133  half4 h4;
134  float4 f4;
135  int i;
136
137  write_imagef(image_write_only_image1d_buffer, i, f4);
138  write_imageh(image_write_only_image1d_buffer, i, h4);
139}
140
141kernel void basic_subgroup(global uint *out) {
142  out[0] = get_sub_group_size();
143#if defined(__OPENCL_CPP_VERSION__)
144  // expected-error@-2{{no matching function for call to 'get_sub_group_size'}}
145  // expected-note@-3{{candidate unavailable as it requires OpenCL extension 'cl_khr_subgroups' to be enabled}}
146#else
147  // expected-error@-5{{use of declaration 'get_sub_group_size' requires cl_khr_subgroups extension to be enabled}}
148#endif
149}
150
151kernel void basic_vector_data() {
152#if __OPENCL_C_VERSION__ >= CL_VERSION_2_0
153  generic void *generic_p;
154#endif
155  constant void *constant_p;
156  local void *local_p;
157  global void *global_p;
158  private void *private_p;
159  size_t s;
160
161  vload4(s, (const __constant ulong *) constant_p);
162  vload16(s, (const __constant short *) constant_p);
163
164#if __OPENCL_C_VERSION__ >= CL_VERSION_2_0
165  vload3(s, (const __generic ushort *) generic_p);
166  vload16(s, (const __generic uchar *) generic_p);
167#endif
168
169  vload8(s, (const __global long *) global_p);
170  vload2(s, (const __local uint *) local_p);
171  vload16(s, (const __private float *) private_p);
172}
173
174kernel void basic_work_item() {
175  uint ui;
176
177  get_enqueued_local_size(ui);
178#if !defined(__OPENCL_CPP_VERSION__) && __OPENCL_C_VERSION__ < CL_VERSION_2_0
179// expected-error@-2{{implicit declaration of function 'get_enqueued_local_size' is invalid in OpenCL}}
180#endif
181}
182