Bug 51970

Summary: [OOP] gimplification failed for polymorphic MOVE_ALLOC
Product: gcc Reporter: Dominique d'Humieres <dominiq>
Component: fortranAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: burnus, janus
Priority: P3 Keywords: ice-on-valid-code
Version: 4.7.0   
Target Milestone: 4.7.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-01-24 00:00:00
Attachments: Draft patch for trans-intrinsic.c

Description Dominique d'Humieres 2012-01-23 17:14:00 UTC
Compiling the following (invalid?) avatar of pr51948

type t
end type t

contains
  function func(x)
    class(t), allocatable :: x(:), func2(:)
    call move_alloc (x, func2)
! FAILS: Error: the 'from' and 'to' arguments of 'move_alloc' intrinsic at (1) must have the same rank 0/1
  end function

  function func2(x)
    type(t), allocatable :: x
    class(t), allocatable :: func2
    call move_alloc (x, func2)
! FAILS: Error: 'to' argument of 'move_alloc' intrinsic at (1) must be a variable
  end function
end

gives the following ICE

gimplification failed:
&func2._data <addr_expr 0x142d38550
    type <pointer_type 0x142d332a0
        type <record_type 0x142d2fdc8 array1_t type_1 BLK
            size <integer_cst 0x142d0db80 constant 384>
            unit size <integer_cst 0x142d0d600 constant 48>
            align 64 symtab 0 alias set -1 canonical type 0x142d2fe70 fields <field_decl 0x142d21a18 data>
            pointer_to_this <pointer_type 0x142d332a0> chain <type_decl 0x142d310b8 D.1886>>
        public unsigned DI
        size <integer_cst 0x142c0c940 constant 64>
        unit size <integer_cst 0x142c0c960 constant 8>
        align 64 symtab 0 alias set -1 canonical type 0x142d33348>
   
    arg 0 <component_ref 0x142d0c540 type <record_type 0x142d2fdc8 array1_t>
       
        arg 0 <var_decl 0x142c0e640 func2 type <record_type 0x142d2f9d8 __class_MAIN___T_1_a>
            addressable used BLK file pr51948_db.f90 line 6 col 0
            size <integer_cst 0x142d0d820 constant 448>
            unit size <integer_cst 0x142d0d860 constant 56>
            align 64 context <function_decl 0x142d2a500 func>>
        arg 1 <field_decl 0x142d32130 _data type <record_type 0x142d2fdc8 array1_t>
            BLK file pr51948_db.f90 line 6 col 0 size <integer_cst 0x142d0db80 384> unit size <integer_cst 0x142d0d600 48>
            align 64 offset_align 128
            offset <integer_cst 0x142c0c980 constant 0>
            bit offset <integer_cst 0x142c0c9e0 constant 0> context <record_type 0x142d2f9d8 __class_MAIN___T_1_a> chain <field_decl 0x142d321c8 _vptr>>
        pr51948_db.f90:7:0>
    pr51948_db.f90:7:0>
pr51948_db.f90: In function 'func':
pr51948_db.f90:7:0: internal compiler error: gimplification failed

This may due to the patch at http://gcc.gnu.org/ml/fortran/2012-01/msg00213.html . The code is accepted by trunk r183357.
Comment 1 Tobias Burnus 2012-01-23 17:43:32 UTC
(In reply to comment #0)
> The code is accepted by trunk r183357.

And it crashes in the same way with 4.7.0 2012-01-18 trunk revision 183273.

(In any case it is not a (real) regression as GCC 4.6 didn't support polymorphic arrays.)
Comment 2 Dominique d'Humieres 2012-01-24 10:22:19 UTC
> And it crashes in the same way with 4.7.0 2012-01-18 trunk revision 183273.

So marked as NEW.
Comment 3 Tobias Burnus 2012-01-24 15:27:43 UTC
Created attachment 26443 [details]
Draft patch for trans-intrinsic.c

There are two issues:

a) from_expr->rank *and* to_expr->rank are 0 instead of 1.
   That's a variant of the issue of PR 51977.

b) trans-intrinsic.c's conv_move_alloc does not properly handle polymorphic
   arrays.

For (b) a draft patch is attached. TODO: Regtest it, try combinations of TYPE and CLASS, try CLASS with allocatable components - and CLASS components.


To use it, you need to either fix issue (a) or you can try the following hack:

--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -2726,0 +2727,5 @@ gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
+  /* THIS IS A HACK! */
+  if (from->ts.type == BT_CLASS && CLASS_DATA (from)->attr.dimension)
+    from->rank = CLASS_DATA (from)->as->rank;
+  if (to->ts.type == BT_CLASS && CLASS_DATA (to)->attr.dimension)
+    to->rank = CLASS_DATA (to)->as->rank;
Comment 4 Dominique d'Humieres 2012-01-24 22:35:43 UTC
The patch attached to comment #4 + the "hack" let the test compile without error (although I don't know if it is valid). I have noticed the following changes:

For 51948, before the patch I had two errors

[macbook] f90/bug% gfc pr51948.f90
pr51948.f90:14.24:

    call move_alloc (x, func)
                        1
Error: 'to' argument of 'move_alloc' intrinsic at (1) must be a variable
pr51948.f90:7.24:

    call move_alloc (x, func)
                        1
Error: the 'from' and 'to' arguments of 'move_alloc' intrinsic at (1) must have the same rank 0/1

After the patch I have only one

[macbook] f90/bug% gfc pr51948.f90
pr51948.f90:14.24:

    call move_alloc (x, func)
                        1
Error: 'to' argument of 'move_alloc' intrinsic at (1) must be a variable

For another avatar and the test in pr51977, the error is replaced by an ICE: before the patch

[macbook] f90/bug% gfc pr51948_db_1.f90
pr51948_db_1.f90:7.24:

    call move_alloc (x, func)
                        1
Error: the 'from' and 'to' arguments of 'move_alloc' intrinsic at (1) must have the same rank 0/1

after the patch

[macbook] f90/bug% gfc pr51948_db_1.f90
pr51948_db_1.f90: In function 'func':
pr51948_db_1.f90:9:0: internal compiler error: in fold_convert_loc, at fold-const.c:2016
Comment 5 Tobias Burnus 2012-01-27 13:08:58 UTC
Author: burnus
Date: Fri Jan 27 13:08:52 2012
New Revision: 183622

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183622
Log:
2012-01-27  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51970
        PR fortran/51977
        * primary.c (gfc_match_varspec. gfc_match_rvalue): Set
        handle array spec for BT_CLASS.
        * expr.c (gfc_get_variable_expr, gfc_lval_expr_from_sym)
        * frontend-passes.c (create_var): Ditto.
        * resolve.c (resolve_actual_arglist, resolve_assoc_var): Ditto.
        * trans-decl.c (gfc_trans_deferred_vars): Use class_pointer
        instead of attr.pointer.
        (gfc_generate_function_code): Use CLASS_DATA (sym) for BT_CLASS.
        * trans-intrinsic.c (conv_intrinsic_move_alloc): Move assert.
        * trans-stmt.c (trans_associate_var): Ask for the descriptor.

2012-01-27  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51970
        PR fortran/51977
        * gfortran.dg/move_alloc_13.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/move_alloc_13.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/frontend-passes.c
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Tobias Burnus 2012-01-27 13:09:26 UTC
FIXED on the trunk (4.7).