Bug 20165 - Pointer does not really escape with write
Summary: Pointer does not really escape with write
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2005-02-23 14:28 UTC by Andrew Pinski
Modified: 2010-10-16 18:53 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-09-26 15:33:38


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-02-23 14:28:57 UTC
Take the following fortran code (were we know that write does not make the pointer escape):
      program main
      dimension x(10)
      write(3)n,(x(n),n=1,10)
      end

in the last tree dump we get:
  _gfortran_transfer_integer (&n, 4);
  n = 1;

<L0>:;
  _gfortran_transfer_real (&x[n + -1], 4);
  n.3 = n;
  n = n.3 + 1;
  if (n.3 == 10) goto <L4>; else goto <L0>;

See how n is marked as not a gimple variable which causes us to store it on the stack.  Even though we 
know that _gfortran_transfer_real will not cause the pointer to escape.  There should be a new attribute 
to mark functions as causing pointers not to escape at all.
Comment 1 Andrew Pinski 2005-02-23 18:59:52 UTC
A C testcase (scanf this time):
#include <stdio.h>

int main(void)
{
  int i;
  int j;
  scanf("%d", &i);
  for(;i<=0;i--)
    printf("H");
  return 0;
}
Comment 2 Andrew Pinski 2005-03-01 18:11:58 UTC
Confirmed.
Comment 3 Daniel Berlin 2005-08-31 15:08:24 UTC
These should simply be marked with pointer_no_escape once that goes in
Comment 4 Andrew Pinski 2007-04-16 22:54:22 UTC
*** Bug 31593 has been marked as a duplicate of this bug. ***
Comment 5 Thomas Koenig 2009-06-08 18:12:13 UTC
This also causes missed warnings about unitizialized variables:

$ cat foo.f
      print *,x
      end
$ gfortran -O3 -Wall -Wextra foo.f

We should have some way of marking that pointer as read-only.
Comment 6 Steven Bosscher 2009-07-02 15:40:08 UTC
Dan, you mentioned a "pointer_no_escape" attribute.  What was that about?  I've never seen that mentioned before (or a patch to implement it).  Sounds like a cool attribute to have (and not just for Fortran, too).
Comment 7 rguenther@suse.de 2009-07-02 15:46:56 UTC
Subject: Re:  Pointer does not really escape
 with write

On Thu, 2 Jul 2009, steven at gcc dot gnu dot org wrote:

> ------- Comment #6 from steven at gcc dot gnu dot org  2009-07-02 15:40 -------
> Dan, you mentioned a "pointer_no_escape" attribute.  What was that about?  I've
> never seen that mentioned before (or a patch to implement it).  Sounds like a
> cool attribute to have (and not just for Fortran, too).

There are several useful things to specify per pointer argument:

 1) whether it escapes
 2) whether pointed-to memory is read
 3) whether pointed-to memory is written to
 4) whether memory reachable from it is read
 5) whether memory reachable form it is written to
 6) whether addresses to pointed-to memory may be returned
 7) whether addresses to reachable memory may be returned

in addition to, of course, if the function reads from / writes to
global memory.

Zdenek hat a patch with some fancy attribute at some point, and
implementation-wise I'd like to have something that shares
representation with IPA-PTA results.

We do have tasks to do both IPA-PTA and the above attribute btw.

Richard.
Comment 8 Richard Biener 2009-09-26 15:33:38 UTC
Mine.
Comment 9 Thomas Koenig 2010-10-16 16:06:14 UTC
Author: tkoenig
Date: Sat Oct 16 16:06:07 2010
New Revision: 165559

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=165559
Log:
2010-10-16  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/20165
	PR fortran/31593
	PR fortran/43665
	* gfortran.map:  Add _gfortran_transfer_array_write,
	_gfortran_transfer_array_write, _gfortran_transfer_character_write,
	_gfortran_transfer_character_wide_write,
	_gfortran_transfer_complex_write,
	_gfortran_transfer_integer_write,
	_gfortran_transfer_logical_write and
	_gfortran_transfer_real_write.
	* io/transfer.c (transfer_integer_write):  Add prototype and
	function body as call to the original function, without the
	_write.
	(transfer_real_write):  Likewise.
	(transfer_logical_write):  Likewise.
	(transfer_character_write):  Likewise.
	(transfer_character_wide_write):  Likewise.
	(transfer_complex_write):  Likewise.
	(transfer_array_write):  Likewise.

2010-10-16  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/20165
	PR fortran/31593
	PR fortran/43665
	* trans-io.c (enum iocall): Add IOCALL_X_INTEGER_WRITE,
	IOCALL_X_LOGICAL_WRITE, IOCALL_X_CHARACTER_WRITE,
	IOCALL_X_CHARACTER_WIDE_WRIE, IOCALL_X_REAL_WRITE,
	IOCALL_X_COMPLEX_WRITE and IOCALL_X_ARRAY_WRITE.
	(gfc_build_io_library_fndecls):  Add corresponding function
	decls.
	(transfer_expr):  If the current transfer is a READ, use
	the iocall with the original version, otherwise the version
	with _WRITE.
	(transfer_array_desc):  Likewise.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-io.c
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/gfortran.map
    trunk/libgfortran/io/transfer.c
Comment 10 Thomas Koenig 2010-10-16 18:53:27 UTC
I think this can now be closed.