Skip to content

Commit 27682a1

Browse files
authored
[Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (llvm#113470) (llvm#4650)
2 parents 0d26563 + bea22ca commit 27682a1

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
@@ -6328,7 +6328,7 @@ def err_typecheck_invalid_restrict_invalid_pointee : Error<
63286328
def ext_typecheck_zero_array_size : Extension<
63296329
"zero size arrays are an extension">, InGroup<ZeroLengthArray>;
63306330
def err_typecheck_zero_array_size : Error<
6331-
"zero-length arrays are not permitted in %select{C++|SYCL device code}0">;
6331+
"zero-length arrays are not permitted in %select{C++|SYCL device code|HIP device code}0">;
63326332
def err_array_size_non_int : Error<"size of array has non-integer type %0">;
63336333
def err_init_element_not_constant : Error<
63346334
"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
@@ -8802,6 +8802,19 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
88028802
}
88038803
}
88048804

8805+
// zero sized static arrays are not allowed in HIP device functions
8806+
if (getLangOpts().HIP && LangOpts.CUDAIsDevice) {
8807+
if (FunctionDecl *FD = getCurFunctionDecl();
8808+
FD &&
8809+
(FD->hasAttr<CUDADeviceAttr>() || FD->hasAttr<CUDAGlobalAttr>())) {
8810+
if (const ConstantArrayType *ArrayT =
8811+
getASTContext().getAsConstantArrayType(T);
8812+
ArrayT && ArrayT->isZeroSize()) {
8813+
Diag(NewVD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
8814+
}
8815+
}
8816+
}
8817+
88058818
bool isVM = T->isVariablyModifiedType();
88068819
if (isVM || NewVD->hasAttr<CleanupAttr>() ||
88078820
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)