This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
[Patch,fortran] PR28914 Code inside loop hangs
- From: Jerry DeLisle <jvdelisle at verizon dot net>
- To: Fortran List <fortran at gcc dot gnu dot org>
- Cc: gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Sat, 09 Sep 2006 20:43:33 -0700
- Subject: [Patch,fortran] PR28914 Code inside loop hangs
The following patch was contributed by Paul Thomas. I am assisting getting this
out for 4.2 release.
The attached patch fixes this problem by creating a temporary copy of the loop
variable used by the constructor so that it does not corrupt that variable if
used in a surrounding DO LOOP. The loop variable is restored after the
constructor is done.
Regression tested on i686-linux-pc-gnu.
OK to commit to 4.2?
Regards,
Jerry
2006-09-09 Paul Thomas <pault@gcc.gnu.org>
PR fortran/28914
* trans-array.c (gfc_trans_array_constructor_value): Create a temporary
loop variable to hold the current loop variable in case it is modified
by the array constructor.
Index: trans-array.c
===================================================================
*** trans-array.c (revision 116710)
--- trans-array.c (working copy)
*************** gfc_trans_array_constructor_value (stmtb
*** 1252,1257 ****
--- 1252,1258 ----
tree exit_label;
tree loopbody;
tree tmp2;
+ tree tmp_loopvar;
loopbody = gfc_finish_block (&body);
*************** gfc_trans_array_constructor_value (stmtb
*** 1260,1265 ****
--- 1261,1272 ----
gfc_add_block_to_block (pblock, &se.pre);
loopvar = se.expr;
+ /* In case this is a variable in the function scope,
+ make a temporary, store the current value in that
+ and return it, once the loop is done. */
+ tmp_loopvar = gfc_create_var (TREE_TYPE (loopvar), "loopvar");
+ gfc_add_modify_expr (pblock, tmp_loopvar, loopvar);
+
/* Initialize the loop. */
gfc_init_se (&se, NULL);
gfc_conv_expr_val (&se, c->iterator->start);
*************** gfc_trans_array_constructor_value (stmtb
*** 1327,1332 ****
--- 1334,1342 ----
/* Add the exit label. */
tmp = build1_v (LABEL_EXPR, exit_label);
gfc_add_expr_to_block (pblock, tmp);
+
+ /* Restore the original value of the loop counter. */
+ gfc_add_modify_expr (pblock, loopvar, tmp_loopvar);
}
}
mpz_clear (size);
! { dg-do run }
! Tests the fix for pr28914, in which array constructors using the loop
! variable within a do loop for the implied do loop of the constructor
! would result in a corrupted do loop counter.
!
! Based on the testscase by Ed Korkven <kornkven@arsc.edu>
!
program pr28914
implicit none
integer n, i
parameter (n = 66000) ! Problem manifests for n > 65535
double precision a(n), summation
summation = 0.0
do i = 1, 1
a = (/ (i, i = 1, n) /) ! This is legal and was broken
a = sqrt(a)
summation = SUM(a)
enddo
summation = abs(summation - 11303932.9138271_8)
if (summation.gt.0.00001) call abort()
end program pr28914