summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorJustin Lebar <jlebar@google.com>2016-10-13 18:45:17 +0000
committerJustin Lebar <jlebar@google.com>2016-10-13 18:45:17 +0000
commit44f547aa3f7239260325dc24720203da63dad5e4 (patch)
tree9bf78d1e8281aea381fe5542bdf851d19ff827a7 /clang
parentaa370bd0d77c136bc6ba87c7b20d007a53e82602 (diff)
downloadbcm5719-llvm-44f547aa3f7239260325dc24720203da63dad5e4.tar.gz
bcm5719-llvm-44f547aa3f7239260325dc24720203da63dad5e4.zip
[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 llvm-svn: 284145
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td4
-rw-r--r--clang/lib/Sema/SemaDecl.cpp11
-rw-r--r--clang/test/SemaCUDA/device-var-init.cu6
-rw-r--r--clang/test/SemaCUDA/static-vars-hd.cu20
4 files changed, 30 insertions, 11 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 58029b03039..872311f6024 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/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/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f3c85c33771..a3444cbdc63 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/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<CUDADeviceAttr>() || FD->hasAttr<CUDAGlobalAttr>()) &&
- !VD->hasAttr<CUDASharedAttr>()) {
- Diag(VD->getLocation(), diag::err_device_static_local_var);
+ if (getLangOpts().CUDA && !VD->hasAttr<CUDASharedAttr>() &&
+ 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<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>() ||
VD->hasAttr<CUDASharedAttr>()) {
- assert((!VD->isStaticLocal() || VD->hasAttr<CUDASharedAttr>()));
+ assert(!VD->isStaticLocal() || VD->hasAttr<CUDASharedAttr>());
bool AllowedInit = false;
if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init))
AllowedInit =
diff --git a/clang/test/SemaCUDA/device-var-init.cu b/clang/test/SemaCUDA/device-var-init.cu
index d807a51b653..122dfca4232 100644
--- a/clang/test/SemaCUDA/device-var-init.cu
+++ b/clang/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/clang/test/SemaCUDA/static-vars-hd.cu b/clang/test/SemaCUDA/static-vars-hd.cu
new file mode 100644
index 00000000000..70cc0417f0e
--- /dev/null
+++ b/clang/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(); }
OpenPOWER on IntegriCloud