Bug 48746 - [4.6/4.7 Regression] Matmul with allocate on assignment
[4.6/4.7 Regression] Matmul with allocate on assignment
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.7.0
: P3 normal
: 4.6.1
Assigned To: Not yet assigned to anyone
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-04-23 22:07 UTC by Thomas Koenig
Modified: 2011-04-30 12:06 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-04-27 14:58:54


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2011-04-23 22:07:46 UTC
The following program crashes with a double free on current trunk:

program main
  implicit none

  integer, parameter :: m=10, n=12, count=4
  double precision :: optmatmul(m, n)
  double precision :: a(m, count), b(count, n), c(m, n)
  double precision, dimension(:,:), allocatable :: tmp
  call random_number(a)
  call random_number(b)
  tmp = matmul(a,b)
end program main

Paul, if you happen to have any idea...
Comment 1 jpr 2011-04-27 05:56:57 UTC
Problem here is that the return array "dtype" field is uninitialized when
lhs is (unallocated) allocatable. Due to this there are also similiar
problems in other intrinsics such as reshape(), etc...
Comment 2 Tobias Burnus 2011-04-27 06:58:21 UTC
I think the PR is related to PR 48462, though the issue mentioned here is not covered there.
Comment 3 Thomas Koenig 2011-04-27 14:58:54 UTC
(In reply to comment #1)
> Problem here is that the return array "dtype" field is uninitialized when
> lhs is (unallocated) allocatable. Due to this there are also similiar
> problems in other intrinsics such as reshape(), etc...

Interesting observation.  This works:

program main
  implicit none
  integer, parameter :: m=10, n=12, count=4
  double precision :: a(m, count), b(count, n), c(m, n)
  double precision, dimension(:,:), allocatable :: tmp

  call random_number(a)
  call random_number(b)
  allocate (tmp(3,3))
  deallocate(tmp)
  tmp = matmul(a,b)
end program main
Comment 4 Thomas Koenig 2011-04-27 15:07:21 UTC
(In reply to comment #3)
> (In reply to comment #1)
> > Problem here is that the return array "dtype" field is uninitialized when
> > lhs is (unallocated) allocatable. Due to this there are also similiar
> > problems in other intrinsics such as reshape(), etc...
> 
> Interesting observation.  This works:

... well, for a given value of "works".  tmp keeps its size of 3*3.
Comment 5 Thomas Koenig 2011-04-28 10:52:51 UTC
The problem is that matmul calls size0, which depends on correct
rank information in the dtype field.

Should be easy enough to fix by adding a default initializer
for the dtype field, if only anybody knew where to look...

Paul?
Comment 6 Paul Thomas 2011-04-29 20:26:59 UTC
Author: pault
Date: Fri Apr 29 20:26:56 2011
New Revision: 173185

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173185
Log:
2011-04-29  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48462
	* trans-expr.c (arrayfunc_assign_needs_temporary): Deal with
	automatic reallocation when the lhs is a target.

	PR fortran/48746
	* trans-expr.c (fcncall_realloc_result): Make sure that the
	result dtype field is set before the function call.

2011-04-29  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48462
	* gfortran.dg/realloc_on_assign_7.f03: Modify to test for lhs
	being a target.

	PR fortran/48746
	* gfortran.dg/realloc_on_assign_7.f03: Add subroutine pr48746.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_7.f03
Comment 7 Thomas Koenig 2011-04-30 11:12:29 UTC
Hi Paul,

the least I can do is to supply you with a more complete test case :-)

program main
  implicit none
  integer, parameter :: m=4, n=3, count=2
  double precision :: a(m, count), b(count, n), c(m,n)
  double precision, dimension(:,:), allocatable :: tmp

  data a /1.d0, -2.d0, 4.d0, -8.d0, 16.d0, -32.d0, 64.d0, -128.d0/
  data b /256.d0, -512.d0, 1024.d0, -2048.d0, 4096.d0, -8182.d0 /
  data c/  -7936.d0,  15872.d0, -31744.d0,  63488.d0, -31744.d0,  &
           63488.d0,-126976.d0, 253952.d0,-126816.d0, 253632.d0, &
           -507264.d0,1014528.d0/

  tmp = matmul(a,b)
  if (size(tmp,1) /= 4 .or. size(tmp,2) /= 3) call abort
  if (any (tmp /= c)) call abort
end program main
Comment 8 Paul Thomas 2011-04-30 11:46:34 UTC
Author: pault
Date: Sat Apr 30 11:46:31 2011
New Revision: 173213

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173213
Log:
2011-04-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48746
	* trans-expr.c (fcncall_realloc_result): Set the bounds and the
	offset so that the lbounds are one.
	(gfc_trans_arrayfunc_assign): Add rank to arguments of above.

2011-04-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48746
	* gfortran.dg/realloc_on_assign_7.f03: Test bounds.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_7.f03
Comment 9 Paul Thomas 2011-04-30 12:00:53 UTC
Author: pault
Date: Sat Apr 30 12:00:50 2011
New Revision: 173214

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173214
Log:
2011-04-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48462
	PR fortran/48746
	* trans-expr.c ( arrayfunc_assign_needs_temporary): Need a temp
	if automatic reallocation on assignement is active, the lhs is a
	target and the rhs an intrinsic function.
	(realloc_lhs_bounds_for_intrinsic_call): Rename as next.
	(fcncall_realloc_result): Renamed version of above function.
	Free the original descriptor data after the function call.Set the bounds and the
	offset so that the lbounds are one.
	(gfc_trans_arrayfunc_assign): Call renamed function.

2011-04-30  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/48462
	PR fortran/48746
	* gfortran.dg/realloc_on_assign_7.f03: New test.


Added:
    branches/gcc-4_6-branch/gcc/testsuite/gfortran.dg/realloc_on_assign_7.f03
Modified:
    branches/gcc-4_6-branch/gcc/fortran/ChangeLog
    branches/gcc-4_6-branch/gcc/fortran/trans-expr.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 10 Paul Thomas 2011-04-30 12:06:07 UTC
Fixed on trunk at 4.6

Thanks for the report.

Paul