Skip to content

Commit aa5be05

Browse files
authored
[Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (#11… (#71)
2 parents d5f3852 + 7cc4722 commit aa5be05

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6374,7 +6374,7 @@ def err_typecheck_invalid_restrict_invalid_pointee : Error<
63746374
def ext_typecheck_zero_array_size : Extension<
63756375
"zero size arrays are an extension">, InGroup<ZeroLengthArray>;
63766376
def err_typecheck_zero_array_size : Error<
6377-
"zero-length arrays are not permitted in %select{C++|SYCL device code}0">;
6377+
"zero-length arrays are not permitted in %select{C++|SYCL device code|HIP device code}0">;
63786378
def err_array_size_non_int : Error<"size of array has non-integer type %0">;
63796379
def err_init_element_not_constant : Error<
63806380
"initializer element is not a compile-time constant">;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8820,6 +8820,19 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
88208820
}
88218821
}
88228822

8823+
// zero sized static arrays are not allowed in HIP device functions
8824+
if (getLangOpts().HIP && LangOpts.CUDAIsDevice) {
8825+
if (FunctionDecl *FD = getCurFunctionDecl();
8826+
FD &&
8827+
(FD->hasAttr<CUDADeviceAttr>() || FD->hasAttr<CUDAGlobalAttr>())) {
8828+
if (const ConstantArrayType *ArrayT =
8829+
getASTContext().getAsConstantArrayType(T);
8830+
ArrayT && ArrayT->isZeroSize()) {
8831+
Diag(NewVD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
8832+
}
8833+
}
8834+
}
8835+
88238836
bool isVM = T->isVariablyModifiedType();
88248837
if (isVM || NewVD->hasAttr<CleanupAttr>() ||
88258838
NewVD->hasAttr<BlocksAttr>())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// REQUIRES: amdgpu-registered-target
2+
// RUN: %clang_cc1 -fsyntax-only -x hip -fcuda-is-device -verify -triple amdgcn %s
3+
#define __device__ __attribute__((device))
4+
#define __host__ __attribute__((host))
5+
#define __global__ __attribute__((global))
6+
#define __shared__ __attribute__((shared))
7+
8+
typedef float ZEROARR[0];
9+
10+
float global_array[0];
11+
12+
__global__ void global_fun() {
13+
extern __shared__ float externArray[];
14+
ZEROARR TypeDef; // expected-error {{zero-length arrays are not permitted in HIP device code}}
15+
float array[0]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
16+
}
17+
18+
// should not throw error for host side code.
19+
__host__ void host_fun() {
20+
float array[0];
21+
}
22+
23+
template <typename Ty, unsigned Size>
24+
__device__ void templated()
25+
{
26+
Ty arr[Size]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
27+
}
28+
29+
__host__ __device__ void host_dev_fun()
30+
{
31+
float array[0]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
32+
}
33+
34+
__device__ void device_fun()
35+
{
36+
__shared__ float array[0]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
37+
templated<int,0>(); // expected-note {{in instantiation of function template specialization 'templated<int, 0U>' requested here}}
38+
}

0 commit comments

Comments
 (0)