public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: Richard Biener <richard.guenther@gmail.com>,
	Tobias Burnus	<tobias@codesourcery.com>
Cc: "Thomas König" <tk@tkoenig.net>,
	"Janne Blomqvist" <blomqvist.janne@gmail.com>,
	"Thomas Koenig" <tkoenig@netcologne.de>,
	"fortran@gcc.gnu.org" <fortran@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [patch, fortran] Load scalar intent-in variables at the beginning of procedures
Date: Thu, 21 Nov 2019 13:17:00 -0000	[thread overview]
Message-ID: <64ac7298-7589-79dc-9ce8-8f801c247b55@codesourcery.com> (raw)
In-Reply-To: <CAFiYyc3dxnQnF=Vs0RnbpAr2CSu-T913joMUTS+hmxaCx8eiBA@mail.gmail.com>

Hi Richard,

On 11/21/19 1:16 PM, Richard Biener wrote:
> OK, so I found it, it's handled via SSA_NAME_POINTS_TO_READONLY_MEMORY 
> which is initialized during the rewrite into SSA form from the 
> information given by the "fn spec" attribute […] so when the frontend 
> sets "fn spec" from the intent it should already work.

Which I can confirm for the following made-up example:

real function foo(n)
   implicit none (type)
   integer, intent(in) :: n
   integer :: i
   foo = 0.5
   if (n /= 0) return
   call bar()
   do i = 1, n
     foo = foo + sin(foo)
   end do
end

The optimized dump shows the following, i.e. GCC nicely optimizes both the loop and the 'sin' call away:

foo (integer(kind=4) & restrict n)
{
   integer(kind=4) _1;
   <bb 2> [local count: 241635843]:
   _1 = *n_9(D);
   if (_1 != 0)
     goto <bb 4>; [51.12%]
   else
     goto <bb 3>; [48.88%]

   <bb 3> [local count: 118111600]:
   bar ();

   <bb 4> [local count: 241635844]:
   return 5.0e-1;
}

I think this optimization permits some crucial optimizations.
I have not fully followed the later versions of the patch whether
they exploit some additional language semantics to permit optimizations
even without intent(in), but the initial discussion started with intent(in).

> But the examples I saw above didn't use INTENT(IN) for the scalar 
> parameters.

I concur that a well-written program should make use of intent(in) where 
sensible.

There are cases, which could be optimized likewise – but based on the 
case that the pointer address cannot escape instead of just assuming 
that the argument cannot change. – The question is how to convey this to 
the middle end.

I wonder whether there is a 'fn attr' which can tell that the first 
argument of 'print_i' does not cause the address of 'i' escape. If so, 
one could mark all procedure arguments such – unless they have the 
pointer, target or asynchronous attribute or are coarrays. [Probably 
needs some fine tuning.]

In this example, variable values do change, but only in a controlled way 
('print_i' could change it, 'bar' cannot). The semantic is also mainly a 
property of the (local) variable (or dummy argument) declaration and not 
of the functions which are called – although, if 'i' has no target 
attribute, print_i's argument cannot have neither – hence, one could 
generated a function interface

real function foo(i, y)
   implicit none (type)
   integer :: i
   real :: y
   foo = 0.0
   call print_i(i)
   i = 5
   call bar()  ! < this prevents optimizing the sin(y) away
   if (i == 5) return
   foo = sin(y)
end

Fortran semantics implies that 'i' can only change after the 'i = 5' if: 
'i' has the TARGET (or POINTER) attribute. Or it is possible if 'i' has 
the ASYNCHRONOUS or VOLATILE attribute – or it is a coarray (where a 
remote image can modify the local value).

For asynchronous, it would be something like "call read_i(i); call 
wait()" which is structurally the same as above.

TARGET: "An object without the TARGET attribute shall not have a pointer 
associated with it."
VOLATILE: "may be referenced, defined, or become undefined, by 
means20not specified by the program"
ASYNCHRONOUS: "An entity with the ASYNCHRONOUS attribute is a variable, 
and may be subject to asynchronous input/output or asynchronous 
communication."

Tobias

  reply	other threads:[~2019-11-21 13:16 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-11 21:57 Thomas König
2019-11-11 22:08 ` Thomas Koenig
2019-11-11 22:53 ` Janne Blomqvist
2019-11-11 23:02   ` Thomas König
2019-11-12  7:48     ` Janne Blomqvist
2019-11-12 12:50       ` Thomas König
2019-11-12 14:33         ` Tobias Burnus
2019-11-12 17:22           ` Thomas König
2019-11-15  7:41 ` Tobias Burnus
2019-11-15 18:07   ` Thomas König
2019-11-16 20:42     ` Thomas Koenig
2019-11-19 10:46       ` Bernhard Reutner-Fischer
2019-11-19 23:04         ` Thomas Koenig
2019-11-20 18:00           ` Bernhard Reutner-Fischer
2019-11-20 20:45             ` Janne Blomqvist
2019-11-20 21:07               ` Steve Kargl
2019-11-20 21:35               ` Bernhard Reutner-Fischer
2019-11-20 20:46       ` Janne Blomqvist
2019-11-20 21:39         ` Thomas König
2019-11-20 22:19           ` Janne Blomqvist
2019-11-20 22:32             ` Janne Blomqvist
2019-11-21  9:35               ` Janne Blomqvist
2019-11-20 22:37             ` Tobias Burnus
2019-11-20 22:41             ` Thomas König
2019-11-20 22:30           ` Tobias Burnus
2019-11-21  9:41           ` Tobias Burnus
2019-11-21 12:30             ` Richard Biener
2019-11-21 13:17               ` Tobias Burnus [this message]
2019-11-21 13:37                 ` Tobias Burnus
2019-11-21 14:10                 ` Richard Biener
2019-11-21 14:39                   ` Tobias Burnus
2019-11-22 10:44                     ` Tobias Burnus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=64ac7298-7589-79dc-9ce8-8f801c247b55@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=blomqvist.janne@gmail.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    --cc=tk@tkoenig.net \
    --cc=tkoenig@netcologne.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).