Skip to content

Commit 7cc4722

Browse files
VigneshwarJbcahoon
authored andcommitted
[Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (llvm#113470)
Added diagnosis to throw error when zero sized arrays are used in the HIP device code. SWDEV-449592 --------- Co-authored-by: vigneshwar jayakumar <vigneshwar.jayakumar@amd.com> (cherry picked from commit 854d730) Reapply a commit which was reverted due to an issue in rocFFT that needed to be fixed first.
1 parent 5ea58a4 commit 7cc4722

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
@@ -6339,7 +6339,7 @@ def err_typecheck_invalid_restrict_invalid_pointee : Error<
63396339
def ext_typecheck_zero_array_size : Extension<
63406340
"zero size arrays are an extension">, InGroup<ZeroLengthArray>;
63416341
def err_typecheck_zero_array_size : Error<
6342-
"zero-length arrays are not permitted in %select{C++|SYCL device code}0">;
6342+
"zero-length arrays are not permitted in %select{C++|SYCL device code|HIP device code}0">;
63436343
def err_array_size_non_int : Error<"size of array has non-integer type %0">;
63446344
def err_init_element_not_constant : Error<
63456345
"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
@@ -8806,6 +8806,19 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
88068806
}
88078807
}
88088808

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