1// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify | FileCheck %s
2// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify -cl-std=CL1.1 | FileCheck %s
3// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify -cl-std=CL1.2 | FileCheck %s
4// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify -cl-std=clc++1.0 | FileCheck %s --check-prefix=CHECK20
5// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify -cl-std=CL3.0 | FileCheck %s
6// RUN: %clang_cc1 -O0 -triple spir-unknown-unknown -internal-isystem ../../lib/Headers -include opencl-c.h -emit-llvm -o - %s -verify -cl-std=clc++2021 | FileCheck %s
7
8// Test including the default header as a module.
9// The module should be compiled only once and loaded from cache afterwards.
10// Change the directory mode to read only to make sure no new modules are created.
11// Check time report to make sure module is used.
12// Check that some builtins occur in the generated IR when called.
13
14// ===
15// Clear current directory.
16// RUN: rm -rf %t
17// RUN: mkdir -p %t
18
19// ===
20// Compile for OpenCL 1.0 for the first time. A module should be generated.
21// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -o - -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -fdisable-module-hash -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-MOD %s
22// RUN: chmod u-w %t/opencl_c.pcm
23
24// ===
25// Compile for OpenCL 1.0 for the second time. The module should not be re-created.
26// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -o - -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -fdisable-module-hash -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-MOD %s
27// RUN: chmod u+w %t/opencl_c.pcm
28// RUN: mv %t/opencl_c.pcm %t/1_0.pcm
29
30// ===
31// Compile for OpenCL 2.0 for the first time. The module should change.
32// RUN: %clang_cc1 -triple spir-unknown-unknown -O0 -emit-llvm -o - -cl-std=CL2.0 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -fdisable-module-hash -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK20 --check-prefix=CHECK-MOD %s
33// RUN: not diff %t/1_0.pcm %t/opencl_c.pcm
34// RUN: chmod u-w %t/opencl_c.pcm
35
36// ===
37// Compile for OpenCL 2.0 for the second time. The module should not change.
38// RUN: %clang_cc1 -triple spir-unknown-unknown -O0 -emit-llvm -o - -cl-std=CL2.0 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -fdisable-module-hash -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK20 --check-prefix=CHECK-MOD %s
39
40// Check cached module works for different OpenCL versions.
41// RUN: rm -rf %t
42// RUN: mkdir -p %t
43// RUN: %clang_cc1 -triple spir64-unknown-unknown -emit-llvm -o - -cl-std=CL1.2 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-MOD %s
44// RUN: %clang_cc1 -triple amdgcn--amdhsa -O0 -emit-llvm -o - -cl-std=CL2.0 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK20 --check-prefix=CHECK-MOD %s
45// RUN: chmod u-w %t
46// RUN: %clang_cc1 -triple spir64-unknown-unknown -emit-llvm -o - -cl-std=CL1.2 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-MOD %s
47// RUN: %clang_cc1 -triple amdgcn--amdhsa -O0 -emit-llvm -o - -cl-std=CL2.0 -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ftime-report %s 2>&1 | FileCheck --check-prefix=CHECK20 --check-prefix=CHECK-MOD %s
48// RUN: chmod u+w %t
49
50// Verify that called builtins occur in the generated IR.
51
52// CHECK-NOT: intel_sub_group_avc_mce_get_default_inter_base_multi_reference_penalty
53// CHECK-NOT: ndrange_t
54// CHECK20: ndrange_t
55// CHECK: _Z16convert_char_rtec
56// CHECK-NOT: _Z3ctzc
57// CHECK20: _Z3ctzc
58// CHECK20: _Z16convert_char_rtec
59char f(char x) {
60// Check functionality from OpenCL 2.0 onwards
61#if (__OPENCL_CPP_VERSION__ == 100) || (__OPENCL_C_VERSION__ == CL_VERSION_2_0)
62  ndrange_t t;
63  x = ctz(x);
64#endif //__OPENCL_C_VERSION__
65  return convert_char_rte(x);
66}
67
68// Verify that a builtin using a write_only image3d_t type is available
69// from OpenCL 2.0 onwards.
70
71// CHECK20: _Z12write_imagef14ocl_image3d_wo
72#if defined(__OPENCL_CPP_VERSION__) || (__OPENCL_C_VERSION__ >= CL_VERSION_2_0)
73void test_image3dwo(write_only image3d_t img) {
74  write_imagef(img, (0), (0.0f));
75}
76#endif //__OPENCL_C_VERSION__
77
78#if defined(__OPENCL_CPP_VERSION__)
79// Test old atomic overloaded with generic addr space.
80void test_atomics(__generic volatile unsigned int* a) {
81  atomic_add(a, 1);
82}
83#endif
84
85// Verify that ATOMIC_VAR_INIT is defined.
86#if (__OPENCL_CPP_VERSION__ == 100) || (__OPENCL_C_VERSION__ == CL_VERSION_2_0)
87global atomic_int z = ATOMIC_VAR_INIT(99);
88#endif //__OPENCL_C_VERSION__
89// CHECK-MOD: Reading modules
90
91// Check that extension macros are defined correctly.
92
93// For SPIR all extensions are supported.
94#if defined(__SPIR__)
95
96// Verify that cl_intel_planar_yuv extension is defined from OpenCL 1.2 onwards.
97#if defined(__OPENCL_CPP_VERSION__) || (__OPENCL_C_VERSION__ >= CL_VERSION_1_2)
98// expected-no-diagnostics
99#else //__OPENCL_C_VERSION__
100// expected-warning@+2{{unknown OpenCL extension 'cl_intel_planar_yuv' - ignoring}}
101#endif //__OPENCL_C_VERSION__
102#pragma OPENCL EXTENSION cl_intel_planar_yuv : enable
103
104#if (defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= 200)
105
106#if cl_khr_subgroup_extended_types != 1
107#error "Incorrectly defined cl_khr_subgroup_extended_types"
108#endif
109#if cl_khr_subgroup_non_uniform_vote != 1
110#error "Incorrectly defined cl_khr_subgroup_non_uniform_vote"
111#endif
112#if cl_khr_subgroup_ballot != 1
113#error "Incorrectly defined cl_khr_subgroup_ballot"
114#endif
115#if cl_khr_subgroup_non_uniform_arithmetic != 1
116#error "Incorrectly defined cl_khr_subgroup_non_uniform_arithmetic"
117#endif
118#if cl_khr_subgroup_shuffle != 1
119#error "Incorrectly defined cl_khr_subgroup_shuffle"
120#endif
121#if cl_khr_subgroup_shuffle_relative != 1
122#error "Incorrectly defined cl_khr_subgroup_shuffle_relative"
123#endif
124#if cl_khr_subgroup_clustered_reduce != 1
125#error "Incorrectly defined cl_khr_subgroup_clustered_reduce"
126#endif
127#if cl_khr_extended_bit_ops != 1
128#error "Incorrectly defined cl_khr_extended_bit_ops"
129#endif
130#if cl_khr_integer_dot_product != 1
131#error "Incorrectly defined cl_khr_integer_dot_product"
132#endif
133#if __opencl_c_integer_dot_product_input_4x8bit != 1
134#error "Incorrectly defined __opencl_c_integer_dot_product_input_4x8bit"
135#endif
136#if __opencl_c_integer_dot_product_input_4x8bit_packed != 1
137#error "Incorrectly defined __opencl_c_integer_dot_product_input_4x8bit_packed"
138#endif
139#if cl_ext_float_atomics != 1
140#error "Incorrectly defined cl_ext_float_atomics"
141#endif
142#if __opencl_c_ext_fp16_global_atomic_load_store != 1
143#error "Incorrectly defined __opencl_c_ext_fp16_global_atomic_load_store"
144#endif
145#if __opencl_c_ext_fp16_local_atomic_load_store != 1
146#error "Incorrectly defined __opencl_c_ext_fp16_local_atomic_load_store"
147#endif
148#if __opencl_c_ext_fp16_global_atomic_add != 1
149#error "Incorrectly defined __opencl_c_ext_fp16_global_atomic_add"
150#endif
151#if __opencl_c_ext_fp32_global_atomic_add != 1
152#error "Incorrectly defined __opencl_c_ext_fp32_global_atomic_add"
153#endif
154#if __opencl_c_ext_fp64_global_atomic_add != 1
155#error "Incorrectly defined __opencl_c_ext_fp64_global_atomic_add"
156#endif
157#if __opencl_c_ext_fp16_local_atomic_add != 1
158#error "Incorrectly defined __opencl_c_ext_fp16_local_atomic_add"
159#endif
160#if __opencl_c_ext_fp32_local_atomic_add != 1
161#error "Incorrectly defined __opencl_c_ext_fp32_local_atomic_add"
162#endif
163#if __opencl_c_ext_fp64_local_atomic_add != 1
164#error "Incorrectly defined __opencl_c_ext_fp64_local_atomic_add"
165#endif
166#if __opencl_c_ext_fp16_global_atomic_min_max != 1
167#error "Incorrectly defined __opencl_c_ext_fp16_global_atomic_min_max"
168#endif
169#if __opencl_c_ext_fp32_global_atomic_min_max != 1
170#error "Incorrectly defined __opencl_c_ext_fp32_global_atomic_min_max"
171#endif
172#if __opencl_c_ext_fp64_global_atomic_min_max != 1
173#error "Incorrectly defined __opencl_c_ext_fp64_global_atomic_min_max"
174#endif
175#if __opencl_c_ext_fp16_local_atomic_min_max != 1
176#error "Incorrectly defined __opencl_c_ext_fp16_local_atomic_min_max"
177#endif
178#if __opencl_c_ext_fp32_local_atomic_min_max != 1
179#error "Incorrectly defined __opencl_c_ext_fp32_local_atomic_min_max"
180#endif
181#if __opencl_c_ext_fp64_local_atomic_min_max != 1
182#error "Incorrectly defined __opencl_c_ext_fp64_local_atomic_min_max"
183#endif
184
185#else
186
187#ifdef cl_khr_subgroup_extended_types
188#error "Incorrect cl_khr_subgroup_extended_types define"
189#endif
190#ifdef cl_khr_subgroup_non_uniform_vote
191#error "Incorrect cl_khr_subgroup_non_uniform_vote define"
192#endif
193#ifdef cl_khr_subgroup_ballot
194#error "Incorrect cl_khr_subgroup_ballot define"
195#endif
196#ifdef cl_khr_subgroup_non_uniform_arithmetic
197#error "Incorrect cl_khr_subgroup_non_uniform_arithmetic define"
198#endif
199#ifdef cl_khr_subgroup_shuffle
200#error "Incorrect cl_khr_subgroup_shuffle define"
201#endif
202#ifdef cl_khr_subgroup_shuffle_relative
203#error "Incorrect cl_khr_subgroup_shuffle_relative define"
204#endif
205#ifdef cl_khr_subgroup_clustered_reduce
206#error "Incorrect cl_khr_subgroup_clustered_reduce define"
207#endif
208#ifdef cl_khr_extended_bit_ops
209#error "Incorrect cl_khr_extended_bit_ops define"
210#endif
211#ifdef cl_khr_integer_dot_product
212#error "Incorrect cl_khr_integer_dot_product define"
213#endif
214#ifdef __opencl_c_integer_dot_product_input_4x8bit
215#error "Incorrect __opencl_c_integer_dot_product_input_4x8bit define"
216#endif
217#ifdef __opencl_c_integer_dot_product_input_4x8bit_packed
218#error "Incorrect __opencl_c_integer_dot_product_input_4x8bit_packed define"
219#endif
220#ifdef cl_ext_float_atomics
221#error "Incorrect cl_ext_float_atomics define"
222#endif
223#ifdef __opencl_c_ext_fp16_global_atomic_load_store
224#error "Incorrectly __opencl_c_ext_fp16_global_atomic_load_store defined"
225#endif
226#ifdef __opencl_c_ext_fp16_local_atomic_load_store
227#error "Incorrectly __opencl_c_ext_fp16_local_atomic_load_store defined"
228#endif
229#ifdef __opencl_c_ext_fp16_global_atomic_add
230#error "Incorrectly __opencl_c_ext_fp16_global_atomic_add defined"
231#endif
232#ifdef __opencl_c_ext_fp32_global_atomic_add
233#error "Incorrectly __opencl_c_ext_fp32_global_atomic_add defined"
234#endif
235#ifdef __opencl_c_ext_fp64_global_atomic_add
236#error "Incorrectly __opencl_c_ext_fp64_global_atomic_add defined"
237#endif
238#ifdef __opencl_c_ext_fp16_local_atomic_add
239#error "Incorrectly __opencl_c_ext_fp16_local_atomic_add defined"
240#endif
241#ifdef __opencl_c_ext_fp32_local_atomic_add
242#error "Incorrectly __opencl_c_ext_fp32_local_atomic_add defined"
243#endif
244#ifdef __opencl_c_ext_fp64_local_atomic_add
245#error "Incorrectly __opencl_c_ext_fp64_local_atomic_add defined"
246#endif
247#ifdef __opencl_c_ext_fp16_global_atomic_min_max
248#error "Incorrectly __opencl_c_ext_fp16_global_atomic_min_max defined"
249#endif
250#ifdef __opencl_c_ext_fp32_global_atomic_min_max
251#error "Incorrectly __opencl_c_ext_fp32_global_atomic_min_max defined"
252#endif
253#ifdef __opencl_c_ext_fp64_global_atomic_min_max
254#error "Incorrectly __opencl_c_ext_fp64_global_atomic_min_max defined"
255#endif
256#ifdef __opencl_c_ext_fp16_local_atomic_min_max
257#error "Incorrectly __opencl_c_ext_fp16_local_atomic_min_max defined"
258#endif
259#ifdef __opencl_c_ext_fp32_local_atomic_min_max
260#error "Incorrectly __opencl_c_ext_fp32_local_atomic_min_max defined"
261#endif
262#ifdef __opencl_c_ext_fp64_local_atomic_min_max
263#error "Incorrectly __opencl_c_ext_fp64_local_atomic_min_max defined"
264#endif
265
266#endif //(defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= 200)
267
268// OpenCL C features.
269#if (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
270
271#if __opencl_c_atomic_scope_all_devices != 1
272#error "Incorrectly defined feature macro __opencl_c_atomic_scope_all_devices"
273#endif
274
275#elif (__OPENCL_CPP_VERSION__ == 100 || __OPENCL_C_VERSION__ == 200)
276
277#ifndef  __opencl_c_pipes
278#error "Feature macro __opencl_c_pipes should be defined"
279#endif
280#ifndef __opencl_c_generic_address_space
281#error "Feature macro __opencl_c_generic_address_space should be defined"
282#endif
283#ifndef __opencl_c_work_group_collective_functions
284#error "Feature macro __opencl_c_work_group_collective_functions should be defined"
285#endif
286#ifndef __opencl_c_atomic_order_acq_rel
287#error "Feature macro __opencl_c_atomic_order_acq_rel should be defined"
288#endif
289#ifndef __opencl_c_atomic_order_seq_cst
290#error "Feature macro __opencl_c_atomic_order_seq_cst should be defined"
291#endif
292#ifndef __opencl_c_atomic_scope_device
293#error "Feature macro __opencl_c_atomic_scope_device should be defined"
294#endif
295#ifndef __opencl_c_atomic_scope_all_devices
296#error "Feature macro __opencl_c_atomic_scope_all_devices should be defined"
297#endif
298#ifndef __opencl_c_device_enqueue
299#error "Feature macro __opencl_c_device_enqueue should be defined"
300#endif
301#ifndef __opencl_c_read_write_images
302#error "Feature macro __opencl_c_read_write_images should be defined"
303#endif
304#ifndef __opencl_c_program_scope_global_variables
305#error "Feature macro __opencl_c_program_scope_global_variables should be defined"
306#endif
307#ifndef __opencl_c_images
308#error "Feature macro __opencl_c_images should be defined"
309#endif
310
311#elif (__OPENCL_C_VERSION__ < 200)
312
313#ifdef  __opencl_c_pipes
314#error "Incorrect feature macro __opencl_c_pipes define"
315#endif
316#ifdef __opencl_c_generic_address_space
317#error "Incorrect feature macro __opencl_c_generic_address_space define"
318#endif
319#ifdef __opencl_c_work_group_collective_functions
320#error "Incorrect feature macro __opencl_c_work_group_collective_functions define"
321#endif
322#ifdef __opencl_c_atomic_order_acq_rel
323#error "Incorrect feature macro __opencl_c_atomic_order_acq_rel define"
324#endif
325#ifdef __opencl_c_atomic_order_seq_cst
326#error "Incorrect feature macro __opencl_c_atomic_order_seq_cst define"
327#endif
328#ifdef __opencl_c_atomic_scope_device
329#error "Incorrect feature macro __opencl_c_atomic_scope_device define"
330#endif
331#ifdef __opencl_c_atomic_scope_all_devices
332#error "Incorrect feature macro __opencl_c_atomic_scope_all_devices define"
333#endif
334#ifdef __opencl_c_device_enqueue
335#error "Incorrect feature macro __opencl_c_device_enqueue define"
336#endif
337#ifdef __opencl_c_read_write_images
338#error "Incorrect feature macro __opencl_c_read_write_images define"
339#endif
340#ifdef __opencl_c_program_scope_global_variables
341#error "Incorrect feature macro __opencl_c_program_scope_global_variables define"
342#endif
343#ifdef __opencl_c_images
344#error "Incorrect feature macro __opencl_c_images define"
345#endif
346#ifdef __opencl_c_3d_image_writes
347#error "Incorrect feature macro __opencl_c_3d_image_writes define"
348#endif
349#ifdef __opencl_c_fp64
350#error "Incorrect feature macro __opencl_c_fp64 define"
351#endif
352#ifdef __opencl_c_subgroups
353#error "Incorrect feature macro __opencl_c_subgroups define"
354#endif
355
356#endif // (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
357
358#endif // defined(__SPIR__)
359