[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-01 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8714,6 +8714,21 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {

VigneshwarJ wrote:

Theres one for CUDA, but I didn't see one for LangOpts.HIP.

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-10-23 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ created 
https://github.com/llvm/llvm-project/pull/113470

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

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device
 code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-10-28 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };
+  QualType NextTy = NewVD->getType();
+  while (NextTy->isAnyPointerType() || NextTy->isArrayType() ||
+ NextTy->isReferenceType()) {

VigneshwarJ wrote:

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.

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-10-28 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ updated 
https://github.com/llvm/llvm-project/pull/113470

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH 1/3] [Clang/AMDGPU] Zero sized arrays not allowed in HIP
 device code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

>From fed2349c62763592866291d35b970d024858d8b1 Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Fri, 25 Oct 2024 14:52:39 -0500
Subject: [PATCH 2/3] fix review comments

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
---
 clang/lib/Sema/SemaDecl.cpp   | 25 +++
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 12 +
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..55e5209d370745 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };

[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-11 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ updated 
https://github.com/llvm/llvm-project/pull/113470

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH 1/4] [Clang/AMDGPU] Zero sized arrays not allowed in HIP
 device code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

>From fed2349c62763592866291d35b970d024858d8b1 Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Fri, 25 Oct 2024 14:52:39 -0500
Subject: [PATCH 2/4] fix review comments

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
---
 clang/lib/Sema/SemaDecl.cpp   | 25 +++
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 12 +
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..55e5209d370745 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };

[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-10-25 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ updated 
https://github.com/llvm/llvm-project/pull/113470

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH 1/2] [Clang/AMDGPU] Zero sized arrays not allowed in HIP
 device code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

>From fed2349c62763592866291d35b970d024858d8b1 Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Fri, 25 Oct 2024 14:52:39 -0500
Subject: [PATCH 2/2] fix review comments

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
---
 clang/lib/Sema/SemaDecl.cpp   | 25 +++
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 12 +
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..55e5209d370745 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };

[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-10-25 Thread Vigneshwar Jayakumar via cfe-commits


@@ -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) {

VigneshwarJ wrote:

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

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-07 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ updated 
https://github.com/llvm/llvm-project/pull/113470

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH 1/4] [Clang/AMDGPU] Zero sized arrays not allowed in HIP
 device code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

>From fed2349c62763592866291d35b970d024858d8b1 Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Fri, 25 Oct 2024 14:52:39 -0500
Subject: [PATCH 2/4] fix review comments

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
---
 clang/lib/Sema/SemaDecl.cpp   | 25 +++
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 12 +
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..55e5209d370745 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };

[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-20 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8714,6 +8714,21 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {

VigneshwarJ wrote:

The reuse of that DeclAttrsMatchCUDAMode with some rechecks doesn't make sense 
because that function returns true if decl is not defined. So, inturn I have to 
check if its inside a functionDecl again else it will wrongly diagnose host 
global zero array as not permitted.

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-20 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ updated 
https://github.com/llvm/llvm-project/pull/113470

>From 0e2ee524f5b5c19169e446c55a386a00cfb0f6bc Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Wed, 23 Oct 2024 09:20:16 -0500
Subject: [PATCH 1/5] [Clang/AMDGPU] Zero sized arrays not allowed in HIP
 device code.

Added diagnosis to throw error when zero sized arrays are used in
the HIP device code. SWDEV-449592
---
 .../clang/Basic/DiagnosticSemaKinds.td|  2 +-
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 25 +++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHIP/zero-sized-device-array.hip

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;
 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(CurContext);
+  if (FD && (FD->hasAttr() ||
+ FD->hasAttr())) {
+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 00..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}}
+}

>From fed2349c62763592866291d35b970d024858d8b1 Mon Sep 17 00:00:00 2001
From: vigneshwar jayakumar 
Date: Fri, 25 Oct 2024 14:52:39 -0500
Subject: [PATCH 2/5] fix review comments

moved code to SemaVarDecl also check the pointer types to figure out
its within any typedefs or pointers.
---
 clang/lib/Sema/SemaDecl.cpp   | 25 +++
 clang/lib/Sema/SemaType.cpp   | 11 
 .../test/SemaHIP/zero-sized-device-array.hip  | 12 +
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 229c9080d558ec..55e5209d370745 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8714,6 +8714,31 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (LangOpts.CUDAIsDevice && LangOpts.HIP) {
+if (FunctionDecl *FD = getCurFunctionDecl();
+FD &&
+(FD->hasAttr() || FD->hasAttr())) {
+
+  auto Check = [&](QualType TypeToCheck, const VarDecl *VD) {
+if (const ConstantArrayType *ArrayT =
+getASTContext().getAsConstantArrayType(TypeToCheck);
+ArrayT && ArrayT->isZeroSize()) {
+  Diag(VD->getLocation(), diag::err_typecheck_zero_array_size) << 2;
+}
+  };

[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-13 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8732,6 +8732,18 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (getLangOpts().HIP &&
+  DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) {

VigneshwarJ wrote:

Yes and that is being checked in the function DeclAttrsMatchCUDAMode.

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-14 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8732,6 +8732,18 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (getLangOpts().HIP &&
+  DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) {

VigneshwarJ wrote:

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. 

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (PR #113470)

2024-11-14 Thread Vigneshwar Jayakumar via cfe-commits


@@ -8732,6 +8732,18 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
 }
   }
 
+  // zero sized static arrays are not allowed in HIP device functions
+  if (getLangOpts().HIP &&
+  DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) {

VigneshwarJ wrote:

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.

https://github.com/llvm/llvm-project/pull/113470
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AMDGPU] Fix opsel for scaled MFMA operations (PR #140183)

2025-05-21 Thread Vigneshwar Jayakumar via cfe-commits

https://github.com/VigneshwarJ closed 
https://github.com/llvm/llvm-project/pull/140183
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits