From: Justin Lebar Date: Thu, 13 Oct 2016 18:45:17 +0000 (+0000) Subject: [CUDA] Allow static variables in __host__ __device__ functions, so long as they're... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=75a3d319e9bdad831c47f449d7406a5ca61781c5;p=clang [CUDA] Allow static variables in __host__ __device__ functions, so long as they're never codegen'ed for device. Reviewers: tra, rnk Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D25150 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284145 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 58029b0303..872311f602 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6741,8 +6741,8 @@ def err_dynamic_var_init : Error< def err_shared_var_init : Error< "initialization is not supported for __shared__ variables.">; def err_device_static_local_var : Error< - "Within a __device__/__global__ function, " - "only __shared__ variables may be marked \"static\"">; + "within a %select{__device__|__global__|__host__|__host__ __device__}0 " + "function, only __shared__ variables may be marked 'static'">; def err_cuda_vla : Error< "cannot use variable-length arrays in " "%select{__device__|__global__|__host__|__host__ __device__}0 functions">; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index f3c85c3377..a3444cbdc6 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -10677,12 +10677,11 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) { // CUDA E.2.9.4: Within the body of a __device__ or __global__ // function, only __shared__ variables may be declared with // static storage class. - if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice && - (FD->hasAttr() || FD->hasAttr()) && - !VD->hasAttr()) { - Diag(VD->getLocation(), diag::err_device_static_local_var); + if (getLangOpts().CUDA && !VD->hasAttr() && + CUDADiagIfDeviceCode(VD->getLocation(), + diag::err_device_static_local_var) + << CurrentCUDATarget()) VD->setInvalidDecl(); - } } } @@ -10696,7 +10695,7 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) { if (Init && VD->hasGlobalStorage()) { if (VD->hasAttr() || VD->hasAttr() || VD->hasAttr()) { - assert((!VD->isStaticLocal() || VD->hasAttr())); + assert(!VD->isStaticLocal() || VD->hasAttr()); bool AllowedInit = false; if (const CXXConstructExpr *CE = dyn_cast(Init)) AllowedInit = diff --git a/test/SemaCUDA/device-var-init.cu b/test/SemaCUDA/device-var-init.cu index d807a51b65..122dfca423 100644 --- a/test/SemaCUDA/device-var-init.cu +++ b/test/SemaCUDA/device-var-init.cu @@ -207,9 +207,9 @@ __device__ void df_sema() { // expected-error@-1 {{initialization is not supported for __shared__ variables.}} static __device__ int ds; - // expected-error@-1 {{Within a __device__/__global__ function, only __shared__ variables may be marked "static"}} + // expected-error@-1 {{within a __device__ function, only __shared__ variables may be marked 'static'}} static __constant__ int dc; - // expected-error@-1 {{Within a __device__/__global__ function, only __shared__ variables may be marked "static"}} + // expected-error@-1 {{within a __device__ function, only __shared__ variables may be marked 'static'}} static int v; - // expected-error@-1 {{Within a __device__/__global__ function, only __shared__ variables may be marked "static"}} + // expected-error@-1 {{within a __device__ function, only __shared__ variables may be marked 'static'}} } diff --git a/test/SemaCUDA/static-vars-hd.cu b/test/SemaCUDA/static-vars-hd.cu new file mode 100644 index 0000000000..70cc0417f0 --- /dev/null +++ b/test/SemaCUDA/static-vars-hd.cu @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fcxx-exceptions -fcuda-is-device -S -o /dev/null -verify %s +// RUN: %clang_cc1 -fcxx-exceptions -S -o /dev/null -D HOST -verify %s + +#include "Inputs/cuda.h" + +#ifdef HOST +// expected-no-diagnostics +#endif + +__host__ __device__ void f() { + static int x = 42; +#ifndef HOST + // expected-error@-2 {{within a __host__ __device__ function, only __shared__ variables may be marked 'static'}} +#endif +} + +inline __host__ __device__ void g() { + static int x = 42; // no error on device because this is never codegen'ed there. +} +void call_g() { g(); }