Tobias originally reported and tracked down this ICE when reviewing one
of Kwok's iterators patches that isn't on mainline yet:
https://gcc.gnu.org/pipermail/gcc-patches/2025-September/695447.html
The bug isn't in that patch, though; it's in an earlier patch in the
series that has already been approved and committed.
The attached patch fixes it, and adds a standalone test case. OK for
GCC 16?
-Sandra
From b9600740155870fa7cff6ccb9a153f2b4240dc5d Mon Sep 17 00:00:00 2001
From: Kwok Cheung Yeung <[email protected]>
Date: Thu, 29 Jan 2026 16:51:32 +0000
Subject: [PATCH] openmp: Fix ICE with "target data" and iterators
gimplify_omp_workshare() correctly handles iterators in the "target"
construct but missed the very similar case for "target data", causing
an ICE.
gcc/ChangeLog
* gimplify.cc (gimplify_omp_workshare): Handle iterators in
"target data".
gcc/testsuite/ChangeLog
* c-c++-common/gomp/target-map-iterators-6.c: New.
Co-authored-by: Sandra Loosemore <[email protected]>
---
gcc/gimplify.cc | 4 ++--
.../c-c++-common/gomp/target-map-iterators-6.c | 17 +++++++++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/gomp/target-map-iterators-6.c
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index ea689e9f734..f4c8bdf9431 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -18912,7 +18912,7 @@ gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
}
gimple_seq iterator_loops_seq = NULL;
- if (TREE_CODE (expr) == OMP_TARGET)
+ if (TREE_CODE (expr) == OMP_TARGET || TREE_CODE (expr) == OMP_TARGET_DATA)
{
remove_unused_omp_iterator_vars (&OMP_CLAUSES (expr));
build_omp_iterators_loops (&OMP_CLAUSES (expr), &iterator_loops_seq);
@@ -19026,7 +19026,7 @@ gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
*uc = NULL_TREE;
*pc = use_device_clauses;
stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
- OMP_CLAUSES (expr));
+ OMP_CLAUSES (expr), iterator_loops_seq);
}
break;
case OMP_TEAMS:
diff --git a/gcc/testsuite/c-c++-common/gomp/target-map-iterators-6.c b/gcc/testsuite/c-c++-common/gomp/target-map-iterators-6.c
new file mode 100644
index 00000000000..bd0e2fbeac4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/target-map-iterators-6.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-fopenmp -fdump-tree-gimple" } */
+
+#define DIM 17
+
+void f (int **x)
+{
+ #pragma omp target data map(iterator(it = 0:DIM), tofrom: x[it])
+ {
+ for (int i = 0; i < DIM; i++)
+ *x[i] += i;
+ }
+}
+
+/* { dg-final { scan-tree-dump "if \\(i <= 16\\) goto <D\\\.\[0-9\]+>; else goto <D\\\.\[0-9\]+>;" "gimple" } } */
+/* { dg-final { scan-tree-dump "map\\(iterator\\(int it=0:17:1, .+\\):tofrom:\\*D\\\.\[0-9\]+" "gimple" } } */
+/* { dg-final { scan-tree-dump "map\\(iterator\\(int it=0:17:1, .+\\):attach:x" "gimple" } } */
--
2.39.5