]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
OpenMP/Fortran: Fix skipping unmatchable metadirectives [PR122570]
authorTobias Burnus <tburnus@baylibre.com>
Wed, 5 Nov 2025 11:51:37 +0000 (12:51 +0100)
committerTobias Burnus <tburnus@baylibre.com>
Wed, 5 Nov 2025 11:51:37 +0000 (12:51 +0100)
Fix a bug in the removal code of always false variants in metadirectives.

PR fortran/122570

gcc/fortran/ChangeLog:

* openmp.cc (resolve_omp_metadirective): Fix 'skip' of
never matchable metadirective variants.

gcc/testsuite/ChangeLog:

* gfortran.dg/gomp/pr122570.f: New test.

gcc/fortran/openmp.cc
gcc/testsuite/gfortran.dg/gomp/pr122570.f [new file with mode: 0644]

index f5db9a81ea6a85847af9912ec164b2134e101d39..770bc5b1200fff20428946fa224a32cd702a2e31 100644 (file)
@@ -12320,6 +12320,7 @@ static void
 resolve_omp_metadirective (gfc_code *code, gfc_namespace *ns)
 {
   gfc_omp_variant *variant = code->ext.omp_variants;
+  gfc_omp_variant *prev_variant = variant;
 
   while (variant)
     {
@@ -12333,15 +12334,19 @@ resolve_omp_metadirective (gfc_code *code, gfc_namespace *ns)
             as the 'otherwise' clause should always match.  */
          if (variant == code->ext.omp_variants && !variant->next)
            break;
-         if (variant == code->ext.omp_variants)
-           code->ext.omp_variants = variant->next;
          gfc_omp_variant *tmp = variant;
-         variant = variant->next;
+         if (variant == code->ext.omp_variants)
+           variant = prev_variant = code->ext.omp_variants = variant->next;
+         else
+           variant = prev_variant->next = variant->next;
          gfc_free_omp_set_selector_list (tmp->selectors);
          free (tmp);
        }
       else
-       variant = variant->next;
+       {
+         prev_variant = variant;
+         variant = variant->next;
+       }
     }
   /* Replace metadirective by its body if only 'nothing' remains.  */
   if (!code->ext.omp_variants->next && code->ext.omp_variants->stmt == ST_NONE)
diff --git a/gcc/testsuite/gfortran.dg/gomp/pr122570.f b/gcc/testsuite/gfortran.dg/gomp/pr122570.f
new file mode 100644 (file)
index 0000000..9897cc6
--- /dev/null
@@ -0,0 +1,29 @@
+! { dg-do compile }
+! { dg-additional-options "-Wall" }
+
+! PR fortran/122570
+
+      SUBROUTINE INITAL
+      implicit none (type, external)
+      integer :: j, n
+      n = 5
+!$omp  metadirective                                                            &
+!$omp&    when(user={condition(.true.)}: target teams                           &
+!$omp&        distribute parallel do)                                           &
+!$omp&    when(user={condition(.false.)}: target teams                          &
+!$omp&        distribute parallel do) 
+      DO J=1,N
+      END DO
+      END SUBROUTINE
+
+      SUBROUTINE CALC3
+       implicit none (type, external)
+       integer :: i, m
+       m = 99
+!$omp  metadirective 
+!$omp& when(user={condition(.false.)}:
+!$omp&      simd)               
+      DO 301 I=1,M
+  301 CONTINUE
+  300 CONTINUE ! { dg-warning "Label 300 at .1. defined but not used \\\[-Wunused-label\\\]" }
+      END SUBROUTINE