Skip to content

[SYCL] Allow non-evaluated globals to be used in a kernel. #3323

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 3 commits into from
Mar 10, 2021
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
12 changes: 8 additions & 4 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,21 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
if (getLangOpts().SYCLIsDevice) {
if (auto VD = dyn_cast<VarDecl>(D)) {
bool IsConst = VD->getType().isConstant(Context);
if (!IsConst && VD->getStorageClass() == SC_Static)
bool IsRuntimeEvaluated =
ExprEvalContexts.empty() ||
(!isUnevaluatedContext() && !isConstantEvaluated());
if (IsRuntimeEvaluated && !IsConst && VD->getStorageClass() == SC_Static)
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
<< Sema::KernelNonConstStaticDataVariable;
// Non-const globals are allowed for SYCL explicit SIMD.
else if (!isSYCLEsimdPrivateGlobal(VD) && !IsConst &&
VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
else if (IsRuntimeEvaluated && !isSYCLEsimdPrivateGlobal(VD) &&
!IsConst && VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
<< Sema::KernelGlobalVariable;
// Disallow const statics and globals that are not zero-initialized
// or constant-initialized.
else if (IsConst && VD->hasGlobalStorage() && !VD->isConstexpr() &&
else if (IsRuntimeEvaluated && IsConst && VD->hasGlobalStorage() &&
!VD->isConstexpr() &&
!checkAllowedSYCLInitializer(VD, /*CheckValueDependent =*/true))
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
<< Sema::KernelConstStaticVariable;
Expand Down
7 changes: 7 additions & 0 deletions clang/test/SemaSYCL/sycl-restrict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ int moar_globals = 5;
}
}

template<const auto &T>
int uses_global(){}


int addInt(int n, int m) {
return n + m;
}
Expand All @@ -401,6 +405,9 @@ int use2(a_type ab, a_type *abp) {
if (ab.fm()) // expected-note {{called by 'use2'}}
return 0;

// No error, as this is not in an evaluated context.
(void)(uses_global<another_global>() + uses_global<ns::glob>());

return another_global; // expected-error {{SYCL kernel cannot use a non-const global variable}}

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