Skip to content

[SYCL] Implement thread-local storage restriction, move vardecl checks #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7060,10 +7060,13 @@ NamedDecl *Sema::ActOnVariableDeclarator(

// Static variables declared inside SYCL device code must be const or
// constexpr
if (getLangOpts().SYCLIsDevice && SCSpec == DeclSpec::SCS_static &&
!R.isConstant(Context))
SYCLDiagIfDeviceCode(D.getIdentifierLoc(), diag::err_sycl_restrict)
<< Sema::KernelNonConstStaticDataVariable;
if (getLangOpts().SYCLIsDevice) {
if (SCSpec == DeclSpec::SCS_static && !R.isConstant(Context))
SYCLDiagIfDeviceCode(D.getIdentifierLoc(), diag::err_sycl_restrict)
<< Sema::KernelNonConstStaticDataVariable;
else if (NewVD->getTSCSpec() == DeclSpec::TSCS_thread_local)
SYCLDiagIfDeviceCode(D.getIdentifierLoc(), diag::err_thread_unsupported);
}

switch (D.getDeclSpec().getConstexprSpecifier()) {
case CSK_unspecified:
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,16 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
ObjCInterfaceDecl *ClassReceiver) {
if (getLangOpts().SYCLIsDevice) {
if (auto VD = dyn_cast<VarDecl>(D)) {
if (VD->getStorageClass() == SC_Static &&
!VD->getType().isConstant(Context))
bool IsConst = VD->getType().isConstant(Context);
if (VD->getTLSKind() != VarDecl::TLS_None)
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_thread_unsupported);

if (!IsConst && VD->getStorageClass() == SC_Static)
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
<< Sema::KernelNonConstStaticDataVariable;
else if (!IsConst && VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
<< Sema::KernelGlobalVariable;
}
}

Expand Down
8 changes: 0 additions & 8 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,6 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {

CheckSYCLType(E->getType(), E->getSourceRange());
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
bool IsConst = VD->getType().getNonReferenceType().isConstQualified();
if (!IsConst && VD->hasGlobalStorage() && !VD->isStaticLocal() &&
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD)) {
if (VD->getTLSKind() != VarDecl::TLS_None)
SemaRef.Diag(E->getLocation(), diag::err_thread_unsupported);
SemaRef.Diag(E->getLocation(), diag::err_sycl_restrict)
<< Sema::KernelGlobalVariable;
}
if (!VD->isLocalVarDeclOrParm() && VD->hasGlobalStorage()) {
VD->addAttr(SYCLDeviceAttr::CreateImplicit(SemaRef.Context));
SemaRef.addSyclDeviceDecl(VD);
Expand Down
50 changes: 50 additions & 0 deletions clang/test/SemaSYCL/prohibit-thread-local.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only %s

thread_local const int prohobit_ns_scope = 0;
thread_local int prohobit_ns_scope2 = 0;
thread_local const int allow_ns_scope = 0;

struct S {
static const thread_local int prohibit_static_member;
static thread_local int prohibit_static_member2;
};

struct T {
static const thread_local int allow_static_member;
};

void foo() {
// expected-error@+1{{thread-local storage is not supported for the current target}}
thread_local const int prohibit_local = 0;
// expected-error@+1{{thread-local storage is not supported for the current target}}
thread_local int prohibit_local2;
}

void bar() { thread_local int allow_local; }

void usage() {
// expected-note@+1 {{called by}}
foo();
// expected-error@+1 {{thread-local storage is not supported for the current target}}
(void)prohobit_ns_scope;
// expected-error@+2 {{SYCL kernel cannot use a non-const global variable}}
// expected-error@+1 {{thread-local storage is not supported for the current target}}
(void)prohobit_ns_scope2;
// expected-error@+1 {{thread-local storage is not supported for the current target}}
(void)S::prohibit_static_member;
// expected-error@+2 {{SYCL kernel cannot use a non-const static data variable}}
// expected-error@+1 {{thread-local storage is not supported for the current target}}
(void)S::prohibit_static_member2;
}

template <typename name, typename Func>
__attribute__((sycl_kernel))
// expected-note@+2 2{{called by}}
void
kernel_single_task(Func kernelFunc) { kernelFunc(); }

int main() {
// expected-note@+1 2{{called by}}
kernel_single_task<class fake_kernel>([]() { usage(); });
return 0;
}
2 changes: 2 additions & 0 deletions clang/test/SemaSYCL/tls_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ void usage() {

template <typename name, typename Func>
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
//expected-note@+1{{called by}}
kernelFunc();
}

int main() {
//expected-note@+1{{called by}}
kernel_single_task<class fake_kernel>([]() { usage(); });
return 0;
}