This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Make attribute functions pure
- From: Paul Brook <paul at codesourcery dot com>
- To: kenner at vlsi1 dot ultra dot nyu dot edu (Richard Kenner)
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 23 Jul 2004 13:51:31 +0100
- Subject: Re: [PATCH] Make attribute functions pure
- Organization: CodeSourcery
- References: <10407231242.AA27180@vlsi1.ultra.nyu.edu>
On Friday 23 July 2004 13:42, Richard Kenner wrote:
> [Moved from gcc-patches to gcc.]
>
> > I don't see how. It's usually accepted that pure functions can be
> > memoized. Can you give an example of the sort of miscompilation
> > you're worried about?
>
> $ cat impure.c
>
> Sorry, I obviously must not have been clear. I perfectly well understand
> that pure functions can't have side effects of the sort you show.
>
> But what was I asking for are examples of miscompilations caused by
> *memoization* in pure functions.
Ah, sorry.
> To be clear, by "memoization" I mean the storing of the result that the
> function returns so that it can return the same value in a later invokation
> with the same parameters.
>
> In other words, suppose I have a version of sqrt that stores its last input
> and output in a global variable and checks to see if its next input was the
> same as its last input and, if so, returns the last output.
sqrt is actually a const function, although similar reasonings apply.
> Does doing that make it impure? I don't see how and would like an example
> of how that can cause miscompilations.
As you describe it, no. However if you store the inputs, outputs and *any
global variables it or its children look at* in *static local* variables,
that should be ok.
Paul