Bug 56378 - [4.8 Regression] ICE with C_LOC
Summary: [4.8 Regression] ICE with C_LOC
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.2
: P5 normal
Target Milestone: 4.8.4
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2013-02-18 16:26 UTC by David Sagan
Modified: 2014-07-11 07:07 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-02-18 00:00:00


Attachments
3 *.f90 files and script to run them (38.45 KB, application/x-tar)
2013-02-18 16:34 UTC, David Sagan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Sagan 2013-02-18 16:26:00 UTC
1) Unpack and cd to the attached directory

2) Run the "doit" script. The result is:

DCS:erpsim1:~/dcs/bmad_distribution/bmad_dist/bad> gfortran --version
GNU Fortran (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.

DCS:erpsim1:~/dcs/bmad_distribution/bmad_dist/bad> ./doit
compiling: precision_def.f90
compiling: bmad_struct.f90
compiling: fortran_cpp_utils.f90
compiling: bmad_cpp_convert_mod.f90
bmad_cpp_convert_mod.f90: In function 'lat_to_c':
bmad_cpp_convert_mod.f90:5622: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.


This bug also appears with 4.6.3 and 4.7.2 (GNU Fortran (MacPorts gcc47 4.7.2_2). I have reproduced this bug on Linux and Mac.
Comment 1 kargls 2013-02-18 16:34:12 UTC
(In reply to comment #0)
> 1) Unpack and cd to the attached directory
> 

There's no attachment.
Comment 2 David Sagan 2013-02-18 16:34:30 UTC
Created attachment 29483 [details]
3 *.f90 files and script to run them
Comment 3 David Sagan 2013-02-18 16:36:14 UTC
Somehow the attachment did not get sent with the initial writeup Now it is downloaded.

(In reply to comment #1)
> (In reply to comment #0)
> > 1) Unpack and cd to the attached directory
> > 
> 
> There's no attachment.
Comment 4 Joost VandeVondele 2013-02-18 18:21:08 UTC
(In reply to comment #2)
> Created attachment 29483 [details]
> 3 *.f90 files and script to run them

confirmed for trunk.

> gfortran -c -ffree-line-length-none -fdollar-ok precision_def.f90 bmad_struct.f90 fortran_cpp_utils.f90 bmad_cpp_convert_mod.f90 
bmad_cpp_convert_mod.f90: In function ‘lat_to_c’:
bmad_cpp_convert_mod.f90:5622:0: internal compiler error: Segmentation fault
     c_logic(F%rf_auto_scale_amp), c_logic(F%use_ptc_layout))
 ^
0x9933bf crash_signal
        ../../gcc/gcc/toplev.c:332
0x616e18 conv_isocbinding_procedure
        ../../gcc/gcc/fortran/trans-expr.c:3728
0x616e18 gfc_conv_procedure_call(gfc_se*, gfc_symbol*, gfc_actual_arglist*, gfc_expr*, vec<tree_node*, va_gc, vl_embed>*)
        ../../gcc/gcc/fortran/trans-expr.c:3968
0x6187ea gfc_conv_function_expr
        ../../gcc/gcc/fortran/trans-expr.c:5566
0x613a8a gfc_conv_procedure_call(gfc_se*, gfc_symbol*, gfc_actual_arglist*, gfc_expr*, vec<tree_node*, va_gc, vl_embed>*)
        ../../gcc/gcc/fortran/trans-expr.c:4230
0x639e06 gfc_trans_call(gfc_code*, bool, tree_node*, tree_node*, bool)
        ../../gcc/gcc/fortran/trans-stmt.c:406
0x5de8b1 trans_code
        ../../gcc/gcc/fortran/trans.c:1500
Comment 5 Joost VandeVondele 2013-02-18 18:48:28 UTC
simplified testcase:


module t
 use, intrinsic :: iso_c_binding
 interface fvec2vec
   module procedure int_fvec2vec
 end interface
contains
 function int_fvec2vec (f_vec, n) result (c_vec)
 integer f_vec(:)
 integer(c_int), target :: c_vec(n)
 end function int_fvec2vec
 subroutine lat_to_c (Fp, C) bind(c)
 integer, allocatable :: ic(:)
 call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))
 end subroutine lat_to_c
end module

looks like related to having a c_loc on an interfacedd function result.
Comment 6 Dominique d'Humieres 2013-02-18 19:21:18 UTC
If I do the following changes to the test in comment #5 in order to silence the warnings " ... may not be C interoperable ... ":

module t
 use, intrinsic :: iso_c_binding

 interface fvec2vec
   module procedure int_fvec2vec
 end interface
contains
 function int_fvec2vec (f_vec, n) result (c_vec)
 integer f_vec(:)
 integer(c_int), target :: c_vec(n)
 end function int_fvec2vec
 subroutine lat_to_c (Fp) bind(c)
 type(c_ptr), value :: Fp
 integer(c_int) :: n1_ic

 integer, allocatable :: ic(:)
 call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))
 end subroutine lat_to_c
end module
end

revision 158253 (2010-04-13) gives a (bogus?) error

pr56378_db.f90:17.23:

 call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))
                       1
Error: Parameter 'fvec2vec' to 'c_loc' at (1) must be either a TARGET or an associated pointer

while revision 162456 (2010-07-23) gives the ICE.
Comment 7 janus 2013-02-18 19:56:07 UTC
Comment 6 segfaults with 4.6, 4.7 and trunk for me, but gives the error message with 4.3.
Comment 8 Tobias Burnus 2013-02-18 20:08:36 UTC
(In reply to comment #5)
...
>  function int_fvec2vec (f_vec, n) result (c_vec)
>  integer f_vec(:)
...
>  subroutine lat_to_c (Fp, C) bind(c)
...
>  call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))



I get – with my half-working patch http://gcc.gnu.org/ml/fortran/2013-02/msg00085.html:

 call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))
                       1
Error: Argument X at (1) to C_LOC shall be either a POINTER or have the TARGET attribute


And I believe that the error is valid. The function result of "fvec2vec" (alias int_fvec2vec) ceases to exist immediately after the function returned – and is thus not suitable for C_LOC. In that case, not even TARGET would help, only POINTER.


The example in comment 6 and the original example (for "c_loc(fvec2vec" and  "c_loc(tensor2vec") give the same error. Thus, it is merely an ICE-on-invalid code/diagnostic bug.


To add some quotes from the standard (F2008, "15.2.3.6 C LOC (X)" – unchanged with TS29113):

"Argument. X shall have either the POINTER or TARGET attribute."
Comment 9 Tobias Burnus 2013-03-25 15:54:45 UTC
Author: burnus
Date: Mon Mar 25 15:40:26 2013
New Revision: 197053

URL: http://gcc.gnu.org/viewcvs?rev=197053&root=gcc&view=rev
Log:
2013-03-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/38536
        PR fortran/38813
        PR fortran/38894
        PR fortran/39288
        PR fortran/40963
        PR fortran/45824
        PR fortran/47023
        PR fortran/47034
        PR fortran/49023
        PR fortran/50269
        PR fortran/50612
        PR fortran/52426
        PR fortran/54263
        PR fortran/55343
        PR fortran/55444
        PR fortran/55574
        PR fortran/56079
        PR fortran/56378
        * check.c (gfc_var_strlen): Properly handle 0-sized string.
        (gfc_check_c_sizeof): Use is_c_interoperable, add checks.
        (is_c_interoperable, gfc_check_c_associated, gfc_check_c_f_pointer,
        gfc_check_c_f_procpointer, gfc_check_c_funloc, gfc_check_c_loc): New
        functions.
        * expr.c (check_inquiry): Add c_sizeof, compiler_version and
        compiler_options.
        (gfc_check_pointer_assign): Refine function result check.
        gfortran.h (gfc_isym_id): Add GFC_ISYM_C_ASSOCIATED,
        GFC_ISYM_C_F_POINTER, GFC_ISYM_C_F_PROCPOINTER, GFC_ISYM_C_FUNLOC,
        GFC_ISYM_C_LOC.
        (iso_fortran_env_symbol, iso_c_binding_symbol): Handle
        NAMED_SUBROUTINE.
        (generate_isocbinding_symbol): Update prototype.
        (get_iso_c_sym): Remove.
        (gfc_isym_id_by_intmod, gfc_isym_id_by_intmod_sym): New prototypes.
        * intrinsic.c (gfc_intrinsic_subroutine_by_id): New function.
        (gfc_intrinsic_sub_interface): Use it.
        (add_functions, add_subroutines): Add missing C-binding intrinsics.
        (gfc_intrinsic_func_interface): Add special case for c_loc.
        gfc_isym_id_by_intmod, gfc_isym_id_by_intmod_sym): New functions.
        (gfc_intrinsic_func_interface, gfc_intrinsic_sub_interface): Use them.
        * intrinsic.h (gfc_check_c_associated, gfc_check_c_f_pointer,
        gfc_check_c_f_procpointer, gfc_check_c_funloc, gfc_check_c_loc,
        gfc_resolve_c_loc, gfc_resolve_c_funloc): New prototypes.
        * iresolve.c (gfc_resolve_c_loc, gfc_resolve_c_funloc): New
        functions.
        * iso-c-binding.def: Split PROCEDURE into NAMED_SUBROUTINE and
        NAMED_FUNCTION.
        * iso-fortran-env.def: Add NAMED_SUBROUTINE for completeness.
        * module.c (create_intrinsic_function): Support subroutines and
        derived-type results.
        (use_iso_fortran_env_module): Update calls.
        (import_iso_c_binding_module): Ditto; update calls to
        generate_isocbinding_symbol.
        * resolve.c (find_arglists): Skip for intrinsic symbols.
        (gfc_resolve_intrinsic): Find intrinsic subs via id.
        (is_scalar_expr_ptr, gfc_iso_c_func_interface,
        set_name_and_label, gfc_iso_c_sub_interface): Remove.
        (resolve_function, resolve_specific_s0): Remove calls to those.
        (resolve_structure_cons): Fix handling.
        * symbol.c (gen_special_c_interop_ptr): Update c_ptr/c_funptr
        generation.
        (gen_cptr_param, gen_fptr_param, gen_shape_param,
        build_formal_args, get_iso_c_sym): Remove.
        (std_for_isocbinding_symbol): Handle NAMED_SUBROUTINE.
        (generate_isocbinding_symbol): Support hidden symbols and
        using c_ptr/c_funptr symtrees for nullptr defs.
        * target-memory.c (gfc_target_encode_expr): Fix handling
        of c_ptr/c_funptr.
        * trans-expr.c (conv_isocbinding_procedure): Remove.
        (gfc_conv_procedure_call): Remove call to it.
        (gfc_trans_subcomponent_assign, gfc_conv_expr): Update handling
        of c_ptr/c_funptr.
        * trans-intrinsic.c (conv_isocbinding_function,
        conv_isocbinding_subroutine): New.
        (gfc_conv_intrinsic_function, gfc_conv_intrinsic_subroutine):
        Call them.
        * trans-io.c (transfer_expr): Fix handling of c_ptr/c_funptr.
        * trans-types.c (gfc_typenode_for_spec,
        gfc_get_derived_type): Ditto.
        (gfc_init_c_interop_kinds): Handle NAMED_SUBROUTINE.

2013-03-25  Tobias Burnus  <burnus@net-b.de>

        PR fortran/38536
        PR fortran/38813
        PR fortran/38894
        PR fortran/39288
        PR fortran/40963
        PR fortran/45824
        PR fortran/47023
        PR fortran/47034
        PR fortran/49023
        PR fortran/50269
        PR fortran/50612
        PR fortran/52426
        PR fortran/54263
        PR fortran/55343
        PR fortran/55444
        PR fortran/55574
        PR fortran/56079
        PR fortran/56378
        * gfortran.dg/c_assoc_2.f03: Update dg-error wording.
        * gfortran.dg/c_f_pointer_shape_test.f90: Ditto.
        * gfortran.dg/c_f_pointer_shape_tests_3.f03: Ditto.
        * gfortran.dg/c_f_pointer_tests_5.f90: Ditto.
        * gfortran.dg/c_funloc_tests_2.f03: Ditto.
        * gfortran.dg/c_funloc_tests_5.f03: Ditto.
        * gfortran.dg/c_funloc_tests_6.f90: Ditto.
        * gfortran.dg/c_loc_tests_10.f03: Add -std=f2008.
        * gfortran.dg/c_loc_tests_11.f03: Ditto, update dg-error.
        * gfortran.dg/c_loc_tests_16.f90: Ditto.
        * gfortran.dg/c_loc_tests_4.f03: Ditto.
        * gfortran.dg/c_loc_tests_15.f90: Update dg-error wording.
        * gfortran.dg/c_loc_tests_3.f03: Valid since F2003 TC5.
        * gfortran.dg/c_loc_tests_8.f03: Ditto.
        * gfortran.dg/c_ptr_tests_14.f90: Update scan-tree-dump-times.
        * gfortran.dg/c_ptr_tests_15.f90: Ditto.
        * gfortran.dg/c_sizeof_1.f90: Fix invalid code.
        * gfortran.dg/iso_c_binding_init_expr.f03: Update dg-error wording.
        * gfortran.dg/pr32601_1.f03: Ditto.
        * gfortran.dg/storage_size_2.f08: Remove dg-error.
        * gfortran.dg/blockdata_7.f90: New.
        * gfortran.dg/c_assoc_4.f90: New.
        * gfortran.dg/c_f_pointer_tests_6.f90: New.
        * gfortran.dg/c_f_pointer_tests_7.f90: New.
        * gfortran.dg/c_funloc_tests_8.f90: New.
        * gfortran.dg/c_loc_test_17.f90: New.
        * gfortran.dg/c_loc_test_18.f90: New.
        * gfortran.dg/c_loc_test_19.f90: New.
        * gfortran.dg/c_loc_test_20.f90: New.
        * gfortran.dg/c_sizeof_5.f90: New.
        * gfortran.dg/iso_c_binding_rename_3.f90: New.
        * gfortran.dg/transfer_resolve_2.f90: New.
        * gfortran.dg/transfer_resolve_3.f90: New.
        * gfortran.dg/transfer_resolve_4.f90: New.
        * gfortran.dg/pr32601.f03: Update dg-error.
        * gfortran.dg/c_ptr_tests_13.f03: Update dg-error.
        * gfortran.dg/c_ptr_tests_9.f03: Fix test case.


Added:
    trunk/gcc/testsuite/gfortran.dg/blockdata_7.f90
    trunk/gcc/testsuite/gfortran.dg/c_assoc_4.f90
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_tests_6.f90
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_tests_7.f90
    trunk/gcc/testsuite/gfortran.dg/c_funloc_tests_8.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_test_17.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_test_18.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_test_19.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_test_20.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_17.f90
    trunk/gcc/testsuite/gfortran.dg/c_sizeof_5.f90
    trunk/gcc/testsuite/gfortran.dg/iso_c_binding_rename_3.f90
    trunk/gcc/testsuite/gfortran.dg/transfer_resolve_2.f90
    trunk/gcc/testsuite/gfortran.dg/transfer_resolve_3.f90
    trunk/gcc/testsuite/gfortran.dg/transfer_resolve_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/intrinsic.c
    trunk/gcc/fortran/intrinsic.h
    trunk/gcc/fortran/iresolve.c
    trunk/gcc/fortran/iso-c-binding.def
    trunk/gcc/fortran/iso-fortran-env.def
    trunk/gcc/fortran/module.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/target-memory.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans-io.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/c_assoc_2.f03
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_test.f90
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_3.f03
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_tests_5.f90
    trunk/gcc/testsuite/gfortran.dg/c_funloc_tests_2.f03
    trunk/gcc/testsuite/gfortran.dg/c_funloc_tests_5.f03
    trunk/gcc/testsuite/gfortran.dg/c_funloc_tests_6.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_10.f03
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_11.f03
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_15.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_16.f90
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_3.f03
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_4.f03
    trunk/gcc/testsuite/gfortran.dg/c_loc_tests_8.f03
    trunk/gcc/testsuite/gfortran.dg/c_ptr_tests_13.f03
    trunk/gcc/testsuite/gfortran.dg/c_ptr_tests_14.f90
    trunk/gcc/testsuite/gfortran.dg/c_ptr_tests_15.f90
    trunk/gcc/testsuite/gfortran.dg/c_ptr_tests_9.f03
    trunk/gcc/testsuite/gfortran.dg/c_sizeof_1.f90
    trunk/gcc/testsuite/gfortran.dg/iso_c_binding_init_expr.f03
    trunk/gcc/testsuite/gfortran.dg/pr32601.f03
    trunk/gcc/testsuite/gfortran.dg/pr32601_1.f03
    trunk/gcc/testsuite/gfortran.dg/storage_size_2.f08
Comment 10 Joost VandeVondele 2013-03-29 08:23:59 UTC
Fixed on trunk (4.9.0):

PR56378.f90:13.23:

 call lat_to_c2 (c_loc(fvec2vec(ic, n1_ic)))
                       1
Error: Argument X at (1) to C_LOC shall have either the POINTER or the TARGET attribute
Comment 11 Jakub Jelinek 2013-04-12 15:15:20 UTC
GCC 4.6.4 has been released and the branch has been closed.
Comment 12 janus 2013-08-21 15:01:19 UTC
(In reply to Joost VandeVondele from comment #10)
> Fixed on trunk (4.9.0):

... by r197053, apparently. Since this looks way too large for backporting, I guess it will not be fixed on the 4.7 and 4.8 branches, right?
Comment 13 Richard Biener 2014-06-12 13:41:03 UTC
The 4.7 branch is being closed, moving target milestone to 4.8.4.
Comment 14 Tobias Burnus 2014-07-11 07:07:06 UTC
As the current patch is rather intrusive and as the issue is fixed in the released compiler 4.9.0, it is unlikely that the fix is backported to GCC 4.8.

Hence:
- WON'T FIX for GCC 4.8.
- FIXED for GCC 4.9/4.10.

Thanks for the report, the patience and sorry that it won't get fixed in GCC 4.7/4.8 any more.