Anastasia created this revision.
Anastasia added reviewers: pekka.jaaskelainen, yaxunl.
Anastasia added a subscriber: cfe-commits.

This patch:
1. Prevents local variables to be declared in global AS
2. Diagnoses AS of local variables with an extern storage class as if they 
would be in a program scope

http://reviews.llvm.org/D17345

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaDecl.cpp
  test/Parser/opencl-storage-class.cl
  test/SemaOpenCL/storageclass-cl20.cl
  test/SemaOpenCL/storageclass.cl

Index: test/SemaOpenCL/storageclass.cl
===================================================================
--- test/SemaOpenCL/storageclass.cl
+++ test/SemaOpenCL/storageclass.cl
@@ -21,6 +21,8 @@
   local int L2;
 
   auto int L3 = 7; // expected-error{{OpenCL does not support the 'auto' storage class specifier}}
+  global int L4;   // expected-error{{non-program scope variable cannot be declared in global address space}}
+  global int *L5;
 }
 
 static void kernel bar() { // expected-error{{kernel functions cannot be declared static}}
@@ -33,4 +35,6 @@
     constant int L1 = 0; // expected-error{{non-kernel function variable cannot be declared in constant address space}}
     local int L2;        // expected-error{{non-kernel function variable cannot be declared in local address space}}
   }
+  global int L3; // expected-error{{non-program scope variable cannot be declared in global address space}}
+  extern constant float L4;
 }
Index: test/SemaOpenCL/storageclass-cl20.cl
===================================================================
--- test/SemaOpenCL/storageclass-cl20.cl
+++ test/SemaOpenCL/storageclass-cl20.cl
@@ -6,6 +6,7 @@
 local int G4 = 0;              // expected-error{{program scope variable must reside in global or constant address space}}
 global int **constant GP5 = 0; // expected-error{{program scope pointer must point to global or constant address space}}
 global int *global GP6 = 0;
+global int * GP7 = 0;
 
 void kernel foo() {
   static int S1 = 5;
@@ -16,4 +17,8 @@
 
   constant int L1 = 0;
   local int L2;
+  global int L3; // expected-error{{non-program scope variable cannot be declared in global address space}}
+
+  extern global int G5;
+  extern int G6; // expected-error{{program scope variable must reside in global or constant address space}}
 }
Index: test/Parser/opencl-storage-class.cl
===================================================================
--- test/Parser/opencl-storage-class.cl
+++ test/Parser/opencl-storage-class.cl
@@ -10,6 +10,6 @@
 #pragma OPENCL EXTENSION cl_clang_storage_class_specifiers : enable
   static int e; // expected-error {{program scope variable must reside in constant address space}}
   register int f;
-  extern int g;
+  extern int g; // expected-error {{program scope variable must reside in constant address space}}
   auto int h;
 }
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6600,7 +6600,8 @@
     // OpenCL v2.0 s6.5.1 - Variables defined at program scope and static
     // variables inside a function can also be declared in the global
     // address space.
-    if (NewVD->isFileVarDecl() || NewVD->isStaticLocal()) {
+    if (NewVD->isFileVarDecl() || NewVD->isStaticLocal() ||
+        NewVD->hasExternalStorage()) {
       // Program scope pointers can only point to data at the constant or global
        // address space.
       const PointerType *PT = nullptr;
@@ -6628,18 +6629,24 @@
         }
       } while (PT);
     } else {
+      if (T.getAddressSpace() == LangAS::opencl_global) {
+        Diag(NewVD->getLocation(), diag::err_opencl_function_variable)
+            << 1 /*is any function*/ << "global";
+        NewVD->setInvalidDecl();
+        return;
+      }
       // OpenCL v1.1 s6.5.2 and s6.5.3 no local or constant variables
       // in functions.
       if (T.getAddressSpace() == LangAS::opencl_constant ||
           T.getAddressSpace() == LangAS::opencl_local) {
         FunctionDecl *FD = getCurFunctionDecl();
         if (FD && !FD->hasAttr<OpenCLKernelAttr>()) {
           if (T.getAddressSpace() == LangAS::opencl_constant)
-            Diag(NewVD->getLocation(), diag::err_opencl_non_kernel_variable)
-                << "constant";
+            Diag(NewVD->getLocation(), diag::err_opencl_function_variable)
+                << 0 /*non-kernel only*/ << "constant";
           else
-            Diag(NewVD->getLocation(), diag::err_opencl_non_kernel_variable)
-                << "local";
+            Diag(NewVD->getLocation(), diag::err_opencl_function_variable)
+                << 0 /*non-kernel only*/ << "local";
           NewVD->setInvalidDecl();
           return;
         }
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -7649,8 +7649,8 @@
   "kernel parameter cannot be declared as a pointer to a pointer">;
 def err_opencl_private_ptr_kernel_param : Error<
   "kernel parameter cannot be declared as a pointer to the __private address space">;
-def err_opencl_non_kernel_variable : Error<
-  "non-kernel function variable cannot be declared in %0 address space">;
+def err_opencl_function_variable : Error<
+  "%select{non-kernel function|non-program scope}0 variable cannot be declared in %1 address space">;
 def err_static_function_scope : Error<
   "variables in function scope cannot be declared static">;
 def err_opencl_bitfields : Error<
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to