Bug 57798 - [4.8/4.9 regression] Incorrect handling of sum over first dimension of a product of automatic arrays
Summary: [4.8/4.9 regression] Incorrect handling of sum over first dimension of a prod...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.1
: P4 normal
Target Milestone: 4.8.2
Assignee: Mikael Morin
URL: http://gcc.gnu.org/ml/fortran/2013-08...
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-03 12:55 UTC by Stephan Kramer
Modified: 2013-08-24 12:50 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-07-03 00:00:00


Attachments
Patch moving the preliminary code before the outermost loop. (1.19 KB, patch)
2013-08-20 10:12 UTC, Mikael Morin
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Kramer 2013-07-03 12:55:44 UTC
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
Comment 1 Dominique d'Humieres 2013-07-03 13:17:18 UTC
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)
Comment 2 Thomas Koenig 2013-08-19 21:07:55 UTC
Mikael,

this seems to be your area of expertise.  Do you have any idea?
Comment 3 Mikael Morin 2013-08-20 09:41:53 UTC
(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.
Comment 4 Mikael Morin 2013-08-20 10:12:32 UTC
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.
Comment 5 Mikael Morin 2013-08-21 13:56:14 UTC
Patch submitted:
http://gcc.gnu.org/ml/fortran/2013-08/msg00059.html
Comment 6 Mikael Morin 2013-08-23 17:51:55 UTC
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
Comment 7 Mikael Morin 2013-08-24 12:49:00 UTC
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
Comment 8 Mikael Morin 2013-08-24 12:50:14 UTC
Fixed for 4.8.2 and 4.9.0