]> granicus.if.org Git - clang/commitdiff
[CUDA] Disallow 'extern __shared__' variables.
authorJustin Lebar <jlebar@google.com>
Fri, 30 Sep 2016 23:57:30 +0000 (23:57 +0000)
committerJustin Lebar <jlebar@google.com>
Fri, 30 Sep 2016 23:57:30 +0000 (23:57 +0000)
Also add a test that we disallow

  __constant__ __shared__ int x;

because it's possible to break this without breaking

  __shared__ __constant__ int x;

Reviewers: rnk

Subscribers: cfe-commits, tra

Differential Revision: https://reviews.llvm.org/D25125

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282985 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/SemaCUDA/bad-attributes.cu
test/SemaCUDA/extern-shared.cu [new file with mode: 0644]

index a65cf97ca600b07d2da2e2915578332c290aea5c..66a5bce86c39ff6f5678c5ac42c584da9f85a84e 100644 (file)
@@ -6722,6 +6722,7 @@ def err_device_static_local_var : Error<
 def err_cuda_vla : Error<
     "cannot use variable-length arrays in "
     "%select{__device__|__global__|__host__|__host__ __device__}0 functions">;
+def err_cuda_extern_shared : Error<"__shared__ variable %0 cannot be 'extern'">;
 
 def warn_non_pod_vararg_with_format_string : Warning<
   "cannot pass %select{non-POD|non-trivial}0 object of type %1 to variadic "
index 250380b6a1cac6705ba94cda3f63957cab73f0c4..ff63d7b1ce681ed07e274becd00cc6d16e0803b4 100644 (file)
@@ -3696,6 +3696,19 @@ static void handleOptimizeNoneAttr(Sema &S, Decl *D,
     D->addAttr(Optnone);
 }
 
+static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
+  if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, Attr.getRange(),
+                                                 Attr.getName()))
+    return;
+  auto *VD = cast<VarDecl>(D);
+  if (VD->hasExternalStorage()) {
+    S.Diag(Attr.getLoc(), diag::err_cuda_extern_shared) << VD;
+    return;
+  }
+  D->addAttr(::new (S.Context) CUDASharedAttr(
+      Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex()));
+}
+
 static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(),
                                                Attr.getName()) ||
@@ -5639,8 +5652,7 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
     handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
     break;
   case AttributeList::AT_CUDAShared:
-    handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D,
-                                                                          Attr);
+    handleSharedAttr(S, D, Attr);
     break;
   case AttributeList::AT_VecReturn:
     handleVecReturnAttr(S, D, Attr);
index 4cb43e240b049c090cbe33f9b058fcdb1b36cc19..f3e1688462d054b7364887b8f9586e012fe2d736 100644 (file)
@@ -42,6 +42,8 @@ __constant__ __shared__ int z8;  // expected-error {{attributes are not compatib
 __shared__ __device__ int z9;
 __shared__ __constant__ int z10;  // expected-error {{attributes are not compatible}}
 // expected-note@-1 {{conflicting attribute is here}}
+__constant__ __shared__ int z10a;  // expected-error {{attributes are not compatible}}
+// expected-note@-1 {{conflicting attribute is here}}
 
 __global__ __device__ void z11();  // expected-error {{attributes are not compatible}}
 // expected-note@-1 {{conflicting attribute is here}}
diff --git a/test/SemaCUDA/extern-shared.cu b/test/SemaCUDA/extern-shared.cu
new file mode 100644 (file)
index 0000000..9450bbf
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -fcuda-is-device -verify %s
+
+#include "Inputs/cuda.h"
+
+__device__ void foo() {
+  extern __shared__ int x; // expected-error {{__shared__ variable 'x' cannot be 'extern'}}
+}
+
+__host__ __device__ void bar() {
+  extern __shared__ int x; // expected-error {{__shared__ variable 'x' cannot be 'extern'}}
+}
+
+extern __shared__ int global; // expected-error {{__shared__ variable 'global' cannot be 'extern'}}