public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch, fortran] Some corrections for DO loop index warnings
@ 2019-08-12 16:27 Thomas Koenig
  2019-08-12 20:05 ` Steve Kargl
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2019-08-12 16:27 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1006 bytes --]

Hello world,

the attached patch fixes three problems with DO loop index warnings:

- DO loops in contained procedures were not checked

- Zero-trip loops gave a false positive

- DO loops in blocks gave the same warning twice

plus it fixes the resulting fallout from the test suite.

Regression-tested.  OK for trunk?  I also think that this is something
that could be safely backported to gcc-9.


2019-08-12  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/91424
	* frontend-passes.c (do_subscript): Do not warn for an
	expression a second time.  Do not warn about a zero-trip loop.
	(doloop_warn): Also look at contained namespaces.

2019-08-12  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/91424
	* gfortran.dg/do_subscript_3.f90: New test.
	* gfortran.dg/do_subscript_4.f90: New test.
	* gfortran.dg/pr70754.f90: Use indices that to not overflow.

2019-08-12  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/91422
	* testsuite/libgomp.oacc-fortran/routine-7.f90: Correct array
	dimension.


[-- Attachment #2: do_subscript_3.f90 --]
[-- Type: text/x-fortran, Size: 526 bytes --]

! { dg-do compile }
! PR fortran/91424
! Check that only one warning is issued inside blocks, and that
! warnings are also issued for contained subroutines.

program main
  real :: a(5)
  block
    integer :: j
    do j=0, 5  ! { dg-warning "out of bounds" }
       a(j) = 2. ! { dg-warning "out of bounds" }
    end do
  end block
  call x
contains
  subroutine x
    integer :: i
    do i=1,6 ! { dg-warning "out of bounds" }
       a(i) = 2.  ! { dg-warning "out of bounds" }
    end do
  end subroutine x
end program main

[-- Attachment #3: p3.diff --]
[-- Type: text/x-patch, Size: 2835 bytes --]

Index: gcc/fortran/frontend-passes.c
===================================================================
--- gcc/fortran/frontend-passes.c	(revision 273855)
+++ gcc/fortran/frontend-passes.c	(working copy)
@@ -2556,6 +2556,12 @@ do_subscript (gfc_expr **e)
   if (in_assoc_list)
     return 0;
 
+  /* We already warned about this.  */
+  if (v->do_not_warn)
+    return 0;
+
+  v->do_not_warn = 1;
+
   for (ref = v->ref; ref; ref = ref->next)
     {
       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT)
@@ -2608,7 +2614,6 @@ do_subscript (gfc_expr **e)
 	      else
 		have_do_start = false;
 
-
 	      if (dl->ext.iterator->end->expr_type == EXPR_CONSTANT)
 		{
 		  have_do_end = true;
@@ -2620,6 +2625,17 @@ do_subscript (gfc_expr **e)
 	      if (!have_do_start && !have_do_end)
 		return 0;
 
+	      /* No warning inside a zero-trip loop.  */
+	      if (have_do_start && have_do_end)
+		{
+		  int sgn, cmp;
+
+		  sgn = mpz_cmp_ui (do_step, 0);
+		  cmp = mpz_cmp (do_end, do_start);
+		  if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
+		    break;
+		}
+
 	      /* May have to correct the end value if the step does not equal
 		 one.  */
 	      if (have_do_start && have_do_end && mpz_cmp_ui (do_step, 1) != 0)
@@ -2761,6 +2777,12 @@ static void
 doloop_warn (gfc_namespace *ns)
 {
   gfc_code_walker (&ns->code, doloop_code, do_function, NULL);
+
+  for (ns = ns->contained; ns; ns = ns->sibling)
+    {
+      if (ns->code == NULL || ns->code->op != EXEC_BLOCK)
+	doloop_warn (ns);
+    }
 }
 
 /* This selction deals with inlining calls to MATMUL.  */
Index: gcc/testsuite/gfortran.dg/pr70754.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr70754.f90	(revision 273855)
+++ gcc/testsuite/gfortran.dg/pr70754.f90	(working copy)
@@ -18,12 +18,13 @@ contains
     integer (ii4), dimension(40,40) :: c
     integer  i, j
 
-    do i=1,20
-      b(i,j) = 123 * a(i,j) + 34 * a(i,j+1) &
-             + 34 * a(i,j-1) + a(i+1,j+1) &
-             + a(i+1,j-1) + a(i-1,j+1) &
-             + a(i-1,j-1)
-      c(i,j) = 123
+    j = 10
+    do i=11,30
+       b(i,j) = 123 * a(i,j) + 34 * a(i,j+1) &
+            + 34 * a(i,j-1) + a(i+1,j+1) &
+            + a(i+1,j-1) + a(i-1,j+1) &
+            + a(i-1,j-1)
+       c(i,j) = 123
     end do
 
     where ((xyz(:,:,2) /= 0) .and. (c /= 0))
Index: libgomp/testsuite/libgomp.oacc-fortran/routine-7.f90
===================================================================
--- libgomp/testsuite/libgomp.oacc-fortran/routine-7.f90	(revision 273855)
+++ libgomp/testsuite/libgomp.oacc-fortran/routine-7.f90	(working copy)
@@ -109,7 +109,7 @@ end subroutine gang
 
 subroutine seq (a)
   !$acc routine seq
-  integer, intent (inout) :: a(M)
+  integer, intent (inout) :: a(N)
   integer :: i
 
   do i = 1, N

[-- Attachment #4: do_subscript_4.f90 --]
[-- Type: text/x-fortran, Size: 212 bytes --]

! { dg-do compile }
! PR 91424 - this used to warn although the DO loop is zero trip.
program main
  implicit none
  integer :: i
  real :: a(2)
  do i=1,3,-1
     a(i) = 2.
  end do
  print *,a
end program main

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [patch, fortran] Some corrections for DO loop index warnings
  2019-08-12 16:27 [patch, fortran] Some corrections for DO loop index warnings Thomas Koenig
@ 2019-08-12 20:05 ` Steve Kargl
  2019-08-13 10:41   ` Thomas Koenig
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Kargl @ 2019-08-12 20:05 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: fortran, gcc-patches

On Mon, Aug 12, 2019 at 05:18:18PM +0200, Thomas Koenig wrote:
> Hello world,
> 
> the attached patch fixes three problems with DO loop index warnings:
> 
> - DO loops in contained procedures were not checked
> 
> - Zero-trip loops gave a false positive
> 
> - DO loops in blocks gave the same warning twice
> 
> plus it fixes the resulting fallout from the test suite.
> 
> Regression-tested.  OK for trunk?  I also think that this is something
> that could be safely backported to gcc-9.
> 

Ok.  If it regression cleanly on gcc9, go ahead an commit there as well.

-- 
Steve

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [patch, fortran] Some corrections for DO loop index warnings
  2019-08-12 20:05 ` Steve Kargl
@ 2019-08-13 10:41   ` Thomas Koenig
  0 siblings, 0 replies; 3+ messages in thread
From: Thomas Koenig @ 2019-08-13 10:41 UTC (permalink / raw)
  To: sgk; +Cc: fortran, gcc-patches

Hi Steve,

> Ok.  If it regression cleanly on gcc9, go ahead an commit there as well.

Committed to both (after doing a regression test on gcc 9 and also
waiting for gcc-testresults containing the revision).

Thanks for the review!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-13 10:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-12 16:27 [patch, fortran] Some corrections for DO loop index warnings Thomas Koenig
2019-08-12 20:05 ` Steve Kargl
2019-08-13 10:41   ` Thomas Koenig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).