Skip to content

Conversation

@VigneshwarJ
Copy link
Contributor

Added diagnosis to throw error when zero sized arrays are used in the HIP device code. SWDEV-449592

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-clang

Author: Vigneshwar Jayakumar (VigneshwarJ)

Changes

Added diagnosis to throw error when zero sized arrays are used in the HIP device code. SWDEV-449592


Full diff: https://github.com/llvm/llvm-project/pull/113470.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+1-1)
  • (modified) clang/lib/Sema/SemaType.cpp (+11)
  • (added) clang/test/SemaHIP/zero-sized-device-array.hip (+25)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8e4718008ece72..b5fad40294c368 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6251,7 +6251,7 @@ def err_typecheck_invalid_restrict_invalid_pointee : Error<
 def ext_typecheck_zero_array_size : Extension<
   "zero size arrays are an extension">, InGroup<ZeroLengthArray>;
 def err_typecheck_zero_array_size : Error<
-  "zero-length arrays are not permitted in %select{C++|SYCL device code}0">;
+  "zero-length arrays are not permitted in %select{C++|SYCL device code|HIP device code}0">;
 def err_array_size_non_int : Error<"size of array has non-integer type %0">;
 def err_init_element_not_constant : Error<
   "initializer element is not a compile-time constant">;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 6387fe9f1129ba..3f940102da51d2 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2259,6 +2259,17 @@ QualType Sema::BuildArrayType(QualType T, ArraySizeModifier ASM,
              isSFINAEContext() ? diag::err_typecheck_zero_array_size
                                : diag::ext_typecheck_zero_array_size)
             << 0 << ArraySize->getSourceRange();
+
+        // zero sized static arrays are not allowed in HIP device functions
+        if (LangOpts.HIP && LangOpts.CUDAIsDevice) {
+          auto *FD = dyn_cast_or_null<FunctionDecl>(CurContext);
+          if (FD && (FD->hasAttr<CUDADeviceAttr>() ||
+                     FD->hasAttr<CUDAGlobalAttr>())) {
+            Diag(ArraySize->getBeginLoc(), diag::err_typecheck_zero_array_size)
+                << 2 << ArraySize->getSourceRange();
+            return QualType();
+          }
+        }
       }
 
       // Is the array too large?
diff --git a/clang/test/SemaHIP/zero-sized-device-array.hip b/clang/test/SemaHIP/zero-sized-device-array.hip
new file mode 100644
index 00000000000000..31fc943f5ae75b
--- /dev/null
+++ b/clang/test/SemaHIP/zero-sized-device-array.hip
@@ -0,0 +1,25 @@
+// REQUIRES: amdgpu-registered-target
+// RUN:  %clang_cc1 -fsyntax-only -x hip -fcuda-is-device -verify -triple amdgcn %s
+#define __device__ __attribute__((device))
+#define __host__ __attribute__((host))
+#define __global__ __attribute__((global))
+#define __shared__ __attribute__((shared))
+
+__global__ void global_fun() {
+    float array[0];  // expected-error {{zero-length arrays are not permitted in HIP device code}}
+}
+
+// should not throw error for host side code.
+__host__ void host_fun() {
+    float array[0];
+}
+
+__host__ __device__ void host_dev_fun()
+{
+    float array[0]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
+}
+
+__device__ void device_fun()
+{
+    __shared__ float array[0]; // expected-error {{zero-length arrays are not permitted in HIP device code}}
+}

@bcahoon bcahoon requested a review from yxsamliu October 23, 2024 15:49
Copy link
Collaborator

@JonChesterfield JonChesterfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error looks good. Might want to add a case for "dynamic shared" to the test file as the syntax is very close to the case being diagnosed - iirc it's things like

extern __shared__ float array[];

Some existing handling in C like languages conflates [] and [0], so might also want extern __shared__ float array2[0];

That's probably already covered by existing HIP test cases though, so if everything else is passing I think we're good. Thanks!

Comment on lines 2265 to 2267
auto *FD = dyn_cast_or_null<FunctionDecl>(CurContext);
if (FD && (FD->hasAttr<CUDADeviceAttr>() ||
FD->hasAttr<CUDAGlobalAttr>())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto *FD = dyn_cast_or_null<FunctionDecl>(CurContext);
if (FD && (FD->hasAttr<CUDADeviceAttr>() ||
FD->hasAttr<CUDAGlobalAttr>())) {
if (const auto FD = dyn_cast_if_present(...);FD && (FD->hasAttr<CUDADeviceAttr>() ||
FD->hasAttr<CUDAGlobalAttr>())) {

<< 0 << ArraySize->getSourceRange();

// zero sized static arrays are not allowed in HIP device functions
if (LangOpts.HIP && LangOpts.CUDAIsDevice) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure you're diagnosing this in the right place; BuildArrayType gets called at the point we parse the type, not the variable, so you can easily avoid the diagnostic using something like the following:

typedef float floatarr[0];
__global__ void global_fun() {
    floatarr array;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out, moved the logic to SemaVarDeclaration, also checking for the typedef and pointers

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
};
QualType NextTy = NewVD->getType();
while (NextTy->isAnyPointerType() || NextTy->isArrayType() ||
NextTy->isReferenceType()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble imagining how a pointer to a zero-size array could cause issues; there isn't any way to tell in IR that pointer points to a zero-length array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. I got confused. That would not cause any issue, The while logic is unnecessary, updated to simply check if its an array type.

Comment on lines 8722 to 8724
if (QualType NextTy = NewVD->getType(); NextTy->isArrayType()) {
if (const ConstantArrayType *ArrayT =
getASTContext().getAsConstantArrayType(NextTy);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (QualType NextTy = NewVD->getType(); NextTy->isArrayType()) {
if (const ConstantArrayType *ArrayT =
getASTContext().getAsConstantArrayType(NextTy);
if (const ConstantArrayType *ArrayT =
getASTContext().getAsConstantArrayType(T);


// zero sized static arrays are not allowed in HIP device functions
if (getLangOpts().HIP &&
DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to check LangOpts.CUDAIsDevice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and that is being checked in the function DeclAttrsMatchCUDAMode.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check there is isDeviceSideDecl == LangOpts.CUDAIsDevice... but you don't want to diagnose this on host, do you? (The regression test only checks the -fcuda-is-device case.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right, I dont want to diagnose this on host, but the device side decl are cuda device specific. So I thought there would not be any problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else it would be cleaner to go back to how I was checking previously as this is the only helper function I found that is closely related.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code, in its current state, will diagnose on host. (The testcase doesn't show this because it only runs with -fcuda-is-device.)

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bcahoon bcahoon merged commit 854d730 into llvm:main Nov 27, 2024
8 checks passed
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Nov 29, 2024
Revert: breaks build of rocFFT
854d730 [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (llvm#113470)

Change-Id: Idb4e97a583e9e595154b2da69ef68568b6d96eb5
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Mar 7, 2025
…m#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.
jrbyrnes pushed a commit to jrbyrnes/llvm-project that referenced this pull request Apr 29, 2025
…m#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.

(cherry picked from commit 7cc4722)
rahulc-gh pushed a commit to ROCm/llvm-project that referenced this pull request Nov 6, 2025
…m#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)
rahulc-gh pushed a commit to ROCm/llvm-project that referenced this pull request Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants