Skip to content

Commit 7743e86

Browse files
authored
[SYCL] Forbid declaration of non-const static variables inside kernels (#1141)
Signed-off-by: Aleksander Fadeev <aleksander.fadeev@intel.com>
1 parent 59f39b2 commit 7743e86

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7057,6 +7057,13 @@ NamedDecl *Sema::ActOnVariableDeclarator(
70577057
NewVD->setTSCSpec(TSCS);
70587058
}
70597059

7060+
// Static variables declared inside SYCL device code must be const or
7061+
// constexpr
7062+
if (getLangOpts().SYCLIsDevice && SCSpec == DeclSpec::SCS_static &&
7063+
!R.isConstant(Context))
7064+
SYCLDiagIfDeviceCode(D.getIdentifierLoc(), diag::err_sycl_restrict)
7065+
<< Sema::KernelNonConstStaticDataVariable;
7066+
70607067
switch (D.getDeclSpec().getConstexprSpecifier()) {
70617068
case CSK_unspecified:
70627069
break;

clang/test/CodeGenSYCL/address-space-of-returns.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const char *ret_char() {
1010
// CHECK: ret i8 addrspace(4)* addrspacecast (i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0) to i8 addrspace(4)*)
1111

1212
const char *ret_arr() {
13-
static char Arr[42];
13+
const static char Arr[36] = "Carrots, cabbage, radish, potatoes!";
1414
return Arr;
1515
}
16-
// CHECK: ret i8 addrspace(4)* getelementptr inbounds ([42 x i8], [42 x i8] addrspace(4)* addrspacecast ([42 x i8] addrspace(1)* @{{.*}}ret_arr{{.*}}Arr to [42 x i8] addrspace(4)*), i64 0, i64 0)
16+
17+
// CHECK: ret i8 addrspace(4)* getelementptr inbounds ([36 x i8], [36 x i8] addrspace(4)* addrspacecast ([36 x i8] addrspace(1)* @{{.*}}ret_arr{{.*}}Arr to [36 x i8] addrspace(4)*), i64 0, i64 0)
1718

1819
const char &ret_ref() {
19-
static char a = 'A';
20+
const static char a = 'A';
2021
return a;
2122
}
2223
// CHECK: ret i8 addrspace(4)* addrspacecast (i8 addrspace(1)* @{{.*}}ret_ref{{.*}} to i8 addrspace(4)*)

clang/test/CodeGenSYCL/address-space-swap.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
// RUN: %clang -fsycl-device-only -S -emit-llvm %s -o - | FileCheck %s
22
#include <algorithm>
33

4-
54
void test() {
6-
static int foo = 0x42;
7-
// CHECK: @[[FOO:[a-zA-Z0-9_]+]] = internal addrspace(1) global i32 66, align 4
5+
int foo = 0x42;
6+
// CHECK: %[[FOO:[a-zA-Z0-9_]+]] = alloca i32, align 4
87
int i = 43;
9-
// CHECK: %[[I:[a-zA-Z0-9_]+]] = alloca i32, align 4
8+
// CHECK: %[[I:[a-zA-Z0-9_]+]] = alloca i32, align 4
109

11-
int* p1 = &foo;
12-
int* p2 = &i;
13-
// CHECK: %[[P1:[a-zA-Z0-9_]+]] = alloca i32 addrspace(4)*, align 8
14-
// CHECK: %[[P2:[a-zA-Z0-9_]+]] = alloca i32 addrspace(4)*, align 8
15-
// CHECK: %[[P1GEN:[a-zA-Z0-9_]+]] = addrspacecast i32 addrspace(4)** %[[P1]] to i32 addrspace(4)* addrspace(4)*
16-
// CHECK: %[[P2GEN:[a-zA-Z0-9_]+]] = addrspacecast i32 addrspace(4)** %[[P2]] to i32 addrspace(4)* addrspace(4)*
10+
int *p1 = &foo;
11+
int *p2 = &i;
12+
// CHECK: %[[P1:[a-zA-Z0-9_]+]] = alloca i32 addrspace(4)*, align 8
13+
// CHECK: %[[P2:[a-zA-Z0-9_]+]] = alloca i32 addrspace(4)*, align 8
14+
// CHECK: %[[P1GEN:[a-zA-Z0-9_]+]] = addrspacecast i32 addrspace(4)** %[[P1]] to i32 addrspace(4)* addrspace(4)*
15+
// CHECK: %[[P2GEN:[a-zA-Z0-9_]+]] = addrspacecast i32 addrspace(4)** %[[P2]] to i32 addrspace(4)* addrspace(4)*
1716

1817
std::swap(p1, p2);
19-
// CHECK: call spir_func void @_ZSt4swap{{.*}}(i32 addrspace(4)* addrspace(4)* dereferenceable(8) %[[P1GEN]], i32 addrspace(4)* addrspace(4)* dereferenceable(8) %[[P2GEN]])
18+
// CHECK: call spir_func void @_ZSt4swap{{.*}}(i32 addrspace(4)* addrspace(4)* dereferenceable(8) %[[P1GEN]], i32 addrspace(4)* addrspace(4)* dereferenceable(8) %[[P2GEN]])
2019

2120
std::swap(foo, i);
22-
// CHECK: %[[ICAST:[a-zA-Z0-9_]+]] = addrspacecast i32* %[[I]] to i32 addrspace(4)*
23-
// CHECK: call spir_func void @_ZSt4swap{{.*}}(i32 addrspace(4)* dereferenceable(4) addrspacecast (i32 addrspace(1)* @[[FOO]] to i32 addrspace(4)*), i32 addrspace(4)* dereferenceable(4) %[[ICAST]])
21+
// CHECK: %[[FOOCAST:[a-zA-Z0-9_]+]] = addrspacecast i32* %[[FOO]] to i32 addrspace(4)*
22+
// CHECK: %[[ICAST:[a-zA-Z0-9_]+]] = addrspacecast i32* %[[I]] to i32 addrspace(4)*
23+
// CHECK: call spir_func void @_ZSt4swap{{.*}}(i32 addrspace(4)* dereferenceable(4) %[[FOOCAST]], i32 addrspace(4)* dereferenceable(4) %[[ICAST]])
2424
}
2525

26-
2726
template <typename name, typename Func>
2827
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
2928
kernelFunc();
3029
}
3130

32-
3331
int main() {
3432
kernel_single_task<class fake_kernel>([]() { test(); });
3533
return 0;

clang/test/CodeGenSYCL/intel-fpga-local.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_cc1 -triple spir64-unknown-unknown-sycldevice -disable-llvm-passes -fsycl-is-device -emit-llvm %s -o - | FileCheck %s -check-prefixes CHECK-DEVICE,CHECK-BOTH
22
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s -check-prefixes CHECK-HOST,CHECK-BOTH
33

4+
// CHECK-BOTH: @_ZZ15attrs_on_staticvE15static_numbanks = internal{{.*}}constant i32 20, align 4
45
// CHECK-DEVICE: [[ANN_numbanks_4:@.str]] = {{.*}}{memory:DEFAULT}{sizeinfo:4}{numbanks:4}
6+
// CHECK-BOTH: @_ZZ15attrs_on_staticvE15static_annotate = internal{{.*}}constant i32 30, align 4
57
// CHECK-BOTH: [[ANN_annotate:@.str[.0-9]*]] = {{.*}}foobar
68
// CHECK-DEVICE: [[ANN_register:@.str.[0-9]*]] = {{.*}}{register:1}
79
// CHECK-DEVICE: [[ANN_memory_default:@.str.[0-9]*]] = {{.*}}{memory:DEFAULT}{sizeinfo:4}
@@ -25,24 +27,17 @@
2527

2628
// CHECK-BOTH: @llvm.global.annotations
2729
// CHECK-DEVICE-SAME: { i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(1)* @_ZZ15attrs_on_staticvE15static_numbanks to i8 addrspace(1)*) to i8*)
28-
// CHECK-DEVICE-SAME: [[ANN_numbanks_4]]{{.*}}i32 38
30+
// CHECK-DEVICE-SAME: [[ANN_numbanks_4]]{{.*}} i32 39
2931
// CHECK-DEVICE-SAME: { i8* addrspacecast (i8 addrspace(1)* bitcast (i32 addrspace(1)* @_ZZ15attrs_on_staticvE15static_annotate to i8 addrspace(1)*) to i8*)
3032
// CHECK-HOST-SAME: { i8* bitcast (i32* @_ZZ15attrs_on_staticvE15static_annotate to i8*)
31-
// CHECK-BOTH-SAME: [[ANN_annotate]]{{.*}}i32 42
33+
// CHECK-BOTH-SAME: [[ANN_annotate]]{{.*}} i32 40
3234

3335
// CHECK-HOST-NOT: llvm.var.annotation
3436
// CHECK-HOST-NOT: llvm.ptr.annotation
3537

3638
void attrs_on_static() {
37-
int a = 42;
38-
static int static_numbanks [[intelfpga::numbanks(4)]];
39-
// CHECK-BOTH: load{{.*}}static_numbanks
40-
// CHECK-BOTH: store{{.*}}static_numbanks
41-
static_numbanks = static_numbanks + a;
42-
static int static_annotate [[clang::annotate("foobar")]];
43-
// CHECK-BOTH: load{{.*}}static_annotate
44-
// CHECK-BOTH: store{{.*}}static_annotate
45-
static_annotate = static_annotate + a;
39+
const static int static_numbanks [[intelfpga::numbanks(4)]] = 20;
40+
const static int static_annotate [[clang::annotate("foobar")]] = 30;
4641
}
4742

4843
void attrs_on_var() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -fsycl-is-device %s
2+
3+
void usage() {
4+
// expected-error@+1{{SYCL kernel cannot use a non-const static data variable}}
5+
static int s1;
6+
const static int cs = 0;
7+
constexpr static int ces = 0;
8+
}
9+
10+
template <typename Name, typename Func>
11+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
12+
// expected-error@+1{{SYCL kernel cannot use a non-const static data variable}}
13+
static int z;
14+
// expected-note-re@+2{{called by 'kernel_single_task<fake_kernel, (lambda at {{.*}})>}}
15+
// expected-note-re@+1{{called by 'kernel_single_task<fake_kernel, (lambda at {{.*}})>}}
16+
kernelFunc();
17+
}
18+
19+
int main() {
20+
static int s2;
21+
kernel_single_task<class fake_kernel>([]() {
22+
// expected-note@+1{{called by 'operator()'}}
23+
usage();
24+
// expected-error@+1{{SYCL kernel cannot use a non-const static data variable}}
25+
static int s3;
26+
});
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)