[clang] 26dde15 - [OpenACC] Add warning for VLAs in a private/firstprivate clause

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 6 13:14:25 PDT 2025


Author: erichkeane
Date: 2025-08-06T13:14:20-07:00
New Revision: 26dde15ed4f1310fa5df3baf03d802ea1cf009b8

URL: https://github.com/llvm/llvm-project/commit/26dde15ed4f1310fa5df3baf03d802ea1cf009b8
DIFF: https://github.com/llvm/llvm-project/commit/26dde15ed4f1310fa5df3baf03d802ea1cf009b8.diff

LOG: [OpenACC] Add warning for VLAs in a private/firstprivate clause

private/firstprivate typically do copy operations, however copying a VLA
isn't really possible.  This patch introduces a warning to alert the
person that this copy isn't happening correctly.

As a future direction, we MIGHT consider doing additional work to make
sure they are initialized/copied/deleted/etc correctly.

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaOpenACC.cpp
    clang/test/SemaOpenACC/private_firstprivate_reduction_required_ops.cpp
    clang/test/SemaOpenACC/sub-array.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index f903b7f4dacd0..cf23594201143 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13531,6 +13531,11 @@ def err_acc_invalid_default_type
 def err_acc_device_type_multiple_archs
     : Error<"OpenACC 'device_type' clause on a 'set' construct only permits "
             "one architecture">;
+def warn_acc_var_referenced_non_const_array
+    : Warning<"variable of array type %0 referenced in OpenACC '%1' clause "
+              "does not have constant bounds; initialization will happen after "
+              "decay to pointer">,
+      InGroup<DiagGroup<"openacc-var-non-const-array">>;
 def warn_acc_var_referenced_lacks_op
     : Warning<"variable of type %0 referenced in OpenACC '%1' clause does not "
               "have a %enum_select<AccVarReferencedReason>{%DefCtor{default "

diff  --git a/clang/lib/Sema/SemaOpenACC.cpp b/clang/lib/Sema/SemaOpenACC.cpp
index c3695f0bb0f9d..4d58b4a5f4079 100644
--- a/clang/lib/Sema/SemaOpenACC.cpp
+++ b/clang/lib/Sema/SemaOpenACC.cpp
@@ -646,8 +646,17 @@ ExprResult CheckVarType(SemaOpenACC &S, OpenACCClauseKind CK, Expr *VarExpr,
   if (auto *RefTy = InnerTy->getAs<ReferenceType>())
     InnerTy = RefTy->getPointeeType();
 
-  if (auto *ArrTy = InnerTy->getAsArrayTypeUnsafe())
+  if (auto *ArrTy = InnerTy->getAsArrayTypeUnsafe()) {
+    // Non constant arrays decay to 'pointer', so warn and return that we're
+    // successful.
+    if (!ArrTy->isConstantArrayType()) {
+      S.Diag(InnerLoc, clang::diag::warn_acc_var_referenced_non_const_array)
+          << InnerTy << CK;
+      return VarExpr;
+    }
+
     return CheckVarType(S, CK, VarExpr, InnerLoc, ArrTy->getElementType());
+  }
 
   auto *RD = InnerTy->getAsCXXRecordDecl();
 

diff  --git a/clang/test/SemaOpenACC/private_firstprivate_reduction_required_ops.cpp b/clang/test/SemaOpenACC/private_firstprivate_reduction_required_ops.cpp
index ce941ad4255da..38eb4e43813a8 100644
--- a/clang/test/SemaOpenACC/private_firstprivate_reduction_required_ops.cpp
+++ b/clang/test/SemaOpenACC/private_firstprivate_reduction_required_ops.cpp
@@ -275,3 +275,20 @@ void firstprivate_arrays() {
 #pragma acc parallel firstprivate(UDCopyArr)
   ;
 }
+
+template<unsigned I>
+void non_const_array_templ() {
+  int CArr[I];
+
+#pragma acc parallel firstprivate(CArr)
+  ;
+}
+
+void non_const_arrays(int I) {
+  non_const_array_templ<5>();
+
+  int NCArr[I];
+  // expected-warning at +1{{variable of array type 'int[I]' referenced in OpenACC 'firstprivate' clause does not have constant bounds; initialization will happen after decay to pointer}}
+#pragma acc parallel firstprivate(NCArr)
+  ;
+}

diff  --git a/clang/test/SemaOpenACC/sub-array.cpp b/clang/test/SemaOpenACC/sub-array.cpp
index 355ac5ef1d3ce..ec929759b6d56 100644
--- a/clang/test/SemaOpenACC/sub-array.cpp
+++ b/clang/test/SemaOpenACC/sub-array.cpp
@@ -61,13 +61,15 @@ void Func(int i, int j) {
 #pragma acc parallel private(ptr[3:])
   while (true);
 
-  // expected-error at +1{{OpenACC sub-array length is unspecified and cannot be inferred because the subscripted value is an array of unknown bound}}
+  // expected-error at +2{{OpenACC sub-array length is unspecified and cannot be inferred because the subscripted value is an array of unknown bound}}
+  // expected-warning at +1{{variable of array type 'int[i]' referenced in OpenACC 'private' clause does not have constant bounds; initialization will happen after decay to pointer}}
 #pragma acc parallel private(VLA[3:])
   while (true);
 
 #pragma acc parallel private(ptr[:3])
   while (true);
 
+  // expected-warning at +1{{variable of array type 'int[i]' referenced in OpenACC 'private' clause does not have constant bounds; initialization will happen after decay to pointer}}
 #pragma acc parallel private(VLA[:3])
   while (true);
 
@@ -159,11 +161,13 @@ void Templ(int i){
   // expected-error at +1{{OpenACC sub-array length is unspecified and cannot be inferred because the subscripted value is not an array}}
 #pragma acc parallel private(ptr[Conv:])
   while (true);
-  // expected-error at +1{{OpenACC sub-array length is unspecified and cannot be inferred because the subscripted value is an array of unknown bound}}
+  // expected-error at +2{{OpenACC sub-array length is unspecified and cannot be inferred because the subscripted value is an array of unknown bound}}
+  // expected-warning at +1{{variable of array type 'int[i]' referenced in OpenACC 'private' clause does not have constant bounds; initialization will happen after decay to pointer}}
 #pragma acc parallel private(VLA[Conv:])
   while (true);
 #pragma acc parallel private(ptr[:Conv])
   while (true);
+  // expected-warning at +1{{variable of array type 'int[i]' referenced in OpenACC 'private' clause does not have constant bounds; initialization will happen after decay to pointer}}
 #pragma acc parallel private(VLA[:Conv])
   while (true);
 


        


More information about the cfe-commits mailing list