Skip to content

Commit cd7b8c5

Browse files
committed
[SYCL] non-const static variables defined outside kernel must be
forbidden in it. (fix #1) Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
1 parent c3aac3a commit cd7b8c5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10626,7 +10626,7 @@ def err_sycl_kernel_name_class_not_top_level : Error<
1062610626
"nest in a namespace: %0">;
1062710627
def err_sycl_restrict : Error<
1062810628
"SYCL kernel cannot "
10629-
"%select{use a global variable"
10629+
"%select{use a non-const global variable"
1063010630
"|use rtti"
1063110631
"|use a non-const static data variable"
1063210632
"|call a virtual function"

clang/lib/Sema/SemaExpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
210210
bool ObjCPropertyAccess,
211211
bool AvoidPartialAvailabilityChecks,
212212
ObjCInterfaceDecl *ClassReceiver) {
213+
214+
if (isa<VarDecl>(D) && getLangOpts().SYCLIsDevice &&
215+
cast<VarDecl>(D)->getStorageClass() == SC_Static &&
216+
!cast<VarDecl>(D)->getType().isConstant(Context))
217+
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
218+
<< Sema::KernelNonConstStaticDataVariable;
219+
213220
SourceLocation Loc = Locs.front();
214221
if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) {
215222
// If there were any diagnostics suppressed by template argument deduction,
@@ -18450,3 +18457,4 @@ bool Sema::IsDependentFunctionNameExpr(Expr *E) {
1845018457
assert(E->isTypeDependent());
1845118458
return isa<UnresolvedLookupExpr>(E);
1845218459
}
18460+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -fsycl-is-device %s
2+
const int glob1 = 1;
3+
int glob2 = 2;
4+
template <typename name, typename Func>
5+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
6+
// expected-note-re@+1{{called by 'kernel_single_task<fake_kernel, (lambda at {{.*}})>}}
7+
kernelFunc();
8+
}
9+
10+
int main() {
11+
static int n = 0;
12+
const static int l = 0;
13+
kernel_single_task<class fake_kernel>([]() {
14+
int m = l;
15+
m = glob1;
16+
// expected-error@+1{{SYCL kernel cannot use a non-const static data variable}}
17+
m = n;
18+
// expected-error@+1{{SYCL kernel cannot use a non-const global variable}}
19+
m = glob2;
20+
});
21+
return 0;
22+
}

0 commit comments

Comments
 (0)