diff mbox series

[fortran] Fix PR 100227, write with implied DO loop

Message ID 849f630f-e6fe-c3c3-e46a-3a64a86db4e3@netcologne.de
State New
Headers show
Series [fortran] Fix PR 100227, write with implied DO loop | expand

Commit Message

Thomas Koenig July 4, 2021, 4:09 p.m. UTC
Hello world,

after a bit of an absence, I am now back, at least for some regression
fixing (and for reviewing patches, if that is called for).

So, here's a regression fix to start with.

OK for trunk and affected branches (down to 9)?

Best regards

	Thomas

Do not replace variable op variable in I/O implied DO loop replacement.

This PR came about because index expressions of the form k+k in
implied DO loops in I/O statements were considered for replacement
by array slices.

Fixed by only doing the transformation if the expression is of the
type expr OP contastant.

gcc/fortran/ChangeLog:

         PR fortran/100227
         * frontend-passes.c (traverse_io_block): Adjust test for
	when a variable is eligible for the transformation to
	array slice.

gcc/testsuite/ChangeLog:

         PR fortran/100227
         * gfortran.dg/implied_do_io_7.f90: New test.

Comments

Jerry D July 5, 2021, 6:33 p.m. UTC | #1
Looks OK Thomas,

Good for backport as well.

Regards,

Jerry

On 7/4/21 9:09 AM, Thomas Koenig via Fortran wrote:
> Hello world,
>
> after a bit of an absence, I am now back, at least for some regression
> fixing (and for reviewing patches, if that is called for).
>
> So, here's a regression fix to start with.
>
> OK for trunk and affected branches (down to 9)?
>
> Best regards
>
>     Thomas
>
> Do not replace variable op variable in I/O implied DO loop replacement.
>
> This PR came about because index expressions of the form k+k in
> implied DO loops in I/O statements were considered for replacement
> by array slices.
>
> Fixed by only doing the transformation if the expression is of the
> type expr OP contastant.
>
> gcc/fortran/ChangeLog:
>
>         PR fortran/100227
>         * frontend-passes.c (traverse_io_block): Adjust test for
>     when a variable is eligible for the transformation to
>     array slice.
>
> gcc/testsuite/ChangeLog:
>
>         PR fortran/100227
>         * gfortran.dg/implied_do_io_7.f90: New test.
Thomas Koenig July 7, 2021, 5:25 a.m. UTC | #2
Hi Jerry,

> Looks OK Thomas,
> 
> Good for backport as well.

Thanks.  Committed to trunk so far, will add a git worktree for
gcc11 next :-)

Best regards

	Thomas
diff mbox series

Patch

diff --git a/gcc/fortran/frontend-passes.c b/gcc/fortran/frontend-passes.c
index 72a4e0410b1..996dcc2e547 100644
--- a/gcc/fortran/frontend-passes.c
+++ b/gcc/fortran/frontend-passes.c
@@ -1299,8 +1299,8 @@  traverse_io_block (gfc_code *code, bool *has_reached, gfc_code *prev)
 		std::swap (start->value.op.op1, start->value.op.op2);
 	      gcc_fallthrough ();
 	    case INTRINSIC_MINUS:
-	      if ((start->value.op.op1->expr_type!= EXPR_VARIABLE
-		   && start->value.op.op2->expr_type != EXPR_CONSTANT)
+	      if (start->value.op.op1->expr_type!= EXPR_VARIABLE
+		  || start->value.op.op2->expr_type != EXPR_CONSTANT
 		  || start->value.op.op1->ref)
 		return false;
 	      if (!stack_top || !stack_top->iter
diff --git a/gcc/testsuite/gfortran.dg/implied_do_io_7.f90 b/gcc/testsuite/gfortran.dg/implied_do_io_7.f90
new file mode 100644
index 00000000000..63927aafea9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/implied_do_io_7.f90
@@ -0,0 +1,16 @@ 
+! { dg-do run }
+! PR 100227 - this was falsely optimized, leading to nonsense  results.
+! Original test case by "Mathieu".
+
+program p
+  implicit none
+  integer, parameter :: nbmode = 3
+  integer :: k
+  real    :: mass(nbmode*2)
+  character (len=80) :: line
+  do k = 1, nbmode*2
+     mass(k) = k
+  end do
+  write (unit=line,fmt='(*(F6.2))') (mass(k+k), k=1,nbmode)
+  if (line /= '  2.00  4.00  6.00') stop 1
+end program