[Bug fortran/35959] Recursive function with allocatable array
--- Comment #10 from michael dot baudin at gmail dot com 2008-04-24 19:55 --- Subject: Re: Recursive function with allocatable array Thank you for fixing the bug. Michaël On Sun, Apr 20, 2008 at 12:32 AM, pault at gcc dot gnu dot org [EMAIL PROTECTED] wrote: --- Comment #9 from pault at gcc dot gnu dot org 2008-04-19 22:32 --- Fixed on trunk and 4.3. Thanks for the report. Paul -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #7 from pault at gcc dot gnu dot org 2008-04-19 21:56 --- Subject: Bug 35959 Author: pault Date: Sat Apr 19 21:55:24 2008 New Revision: 134472 URL: http://gcc.gnu.org/viewcvs?root=gccview=revrev=134472 Log: 2008-04-19 Paul Thomas [EMAIL PROTECTED] PR fortran/35944 PR fortran/35946 PR fortran/35947 * trans_array.c (gfc_trans_array_constructor): Temporarily realign loop, if loop-from is not zero, before creating the temporary array and provide an offset. PR fortran/35959 * trans-decl.c (gfc_init_default_dt): Add gfc_ prefix to name and allow for NULL body. Change all references from init_default_dt to gfc_init_default_dt. * trans.h : Add prototype for gfc_init_default_dt. * trans-array.c (gfc_trans_deferred_vars): After nullification call gfc_init_default_dt for derived types with allocatable components. 2008-04-19 Paul Thomas [EMAIL PROTECTED] PR fortran/35944 PR fortran/35946 PR fortran/35947 * gfortran.dg/array_constructor_23.f: New test. PR fortran/35959 * gfortran.dg/alloc_comp_default_init_2.f90: New test. * gfortran.dg/alloc_comp_basics_1.f90: Change occurrences of builtin_free to 27. * gfortran.dg/alloc_comp_constructor_1.f90: Change occurrences of builtin_free to 21. Added: trunk/gcc/testsuite/gfortran.dg/alloc_comp_default_init_2.f90 trunk/gcc/testsuite/gfortran.dg/array_constructor_23.f Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/trans-array.c trunk/gcc/fortran/trans-decl.c trunk/gcc/fortran/trans.h trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gfortran.dg/alloc_comp_basics_1.f90 trunk/gcc/testsuite/gfortran.dg/alloc_comp_constructor_1.f90 -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #8 from pault at gcc dot gnu dot org 2008-04-19 22:31 --- Subject: Bug 35959 Author: pault Date: Sat Apr 19 22:30:03 2008 New Revision: 134475 URL: http://gcc.gnu.org/viewcvs?root=gccview=revrev=134475 Log: 2008-04-19 Paul Thomas [EMAIL PROTECTED] PR fortran/35944 PR fortran/35946 PR fortran/35947 * trans_array.c (gfc_trans_array_constructor): Temporarily realign loop, if loop-from is not zero, before creating the temporary array and provide an offset. PR fortran/35959 * trans-decl.c (gfc_init_default_dt): Add gfc_ prefix to name and allow for NULL body. Change all references from init_default_dt to gfc_init_default_dt. * trans.h : Add prototype for gfc_init_default_dt. * trans-array.c (gfc_trans_deferred_vars): After nullification call gfc_init_default_dt for derived types with allocatable components. 2008-04-19 Paul Thomas [EMAIL PROTECTED] PR fortran/35944 PR fortran/35946 PR fortran/35947 * gfortran.dg/array_constructor_23.f: New test. PR fortran/35959 * gfortran.dg/alloc_comp_default_init_2.f90: New test. * gfortran.dg/alloc_comp_basics_1.f90: Change occurrences of builtin_free to 27. * gfortran.dg/alloc_comp_constructor_1.f90: Change occurrences of builtin_free to 21. Added: branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/alloc_comp_default_init_2.f90 branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/array_constructor_23.f Modified: branches/gcc-4_3-branch/gcc/fortran/ChangeLog branches/gcc-4_3-branch/gcc/fortran/trans-array.c branches/gcc-4_3-branch/gcc/fortran/trans-decl.c branches/gcc-4_3-branch/gcc/fortran/trans.h branches/gcc-4_3-branch/gcc/testsuite/ChangeLog branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/alloc_comp_basics_1.f90 branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/alloc_comp_constructor_1.f90 -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #9 from pault at gcc dot gnu dot org 2008-04-19 22:32 --- Fixed on trunk and 4.3. Thanks for the report. Paul -- pault at gcc dot gnu dot org changed: What|Removed |Added Status|ASSIGNED|RESOLVED Resolution||FIXED http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #3 from michael dot baudin at gmail dot com 2008-04-18 08:13 --- Subject: Re: Recursive function with allocatable array Hi Paul, The generic source code that you sent to me is interesting and you analysis is very clear. Thank you for taking the time to fix this. But I wonder how you produced this source (which have many in common with C), because I looked in the gfortran manual, and I obviously did not find any option to generate the generic source code, which is an internal step. Is that available only with the middle-end of gfortran and not available for end-users ? Best regards, Michaël On Thu, Apr 17, 2008 at 10:37 PM, pault at gcc dot gnu dot org [EMAIL PROTECTED] wrote: --- Comment #2 from pault at gcc dot gnu dot org 2008-04-17 20:37 --- This is indeed a bug. A fix will be posted in half an hour or so. Paul -- pault at gcc dot gnu dot org changed: What|Removed |Added AssignedTo|unassigned at gcc dot gnu |pault at gcc dot gnu dot org |dot org | Status|NEW |ASSIGNED Last reconfirmed|2008-04-17 05:59:41 |2008-04-17 20:37:53 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959 --- You are receiving this mail because: --- You reported the bug, or are watching the reporter. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #4 from jvdelisle at gcc dot gnu dot org 2008-04-18 14:04 --- Reply to: But I wonder how you produced this source (which have many in common with C), The intermediate code can be listed using the -fdump-tree-original compiler flag. It will be placed in a file with the word original appended to the name. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #5 from michael dot baudin at gmail dot com 2008-04-18 14:11 --- Subject: Re: Recursive function with allocatable array Thank you very much. I will try immediately. On Fri, Apr 18, 2008 at 4:04 PM, jvdelisle at gcc dot gnu dot org [EMAIL PROTECTED] wrote: --- Comment #4 from jvdelisle at gcc dot gnu dot org 2008-04-18 14:04 --- Reply to: But I wonder how you produced this source (which have many in common with C), The intermediate code can be listed using the -fdump-tree-original compiler flag. It will be placed in a file with the word original appended to the name. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #6 from michael dot baudin at gmail dot com 2008-04-18 15:07 --- Subject: Re: Recursive function with allocatable array I used the -fdump-tree-original option to compare the version produced on this source code with allocatable and pointer. Only the allocatable contains the following statements : static struct t_type subpattern = {}; subpattern.chars.data = 0B; I understand that the statement subpattern.chars.data = 0B; allows to free the array when it goes out of scope, as the standard says about allocatable arrays (which explains why it is not in the pointer version) : if (subpattern.chars.data != 0B) { __builtin_free (subpattern.chars.data); } subpattern.chars.data = 0B; The statement static struct t_type subpattern = {};, which is not in the pointer version, is the cause of the problem so that the solution is to remove it : at second call, the array has been initialiazed to null, and because it is static, it memory is still deallocated on recursive calls. An allocatable array in a recursive subroutine should not be static (in fortran, that means having the SAVE attribute), isn't it ? Am I right ? Best regards, Michaël -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #1 from pault at gcc dot gnu dot org 2008-04-17 05:59 --- Here is a further reduced version of the testcase: program testprog type :: t_type integer, dimension(:), allocatable :: chars end type t_type integer, save :: callnb = 0 type(t_type) :: this allocate ( this % chars ( 4)) print * , match :, recursivefunc ( this ), callnb contains recursive function recursivefunc ( this ) result ( match ) type(t_type), intent(in) :: this type(t_type) :: subpattern logical :: match callnb = callnb + 1 match = (callnb == 10) if ((.NOT. allocated (this % chars)) .OR. match) return allocate ( subpattern % chars ( 4 ) ) match = recursivefunc ( subpattern ) end function recursivefunc end program testprog The code that the function produces is: recursivefunc (struct t_type this) { logical(kind=4) match; static struct t_type subpattern = {}; subpattern.chars.data = 0B; callnb = callnb + 1; match = callnb == 10; if ((integer(kind=4)[0:] *) this-chars.data == 0B || match) goto __return_recursivefunc; { void * D.998; subpattern.chars.dtype = 265; subpattern.chars.dim[0].lbound = 1; subpattern.chars.dim[0].ubound = 4; subpattern.chars.dim[0].stride = 1; if (subpattern.chars.data == 0B) { { void * D.999; D.999 = __builtin_malloc (16); if (D.999 == 0B) { _gfortran_os_error (Out of memory[1]{lb: 1 sz: 1}); } D.998 = D.999; } } else { _gfortran_runtime_error (Attempting to allocate already allocated array[1]{lb: 1 sz: 1}); } subpattern.chars.data = D.998; subpattern.chars.offset = -1; } match = recursivefunc (subpattern); __return_recursivefunc:; if (subpattern.chars.data != 0B) { __builtin_free (subpattern.chars.data); } subpattern.chars.data = 0B; return match; } Being a local variable in a recursive procedure, it is declared to be static. Having allocatable components, it is nullified on entry to the procedure. This explains the observed behaviour. I believe that it is a bug but will have to take a close look at the standard to confirm it - in fact, I have a suspicion that it might not be covered. If the latter is the case, I would be inclined to modify gfortran because the unexpectd behaviour. Confirmed for now Paul -- pault at gcc dot gnu dot org changed: What|Removed |Added CC||pault at gcc dot gnu dot org Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 Last reconfirmed|-00-00 00:00:00 |2008-04-17 05:59:41 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #2 from pault at gcc dot gnu dot org 2008-04-17 20:37 --- This is indeed a bug. A fix will be posted in half an hour or so. Paul -- pault at gcc dot gnu dot org changed: What|Removed |Added AssignedTo|unassigned at gcc dot gnu |pault at gcc dot gnu dot org |dot org | Status|NEW |ASSIGNED Last reconfirmed|2008-04-17 05:59:41 |2008-04-17 20:37:53 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959