]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Accept commas between clauses in OpenMP declare variant
authorPaul-Antoine Arras <parras@baylibre.com>
Mon, 6 Jan 2025 17:38:11 +0000 (18:38 +0100)
committerPaul-Antoine Arras <parras@baylibre.com>
Mon, 13 Jan 2025 17:56:18 +0000 (18:56 +0100)
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.

gcc/fortran/openmp.cc
gcc/testsuite/c-c++-common/gomp/adjust-args-5.c [new file with mode: 0644]
gcc/testsuite/gfortran.dg/gomp/adjust-args-11.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90

index e00044db7d08408da1700169ef20ff51aec92af5..0f1aaa021812d4d2f13837f1efda46e406410b86 100644 (file)
@@ -6559,7 +6559,6 @@ gfc_match_omp_context_selector_specification (gfc_omp_declare_variant *odv)
 match
 gfc_match_omp_declare_variant (void)
 {
-  bool first_p = true;
   char buf[GFC_MAX_SYMBOL_LEN + 1];
 
   if (gfc_match (" (") != MATCH_YES)
@@ -6617,11 +6616,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,
@@ -6637,13 +6640,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)
@@ -6689,8 +6688,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/c-c++-common/gomp/adjust-args-5.c b/gcc/testsuite/c-c++-common/gomp/adjust-args-5.c
new file mode 100644 (file)
index 0000000..8aff73c
--- /dev/null
@@ -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 (file)
index 0000000..d2eb7c1
--- /dev/null
@@ -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
index 62d2cb96fac36529db9575ba315421982464db44..17b112f065bf129d2638b0860ecbf1aec967a443 100644 (file)
@@ -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." }