Skip to content

Commit

Permalink
[OpenCL] Supports optional program scope global variables in C++ for …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Topotuna committed Sep 16, 2021
1 parent 951f362 commit 054e331
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 57 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/OpenCLOptions.h
Expand Up @@ -80,7 +80,7 @@ class OpenCLOptions {
// C++ for OpenCL inherits rule from OpenCL C v2.0.
bool areProgramScopeVariablesSupported(const LangOptions &Opts) const {
return Opts.getOpenCLCompatibleVersion() == 200 ||
(Opts.OpenCLVersion == 300 &&
(Opts.getOpenCLCompatibleVersion() == 300 &&
isSupported("__opencl_c_program_scope_global_variables", Opts));
}

Expand Down
137 changes: 81 additions & 56 deletions clang/test/SemaOpenCL/storageclass.cl
Expand Up @@ -3,6 +3,10 @@
// 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
// 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
// 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
// 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
// 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
// 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
// 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
static constant int G1 = 0;
constant int G2 = 0;

Expand Down Expand Up @@ -43,19 +47,23 @@ static private float g_private_static_var = 0;
#endif

static generic float g_generic_static_var = 0;
#if (__OPENCL_C_VERSION__ < 300)
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
// expected-error@-3 {{program scope variable must reside in constant address space}}
#elif (__OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-9 {{program scope variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-12 {{program scope variable must reside in global or constant address space}}
#endif
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
#if (__OPENCL_C_VERSION__ == 300)
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#elif (__OPENCL_CPP_VERSION__ == 202100)
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
#endif
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-13 {{program scope variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-16 {{program scope variable must reside in global or constant address space}}
#endif
#endif

extern float g_implicit_extern_var;
Expand Down Expand Up @@ -85,32 +93,36 @@ extern private float g_private_extern_var;
#endif

extern generic float g_generic_extern_var;
#if (__OPENCL_C_VERSION__ < 300)
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
// expected-error@-3 {{extern variable must reside in constant address space}}
#elif (__OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-9 {{extern variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-12 {{extern variable must reside in global or constant address space}}
#endif
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
#if (__OPENCL_C_VERSION__ == 300)
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#elif (__OPENCL_CPP_VERSION__ == 202100)
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
#endif
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-13 {{extern variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-16 {{extern variable must reside in global or constant address space}}
#endif
#endif

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

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

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

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

Expand Down Expand Up @@ -151,26 +168,26 @@ void f() {
}

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

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

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

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

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

static generic float l_generic_static_var = 0;
#if (__OPENCL_C_VERSION__ < 300)
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
// expected-error@-3 {{variables in function scope cannot be declared static}}
#elif (__OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-9 {{static local variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-12 {{static local variable must reside in global or constant address space}}
#endif
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
#if (__OPENCL_C_VERSION__ == 300)
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#elif (__OPENCL_CPP_VERSION__ == 202100)
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
#endif
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-13 {{static local variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-16 {{static local variable must reside in global or constant address space}}
#endif
#endif

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

extern global float l_global_extern_var;
#if __OPENCL_C_VERSION__ < 300
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
// expected-error@-2 {{extern variable must reside in constant address space}}
#elif !defined(__opencl_c_program_scope_global_variables)
// expected-error@-4 {{extern variable must reside in constant address space}}
#endif

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

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

extern generic float l_generic_extern_var;
#if (__OPENCL_C_VERSION__ < 300)
#if (defined(__OPENCL_C_VERSION__) && __OPENCL_C_VERSION__ < 300)
// expected-error@-2 {{OpenCL C version 1.2 does not support the 'generic' type qualifier}}
// expected-error@-3 {{extern variable must reside in constant address space}}
#elif (__OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
// expected-error@-6 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-9 {{extern variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-12 {{extern variable must reside in global or constant address space}}
#endif
#elif (__OPENCL_CPP_VERSION__ == 202100 || __OPENCL_C_VERSION__ == 300)
#if !defined(__opencl_c_generic_address_space)
#if (__OPENCL_C_VERSION__ == 300)
// expected-error@-7 {{OpenCL C version 3.0 does not support the 'generic' type qualifier}}
#elif (__OPENCL_CPP_VERSION__ == 202100 && !defined(__opencl_c_generic_address_space))
// expected-error@-9 {{C++ for OpenCL version 2021 does not support the 'generic' type qualifier}}
#endif
#endif
#if !defined(__opencl_c_program_scope_global_variables)
// expected-error@-13 {{extern variable must reside in constant address space}}
#endif
#if defined(__opencl_c_generic_address_space) && defined(__opencl_c_program_scope_global_variables)
// expected-error@-16 {{extern variable must reside in global or constant address space}}
#endif
#endif
}

0 comments on commit 054e331

Please sign in to comment.