Skip to content

Commit e6ce614

Browse files
[SYCL] Reject zero length arrays (#1153)
Signed-off-by: Chris Perkins <[email protected]>
1 parent 7d046b4 commit e6ce614

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

clang/lib/Sema/SemaType.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,12 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
23442344
<< ArraySize->getSourceRange();
23452345
ASM = ArrayType::Normal;
23462346
}
2347+
2348+
// Zero length arrays are disallowed in SYCL device code.
2349+
if (getLangOpts().SYCLIsDevice)
2350+
SYCLDiagIfDeviceCode(ArraySize->getBeginLoc(),
2351+
diag::err_typecheck_zero_array_size)
2352+
<< ArraySize->getSourceRange();
23472353
} else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
23482354
!T->isIncompleteType() && !T->isUndeducedType()) {
23492355
// Is the array too large?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %clang_cc1 -fsycl -triple spir64 -fsycl-is-device -verify -fsyntax-only %s
2+
//
3+
// Ensure that the SYCL diagnostics that are typically deferred are correctly emitted.
4+
5+
// testing that the deferred diagnostics work in conjunction with the SYCL namespaces.
6+
inline namespace cl {
7+
namespace sycl {
8+
9+
template <typename name, typename Func>
10+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
11+
// expected-note@+1 2{{called by 'kernel_single_task<AName, (lambda}}
12+
kernelFunc();
13+
}
14+
15+
} // namespace sycl
16+
} // namespace cl
17+
18+
//variadic functions from SYCL kernels emit a deferred diagnostic
19+
void variadic(int, ...);
20+
21+
int calledFromKernel(int a) {
22+
// expected-error@+1 {{zero-length arrays are not permitted in C++}}
23+
int MalArray[0];
24+
25+
// expected-error@+1 {{__float128 is not supported on this target}}
26+
__float128 malFloat = 40;
27+
28+
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
29+
variadic(5);
30+
31+
return a + 20;
32+
}
33+
34+
// template used to specialize a function that contains a lambda that should
35+
// result in a deferred diagnostic being emitted.
36+
// HOWEVER, this is not working presently.
37+
// TODO: re-test after new deferred diagnostic system is merged.
38+
// restore the "FIX!!" tests below
39+
40+
template <typename T>
41+
void setup_sycl_operation(const T VA[]) {
42+
43+
cl::sycl::kernel_single_task<class AName>([]() {
44+
// FIX!! xpected-error@+1 {{zero-length arrays are not permitted in C++}}
45+
int OverlookedBadArray[0];
46+
47+
// FIX!! xpected-error@+1 {{__float128 is not supported on this target}}
48+
__float128 overlookedBadFloat = 40;
49+
});
50+
}
51+
52+
int main(int argc, char **argv) {
53+
54+
// --- direct lambda testing ---
55+
cl::sycl::kernel_single_task<class AName>([]() {
56+
// expected-error@+1 {{zero-length arrays are not permitted in C++}}
57+
int BadArray[0];
58+
59+
// expected-error@+1 {{__float128 is not supported on this target}}
60+
__float128 badFloat = 40; // this SHOULD trigger a diagnostic
61+
62+
//expected-error@+1 {{SYCL kernel cannot call a variadic function}}
63+
variadic(5);
64+
65+
// expected-note@+1 {{called by 'operator()'}}
66+
calledFromKernel(10);
67+
});
68+
69+
// --- lambda in specialized function testing ---
70+
71+
//array A is only used to feed the template
72+
const int array_size = 4;
73+
int A[array_size] = {1, 2, 3, 4};
74+
setup_sycl_operation(A);
75+
76+
return 0;
77+
}

clang/test/SemaSYCL/sycl-restrict.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ void usage(myFuncDef functionPtr) {
141141

142142
// expected-error@+1 {{__float128 is not supported on this target}}
143143
__float128 A;
144+
145+
// expected-error@+1 {{zero-length arrays are not permitted in C++}}
146+
int BadArray[0];
144147
}
145148

146149
namespace ns {

0 commit comments

Comments
 (0)