Skip to content

Commit d988bb3

Browse files
author
Erich Keane
authored
[SYCL] Allow non-evaluated globals to be used in a kernel. (#3323)
As requested to support SYCL-2020 specialization constants, this makes our global variable diagnostics not happen unless it is in a runtime evaluated context. This is OK, because the SYCL standard prohibits ODR uses, of which non-runtime-evaluated uses are not.
1 parent a539197 commit d988bb3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,21 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
215215
if (getLangOpts().SYCLIsDevice) {
216216
if (auto VD = dyn_cast<VarDecl>(D)) {
217217
bool IsConst = VD->getType().isConstant(Context);
218-
if (!IsConst && VD->getStorageClass() == SC_Static)
218+
bool IsRuntimeEvaluated =
219+
ExprEvalContexts.empty() ||
220+
(!isUnevaluatedContext() && !isConstantEvaluated());
221+
if (IsRuntimeEvaluated && !IsConst && VD->getStorageClass() == SC_Static)
219222
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
220223
<< Sema::KernelNonConstStaticDataVariable;
221224
// Non-const globals are allowed for SYCL explicit SIMD.
222-
else if (!isSYCLEsimdPrivateGlobal(VD) && !IsConst &&
223-
VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
225+
else if (IsRuntimeEvaluated && !isSYCLEsimdPrivateGlobal(VD) &&
226+
!IsConst && VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
224227
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
225228
<< Sema::KernelGlobalVariable;
226229
// Disallow const statics and globals that are not zero-initialized
227230
// or constant-initialized.
228-
else if (IsConst && VD->hasGlobalStorage() && !VD->isConstexpr() &&
231+
else if (IsRuntimeEvaluated && IsConst && VD->hasGlobalStorage() &&
232+
!VD->isConstexpr() &&
229233
!checkAllowedSYCLInitializer(VD, /*CheckValueDependent =*/true))
230234
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
231235
<< Sema::KernelConstStaticVariable;

clang/test/SemaSYCL/sycl-restrict.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ int moar_globals = 5;
384384
}
385385
}
386386

387+
template<const auto &T>
388+
int uses_global(){}
389+
390+
387391
int addInt(int n, int m) {
388392
return n + m;
389393
}
@@ -401,6 +405,9 @@ int use2(a_type ab, a_type *abp) {
401405
if (ab.fm()) // expected-note {{called by 'use2'}}
402406
return 0;
403407

408+
// No error, as this is not in an evaluated context.
409+
(void)(uses_global<another_global>() + uses_global<ns::glob>());
410+
404411
return another_global; // expected-error {{SYCL kernel cannot use a non-const global variable}}
405412

406413
return ns::glob + // expected-error {{SYCL kernel cannot use a non-const global variable}}

0 commit comments

Comments
 (0)