Bug 40187 - c_f_pointer with stride in SHAPE
Summary: c_f_pointer with stride in SHAPE
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-05-18 17:58 UTC by Thomas Koenig
Modified: 2009-07-27 09:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-05-19 17:18:27


Attachments
Failing test case (687 bytes, text/plain)
2009-05-19 17:14 UTC, Thomas Koenig
Details
Driver for failing test case (identical to c_f_pointer_shape_tests_2_driver.c) (404 bytes, text/plain)
2009-05-19 17:17 UTC, Thomas Koenig
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2009-05-18 17:58:28 UTC
Using a SHAPE with stride doesn't work with c_f_pointer doesn't work
(modified test case from the testsuite):

$ diff -u c_f_pointer_shape_tests_4.f03 ~/gcc/trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_2.f03
--- c_f_pointer_shape_tests_4.f03       2009-05-18 19:55:36.000000000 +0200
+++ /home/ig25/gcc/trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_2.f03        2007-11-17 17:27:32.000000000 +0100
@@ -29,13 +29,12 @@
     integer(c_int), value :: num_rows
     integer(c_int), value :: num_cols
     integer, dimension(:,:), pointer :: myArrayPtr
-    integer(c_long_long), dimension(3) :: shape
+    integer(c_long_long), dimension(2) :: shape
     integer :: i,j

     shape(1) = num_rows
-    shape(2) = -3;
-    shape(3) = num_cols
-    call c_f_pointer(cPtr, myArrayPtr, shape(1:3:2))
+    shape(2) = num_cols
+    call c_f_pointer(cPtr, myArrayPtr, shape)
     do j = 1, num_cols
        do i = 1, num_rows
           if(myArrayPtr(i,j) /= ((j-1)*num_rows)+(i-1)) call abort ()
$ gfortran ~/gcc/trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_2_driver.c c_f_pointer_shape_tests_4.f03
$ ./a.out
Aborted
Comment 1 Tobias Burnus 2009-05-18 21:20:07 UTC
Can you attach a working example? I failed to apply your patch and figure out which diffed file is what.
Comment 2 Paul Thomas 2009-05-19 05:06:56 UTC
(In reply to comment #1)
> Can you attach a working example? I failed to apply your patch and figure out
> which diffed file is what.
> 

Hi Tobias,

It's a reversed diff!

Hi Thomas,

Works for me on FC9/x86_64 - what platform are you reporting?

Cheers

Paul
Comment 3 Thomas Koenig 2009-05-19 17:14:03 UTC
Created attachment 17893 [details]
Failing test case
Comment 4 Thomas Koenig 2009-05-19 17:17:59 UTC
Created attachment 17894 [details]
Driver for failing test case (identical to c_f_pointer_shape_tests_2_driver.c)

$ gfortran c_f_pointer_shape_tests_4_driver.c c_f_pointer_shape_tests_4.f03
$ ./a.out
Aborted
$ gfortran -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../../gcc/branches/fortran-dev/configure --prefix=/home/ig25 --enable-languages=c,fortran --enable-maintainer-mode
Thread model: posix
gcc version 4.5.0 20090506 (experimental) (GCC)

The reason this fails is fairly obvious if you look at the code:  In
c_f_pointer_u0, there's

      for (i = 0; i < shapeSize; i++)
        {
          /* Lower bound is 1, as specified by the draft.  */
          f_ptr_out->dim[i].lbound = 1;
          /* Have to allow for the SHAPE array to be any valid kind for
             an INTEGER type.  */
#ifdef HAVE_GFC_INTEGER_1
          if (GFC_DESCRIPTOR_SIZE (shape) == 1)
            f_ptr_out->dim[i].ubound = ((GFC_INTEGER_1 *) (shape->data))[i];

which ignores the (sad) fact that shape could have a stride not equal
to one.  I found this when trying to convert this part to the array access
macros.
Comment 5 Thomas Koenig 2009-05-19 17:18:27 UTC
Since I'm working in this area anyway, I might as well take this.
Comment 6 Thomas Koenig 2009-05-27 05:27:46 UTC
Subject: Bug 40187

Author: tkoenig
Date: Wed May 27 05:27:31 2009
New Revision: 147894

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=147894
Log:
2009-05-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/40187
	* intrinsics/iso_c_binding.c (c_f_pointer_u0):  Take care
	of stride in "shape" argument.

2009-05-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/40187
	* gfortran.dg/c_f_pointer_shape_tests_4.f03:  New file.
	* gfortran.dg/c_f_pointer_shape_tests_4_driver.c:  New file.


Added:
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_4.f03
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_4_driver.c
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/intrinsics/iso_c_binding.c

Comment 7 Thomas Koenig 2009-05-30 13:17:31 UTC
Subject: Bug 40187

Author: tkoenig
Date: Sat May 30 13:17:14 2009
New Revision: 148002

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148002
Log:
2009-05-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/37577
	PR libfortran/40187
	* runtime/in_pack_generic (internal_pack):  Remove unnecessary
	test for stride == 0.
	* runtime/in_unpack_generic.c (internal_unpack):  Likewise.
	* intrinsics/iso_c_binding.c (c_f_pointer_u0):  Take care
	of stride in "shape" argument.  Use array access macros for
	accessing array descriptors.
	* libgfortran.h (struct descriptor_dimension):  Change stride
	to _stride, lbound to _lbound and ubound to _ubound.
	(GFC_DIMENSION_LBOUND):  Use new name(s) in struct
	descriptor_dimension.
	(GFC_DIMENSION_UBOUND):  Likewise.
	(GFC_DIMENSION_STRIDE):  Likewise.
	(GFC_DIMENSION_EXTENT):  Likewise.
	(GFC_DIMENSION_SET):  Likewise.
	(GFC_DESCRIPTOR_LBOUND):  Likewise.
	(GFC_DESCRIPTOR_UBOUND):  Likewise.
	(GFC_DESCRIPTOR_EXTENT):  Likewise.
	(GFC_DESCRIPTOR_STRIDE):  Likewise.
	* io/transfer.c (transfer_array):  Use array access macros.
	Use byte-sized strides.

2009-05-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/40187
	* gfortran.dg/c_f_pointer_shape_tests_4.f03:  New file.
	* gfortran.dg/c_f_pointer_shape_tests_4_driver.c:  New file.


Added:
    branches/fortran-dev/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_4.f03
    branches/fortran-dev/gcc/testsuite/gfortran.dg/c_f_pointer_shape_tests_4_driver.c
Modified:
    branches/fortran-dev/gcc/testsuite/ChangeLog.fortran-dev
    branches/fortran-dev/libgfortran/ChangeLog.dev
    branches/fortran-dev/libgfortran/intrinsics/iso_c_binding.c
    branches/fortran-dev/libgfortran/io/transfer.c
    branches/fortran-dev/libgfortran/libgfortran.h
    branches/fortran-dev/libgfortran/runtime/in_pack_generic.c
    branches/fortran-dev/libgfortran/runtime/in_unpack_generic.c

Comment 8 Thomas Koenig 2009-06-07 14:24:37 UTC
Does this need a 4.4 backport?  I'm inclined to think not.
Comment 9 Tobias Burnus 2009-06-07 14:37:00 UTC
> Does this need a 4.4 backport?  I'm inclined to think not.

Good question. As it is no regression, backporting is not required; it is a wrong-code bug, but strides are rather special. Thus for me either is OK - backporting or just closing as FIXED.
Comment 10 Thomas Koenig 2009-07-27 09:11:09 UTC
Closing (fixed).