Skip to content

Commit e365904

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

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,14 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
211211
bool AvoidPartialAvailabilityChecks,
212212
ObjCInterfaceDecl *ClassReceiver) {
213213

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-
214+
if (getLangOpts().SYCLIsDevice) {
215+
if (auto VD = dyn_cast<VarDecl>(D)) {
216+
if (VD->getStorageClass() == SC_Static &&
217+
!VD->getType().isConstant(Context))
218+
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
219+
<< Sema::KernelNonConstStaticDataVariable;
220+
}
221+
}
220222
SourceLocation Loc = Locs.front();
221223
if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) {
222224
// If there were any diagnostics suppressed by template argument deduction,
@@ -18457,4 +18459,3 @@ bool Sema::IsDependentFunctionNameExpr(Expr *E) {
1845718459
assert(E->isTypeDependent());
1845818460
return isa<UnresolvedLookupExpr>(E);
1845918461
}
18460-

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,6 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
321321
return true;
322322
}
323323

324-
bool VisitMemberExpr(MemberExpr *E) {
325-
if (VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
326-
bool IsConst = VD->getType().getNonReferenceType().isConstQualified();
327-
if (!IsConst && VD->isStaticDataMember())
328-
SemaRef.Diag(E->getExprLoc(), diag::err_sycl_restrict)
329-
<< Sema::KernelNonConstStaticDataVariable;
330-
}
331-
return true;
332-
}
333-
334324
bool VisitDeclRefExpr(DeclRefExpr *E) {
335325
Decl* D = E->getDecl();
336326
if (SemaRef.isKnownGoodSYCLDecl(D))
@@ -339,10 +329,7 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
339329
CheckSYCLType(E->getType(), E->getSourceRange());
340330
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
341331
bool IsConst = VD->getType().getNonReferenceType().isConstQualified();
342-
if (!IsConst && VD->isStaticDataMember())
343-
SemaRef.Diag(E->getExprLoc(), diag::err_sycl_restrict)
344-
<< Sema::KernelNonConstStaticDataVariable;
345-
else if (!IsConst && VD->hasGlobalStorage() && !VD->isStaticLocal() &&
332+
if (!IsConst && VD->hasGlobalStorage() && !VD->isStaticLocal() &&
346333
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD)) {
347334
if (VD->getTLSKind() != VarDecl::TLS_None)
348335
SemaRef.Diag(E->getLocation(), diag::err_thread_unsupported);

clang/test/SemaSYCL/sycl-device-static-restrict.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ int main() {
1111
static int n = 0;
1212
const static int l = 0;
1313
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;
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;
2020
});
2121
return 0;
2222
}

clang/test/SemaSYCL/sycl-restrict.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ typedef struct A {
9191

9292
int fm(void)
9393
{
94-
// expected-error@+2 {{SYCL kernel cannot use a non-const static data variable}}
9594
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
9695
return stat_member;
9796
}
@@ -164,12 +163,11 @@ int use2 ( a_type ab, a_type *abp ) {
164163

165164
if (ab.constexpr_stat_member) return 2;
166165
if (ab.const_stat_member) return 1;
167-
// expected-error@+2 {{SYCL kernel cannot use a non-const static data variable}}
168166
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
169167
if (ab.stat_member) return 0;
170-
// expected-error@+2 {{SYCL kernel cannot use a non-const static data variable}}
171168
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
172169
if (abp->stat_member) return 0;
170+
// expected-note@+1 {{called by 'use2'}}
173171
if (ab.fm()) return 0;
174172
// expected-error@+1 {{SYCL kernel cannot use a non-const global variable}}
175173
return another_global ;
@@ -194,7 +192,7 @@ __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
194192
kernelFunc();
195193
a_type ab;
196194
a_type *p;
197-
// expected-note@+1 5{{called by 'kernel_single_task}}
195+
// expected-note@+1 7{{called by 'kernel_single_task}}
198196
use2(ab, p);
199197
}
200198

0 commit comments

Comments
 (0)