]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openmp: Adjust calls to OMPT variants depending on loop-end call master trunk
authorPaul-Antoine Arras <parras@baylibre.com>
Fri, 7 Aug 2026 13:33:37 +0000 (15:33 +0200)
committerPaul-Antoine Arras <parras@baylibre.com>
Mon, 10 Aug 2026 13:27:01 +0000 (15:27 +0200)
When the inscan modifier is present on a for construct, a call to GOMP_loop_end
(or one of its variants) is already emitted. The extra call to one of the
*_worksharing_end functions, enabled by -fopenmp-ompt, is therefore redundant.

Furthermore, even without -fopenmp-ompt, the inscan modifier implies a call to
GOMP_loop_end which requires the start variant of GOMP_loop_static_worksharing.

gcc/ChangeLog:

* omp-expand.cc (expand_omp_for_static_nochunk,
expand_omp_for_static_chunk): Adjust calls to OMPT variants with
GOMP_loop_end.

gcc/testsuite/ChangeLog:

* c-c++-common/gomp/scan-8.c: New test.
* c-c++-common/gomp/scan-9.c: New test.

libgomp/ChangeLog:

* loop.c (GOMP_loop_static_worksharing_start,
GOMP_loop_static_worksharing_end): Update comments.

gcc/omp-expand.cc
gcc/testsuite/c-c++-common/gomp/scan-8.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/gomp/scan-9.c [new file with mode: 0644]
libgomp/loop.c

index d6c44ebdbe21dd639e8b0d2f43ec729b5f18520b..50b9d9817d0cf3a2ae17c43875e96612b2b1f351 100644 (file)
@@ -5278,6 +5278,12 @@ expand_omp_for_static_nochunk (struct omp_region *region,
   t = fold_convert (itype, t);
   n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, true, GSI_SAME_STMT);
 
+  /* When GOMP_loop_end (or one of its variants) is emitted (e.g. with the
+     inscan modifier), which already implies the end of the scope, _start
+     variants of GOMP builtin calls have to be used and _end can be skipped.  */
+  bool has_gomp_loop_end = fd->have_reductemp
+                          || ((fd->have_pointer_condtemp || fd->have_scantemp)
+                              && !fd->have_nonctrl_scantemp);
   {
     /* Fetch the thread/team id and the number of threads/teams in a single
        call to GOMP_loop_static_worksharing or
@@ -5292,13 +5298,15 @@ expand_omp_for_static_nochunk (struct omp_region *region,
       {
       case GF_OMP_FOR_KIND_FOR:
        decl = builtin_decl_explicit (
-         flag_openmp_ompt ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
-                          : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
+         flag_openmp_ompt || has_gomp_loop_end
+           ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
+           : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
        break;
       case GF_OMP_FOR_KIND_DISTRIBUTE:
        decl = builtin_decl_explicit (
-         flag_openmp_ompt ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
-                          : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
+         flag_openmp_ompt || has_gomp_loop_end
+           ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
+           : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
        break;
       default:
        gcc_unreachable ();
@@ -5663,7 +5671,7 @@ expand_omp_for_static_nochunk (struct omp_region *region,
     }
 
   gsi = gsi_last_nondebug_bb (exit_bb);
-  if (flag_openmp_ompt)
+  if (flag_openmp_ompt && !has_gomp_loop_end)
     {
       /* Insert call to GOMP_*_static_worksharing_end at the end of exit_bb.
        */
@@ -6109,6 +6117,10 @@ expand_omp_for_static_chunk (struct omp_region *region,
   n = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE,
                                true, GSI_SAME_STMT);
 
+  /* When GOMP_loop_end (or one of its variants) is emitted (e.g. with the
+     inscan modifier), which already implies the end of the scope, _start
+     variants of GOMP builtin calls have to be used and _end can be skipped.  */
+  bool has_gomp_loop_end = fd->have_reductemp || fd->have_pointer_condtemp;
   {
     /* Fetch the thread/team id and the number of threads/teams in a single
        call to GOMP_loop_static_worksharing or
@@ -6123,13 +6135,15 @@ expand_omp_for_static_chunk (struct omp_region *region,
       {
       case GF_OMP_FOR_KIND_FOR:
        decl = builtin_decl_explicit (
-         flag_openmp_ompt ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
-                          : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
+         flag_openmp_ompt || has_gomp_loop_end
+           ? BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING_START
+           : BUILT_IN_GOMP_LOOP_STATIC_WORKSHARING);
        break;
       case GF_OMP_FOR_KIND_DISTRIBUTE:
        decl = builtin_decl_explicit (
-         flag_openmp_ompt ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
-                          : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
+         flag_openmp_ompt || has_gomp_loop_end
+           ? BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING_START
+           : BUILT_IN_GOMP_DISTRIBUTE_STATIC_WORKSHARING);
        break;
       default:
        gcc_unreachable ();
@@ -6420,7 +6434,7 @@ expand_omp_for_static_chunk (struct omp_region *region,
     }
 
   gsi = gsi_last_nondebug_bb (exit_bb);
-  if (flag_openmp_ompt)
+  if (flag_openmp_ompt && !has_gomp_loop_end)
     {
       /* Insert call to GOMP_*_static_worksharing_end at the end of exit_bb.
        */
diff --git a/gcc/testsuite/c-c++-common/gomp/scan-8.c b/gcc/testsuite/c-c++-common/gomp/scan-8.c
new file mode 100644 (file)
index 0000000..cff8c03
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fopenmp-ompt -fdump-tree-ompexp" } */
+
+/* Check that an extra, OMPT variant of GOMP_loop_end is not emitted when the
+   inscan modifier is present on the for construct.  */
+
+#define N 100
+
+void f(void) {
+    int a[N], b[N];
+    int x = 0;
+
+#pragma omp parallel for simd reduction(inscan, +: x)
+    for (int k = 0; k < N; k++) {
+        x += a[k];
+#pragma omp scan inclusive(x)
+        b[k] = x;
+    }
+}
+
+/* { dg-final { scan-tree-dump "__builtin_GOMP_loop_end_nowait \\(" "ompexp" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_GOMP_loop_end_nowait \\(\\);\[\t\n \]*__builtin_GOMP_loop_static_worksharing_end \\(\\);" "ompexp" } } */
diff --git a/gcc/testsuite/c-c++-common/gomp/scan-9.c b/gcc/testsuite/c-c++-common/gomp/scan-9.c
new file mode 100644 (file)
index 0000000..f033553
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fdump-tree-ompexp" } */
+
+/* Check that the start variant of GOMP_loop_static_worksharing is emitted
+   when the inscan modifier is present on the for construct.  */
+
+#define N 100
+
+void f(void) {
+    int a[N], b[N];
+    int x = 0;
+
+#pragma omp parallel for simd reduction(inscan, +: x)
+    for (int k = 0; k < N; k++) {
+        x += a[k];
+#pragma omp scan inclusive(x)
+        b[k] = x;
+    }
+}
+
+/* { dg-final { scan-tree-dump "__builtin_GOMP_loop_static_worksharing_start \\(" "ompexp" } } */
index 0692663215fa447a0ef22fe7d01aef702681e6fb..7474279fcd62f94b93f6891192ec1a4ce0d51057 100644 (file)
@@ -1201,7 +1201,8 @@ GOMP_loop_static_worksharing (unsigned long long niter
   return nthreads + tid * 1I;
 }
 
-/* OMPT variant enabled by -fopenmp-ompt.  */
+/* OMPT variant enabled by -fopenmp-ompt and when GOMP_loop_end is called
+   (e.g. with the inscan modifier).  */
 
 _Complex int
 GOMP_loop_static_worksharing_start (unsigned long long niter
@@ -1224,7 +1225,8 @@ GOMP_loop_static_worksharing_dispatch (unsigned long long start
                                       __attribute__ ((unused)))
 {}
 
-/* Stub for OMPT callback enabled by -fopenmp-ompt.  */
+/* Stub for OMPT callback enabled by -fopenmp-ompt, except when GOMP_loop_end is
+   already called (e.g. with the inscan modifier).  */
 
 void
 GOMP_loop_static_worksharing_end (void)