The test code below results in a segfault evaluating the sum() in the print statement. This is compiling with gfortran 4.8.1 (4.8.1-4ubuntu1 on Ubuntu Saucy) without any flags. If bounds checking is switched on (-fbounds-check) I get a bounds violation error with a random number reported for one of the bounds: Fortran runtime error: Dimension 1 of array 'func' has extent 2 instead of 4196256 Is this possibly related to the optimisation requested in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36841 ? program test implicit none call sub(2, 11) contains function func(m, n) integer, intent(in):: m,n real, dimension(m, n):: func func = 0.0 end function func subroutine sub(m, n) integer, intent(in):: m, n real, dimension(m,n):: y y = 0.0 print *, sum(y*func(m,n), dim=1) end subroutine sub end program test
Confirmed. It appeared between revision 190090 (2012-08-02: OK) and 190271 (2012-08-09: wrong bounds). Valgrind reports ==70092== Invalid write of size 4 ==70092== at 0x100000DD1: func.1887 (pr57798.f90:12) ==70092== by 0x100000C32: sub.1867 (pr57798.f90:21) ==70092== by 0x100000D2F: MAIN__ (pr57798.f90:4) ==70092== by 0x100000E1A: main (pr57798.f90:4) ==70092== Address 0x100440480 is 0 bytes inside a block of size 1 alloc'd ==70092== at 0x100012679: malloc (vg_replace_malloc.c:266) ==70092== by 0x100000C0B: sub.1867 (pr57798.f90:21) ==70092== by 0x100000D2F: MAIN__ (pr57798.f90:4) ==70092== by 0x100000E1A: main (pr57798.f90:4)
Mikael, this seems to be your area of expertise. Do you have any idea?
(In reply to Thomas Koenig from comment #2) > Mikael, > > this seems to be your area of expertise. Do you have any idea? Hmm, in the dump, D.1917 is initialized to late, just before the inner loop inside the outer loop, but the first use comes before the outer loop. I'm having a look.
Created attachment 30677 [details] Patch moving the preliminary code before the outermost loop. I suppose the regression appeared at r190097. I'm the author, but I think the bug was just latent before. However, as I'm the one who introduced nested loops with sum, I'm the culprit in any case. :-( The patch above (against 4.9-20130715) fixes the problem here.
Patch submitted: http://gcc.gnu.org/ml/fortran/2013-08/msg00059.html
Author: mikael Date: Fri Aug 23 17:49:39 2013 New Revision: 201947 URL: http://gcc.gnu.org/viewcvs?rev=201947&root=gcc&view=rev Log: fortran/ PR fortran/57798 * trans-array.c (gfc_conv_ss_startstride, set_loop_bounds, gfc_set_delta): Generate preliminary code before the outermost loop. testsuite/ PR fortran/57798 * gfortran.dg/inline_sum_5.f90: New. Added: trunk/gcc/testsuite/gfortran.dg/inline_sum_5.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/trans-array.c trunk/gcc/testsuite/ChangeLog
Author: mikael Date: Sat Aug 24 12:46:17 2013 New Revision: 201966 URL: http://gcc.gnu.org/viewcvs?rev=201966&root=gcc&view=rev Log: fortran/ PR fortran/57798 * trans-array.c (gfc_conv_ss_startstride, set_loop_bounds, gfc_set_delta): Generate preliminary code before the outermost loop. testsuite/ PR fortran/57798 * gfortran.dg/inline_sum_5.f90: New. Added: branches/gcc-4_8-branch/gcc/testsuite/gfortran.dg/inline_sum_5.f90 Modified: branches/gcc-4_8-branch/gcc/fortran/ChangeLog branches/gcc-4_8-branch/gcc/fortran/trans-array.c branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
Fixed for 4.8.2 and 4.9.0