Fortran: OpenMP fix declare simd inside modules and absent linear [PR106566]

Message ID 9dde1b4a-03cb-214c-8eb7-8f75978754c7@codesourcery.com
State New, archived
Headers
Series Fortran: OpenMP fix declare simd inside modules and absent linear [PR106566] |

Commit Message

Tobias Burnus Aug. 16, 2022, 2:06 p.m. UTC
  This patch fixes two issues – the first was reported to me by email but it
also shows up in the official OpenMP examples (see PR).

Namely: Inside a module, 'gfc_match(" ( %s )")' fails as the symbol is already
host associated. (The symbol is the current procedure name.)

Solution: Match with passing (permit) host_assoc = true to the match function
instead of 'false' as done with '%s'.

Afterwards, it was failing when folding a NULL_TREE. Solution: Init the linear
step with 1 in that case.

OK for mainline?

  * * *

I have not checked GCC < mainline. The current example is OpenMP 5.2 only
and only supported since June 7, 2022 in C/C++ and July 4 for Fortran.
However, I assume the same issue also affects GCC < 13 with a tailored
testcase. - If there is the sentiment to fix it also for older GCC,
I can come up with modified testcases and a GCC 12 (and GCC 11?) patch.
Thoughts?

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Comments

Tobias Burnus Aug. 16, 2022, 2:45 p.m. UTC | #1
Fixed subject line: "absent linear" should be "absent linear step" in the subject line;
i.e. with "step" added: "Fortran: OpenMP fix declare simd inside modules and absent linear step [PR106566]"

I have also decided to move the 'step = 1' to openmp.cc, which also set it before with
the old pre-OpenMP 5.2 syntax.

I also added a pre-OpenMP-5.2-syntax example.

  * * *

For GCC 12 (and GCC 11), only the '%s' fix and the third, now added example apply;
for the 5.1 syntax, 'step' was already set.

OK? And thoughts regarding the backports (none? Only 12? Or 11+12?)?

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  
Jakub Jelinek Aug. 17, 2022, 1:09 p.m. UTC | #2
On Tue, Aug 16, 2022 at 04:45:07PM +0200, Tobias Burnus wrote:
> Fixed subject line: "absent linear" should be "absent linear step" in the subject line;
> i.e. with "step" added: "Fortran: OpenMP fix declare simd inside modules and absent linear step [PR106566]"
> 
> I have also decided to move the 'step = 1' to openmp.cc, which also set it before with
> the old pre-OpenMP 5.2 syntax.
> 
> I also added a pre-OpenMP-5.2-syntax example.
> 
>  * * *
> 
> For GCC 12 (and GCC 11), only the '%s' fix and the third, now added example apply;
> for the 5.1 syntax, 'step' was already set.
> 
> OK? And thoughts regarding the backports (none? Only 12? Or 11+12?)?
> 
> Tobias
> -----------------
> Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

> Fortran: OpenMP fix declare simd inside modules and absent linear step [PR106566]
> 
> gcc/fortran/ChangeLog:
> 
> 	PR fortran/106566
> 	* openmp.cc (gfc_match_omp_clauses): Fix setting linear-step value
> 	to 1 when not specified.
> 	(gfc_match_omp_declare_simd): Accept module procedures.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR fortran/106566
> 	* gfortran.dg/gomp/declare-simd-4.f90: New test.
> 	* gfortran.dg/gomp/declare-simd-5.f90: New test.
> 	* gfortran.dg/gomp/declare-simd-6.f90: New test.
> 
>  gcc/fortran/openmp.cc                             | 10 +++--
>  gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90 | 42 +++++++++++++++++++
>  gcc/testsuite/gfortran.dg/gomp/declare-simd-5.f90 | 49 +++++++++++++++++++++++
>  gcc/testsuite/gfortran.dg/gomp/declare-simd-6.f90 | 42 +++++++++++++++++++
>  4 files changed, 140 insertions(+), 3 deletions(-)
> 
> diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
> index a7eb6c3e8f4..594907714ff 100644
> --- a/gcc/fortran/openmp.cc
> +++ b/gcc/fortran/openmp.cc
> @@ -2480,7 +2480,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
>  		      goto error;
>  		    }
>  		}
> -	      else
> +	      if (step == NULL)
>  		{
>  		  step = gfc_get_constant_expr (BT_INTEGER,
>  						gfc_default_integer_kind,

Ah, didn't know that gfc_match ("%e ) ", &step) will free and clear
step if it successfully matched it first and then doesn't match ) after it.
So ok.

> @@ -4213,9 +4213,13 @@ gfc_match_omp_declare_simd (void)
>    gfc_omp_declare_simd *ods;
>    bool needs_space = false;
>  
> -  switch (gfc_match (" ( %s ) ", &proc_name))
> +  switch (gfc_match (" ( "))
>      {
> -    case MATCH_YES: break;
> +    case MATCH_YES:
> +      if (gfc_match_symbol (&proc_name, /* host assoc = */ true) != MATCH_YES
> +	  || gfc_match (" ) ") != MATCH_YES)
> +	return MATCH_ERROR;
> +      break;
>      case MATCH_NO: proc_name = NULL; needs_space = true; break;
>      case MATCH_ERROR: return MATCH_ERROR;
>      }

LGTM.

> diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90
> new file mode 100644
> index 00000000000..44132525963
> --- /dev/null
> +++ b/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90
> @@ -0,0 +1,42 @@
> +! { dg-do compile }
> +! { dg-additional-options "-fdump-tree-gimple" }
> +!
> +! PR fortran/106566
> +!
> +! { dg-final { scan-tree-dump-times "__attribute__\\(\\(omp declare simd \\(linear\\(0:ref,step\\(4\\)\\) simdlen\\(8\\)\\)\\)\\)" 2 "gimple" } }
> +! { dg-final { scan-tree-dump-times "__attribute__\\(\\(omp declare simd \\(linear\\(0:ref,step\\(8\\)\\) simdlen\\(8\\)\\)\\)\\)" 2 "gimple" } }
> +
> +subroutine add_one2(p)
> +  implicit none
> +  !$omp declare simd(add_one2) linear(p: ref) simdlen(8)
> +  integer :: p

Wonder if it wouldn't be better to use integer(kind=4) explicitly
when you try to match the size of that multiplied by 1 or 2 in
dg-final, as say with -fdefault-integer-8 this will fail miserably
otherwise.  Ditto in other spots in this as well as other tests.

Ok with/without that change.

As for backports, I'd wait some time with just trunk and then
backport wherever you are willing to test it.

	Jakub
  
Tobias Burnus Aug. 17, 2022, 1:34 p.m. UTC | #3
On 17.08.22 15:09, Jakub Jelinek wrote:
> On Tue, Aug 16, 2022 at 04:45:07PM +0200, Tobias Burnus wrote:
>> Fortran: OpenMP fix declare simd inside modules and absent linear step [PR106566]
>> ...
> LGTM.
>
>> +! { dg-final { scan-tree-dump-times "__attribute__\\(\\(omp declare simd \\(linear\\(0:ref,step\\(4\\)\\) simdlen\\(8\\)\\)\\)\\)" 2 "gimple" } }
...
>> +  integer :: p
> Wonder if it wouldn't be better to use integer(kind=4) explicitly
> when you try to match the size of that multiplied by 1 or 2 in
> dg-final, as say with -fdefault-integer-8 this will fail miserably
> otherwise.  Ditto in other spots in this as well as other tests.

I assume(d) that no one would compile with -fdefault-integer-8.

But possibly it should be changed to permit testing with that flag,
given that API routines should be able to handle different default-kind
integers. On the other hand, API routines implies 'omp_lib', which is
only available in libgomp/testsuite/.

> Ok with/without that change.

Thanks for the review.

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Patch

Fortran: OpenMP fix declare simd inside modules and absent linear [PR106566]

gcc/fortran/ChangeLog:

	PR fortran/106566
	* openmp.cc (gfc_match_omp_declare_simd): Accept module procedures.
	* trans-openmp.cc (gfc_trans_omp_clauses): Fix declare simd without
	linear-step value.

gcc/testsuite/ChangeLog:

	PR fortran/106566
	* gfortran.dg/gomp/declare-simd-4.f90: New test.
	* gfortran.dg/gomp/declare-simd-5.f90: New test.

 gcc/fortran/openmp.cc                             |  8 +++-
 gcc/fortran/trans-openmp.cc                       |  2 +
 gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90 | 42 +++++++++++++++++++
 gcc/testsuite/gfortran.dg/gomp/declare-simd-5.f90 | 49 +++++++++++++++++++++++
 4 files changed, 99 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index a7eb6c3e8f4..e430f4c49dd 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -4213,9 +4213,13 @@  gfc_match_omp_declare_simd (void)
   gfc_omp_declare_simd *ods;
   bool needs_space = false;
 
-  switch (gfc_match (" ( %s ) ", &proc_name))
+  switch (gfc_match (" ( "))
     {
-    case MATCH_YES: break;
+    case MATCH_YES:
+      if (gfc_match_symbol (&proc_name, /* host assoc = */ true) != MATCH_YES
+	  || gfc_match (" ) ") != MATCH_YES)
+	return MATCH_ERROR;
+      break;
     case MATCH_NO: proc_name = NULL; needs_space = true; break;
     case MATCH_ERROR: return MATCH_ERROR;
     }
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index de27ed52c02..22e6dd254c7 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -2798,6 +2798,8 @@  gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
 			  }
 			else
 			  {
+			    if (last_step == NULL_TREE)
+			      last_step = size_one_node;
 			    if (kind == OMP_CLAUSE_LINEAR_REF)
 			      {
 				tree type;
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90
new file mode 100644
index 00000000000..44132525963
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-simd-4.f90
@@ -0,0 +1,42 @@ 
+! { dg-do compile }
+! { dg-additional-options "-fdump-tree-gimple" }
+!
+! PR fortran/106566
+!
+! { dg-final { scan-tree-dump-times "__attribute__\\(\\(omp declare simd \\(linear\\(0:ref,step\\(4\\)\\) simdlen\\(8\\)\\)\\)\\)" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "__attribute__\\(\\(omp declare simd \\(linear\\(0:ref,step\\(8\\)\\) simdlen\\(8\\)\\)\\)\\)" 2 "gimple" } }
+
+subroutine add_one2(p)
+  implicit none
+  !$omp declare simd(add_one2) linear(p: ref) simdlen(8)
+  integer :: p
+
+  p = p + 1
+end subroutine
+
+subroutine linear_add_one2(p)
+  implicit none
+  !$omp declare simd(linear_add_one2) linear(p: ref, step(2)) simdlen(8)
+  integer :: p
+
+  p = p + 1
+end subroutine
+
+module m
+   integer, parameter :: NN = 1023
+   integer :: a(NN)
+contains
+  subroutine module_add_one2(q)
+    implicit none
+    !$omp declare simd(module_add_one2) linear(q: ref) simdlen(8)
+    integer :: q
+    q = q + 1
+  end subroutine
+
+  subroutine linear_add_one2(q)
+    implicit none
+    !$omp declare simd(linear_add_one2) linear(q: ref, step(2)) simdlen(8)
+    integer :: q
+    q = q + 1
+  end subroutine
+end module
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-simd-5.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-simd-5.f90
new file mode 100644
index 00000000000..f5880f50090
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-simd-5.f90
@@ -0,0 +1,49 @@ 
+! { dg-do compile }
+!
+! PR fortran/106566
+!
+
+subroutine add_one2(p)
+  implicit none
+  procedure(add_one2) :: ext1
+  !$omp declare simd(ext1) linear(p: ref) simdlen(8)  ! { dg-error "OMP DECLARE SIMD should refer to containing procedure 'add_one2'" }
+  integer :: p
+
+  p = p + 1
+end subroutine
+
+subroutine linear_add_one2(p)
+  implicit none
+  procedure(linear_add_one2) :: ext2
+  !$omp declare simd(ext2) linear(p: ref, step(2)) simdlen(8)  ! { dg-error "OMP DECLARE SIMD should refer to containing procedure 'linear_add_one2'" }
+  integer :: p
+
+  p = p + 1
+end subroutine
+
+module m
+   integer, parameter :: NN = 1023
+   integer :: a(NN)
+contains
+  subroutine some_proc(r)
+    integer :: r
+  end subroutine
+  subroutine module_add_one2(q)
+    implicit none
+    !$omp declare simd(some_proc) linear(q: ref) simdlen(8)  ! { dg-error "OMP DECLARE SIMD should refer to containing procedure 'module_add_one2'" }
+    integer :: q
+    q = q + 1
+  end subroutine
+
+  subroutine module_linear_add_one2(q)
+    implicit none
+    interface
+      subroutine other_proc(r)
+        integer :: r
+      end subroutine
+    end interface
+    !$omp declare simd(other_proc) linear(q: ref, step(2)) simdlen(8)  ! { dg-error "OMP DECLARE SIMD should refer to containing procedure 'module_linear_add_one2'" }
+    integer :: q
+    q = q + 1
+  end subroutine
+end module