Skip to content

Commit d6fa510

Browse files
committed
[SYCL] non-const static variables defined outside kernel must be forbidden in it. (fix #4)
Signed-off-by: Aleksander Fadeev <[email protected]>
1 parent e365904 commit d6fa510

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

clang/lib/Sema/SemaSYCL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,15 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
322322
}
323323

324324
bool VisitDeclRefExpr(DeclRefExpr *E) {
325-
Decl* D = E->getDecl();
325+
Decl *D = E->getDecl();
326326
if (SemaRef.isKnownGoodSYCLDecl(D))
327327
return true;
328328

329329
CheckSYCLType(E->getType(), E->getSourceRange());
330330
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
331331
bool IsConst = VD->getType().getNonReferenceType().isConstQualified();
332332
if (!IsConst && VD->hasGlobalStorage() && !VD->isStaticLocal() &&
333-
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD)) {
333+
!VD->isStaticDataMember() && !isa<ParmVarDecl>(VD)) {
334334
if (VD->getTLSKind() != VarDecl::TLS_None)
335335
SemaRef.Diag(E->getLocation(), diag::err_thread_unsupported);
336336
SemaRef.Diag(E->getLocation(), diag::err_sycl_restrict)

clang/test/SemaSYCL/sycl-restrict.cpp

+62-59
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22
// RUN: %clang_cc1 -fcxx-exceptions -triple spir64 -fsycl-is-device -fno-sycl-allow-func-ptr -Wno-return-type -verify -fsyntax-only -std=c++17 %s
33
// RUN: %clang_cc1 -fcxx-exceptions -triple spir64 -fsycl-is-device -DALLOW_FP=1 -fsycl-allow-func-ptr -Wno-return-type -verify -fsyntax-only -std=c++17 %s
44

5-
65
namespace std {
7-
class type_info;
8-
typedef __typeof__(sizeof(int)) size_t;
9-
}
6+
class type_info;
7+
typedef __typeof__(sizeof(int)) size_t;
8+
} // namespace std
109
namespace Check_User_Operators {
11-
class Fraction
12-
{
13-
// expected-error@+2 {{SYCL kernel cannot call a recursive function}}
14-
// expected-note@+1 {{function implemented using recursion declared here}}
15-
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
16-
int n, d;
10+
class Fraction {
11+
// expected-error@+2 {{SYCL kernel cannot call a recursive function}}
12+
// expected-note@+1 {{function implemented using recursion declared here}}
13+
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
14+
int n, d;
15+
1716
public:
18-
Fraction(int n, int d = 1) : n(n/gcd(n, d)), d(d/gcd(n, d)) { }
19-
int num() const { return n; }
20-
int den() const { return d; }
17+
Fraction(int n, int d = 1) : n(n / gcd(n, d)), d(d / gcd(n, d)) {}
18+
int num() const { return n; }
19+
int den() const { return d; }
2120
};
22-
bool operator==(const Fraction& lhs, const Fraction& rhs)
23-
{
24-
new int; // expected-error {{SYCL kernel cannot allocate storage}}
25-
return lhs.num() == rhs.num() && lhs.den() == rhs.den();
26-
}}
21+
bool operator==(const Fraction &lhs, const Fraction &rhs) {
22+
new int; // expected-error {{SYCL kernel cannot allocate storage}}
23+
return lhs.num() == rhs.num() && lhs.den() == rhs.den();
24+
}
25+
} // namespace Check_User_Operators
2726

2827
namespace Check_VLA_Restriction {
2928
void no_restriction(int p) {
30-
int index[p+2];
29+
int index[p + 2];
3130
}
3231
void restriction(int p) {
3332
// expected-error@+1 {{variable length arrays are not supported for the current target}}
34-
int index[p+2];
35-
}
33+
int index[p + 2];
3634
}
35+
} // namespace Check_VLA_Restriction
3736

38-
void* operator new (std::size_t size, void* ptr) throw() { return ptr; };
37+
void *operator new(std::size_t size, void *ptr) throw() { return ptr; };
3938
namespace Check_RTTI_Restriction {
4039
struct A {
4140
virtual ~A(){};
@@ -50,35 +49,38 @@ struct OverloadedNewDelete {
5049
void *operator new(std::size_t size) throw() {
5150
// expected-error@+1 {{SYCL kernel cannot allocate storage}}
5251
float *pt = new float;
53-
return 0;}
52+
return 0;
53+
}
5454
// This overload does not allocate: no diagnostic.
55-
void *operator new[](std::size_t size) throw() {return 0;}
55+
void *operator new[](std::size_t size) throw() { return 0; }
5656
void operator delete(void *){};
5757
void operator delete[](void *){};
5858
};
5959

6060
bool isa_B(A *a) {
6161
Check_User_Operators::Fraction f1(3, 8), f2(1, 2), f3(10, 2);
62-
if (f1 == f2) return false;
62+
if (f1 == f2)
63+
return false;
6364

6465
Check_VLA_Restriction::restriction(7);
6566
// expected-error@+1 {{SYCL kernel cannot allocate storage}}
6667
int *ip = new int;
67-
int i; int *p3 = new(&i) int; // no error on placement new
68+
int i;
69+
int *p3 = new (&i) int; // no error on placement new
6870
// expected-note@+1 {{called by 'isa_B'}}
69-
OverloadedNewDelete *x = new( struct OverloadedNewDelete );
70-
auto y = new struct OverloadedNewDelete [5];
71+
OverloadedNewDelete *x = new (struct OverloadedNewDelete);
72+
auto y = new struct OverloadedNewDelete[5];
7173
// expected-error@+1 {{SYCL kernel cannot use rtti}}
7274
(void)typeid(int);
7375
// expected-error@+1 {{SYCL kernel cannot use rtti}}
7476
return dynamic_cast<B *>(a) != 0;
7577
}
7678

77-
template<typename N, typename L>
79+
template <typename N, typename L>
7880
__attribute__((sycl_kernel)) void kernel1(L l) {
7981
l();
8082
}
81-
}
83+
} // namespace Check_RTTI_Restriction
8284

8385
typedef struct Base {
8486
virtual void f() const {}
@@ -87,22 +89,19 @@ typedef struct Base {
8789
typedef struct A {
8890
static int stat_member;
8991
const static int const_stat_member;
90-
constexpr static int constexpr_stat_member=0;
92+
constexpr static int constexpr_stat_member = 0;
9193

92-
int fm(void)
93-
{
94+
int fm(void) {
9495
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
9596
return stat_member;
9697
}
9798
} a_type;
9899

99-
100100
b_type b;
101101

102-
using myFuncDef = int(int,int);
102+
using myFuncDef = int(int, int);
103103

104-
void eh_ok(void)
105-
{
104+
void eh_ok(void) {
106105
__float128 A;
107106
try {
108107
;
@@ -112,8 +111,7 @@ void eh_ok(void)
112111
throw 20;
113112
}
114113

115-
void eh_not_ok(void)
116-
{
114+
void eh_not_ok(void) {
117115
// expected-error@+1 {{SYCL kernel cannot use exceptions}}
118116
try {
119117
;
@@ -146,45 +144,51 @@ void usage(myFuncDef functionPtr) {
146144
}
147145

148146
namespace ns {
149-
int glob;
147+
int glob;
150148
}
151149
extern "C++" {
152-
int another_global = 5;
153-
namespace AnotherNS {
154-
int moar_globals = 5;
155-
}
150+
int another_global = 5;
151+
namespace AnotherNS {
152+
int moar_globals = 5;
153+
}
156154
}
157155

158156
int addInt(int n, int m) {
159-
return n+m;
157+
return n + m;
160158
}
161159

162-
int use2 ( a_type ab, a_type *abp ) {
160+
int use2(a_type ab, a_type *abp) {
163161

164-
if (ab.constexpr_stat_member) return 2;
165-
if (ab.const_stat_member) return 1;
162+
if (ab.constexpr_stat_member)
163+
return 2;
164+
if (ab.const_stat_member)
165+
return 1;
166166
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
167-
if (ab.stat_member) return 0;
167+
if (ab.stat_member)
168+
return 0;
168169
// expected-error@+1 {{SYCL kernel cannot use a non-const static data variable}}
169-
if (abp->stat_member) return 0;
170+
if (abp->stat_member)
171+
return 0;
170172
// expected-note@+1 {{called by 'use2'}}
171-
if (ab.fm()) return 0;
173+
if (ab.fm())
174+
return 0;
172175
// expected-error@+1 {{SYCL kernel cannot use a non-const global variable}}
173-
return another_global ;
176+
return another_global;
174177
// expected-error@+1 {{SYCL kernel cannot use a non-const global variable}}
175178
return ns::glob +
176-
// expected-error@+1 {{SYCL kernel cannot use a non-const global variable}}
177-
AnotherNS::moar_globals;
179+
// expected-error@+1 {{SYCL kernel cannot use a non-const global variable}}
180+
AnotherNS::moar_globals;
178181
// expected-note@+1 {{called by 'use2'}}
179182
eh_not_ok();
180-
Check_RTTI_Restriction:: A *a;
183+
Check_RTTI_Restriction::A *a;
181184
// expected-note@+1 2{{called by 'use2'}}
182-
Check_RTTI_Restriction:: isa_B(a);
185+
Check_RTTI_Restriction::isa_B(a);
183186
// expected-note@+1 {{called by 'use2'}}
184187
usage(&addInt);
185188
Check_User_Operators::Fraction f1(3, 8), f2(1, 2), f3(10, 2);
186189
// expected-note@+1 {{called by 'use2'}}
187-
if (f1 == f2) return false;
190+
if (f1 == f2)
191+
return false;
188192
}
189193

190194
template <typename name, typename Func>
@@ -198,7 +202,6 @@ __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
198202

199203
int main() {
200204
a_type ab;
201-
kernel_single_task<class fake_kernel>([]() { usage( &addInt ); });
205+
kernel_single_task<class fake_kernel>([]() { usage(&addInt); });
202206
return 0;
203207
}
204-

0 commit comments

Comments
 (0)