Bug 48360

Summary: [4.6/4.7 Regression] ICE on array assignment statement with allocatable LHS
Product: gcc Reporter: Keith Refson <krefson>
Component: fortranAssignee: Paul Thomas <pault>
Status: RESOLVED FIXED    
Severity: normal CC: burnus, jakub, jvdelisle2, krefson, pault
Priority: P4 Keywords: ice-on-valid-code
Version: 4.6.0   
Target Milestone: 4.6.1   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-04-08 04:16:46
Bug Depends on:    
Bug Blocks: 48456, 48462    
Attachments: Fortran 95 source which gives ICE on compilation attempt.

Description Keith Refson 2011-03-30 11:02:10 UTC
Created attachment 23816 [details]
Fortran 95 source which gives ICE on compilation attempt.

The attached code gives an ICE when compiled with gfortran 4.6.0 

$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6/libexec/gcc/x86_64-unknown-linux-gnu/4.6.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.6-source/gcc-4.6-20110312/configure --enable-languages=c,c++,fortran --enable-checking=release --disable-bootstrap --disable-libmudflap --enable-libgomp --enable-lto --enable-gold --with-plugin-ld=/usr/bin/gold --prefix=/usr/local/gcc-4.6
Thread model: posix
gcc version 4.6.0 20110312 (experimental) (GCC) 

$ gfortran -c gf46-ice.f90
gf46-ice.f90: In function ‘assignit’:
gf46-ice.f90:19:0: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.

Fine with 4.4.3 and 4.5

Disabling the possibility of "allocate-on-assignment" by changing

   hmat = mmv%h0

to
   hmat(:,:) = mmv%h0

compiles correctly.
Comment 1 Tobias Burnus 2011-03-30 11:55:51 UTC
(In reply to comment #0)
> Disabling the possibility of "allocate-on-assignment" by changing
[...]

Or simply use: -fno-realloc-lhs


With my - admittedly not clean - tree, that gives the following valgrind output:

Invalid read of size 8
    at 0x56B13A: get_std_lbound (trans-array.c:6825)
    by 0x573E88: gfc_alloc_allocatable_for_assignment (trans-array.c:7083)
    by 0x58EBDB: gfc_trans_assignment_1 (trans-expr.c:6186)

That's the lines:

6822      else if (expr->expr_type == EXPR_VARIABLE)
6823        {
6824          tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
6825          return GFC_TYPE_ARRAY_LBOUND(tmp, dim);

The issue is that
  TYPE_LANG_SPECIFIC (tmp) == tmp->type.lang_specific == NULL
and, thus, accessing TYPE_LANG_SPECIFIC(node)->lbound[dim]  gives an ICE.

The issue is that for "mmv%h0" the proper backend is not:
  expr->symtree->n.sym->backend_decl
but rather
  expr->ref->u.c->component->backend_decl
Comment 2 Jerry DeLisle 2011-03-31 13:05:01 UTC
I am a little suspicious of this:

 static int count_arglist;
 
+/* Pointer to an array of gfc_expr ** we operate on, plus its size
+   and counter.  */
+
+static gfc_expr ***expr_array;
+static int expr_size, expr_count;


It could be getting walked on or optimized away by the optimizers.  Is there another way to do this that is less indirect or relies less on static.

Just seems too tricky.  I am not looking further on this one.
Comment 3 Jerry DeLisle 2011-03-31 13:06:06 UTC
Disregard 2, wrong PR
Comment 4 Jerry DeLisle 2011-04-01 17:54:06 UTC
This regression was caused by r167220.

Revision 167220 - (view) (download) (annotate) - [select for diffs]
Modified Sun Nov 28 13:47:26 2010 UTC (4 months ago) by pault
Comment 5 Paul Thomas 2011-04-08 04:16:46 UTC
This is obviously mine :-)

This fixes it but I suspect that a few more conditions (no ARRAY_ELEMENT for example) will be needed for it to be correct.

Index: gcc/fortran/trans-array.c
===================================================================
*** gcc/fortran/trans-array.c	(revision 171573)
--- gcc/fortran/trans-array.c	(working copy)
*************** get_std_lbound (gfc_expr *expr, tree des
*** 6707,6712 ****
--- 6707,6713 ----
    tree stride;
    tree cond, cond1, cond3, cond4;
    tree tmp;
+   gfc_ref *ref;
    if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
      {
        tmp = gfc_rank_cst[dim];
*************** get_std_lbound (gfc_expr *expr, tree des
*** 6740,6745 ****
--- 6741,6752 ----
    else if (expr->expr_type == EXPR_VARIABLE)
      {
        tmp = TREE_TYPE (expr->symtree->n.sym->backend_decl);
+       for (ref = expr->ref; ref; ref = ref->next)
+ 	{
+ 	  if (ref->type == REF_COMPONENT
+ 		&& ref->u.c.component->as)
+ 	    tmp = TREE_TYPE (ref->u.c.component->backend_decl);
+ 	}
        return GFC_TYPE_ARRAY_LBOUND(tmp, dim);
      }
    else if (expr->expr_type == EXPR_FUNCTION)

Paul
Comment 6 Paul Thomas 2011-04-12 19:14:52 UTC
Author: pault
Date: Tue Apr 12 19:14:49 2011
New Revision: 172339

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

	PR fortran/48360
	PR fortran/48456
	* trans-array.c (get_std_lbound): For derived type variables
	return array valued component lbound.

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

	PR fortran/48360
	PR fortran/48456
	* gfortran.dg/realloc_on_assign_6.f03: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_6.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Paul Thomas 2011-04-13 18:38:21 UTC
Author: pault
Date: Wed Apr 13 18:38:17 2011
New Revision: 172390

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

	PR fortran/48360
	PR fortran/48456
	* trans-array.c (get_std_lbound): For derived type variables
	return array valued component lbound.

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

	PR fortran/48360
	PR fortran/48456
	* gfortran.dg/realloc_on_assign_6.f03: New test.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/gfortran.dg/realloc_on_assign_6.f03
Modified:
    branches/gcc-4_6-branch/gcc/fortran/ChangeLog
    branches/gcc-4_6-branch/gcc/fortran/trans-array.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 8 Paul Thomas 2011-04-13 19:20:12 UTC
Fixed on trunk and 4.6.

Thanks for the report

Paul