User account creation filtered due to spam.

Bug 29785 - Fortran 2003: POINTER Rank Remapping
Summary: Fortran 2003: POINTER Rank Remapping
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Daniel Kraft
URL:
Keywords:
Depends on:
Blocks: 20585
  Show dependency treegraph
 
Reported: 2006-11-09 18:47 UTC by Tobias Burnus
Modified: 2010-08-19 16:06 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-07-21 09:06:34


Attachments
Partial patch. (5.79 KB, patch)
2010-08-18 19:34 UTC, Daniel Kraft
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2006-11-09 18:47:59 UTC
POINTER Rank Remapping

(From http://www.fortran.bcs.org/forum2002/f2000dme.htm)
Motivation: ability to have pointers to diagonals of matrices.

  REAL,ALLOCATABLE,TARGET :: base_array(:)
  REAL,POINTER :: matrix(:,:)
  REAL,POINTER :: diagonal(:)
  ...
  ALLOCATE(base_array(n*n))
  matrix(1:n,1:n) => base_array ! rank remapping
  diagonal => base_array(::n+1)

Notes:

* The base array must be rank one, to ensure that the remapping is a simple linear transformation.
* Both lower bound and upper bound must be specified for each dimension.

Fortran 2003 standard, see "7.4.2.1 Data pointer assignment":
"R735 pointer-assignment-stmt
   is data-pointer-object [ (bounds-spec-list) ] => data-target
   or data-pointer-object (bounds-remapping-list ) => data-target
   or proc-pointer-object => proc-target

C718 (R735) If bounds-spec-list is specified, the number of bounds-specs shall 
            equal the rank of data pointer-object.
C719 (R735) If bounds-remapping-list is specified, the number of 
            bounds-remappings shall equal the rank of data-pointer-object.
C720 (R735) If bounds-remapping-list is specified, data-target shall have rank 
            one; otherwise, the ranks of data-pointer-object and data-target
            shall be the same."

and "7.4.2.1 Data pointer assignment":
"If bounds-remapping-list is specified, data-target shall not be a disassociated or undefined pointer, and the size of data-target shall not be less than the size of data-pointer-object. The elements of the target of data-pointer-object, in array element order (6.2.2.2), are the first SIZE(data-pointer-object) elements of data-target."
Comment 1 Tobias Burnus 2007-04-12 09:19:31 UTC
Cf. http://gcc.gnu.org/ml/fortran/2007-04/msg00120.html
Comment 2 Tobias Burnus 2008-07-03 16:09:49 UTC
In Fortran 2008 the rank-one constraint is gone:

"C720 If bounds-remapping-list is not specified, the ranks of data-pointer-object and data-target shall be the same."
Comment 3 Joost VandeVondele 2009-08-07 07:35:07 UTC
still missing 4.5.0, this seems a particularly useful F03 feature ;-) 

For an expert, should be 'easy' to add (ignoring the F2008 variant) ?

Comment 4 Tobias Burnus 2010-07-21 09:03:19 UTC
Fortran 2008 has the following, which I missed to quote:

"If bounds-remapping-list is specified, the pointer target shall be simply contiguous (6.5.4) or of rank one. It shall not be a disassociated or undefined pointer, and the size of the pointer target shall not be less than the size of the pointer object. The elements of the target of the pointer object, in array element order (6.5.3.2), are the first SIZE (data-pointer-object) elements of the pointer target."

"C717 (R733) If bounds-remapping-list is specified, the number of bounds-remappings shall equal the rank of data-pointer-object."
Comment 5 Daniel Kraft 2010-07-21 09:06:34 UTC
I'll work on this.
Comment 6 Tobias Burnus 2010-07-21 10:14:34 UTC
See also PR 45016, which is not about bounds remapping but about setting the correct bounds in an array assignment. (Wrong-code F95 bug + missing F2003 feature).

Maybe all three (remapping, fix bounds, F2003 lower bounds) can be fixed simultaneously.
Comment 7 Daniel Kraft 2010-08-17 17:38:28 UTC
Tobias (and all):  Do you think we should check for "the size of data-target shall not be less than the size of data-pointer-object" at runtime when -fcheck=bounds is given?
Comment 8 Mikael Morin 2010-08-17 18:21:31 UTC
(In reply to comment #7)
> Tobias (and all):  Do you think we should check for "the size of data-target
> shall not be less than the size of data-pointer-object" at runtime when
> -fcheck=bounds is given?
> 

Yes.
The existing bounds-check code checks that we don't access an array beyond what its descriptor permits. 
We have to check that we don't create a descriptor that has bounds beyond the array limits, thus by-passing the bounds-check code.
Comment 9 Daniel Kraft 2010-08-18 19:34:44 UTC
Created attachment 21514 [details]
Partial patch.

This implements rank remapping and also bounds remapping as for PR 45016.  Tests seem to be successful, although it's not yet been fully regtested.

I still have to / want to add a compile-time and -fcheck=bounds test for too small target.
Comment 10 Daniel Kraft 2010-08-19 16:02:50 UTC
Subject: Bug 29785

Author: domob
Date: Thu Aug 19 16:02:30 2010
New Revision: 163377

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163377
Log:
2010-08-19  Daniel Kraft  <d@domob.eu>

	PR fortran/29785
	PR fortran/45016
	* trans.h (struct gfc_se): New flag `byref_noassign'.
	* trans-array.h (gfc_conv_shift_descriptor_lbound): New method.
	(gfc_conv_array_extent_dim), (gfc_conv_descriptor_size): New methods.
	* expr.c (gfc_check_pointer_assign): Allow bounds and rank remapping
	and check for compile-time errors with those.
	* trans-decl.c (trans_associate_var): Use new routine
	`gfc_conv_shift_descriptor_lbound' instead of doing it manually.
	* trans-array.c (gfc_conv_shift_descriptor_lbound): New method.
	(gfc_conv_array_extent_dim), (gfc_conv_descriptor_size): New methods.
	(gfc_array_init_size): Use new `gfc_conv_array_extent_dim'.
	(gfc_conv_expr_descriptor): Handle new flag `byref_noassign'.
	* trans-expr.c (gfc_trans_pointer_assignment): Handle bounds and
	rank remapping for assignment.

2010-08-19  Daniel Kraft  <d@domob.eu>

	PR fortran/29785
	PR fortran/45016
	* gfortran.dg/pointer_assign_5.f90: Remove 'not implemented' error.
	* gfortran.dg/pointer_remapping_1.f90: New test.
	* gfortran.dg/pointer_remapping_2.f03: New test.
	* gfortran.dg/pointer_remapping_3.f08: New test.
	* gfortran.dg/pointer_remapping_4.f03: New test.
	* gfortran.dg/pointer_remapping_5.f08: New test.
	* gfortran.dg/pointer_remapping_6.f08: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_1.f90
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_2.f03
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_3.f08
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_4.f03
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_5.f08
    trunk/gcc/testsuite/gfortran.dg/pointer_remapping_6.f08
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-array.h
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/pointer_assign_5.f90

Comment 11 Daniel Kraft 2010-08-19 16:06:12 UTC
Fixed (with an extended version of the patch attached above).