Hi

This patch fixes the regression in libgomp.c++/target-6.C seen in x86-linux with -m32, aarch64 and possibly others.

The code to allocate memory for variable-sized variables in private clauses was initially:

var = OMP_CLAUSE_DECL (c);
if (is_variable_sized (var))
  {
    ...
    tree x = TYPE_SIZE_UNIT (TREE_TYPE (new_var));
    ...
  }
else if (omp_privatize_by_reference (var)
         && !is_gimple_omp_oacc (ctx->stmt))
  {
    ...
    tree x = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (new_var)));
    ...
  }

So the first branch was for objects that are directly variably sized (e.g. int a[x]), while the second was for references to variably sized objects (e.g. int (&b)[x] = a).

The patch changed the first condition to is_variable_sized (var, by_ref). If var is a reference to a VLA, then this would be true (as var is referencing a variably-sized object) and the first branch would be taken, resulting in the memory allocated being the size of the reference instead of the size of the referred object.

Pushed fix in commit 92325afa259194e44fc21b36ba1d70d00c2e70fe on mainline and 85115e349472e1afc5f82d21cbf9ffd30afdb532 on devel/omp/gcc-15. I have also added a compile-time test to check that the size for the memory allocation is from a variable and not a constant.

Kwok

On 13/02/2026 11:35 am, Tobias Burnus wrote:
Kwok Cheung Yeung wrote:
This patch has been committed to mainline and also backported to the devel/omp/gcc-15 branch as commit 64a5a58901e0a5829da1452254a7934afcd67faa.

The testcases in gcc/testsuite/ had the rather common problem
of assuming that omp.h and omp_lib are available. However, those
can only be used in libgomp/testsuite/.

[Depending on how one tests, this is hidden by them getting picked up
from elsewhere.]

Sorry for the breakage and missing it during the review.
Fixed by commit r16-7498-geb311dc3db842d (as attached).

Tobias
From 92325afa259194e44fc21b36ba1d70d00c2e70fe Mon Sep 17 00:00:00 2001
From: Kwok Cheung Yeung <[email protected]>
Date: Fri, 20 Feb 2026 16:07:45 +0000
Subject: [PATCH] openmp: Fix regression in libgomp.c++/target-6.C testcase
 [PR113436]

The fix for PR113436 introduced a regression causing the
libgomp.c++/target-6.C testcase to fail on some configurations.

This was caused by a change in how is_variable_sized is applied for variables
that are references in private clauses, causing the path that was intended for
variables that are variable-sized in themselves to be taken, instead of
that for referencing a variable-sized object.

2026-02-20  Kwok Cheung Yeung  <[email protected]>

gcc/

        PR middle-end/113436
        * omp-low.cc (omp_lower_target):  Do not check for variable-length
        variables in private clauses by reference when allocating memory.

gcc/testsuite/

        PR middle-end/113436
        * g++.dg/gomp/pr113436-2.C: New.
---
 gcc/omp-low.cc                         |  2 +-
 gcc/testsuite/g++.dg/gomp/pr113436-2.C | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/gomp/pr113436-2.C

diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 15245eae9de..aeed1d25e8a 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -14458,7 +14458,7 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, 
omp_context *ctx)
          case OMP_CLAUSE_PRIVATE:
            var = OMP_CLAUSE_DECL (c);
            by_ref = omp_privatize_by_reference (var);
-           if (is_variable_sized (var, by_ref))
+           if (is_variable_sized (var))
              {
                tree new_var = lookup_decl (var, ctx);
                tree *allocate_ptr = alloc_map.get (new_var);
diff --git a/gcc/testsuite/g++.dg/gomp/pr113436-2.C 
b/gcc/testsuite/g++.dg/gomp/pr113436-2.C
new file mode 100644
index 00000000000..70f6ffc196e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/pr113436-2.C
@@ -0,0 +1,17 @@
+// PR middle-end/113436
+// { dg-do "compile" }
+// { dg-options "-fopenmp -fdump-tree-omplower" }
+
+void f(int x)
+{
+  int a[x];
+  int (&c)[x] = a;
+
+  #pragma omp target private (c)
+  {
+    c[0] = 1;
+  }
+}
+
+// Ensure that the size of memory allocated for the VLA is from a variable 
rather than a constant.
+// { dg-final { scan-tree-dump "D\\\.\[0-9\]\+ = __builtin_alloca_with_align 
\\\(D\\\.\[0-9\]\+, \[0-9\]\+\\\);" "omplower" } }
-- 
2.43.0

Reply via email to