Bug 78392 - ICE in gfc_trans_auto_array_allocation, at fortran/trans-array.c:5979
Summary: ICE in gfc_trans_auto_array_allocation, at fortran/trans-array.c:5979
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 7.0
Assignee: janus
URL:
Keywords: ice-on-valid-code
: 42359 (view as bug list)
Depends on:
Blocks: 55207
  Show dependency treegraph
 
Reported: 2016-11-17 10:09 UTC by janus
Modified: 2018-09-14 18:25 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-11-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description janus 2016-11-17 10:09:48 UTC
Consider this short test case:

module mytypes
   implicit none
 contains
   pure integer function get_i ()
     get_i = 13
   end function
end module

program test
  use mytypes
  implicit none
  integer, dimension(get_i()), save :: x
  print *, size (x)
end


It seems to be valid Fortran, but generates an ICE with every gfortran version I tried (from 4.7 to to 7-trunk).

On trunk, the error looks like this:

internal compiler error: in gfc_trans_auto_array_allocation, at fortran/trans-array.c:5979
0x8dff23 gfc_trans_auto_array_allocation(tree_node*, gfc_symbol*, gfc_wrapped_block*)
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/trans-array.c:5979
0x90494d gfc_trans_deferred_vars(gfc_symbol*, gfc_wrapped_block*)
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/trans-decl.c:4321
0x90b66e gfc_generate_function_code(gfc_namespace*)
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/trans-decl.c:6366
0x8cdcfd gfc_generate_code(gfc_namespace*)
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/trans.c:2030
0x85cefd translate_all_program_units
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/parse.c:6038
0x85d572 gfc_parse_file()
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/parse.c:6238
0x8b66a5 gfc_be_parse_file
	/home/jweil/gcc/gcc7/trunk/gcc/fortran/f95-lang.c:202


It is related to typebound_proc_19.f90 in the testsuite (from PR 47399).
Comment 1 janus 2016-11-17 10:16:11 UTC
When removing the SAVE attribute ...


module mytypes
   implicit none
 contains
   pure integer function get_i ()
     get_i = 13
   end function
end module

program test
  use mytypes
  implicit none
  integer, dimension(get_i()) :: x
  print *, size (x)
end


... it compiles fine with all tested versions. However, this variant is a regression of the patch for PR 55207, see:

https://gcc.gnu.org/ml/fortran/2014-03/msg00108.html

However, as comment 0 shows, it is not a problem introduced with that patch, but it is rather a long-standing problem that is only uncovered by the patch (according to F08, variables in the main PROGRAM implicitly get the SAVE attribute).
Comment 2 janus 2016-11-17 12:54:25 UTC
(In reply to janus from comment #0)
> On trunk, the error looks like this:
> 
> internal compiler error: in gfc_trans_auto_array_allocation, at
> fortran/trans-array.c:5979

This line is an assert, and simply removing it makes the code in comment 0 compile and run without errors:


Index: gcc/fortran/trans-array.c
===================================================================
--- gcc/fortran/trans-array.c	(Revision 242535)
+++ gcc/fortran/trans-array.c	(Arbeitskopie)
@@ -5976,7 +5976,6 @@ gfc_trans_auto_array_allocation (tree decl, gfc_sy
   type = TREE_TYPE (type);
 
   gcc_assert (!sym->attr.use_assoc);
-  gcc_assert (!TREE_STATIC (decl));
   gcc_assert (!sym->module);
 
   if (sym->ts.type == BT_CHARACTER


It also regtests cleanly on current trunk. With this patch, the only difference in the dump of comment 0 and comment 1 is:


--- c0.f90.003t.original	2016-11-17 13:15:57.487735846 +0100
+++ c1.f90.003t.original	2016-11-17 13:13:07.096377920 +0100
@@ -12,7 +12,7 @@ test ()
   void * restrict D.3458;
   integer(kind=8) ubound.0;
   integer(kind=8) size.1;
-  static integer(kind=4)[0:D.3455] * restrict x;
+  integer(kind=4)[0:D.3455] * restrict x;
   integer(kind=8) D.3455;
   bitsizetype D.3456;
   sizetype D.3457;


As an alternative to removing the assert, one could possibly prevent SAVEd variables in the main PROGRAM from being declared as "static" (which might also cure the performance regressions that Dominique reported for my patch for PR 55207)?
Comment 3 janus 2016-11-18 11:33:51 UTC
(In reply to janus from comment #2)
> As an alternative to removing the assert, one could possibly prevent SAVEd
> variables in the main PROGRAM from being declared as "static" (which might
> also cure the performance regressions that Dominique reported for my patch
> for PR 55207)?

This could possibly be accomplished like this:

Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(Revision 242584)
+++ gcc/fortran/trans-decl.c	(Arbeitskopie)
@@ -635,7 +635,8 @@ gfc_finish_var_decl (tree decl, gfc_symbol * sym)
      initialized variables are SAVE_IMPLICIT and explicitly saved are
      SAVE_EXPLICIT.  */
   if (!sym->attr.use_assoc
-	&& (sym->attr.save != SAVE_NONE || sym->attr.data
+	&& ((sym->attr.save != SAVE_NONE && !sym->ns->proc_name->attr.is_main_program)
+	    || sym->attr.data
 	    || (sym->value && sym->ns->proc_name->attr.is_main_program)
 	    || (flag_coarray == GFC_FCOARRAY_LIB
 		&& sym->attr.codimension && !sym->attr.allocatable)))

That indeed removes the ICE on comment 0, however it causes loads of testsuite regressions, so it's certainly not a very good idea.
Comment 4 Dominique d'Humieres 2016-11-18 11:38:20 UTC
Confirmed from 4.8 up to trunk (7.0).
Comment 5 Dominique d'Humieres 2016-11-18 18:12:32 UTC
> As an alternative to removing the assert, one could possibly prevent SAVEd
> variables in the main PROGRAM from being declared as "static" (which might
> also cure the performance regressions that Dominique reported for my patch
> for PR 55207)?

Related to pr42122?
Comment 6 janus 2016-11-18 18:29:21 UTC
(In reply to Dominique d'Humieres from comment #5)
> Related to pr42122?

Don't think so.
Comment 7 Dominique d'Humieres 2016-11-19 10:08:18 UTC
Likely a duplicate of pr42359.
Comment 8 janus 2016-11-19 12:24:01 UTC
(In reply to Dominique d'Humieres from comment #7)
> Likely a duplicate of pr42359.

Agreed.
Comment 9 Dominique d'Humieres 2016-11-22 14:05:09 UTC
*** Bug 42359 has been marked as a duplicate of this bug. ***
Comment 10 Dominique d'Humieres 2016-11-22 14:06:36 UTC
Working patch at https://gcc.gnu.org/ml/fortran/2016-11/msg00188.html.
Comment 11 janus 2016-11-26 18:12:45 UTC
As noted by Dominique, the problem here has originally been introduced by r126826.
Comment 12 janus 2016-12-12 18:55:26 UTC
Author: janus
Date: Mon Dec 12 18:54:54 2016
New Revision: 243580

URL: https://gcc.gnu.org/viewcvs?rev=243580&root=gcc&view=rev
Log:
2016-12-12  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/78392
	* expr.c (gfc_is_constant_expr): Specification functions are not
	compile-time constants. Update documentation (add reference to F08
	standard), add a FIXME.
	(external_spec_function): Add reference to F08 standard.
	* resolve.c (resolve_fl_variable): Ditto.

2016-12-12  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/78392
	* gfortran.dg/constant_shape.f90: New test case.

Added:
    trunk/gcc/testsuite/gfortran.dg/constant_shape.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
Comment 13 janus 2016-12-12 18:57:39 UTC
Fixed on 7-trunk with r243580. Closing.