Mentioned at https://gcc.gnu.org/ml/fortran/2019-11/msg00060.html refers to https://github.com/j3-fortran/fortran_proposals/issues/57#issuecomment-552680503 (the two Fortran codes in this comment are identical). The problem is the handling of the lower bound and CFI_address. Passing a Fortran array to bind(C), one has for the bounds (F2018, 18.5.3, para 3): "For a C descriptor of an array pointer or allocatable array, the value of the lower_bound member of each element of the dim member of the descriptor is determined by argument association, allocation, or pointer association. For a C descriptor of a nonallocatable nonpointer object, the value of the lower_bound member of each element of the dim member of the descriptor is zero." Hence, for allocate(A(-2:5)) - A's lower bound has also in C the value -2. Thus, when calling CFI_address(dv, lb); with lb = -2, the result should be the original unmodified "data" pointer. The problem is that CFI_address assumes that the lower bound is 0; libgfortran/runtime/ISO_Fortran_binding.c has: base_addr = base_addr + (CFI_index_t)(subscripts[i] * dv->dim[i].sm); Expected: Either add a case separation (e.g. based on the type) or unconditionally honour the lower bound value.
Created attachment 47215 [details] Lightly tested patch
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00928.html * * * While fixing it, I run into the question which lower_bound is to be expected for CFI_section and for CFI_establish, cf. https://mailman.j3-fortran.org/pipermail/j3/2019-November/thread.html#11740 (PR 89843 also talks about CFI_section.) The current implementation of CFI_section seems to be fine (according to Bob and Bill), however, I still think it is not well specified. Regarding CFI_establish, there is still the question what should be the value of lower_bound for CFI_attribute_other. (gfortran uses '0' for pointer as demanded by the standard and '1' otherwise (unspecified).)
Author: burnus Date: Tue Nov 12 19:33:10 2019 New Revision: 278101 URL: https://gcc.gnu.org/viewcvs?rev=278101&root=gcc&view=rev Log: PR fortran/92470 Fixes for CFI_address libgfortran/ PR fortran/92470 * runtime/ISO_Fortran_binding.c (CFI_address): Handle non-zero lower_bound; update error message. (CFI_allocate): Fix comment typo. (CFI_establish): Fix identation, fix typos, don't check values of 'dv' argument. gcc/testsuite/ PR fortran/92470 * gfortran.dg/ISO_Fortran_binding_17.c: New. * gfortran.dg/ISO_Fortran_binding_17.f90: New. * gfortran.dg/ISO_Fortran_binding_1.c (elemental_mult_c, allocate_c, section_c, select_part_c): Update for CFI_{address} changes; add asserts. Added: trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.f90 Modified: trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c trunk/libgfortran/ChangeLog trunk/libgfortran/runtime/ISO_Fortran_binding.c
Author: burnus Date: Wed Nov 13 11:13:57 2019 New Revision: 278128 URL: https://gcc.gnu.org/viewcvs?rev=278128&root=gcc&view=rev Log: PR fortran/92470 Fixes for CFI_address libgfortran/ PR fortran/92470 * runtime/ISO_Fortran_binding.c (CFI_establish): Set lower_bound to 0 also for CFI_attribute_other. gcc/testsuite/ PR fortran/92470 * gfortran.dg/ISO_Fortran_binding_1.c (establish_c): Add assert for lower_bound == 0. Modified: trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c trunk/libgfortran/ChangeLog trunk/libgfortran/runtime/ISO_Fortran_binding.c
Author: burnus Date: Wed Nov 13 15:06:47 2019 New Revision: 278143 URL: https://gcc.gnu.org/viewcvs?rev=278143&root=gcc&view=rev Log: PR fortran/92470 Fixes for CFI_address Backport from mainline libgfortran/ 2019-11-13 Tobias Burnus <tobias@codesourcery.com> PR fortran/92470 * runtime/ISO_Fortran_binding.c (CFI_establish): Set lower_bound to 0 also for CFI_attribute_other. 2019-11-12 Tobias Burnus <tobias@codesourcery.com> PR fortran/92470 * runtime/ISO_Fortran_binding.c (CFI_address): Handle non-zero lower_bound; update error message. (CFI_allocate): Fix comment typo. (CFI_establish): Fix identation, fix typos, don't check values of 'dv' argument. gcc/testsuite/ 2019-11-13 Tobias Burnus <tobias@codesourcery.com> PR fortran/92470 * gfortran.dg/ISO_Fortran_binding_1.c (establish_c): Add assert for lower_bound == 0. 2019-11-12 Tobias Burnus <tobias@codesourcery.com> PR fortran/92470 * gfortran.dg/ISO_Fortran_binding_17.c: New. * gfortran.dg/ISO_Fortran_binding_17.f90: New. * gfortran.dg/ISO_Fortran_binding_1.c (elemental_mult_c, allocate_c, section_c, select_part_c): Update for CFI_{address} changes; add asserts. Added: branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.f90 Modified: branches/gcc-9-branch/gcc/testsuite/ChangeLog branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c branches/gcc-9-branch/libgfortran/ChangeLog branches/gcc-9-branch/libgfortran/runtime/ISO_Fortran_binding.c
FIXED for trunk/GCC 10 and GCC 9.
Author: burnus Date: Thu Nov 14 08:02:42 2019 New Revision: 278201 URL: https://gcc.gnu.org/viewcvs?rev=278201&root=gcc&view=rev Log: Fix gfortran.dg/ISO_Fortran_binding_17.c using rel. #include PR fortran/92470 PR fortran/92500 * gfortran.dg/ISO_Fortran_binding_17.c: Include ISO_Fortran_binding.h with relative path. Modified: branches/gcc-9-branch/gcc/testsuite/ChangeLog branches/gcc-9-branch/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_17.c