From: Justin Lebar Date: Fri, 30 Sep 2016 23:57:34 +0000 (+0000) Subject: [CUDA] Disallow __constant__ local variables. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=682d473d4d784f32f657a141fad8640e6cb75278;p=clang [CUDA] Disallow __constant__ local variables. Reviewers: tra, rnk Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D25129 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282986 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 66a5bce86c..eb8bf915e8 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6723,6 +6723,7 @@ 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 err_cuda_nonglobal_constant : Error<"__constant__ variables must be global">; def warn_non_pod_vararg_with_format_string : Warning< "cannot pass %select{non-POD|non-trivial}0 object of type %1 to variadic " diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index ff63d7b1ce..21cace432c 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -3696,6 +3696,19 @@ static void handleOptimizeNoneAttr(Sema &S, Decl *D, D->addAttr(Optnone); } +static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { + if (checkAttrMutualExclusion(S, D, Attr.getRange(), + Attr.getName())) + return; + auto *VD = cast(D); + if (!VD->hasGlobalStorage()) { + S.Diag(Attr.getLoc(), diag::err_cuda_nonglobal_constant); + return; + } + D->addAttr(::new (S.Context) CUDAConstantAttr( + Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); +} + static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (checkAttrMutualExclusion(S, D, Attr.getRange(), Attr.getName())) @@ -5541,8 +5554,7 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, handleCommonAttr(S, D, Attr); break; case AttributeList::AT_CUDAConstant: - handleSimpleAttributeWithExclusions(S, D, - Attr); + handleConstantAttr(S, D, Attr); break; case AttributeList::AT_PassObjectSize: handlePassObjectSizeAttr(S, D, Attr); diff --git a/test/SemaCUDA/bad-attributes.cu b/test/SemaCUDA/bad-attributes.cu index f3e1688462..4fb584aec4 100644 --- a/test/SemaCUDA/bad-attributes.cu +++ b/test/SemaCUDA/bad-attributes.cu @@ -61,3 +61,11 @@ __global__ static inline void foobar() {}; #ifdef EXPECT_INLINE_WARNING // expected-warning@-2 {{ignored 'inline' attribute on kernel function 'foobar'}} #endif + +__constant__ int global_constant; +void host_fn() { + __constant__ int c; // expected-error {{__constant__ variables must be global}} +} +__device__ void device_fn() { + __constant__ int c; // expected-error {{__constant__ variables must be global}} +}