Topotuna created this revision.
Topotuna added a reviewer: Anastasia.
Herald added subscribers: ldrumm, yaxunl.
Topotuna requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109305

Files:
  clang/include/clang/Basic/OpenCLOptions.h
  clang/test/SemaOpenCL/storageclass.cl

Index: clang/test/SemaOpenCL/storageclass.cl
===================================================================
--- clang/test/SemaOpenCL/storageclass.cl
+++ clang/test/SemaOpenCL/storageclass.cl
@@ -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;
 
@@ -43,19 +47,23 @@
 #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;
@@ -85,32 +93,36 @@
 #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
 
@@ -122,7 +134,12 @@
     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}}
 
@@ -151,26 +168,26 @@
   }
 
   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}}
@@ -179,7 +196,7 @@
 #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}}
@@ -188,23 +205,27 @@
 #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}}
@@ -213,14 +234,14 @@
   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}}
@@ -229,7 +250,7 @@
 #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}}
@@ -238,18 +259,22 @@
 #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
 }
Index: clang/include/clang/Basic/OpenCLOptions.h
===================================================================
--- clang/include/clang/Basic/OpenCLOptions.h
+++ clang/include/clang/Basic/OpenCLOptions.h
@@ -80,7 +80,7 @@
   // 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));
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D109305: [OpenCL... Justas Janickas via Phabricator via cfe-commits

Reply via email to