https://gcc.gnu.org/g:0a49642cf5173c4a8c6752c1e3fd4bad4dbcebab

commit 0a49642cf5173c4a8c6752c1e3fd4bad4dbcebab
Author: Paul-Antoine Arras <par...@baylibre.com>
Date:   Mon Jan 6 18:38:11 2025 +0100

    Accept commas between clauses in OpenMP declare variant
    
    Add support to the Fortran parser for the OpenMP syntax that allows a comma
    after the directive name and between clauses of declare variant. The C and 
C++
    parsers already support this syntax so only a new test is added.
    
    gcc/fortran/ChangeLog:
    
            * openmp.cc (gfc_match_omp_declare_variant): Match comma after 
directive
            name and between clauses. Emit more useful diagnostics.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/gomp/declare-variant-2.f90: Remove error test for a 
comma
            after the directive name. Add tests for other invalid syntaxes 
(extra
            comma and invalid clause).
            * c-c++-common/gomp/adjust-args-5.c: New test.
            * gfortran.dg/gomp/adjust-args-11.f90: New test.
    
    (cherry picked from commit 2ea4801cf7a4ebc0145b84f84ae7f4961e57b64f)

Diff:
---
 gcc/fortran/ChangeLog.omp                          |  8 ++++
 gcc/fortran/openmp.cc                              | 23 ++++++-----
 gcc/testsuite/ChangeLog.omp                        | 11 ++++++
 gcc/testsuite/c-c++-common/gomp/adjust-args-5.c    | 21 ++++++++++
 gcc/testsuite/gfortran.dg/gomp/adjust-args-11.f90  | 45 ++++++++++++++++++++++
 .../gfortran.dg/gomp/declare-variant-2.f90         | 10 ++++-
 6 files changed, 106 insertions(+), 12 deletions(-)

diff --git a/gcc/fortran/ChangeLog.omp b/gcc/fortran/ChangeLog.omp
index 6b92d87bd68f..65c4c761e331 100644
--- a/gcc/fortran/ChangeLog.omp
+++ b/gcc/fortran/ChangeLog.omp
@@ -1,3 +1,11 @@
+2025-01-27  Paul-Antoine Arras  <par...@baylibre.com>
+
+       Backported from master:
+       2025-01-13  Paul-Antoine Arras  <par...@baylibre.com>
+
+       * openmp.cc (gfc_match_omp_declare_variant): Match comma after directive
+       name and between clauses. Emit more useful diagnostics.
+
 2025-01-27  Paul-Antoine Arras  <par...@baylibre.com>
 
        Backported from master:
diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index 1411521ed9c2..edc88d549800 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -6857,7 +6857,6 @@ gfc_match_omp_context_selector_specification 
(gfc_omp_set_selector **oss_head,
 match
 gfc_match_omp_declare_variant (void)
 {
-  bool first_p = true;
   char buf[GFC_MAX_SYMBOL_LEN + 1];
 
   if (gfc_match (" (") != MATCH_YES)
@@ -6915,11 +6914,15 @@ gfc_match_omp_declare_variant (void)
       return MATCH_ERROR;
     }
 
-  bool has_match = false, has_adjust_args = false;
+  bool has_match = false, has_adjust_args = false, error_p = false;
   locus adjust_args_loc;
 
   for (;;)
     {
+      gfc_gobble_whitespace ();
+      gfc_match_char (',');
+      gfc_gobble_whitespace ();
+
       enum clause
       {
        match,
@@ -6935,13 +6938,9 @@ gfc_match_omp_declare_variant (void)
        }
       else
        {
-         if (first_p)
-           {
-             gfc_error ("expected %<match%> or %<adjust_args%> at %C");
-             return MATCH_ERROR;
-           }
-         else
-           break;
+         if (gfc_match_omp_eos () != MATCH_YES)
+           error_p = true;
+         break;
        }
 
       if (gfc_match (" (") != MATCH_YES)
@@ -6988,8 +6987,12 @@ gfc_match_omp_declare_variant (void)
            for (gfc_omp_namelist *n = *head; n != NULL; n = n->next)
              n->u.need_device_ptr = true;
        }
+    }
 
-      first_p = false;
+  if (error_p || (!has_match && !has_adjust_args))
+    {
+      gfc_error ("expected %<match%> or %<adjust_args%> at %C");
+      return MATCH_ERROR;
     }
 
   if (has_adjust_args && !has_match)
diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index 0a2661989f6f..db5c67dc4da6 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -1,3 +1,14 @@
+2025-01-27  Paul-Antoine Arras  <par...@baylibre.com>
+
+       Backported from master:
+       2025-01-13  Paul-Antoine Arras  <par...@baylibre.com>
+
+       * gfortran.dg/gomp/declare-variant-2.f90: Remove error test for a comma
+       after the directive name. Add tests for other invalid syntaxes (extra
+       comma and invalid clause).
+       * c-c++-common/gomp/adjust-args-5.c: New test.
+       * gfortran.dg/gomp/adjust-args-11.f90: New test.
+
 2025-01-27  Paul-Antoine Arras  <par...@baylibre.com>
 
        Backported from master:
diff --git a/gcc/testsuite/c-c++-common/gomp/adjust-args-5.c 
b/gcc/testsuite/c-c++-common/gomp/adjust-args-5.c
new file mode 100644
index 000000000000..8aff73cc6e55
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/adjust-args-5.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+
+/* Check that the OpenMP 5.1 syntax with commas after the directive name and
+   between clauses is supported. */
+
+int f (int a, void *b, float c[2]);
+
+#pragma omp declare variant (f), match (construct={dispatch}), adjust_args 
(nothing: a), adjust_args (need_device_ptr: b, c)
+int f0 (int a, void *b, float c[2]);
+
+int test () {
+  int a;
+  void *b;
+  float c[2];
+  struct {int a;} s;
+
+  #pragma omp dispatch, novariants(0), nocontext(1)
+  s.a = f0 (a, b, c);
+
+  return s.a;
+}
diff --git a/gcc/testsuite/gfortran.dg/gomp/adjust-args-11.f90 
b/gcc/testsuite/gfortran.dg/gomp/adjust-args-11.f90
new file mode 100644
index 000000000000..d2eb7c1d72cc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/adjust-args-11.f90
@@ -0,0 +1,45 @@
+! { dg-do compile }
+
+! Check that the OpenMP syntax with commas between clauses is supported.
+! A comma after the directive name is introduced in 5.2, which currently is 
only
+! partially supported.
+
+module main
+  use iso_c_binding, only: c_ptr
+  implicit none
+
+  type :: struct
+    integer :: a
+    real :: b
+  end type
+
+  interface
+    integer function f(a, b, c)
+      import c_ptr
+      integer, intent(in) :: a
+      type(c_ptr), intent(inout) :: b
+      type(c_ptr), intent(out) :: c(:)
+    end function
+    integer function f0(a, b, c)
+      import c_ptr
+      integer, intent(in) :: a
+      type(c_ptr), intent(inout) :: b
+      type(c_ptr), intent(out) :: c(:)
+      !$omp  declare variant (f), match (construct={dispatch}) , &
+      !$omp&         adjust_args (nothing: a) ,adjust_args (need_device_ptr: 
b),adjust_args (need_device_ptr: c)
+    end function
+  end interface
+
+contains
+subroutine test
+  integer :: a
+  type(c_ptr) :: b
+  type(c_ptr) :: c(2)
+  type(struct) :: s
+
+  !!$omp dispatch, nocontext(.false.), novariants(.false.)   ! Not supported 
yet
+  !$omp dispatch nocontext(.false.), novariants(.false.)
+  s%a = f0 (a, b, c)
+
+end subroutine
+end module
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90 
b/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
index 62d2cb96fac3..17b112f065bf 100644
--- a/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
@@ -182,8 +182,14 @@ contains
   subroutine f74 ()
     !$omp declare variant (f1) match(construct={requires})     ! { dg-warning 
"unknown selector 'requires' for context selector set 'construct' at .1." }
   end subroutine
-  subroutine f75 ()
-    !$omp declare variant (f1),match(construct={parallel})     ! { dg-error 
"expected 'match' or 'adjust_args' at .1." }
+  subroutine f75a ()
+    !$omp declare variant(f1) ,,match(construct={dispatch}) 
adjust_args(need_device_ptr : c)        ! { dg-error "expected 'match' or 
'adjust_args' at .1." }
+  end subroutine
+  subroutine f75b ()
+    !$omp declare variant(f1) 
match(construct={dispatch}),,adjust_args(need_device_ptr : c)        ! { 
dg-error "expected 'match' or 'adjust_args' at .1." }
+  end subroutine
+  subroutine f75c ()
+    !$omp declare variant(f1) match(construct={dispatch}),nowait(a)        ! { 
dg-error "expected 'match' or 'adjust_args' at .1." }
   end subroutine
   subroutine f76 ()
     !$omp declare variant (f1) 
match(implementation={atomic_default_mem_order("relaxed")})     ! { dg-error 
"expected identifier at .1." }

Reply via email to