Skip to content

Commit ddc1a7f

Browse files
premanandraobader
authored andcommitted
[SYCL] Do not emit errors for TLS declarations
Use of TLS variables in SYCL code is diagnosed instead. Signed-off-by: Premanand M Rao <premanand.m.rao@intel.com>
1 parent 96cd4ac commit ddc1a7f

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

clang/lib/Basic/Targets/SPIR.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ class LLVM_LIBRARY_VISIBILITY SPIR32SYCLDeviceTargetInfo
164164
// to parse host code. So we allow compilation of exception_ptr but
165165
// if exceptions are used in device code we should emit a diagnostic.
166166
MaxAtomicInlineWidth = 32;
167-
// This is workaround for mutex class.
168-
// I'm not sure about this hack but I guess that mutex_class is same
169-
// problem.
170-
TLSSupported = true;
171167
}
172168
};
173169

@@ -182,10 +178,6 @@ class LLVM_LIBRARY_VISIBILITY SPIR64SYCLDeviceTargetInfo
182178
// to parse host code. So we allow compilation of exception_ptr but
183179
// if exceptions are used in device code we should emit a diagnostic.
184180
MaxAtomicInlineWidth = 64;
185-
// This is workaround for mutex class.
186-
// I'm not sure about this hack but I guess that mutex_class is same
187-
// problem.
188-
TLSSupported = true;
189181
}
190182
};
191183

clang/lib/Sema/SemaDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6729,6 +6729,14 @@ NamedDecl *Sema::ActOnVariableDeclarator(
67296729
// proper storage class for other tools to use even if we're not going
67306730
// to emit any code for it.
67316731
NewVD->setTSCSpec(TSCS);
6732+
} else if (getLangOpts().SYCLIsDevice) {
6733+
// While SYCL does not support TLS, emitting the diagnostic here
6734+
// prevents the compilation of header files with TLS declarations.
6735+
// When TLS objects are used in kernel code, they are diagnosed.
6736+
// We still need to mark the variable as TLS so it shows up in AST with
6737+
// proper storage class for other tools to use even if we're not going
6738+
// to emit any code for it.
6739+
NewVD->setTSCSpec(TSCS);
67326740
} else
67336741
Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
67346742
diag::err_thread_unsupported);

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
209209
SemaRef.Diag(E->getExprLoc(), diag::err_sycl_restrict)
210210
<< Sema::KernelNonConstStaticDataVariable;
211211
else if (!IsConst && VD->hasGlobalStorage() && !VD->isStaticLocal() &&
212-
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD))
212+
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD)) {
213+
if (VD->getTLSKind() != VarDecl::TLS_None)
214+
SemaRef.Diag(E->getLocation(), diag::err_thread_unsupported);
213215
SemaRef.Diag(E->getLocation(), diag::err_sycl_restrict)
214216
<< Sema::KernelGlobalVariable;
217+
}
215218
if (!VD->isLocalVarDeclOrParm() && VD->hasGlobalStorage()) {
216219
VD->addAttr(SYCLDeviceAttr::CreateImplicit(SemaRef.Context));
217220
SemaRef.addSyclDeviceDecl(VD);

clang/test/SemaSYCL/tls_error.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -fsycl-is-device %s
2+
3+
extern __thread void* __once_callable; // expected-no-error
4+
extern __thread void (*__once_call)(); // expected-no-error
5+
6+
void usage() {
7+
// expected-error@+2{{thread-local storage is not supported for the current target}}
8+
// expected-error@+1{{SYCL kernel cannot use a global variable}}
9+
__once_callable = 0;
10+
// expected-error@+3{{thread-local storage is not supported for the current target}}
11+
// expected-error@+2{{SYCL kernel cannot use a global variable}}
12+
// expected-error@+1{{SYCL kernel cannot call through a function pointer}}
13+
__once_call();
14+
}
15+
16+
template <typename name, typename Func>
17+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
18+
kernelFunc();
19+
}
20+
21+
int main() {
22+
kernel_single_task<class fake_kernel>([]() { usage(); });
23+
return 0;
24+
}

0 commit comments

Comments
 (0)