Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 054e331

Browse files
committed
[OpenCL] Supports optional program scope global variables in C++ for OpenCL 2021
Adds support for macro `__opencl_c_program_scope_global_variables` in C++ for OpenCL 2021 enabling a respective optional core feature from OpenCL 3.0. This change aims to achieve compatibility between C++ for OpenCL 2021 and OpenCL 3.0. Differential Revision: https://reviews.llvm.org/D109305
1 parent 951f362 commit 054e331

File tree

2 files changed

+82
-57
lines changed

2 files changed

+82
-57
lines changed

clang/include/clang/Basic/OpenCLOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class OpenCLOptions {
8080
// C++ for OpenCL inherits rule from OpenCL C v2.0.
8181
bool areProgramScopeVariablesSupported(const LangOptions &Opts) const {
8282
return Opts.getOpenCLCompatibleVersion() == 200 ||
83-
(Opts.OpenCLVersion == 300 &&
83+
(Opts.getOpenCLCompatibleVersion() == 300 &&
8484
isSupported("__opencl_c_program_scope_global_variables", Opts));
8585
}
8686

clang/test/SemaOpenCL/storageclass.cl

Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0 -cl-ext=+__opencl_c_program_scope_global_variables,-__opencl_c_generic_address_space,-__opencl_c_pipes
44
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0 -cl-ext=-__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space
55
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0 -cl-ext=+__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space
6+
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=clc++2021 -cl-ext=-__opencl_c_program_scope_global_variables,-__opencl_c_generic_address_space,-__opencl_c_pipes
7+
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=clc++2021 -cl-ext=+__opencl_c_program_scope_global_variables,-__opencl_c_generic_address_space,-__opencl_c_pipes
8+
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=clc++2021 -cl-ext=-__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space
9+
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=clc++2021 -cl-ext=+__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space
610
static constant int G1 = 0;
711
constant int G2 = 0;
812

@@ -43,19 +47,23 @@ static private float g_private_static_var = 0;
4347
#endif
4448

4549
static generic float g_generic_static_var = 0;
46-
#if (__OPENCL_C_VERSION__ < 300)
50+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
4751
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
4852
// expected-error@-3 {{program scope variable must reside in constant address space}}
49-
#elif (__OPENCL_C_VERSION__ == 300)
50-
#if !defined(__opencl_c_generic_address_space)
51-
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
52-
#endif
53-
#if !defined(__opencl_c_program_scope_global_variables)
54-
// expected-error@-9 {{program scope variable must reside in constant address space}}
55-
#endif
56-
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
57-
// expected-error@-12 {{program scope variable must reside in global or constant address space}}
58-
#endif
53+
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
54+
#if !defined(__opencl_c_generic_address_space)
55+
#if (__OPENCL_C_VERSION__ == 300)
56+
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
57+
#elif (__OPENCL_CPP_VERSION__ == 202100)
58+
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
59+
#endif
60+
#endif
61+
#if !defined(__opencl_c_program_scope_global_variables)
62+
// expected-error@-13 {{program scope variable must reside in constant address space}}
63+
#endif
64+
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
65+
// expected-error@-16 {{program scope variable must reside in global or constant address space}}
66+
#endif
5967
#endif
6068

6169
extern float g_implicit_extern_var;
@@ -85,32 +93,36 @@ extern private float g_private_extern_var;
8593
#endif
8694

8795
extern generic float g_generic_extern_var;
88-
#if (__OPENCL_C_VERSION__ < 300)
96+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
8997
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
9098
// expected-error@-3 {{extern variable must reside in constant address space}}
91-
#elif (__OPENCL_C_VERSION__ == 300)
92-
#if !defined(__opencl_c_generic_address_space)
93-
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
94-
#endif
95-
#if !defined(__opencl_c_program_scope_global_variables)
96-
// expected-error@-9 {{extern variable must reside in constant address space}}
97-
#endif
98-
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
99-
// expected-error@-12 {{extern variable must reside in global or constant address space}}
100-
#endif
99+
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
100+
#if !defined(__opencl_c_generic_address_space)
101+
#if (__OPENCL_C_VERSION__ == 300)
102+
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
103+
#elif (__OPENCL_CPP_VERSION__ == 202100)
104+
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
105+
#endif
106+
#endif
107+
#if !defined(__opencl_c_program_scope_global_variables)
108+
// expected-error@-13 {{extern variable must reside in constant address space}}
109+
#endif
110+
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
111+
// expected-error@-16 {{extern variable must reside in global or constant address space}}
112+
#endif
101113
#endif
102114

103115
void kernel foo(int x) {
104116
// static is not allowed at local scope before CL2.0
105117
static int S1 = 5;
106-
#if __OPENCL_C_VERSION__ < 300
118+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
107119
// expected-error@-2 {{variables in function scope cannot be declared static}}
108120
#elif !defined(__opencl_c_program_scope_global_variables)
109121
// expected-error@-4 {{static local variable must reside in constant address space}}
110122
#endif
111123

112124
static constant int S2 = 5;
113-
#if __OPENCL_C_VERSION__ < 300
125+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
114126
// expected-error@-2 {{variables in function scope cannot be declared static}}
115127
#endif
116128

@@ -122,7 +134,12 @@ void kernel foo(int x) {
122134
constant int L1 = 42; // expected-error {{variables in the constant address space can only be declared in the outermost scope of a kernel function}}
123135
}
124136

125-
auto int L3 = 7; // expected-error-re{{OpenCL C version {{1.2|3.0}} does not support the 'auto' storage class specifier}}
137+
auto int L3 = 7;
138+
#if (__OPENCL_CPP_VERSION__ == 202100)
139+
// expected-error@-2{{C++ for OpenCL version 2021 does not support the 'auto' storage class specifier}}
140+
#else
141+
// expected-error-re@-4{{OpenCL C version {{1.2|3.0}} does not support the 'auto' storage class specifier}}
142+
#endif
126143
global int L4; // expected-error{{function scope variable cannot be declared in global address space}}
127144
__attribute__((address_space(100))) int L5; // expected-error{{automatic variable qualified with an invalid address space}}
128145

@@ -151,26 +168,26 @@ void f() {
151168
}
152169

153170
static float l_implicit_static_var = 0;
154-
#if __OPENCL_C_VERSION__ < 300
171+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
155172
// expected-error@-2 {{variables in function scope cannot be declared static}}
156173
#elif !defined(__opencl_c_program_scope_global_variables)
157174
// expected-error@-4 {{static local variable must reside in constant address space}}
158175
#endif
159176

160177
static constant float l_constant_static_var = 0;
161-
#if __OPENCL_C_VERSION__ < 300
178+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
162179
// expected-error@-2 {{variables in function scope cannot be declared static}}
163180
#endif
164181

165182
static global float l_global_static_var = 0;
166-
#if __OPENCL_C_VERSION__ < 300
183+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
167184
// expected-error@-2 {{variables in function scope cannot be declared static}}
168185
#elif !defined(__opencl_c_program_scope_global_variables)
169186
// expected-error@-4 {{static local variable must reside in constant address space}}
170187
#endif
171188

172189
static local float l_local_static_var = 0;
173-
#if __OPENCL_C_VERSION__ < 300
190+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
174191
// expected-error@-2 {{variables in function scope cannot be declared static}}
175192
#elif !defined(__opencl_c_program_scope_global_variables)
176193
// expected-error@-4 {{static local variable must reside in constant address space}}
@@ -179,7 +196,7 @@ void f() {
179196
#endif
180197

181198
static private float l_private_static_var = 0;
182-
#if __OPENCL_C_VERSION__ < 300
199+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
183200
// expected-error@-2 {{variables in function scope cannot be declared static}}
184201
#elif !defined(__opencl_c_program_scope_global_variables)
185202
// expected-error@-4 {{static local variable must reside in constant address space}}
@@ -188,23 +205,27 @@ void f() {
188205
#endif
189206

190207
static generic float l_generic_static_var = 0;
191-
#if (__OPENCL_C_VERSION__ < 300)
208+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
192209
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
193210
// expected-error@-3 {{variables in function scope cannot be declared static}}
194-
#elif (__OPENCL_C_VERSION__ == 300)
195-
#if !defined(__opencl_c_generic_address_space)
196-
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
197-
#endif
198-
#if !defined(__opencl_c_program_scope_global_variables)
199-
// expected-error@-9 {{static local variable must reside in constant address space}}
200-
#endif
201-
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
202-
// expected-error@-12 {{static local variable must reside in global or constant address space}}
203-
#endif
211+
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
212+
#if !defined(__opencl_c_generic_address_space)
213+
#if (__OPENCL_C_VERSION__ == 300)
214+
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
215+
#elif (__OPENCL_CPP_VERSION__ == 202100)
216+
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
217+
#endif
218+
#endif
219+
#if !defined(__opencl_c_program_scope_global_variables)
220+
// expected-error@-13 {{static local variable must reside in constant address space}}
221+
#endif
222+
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
223+
// expected-error@-16 {{static local variable must reside in global or constant address space}}
224+
#endif
204225
#endif
205226

206227
extern float l_implicit_extern_var;
207-
#if __OPENCL_C_VERSION__ < 300
228+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
208229
// expected-error@-2 {{extern variable must reside in constant address space}}
209230
#elif !defined(__opencl_c_program_scope_global_variables)
210231
// expected-error@-4 {{extern variable must reside in constant address space}}
@@ -213,14 +234,14 @@ void f() {
213234
extern constant float l_constant_extern_var;
214235

215236
extern global float l_global_extern_var;
216-
#if __OPENCL_C_VERSION__ < 300
237+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
217238
// expected-error@-2 {{extern variable must reside in constant address space}}
218239
#elif !defined(__opencl_c_program_scope_global_variables)
219240
// expected-error@-4 {{extern variable must reside in constant address space}}
220241
#endif
221242

222243
extern local float l_local_extern_var;
223-
#if __OPENCL_C_VERSION__ < 300
244+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
224245
// expected-error@-2 {{extern variable must reside in constant address space}}
225246
#elif !defined(__opencl_c_program_scope_global_variables)
226247
// expected-error@-4 {{extern variable must reside in constant address space}}
@@ -229,7 +250,7 @@ void f() {
229250
#endif
230251

231252
extern private float l_private_extern_var;
232-
#if __OPENCL_C_VERSION__ < 300
253+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
233254
// expected-error@-2 {{extern variable must reside in constant address space}}
234255
#elif !defined(__opencl_c_program_scope_global_variables)
235256
// expected-error@-4 {{extern variable must reside in constant address space}}
@@ -238,18 +259,22 @@ void f() {
238259
#endif
239260

240261
extern generic float l_generic_extern_var;
241-
#if (__OPENCL_C_VERSION__ < 300)
262+
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
242263
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
243264
// expected-error@-3 {{extern variable must reside in constant address space}}
244-
#elif (__OPENCL_C_VERSION__ == 300)
245-
#if !defined(__opencl_c_generic_address_space)
246-
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
247-
#endif
248-
#if !defined(__opencl_c_program_scope_global_variables)
249-
// expected-error@-9 {{extern variable must reside in constant address space}}
250-
#endif
251-
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
252-
// expected-error@-12 {{extern variable must reside in global or constant address space}}
253-
#endif
265+
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
266+
#if !defined(__opencl_c_generic_address_space)
267+
#if (__OPENCL_C_VERSION__ == 300)
268+
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
269+
#elif (__OPENCL_CPP_VERSION__ == 202100 && !defined(__opencl_c_generic_address_space))
270+
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
271+
#endif
272+
#endif
273+
#if !defined(__opencl_c_program_scope_global_variables)
274+
// expected-error@-13 {{extern variable must reside in constant address space}}
275+
#endif
276+
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
277+
// expected-error@-16 {{extern variable must reside in global or constant address space}}
278+
#endif
254279
#endif
255280
}

0 commit comments

Comments
 (0)