Skip to content

Commit 5462e21

Browse files
committed
[SYCL] Do not emit const sdm initializers
Const static variables need to be either zero-initialized or constant-initialized. We were emitting initializers for all const static variables before, which needed invalid address space casts from constant-space to private-space. We now diagnose when they are neither zero- nor constant-initialized and used in device code. This change also reverts the address-space cast introduced in intel#1774 Signed-off-by: Premanand M Rao <premanand.m.rao@intel.com>
1 parent fedfdd3 commit 5462e21

7 files changed

Lines changed: 19 additions & 95 deletions

File tree

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10850,7 +10850,7 @@ def err_sycl_restrict : Error<
1085010850
"|call a dllimport function"
1085110851
"|call a variadic function"
1085210852
"|call an undefined function without SYCL_EXTERNAL attribute"
10853-
"|use a const static data variable that is neither zero-initialized "
10853+
"|use a const static variable that is neither zero-initialized "
1085410854
"nor constant-initialized"
1085510855
"}0">;
1085610856
def err_sycl_virtual_types : Error<

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12662,7 +12662,7 @@ class Sema final {
1266212662
KernelCallDllimportFunction,
1266312663
KernelCallVariadicFunction,
1266412664
KernelCallUndefinedFunction,
12665-
KernelConstStaticDataVariable
12665+
KernelConstStaticVariable
1266612666
};
1266712667

1266812668
bool isKnownGoodSYCLDecl(const Decl *D);

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,12 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
223223
VD->hasGlobalStorage() && !isa<ParmVarDecl>(VD))
224224
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
225225
<< Sema::KernelGlobalVariable;
226-
// Disallow const static data members that are not zero-initialized
226+
// Disallow const statics that are not zero-initialized
227227
// or constant-initialized
228-
else if (IsConst && VD->isStaticDataMember() &&
229-
!VD->isConstexpr() &&
230-
!checkAllowedSYCLInitializer(VD, /*checkValueDependent =*/true))
228+
else if (IsConst && VD->hasGlobalStorage() && !VD->isConstexpr() &&
229+
!checkAllowedSYCLInitializer(VD, /*CheckValueDependent =*/true))
231230
SYCLDiagIfDeviceCode(*Locs.begin(), diag::err_sycl_restrict)
232-
<< Sema::KernelConstStaticDataVariable;
231+
<< Sema::KernelConstStaticVariable;
233232
}
234233
}
235234

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,13 +1854,9 @@ bool Sema::checkAllowedSYCLInitializer(VarDecl *VD, bool CheckValueDependent) {
18541854

18551855
const Expr *Init = VD->getInit();
18561856
bool ValueDependent = CheckValueDependent && Init->isValueDependent();
1857-
bool isZeroInit = Init && !ValueDependent &&
1858-
Init->isIntegerConstantExpr(Context) &&
1859-
Init->EvaluateKnownConstInt(Context) == 0;
18601857
bool isConstantInit = Init && !ValueDependent &&
18611858
Init->isConstantInitializer(Context, false);
1862-
if (!VD->isConstexpr() && Init && !ValueDependent && !isZeroInit &&
1863-
!isConstantInit)
1859+
if (!VD->isConstexpr() && Init && !ValueDependent && !isConstantInit)
18641860
return false;
18651861

18661862
return true;

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5104,7 +5104,7 @@ void Sema::InstantiateVariableInitializer(
51045104

51055105
if (getLangOpts().SYCLIsDevice && !checkAllowedSYCLInitializer(Var))
51065106
SYCLDiagIfDeviceCode(Var->getLocation(), diag::err_sycl_restrict)
5107-
<< Sema::KernelConstStaticDataVariable;
5107+
<< Sema::KernelConstStaticVariable;
51085108
}
51095109

51105110
/// Instantiate the definition of the given variable from its

clang/test/CodeGenSYCL/sycl-device-const-sdm.cpp

Lines changed: 0 additions & 78 deletions
This file was deleted.

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
struct Base {};
44
struct S {
5-
__attribute__((sycl_device)) void foo();
6-
S();
5+
void foo() {}
6+
S() {}
77
};
88

99
struct T {
@@ -20,13 +20,20 @@ const S U<T>::s2;
2020

2121
template struct U<Base>;
2222

23+
const S s5;
24+
2325
void usage() {
2426
// expected-error@+1{{SYCL kernel cannot use a non-const static data variable}}
2527
static int s1;
2628
const static int cs = 0;
2729
constexpr static int ces = 0;
28-
// expected-error@+1{{SYCL kernel cannot use a const static data variable that is neither zero-initialized nor constant-initialized}}
30+
static const S s6;
31+
// expected-error@+1{{SYCL kernel cannot use a const static variable that is neither zero-initialized nor constant-initialized}}
2932
(void)T::s1;
33+
// expected-error@+1{{SYCL kernel cannot use a const static variable that is neither zero-initialized nor constant-initialized}}
34+
(void)s5;
35+
// expected-error@+1{{SYCL kernel cannot use a const static variable that is neither zero-initialized nor constant-initialized}}
36+
(void)s6;
3037
}
3138

3239

@@ -36,7 +43,7 @@ __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
3643
static int z;
3744
// expected-note-re@+3{{called by 'kernel_single_task<fake_kernel, (lambda at {{.*}})>}}
3845
// expected-note-re@+2{{called by 'kernel_single_task<fake_kernel, (lambda at {{.*}})>}}
39-
// expected-error@+1{{SYCL kernel cannot use a const static data variable that is neither zero-initialized nor constant-initialized}}
46+
// expected-error@+1{{SYCL kernel cannot use a const static variable that is neither zero-initialized nor constant-initialized}}
4047
kernelFunc(U<Base>::s2);
4148
}
4249

0 commit comments

Comments
 (0)