public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
@ 2022-08-24  7:18 ` rguenth at gcc dot gnu.org
  2022-08-24 16:42 ` dthorn at google dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-08-24  7:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
For GCC "leaf" is interpreted at the LTO WPA stage where 'compilation unit'
then
means the whole program.  Note that "leaf" doesn't mean calls "back" into the
CU that GCC _can see_ are invalid - those are treated correctly.  It's
basically an optimization promise that if GCC doesn't see such call it can
assume there
are no "hidden" ones.

If GCC, with LTO, would partition the program into two LTRANS partitions,
one containing main and bar and one containing foo then applying this
optimization promise during LTRANS time on the main/bar partition would
be wrong as you say - but I think GCC doesn't do this.

As for documentation I think 'compilation unit' should be changed to
'translation unit', since that's the only thing a user can reason about.
The compiler then has to make sure to apply compatible reasoning when
combining multiple translation units.

Honza?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
  2022-08-24  7:18 ` [Bug middle-end/106725] LTO semantics for __attribute__((leaf)) rguenth at gcc dot gnu.org
@ 2022-08-24 16:42 ` dthorn at google dot com
  2022-08-25  5:48 ` rguenther at suse dot de
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: dthorn at google dot com @ 2022-08-24 16:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #2 from Daniel Thornburgh <dthorn at google dot com> ---
(In reply to Richard Biener from comment #1)
> If GCC, with LTO, would partition the program into two LTRANS partitions,
> one containing main and bar and one containing foo then applying this
> optimization promise during LTRANS time on the main/bar partition would
> be wrong as you say - but I think GCC doesn't do this.

In this case, foo() was already compiled to native code outside of LTO.
Wouldn't this then mean that its contents wouldn't be available for the WPA and
LTRANS phases of the LTO code generation? It seems like the compiler wouldn't
know that foo() might call bar(), and the presence of `__attribute__((leaf))`
would cause it to assume that it doesn't call bar().

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
  2022-08-24  7:18 ` [Bug middle-end/106725] LTO semantics for __attribute__((leaf)) rguenth at gcc dot gnu.org
  2022-08-24 16:42 ` dthorn at google dot com
@ 2022-08-25  5:48 ` rguenther at suse dot de
  2022-08-25 16:26 ` dthorn at google dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: rguenther at suse dot de @ 2022-08-25  5:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #3 from rguenther at suse dot de <rguenther at suse dot de> ---
On Wed, 24 Aug 2022, dthorn at google dot com wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725
> 
> --- Comment #2 from Daniel Thornburgh <dthorn at google dot com> ---
> (In reply to Richard Biener from comment #1)
> > If GCC, with LTO, would partition the program into two LTRANS partitions,
> > one containing main and bar and one containing foo then applying this
> > optimization promise during LTRANS time on the main/bar partition would
> > be wrong as you say - but I think GCC doesn't do this.
> 
> In this case, foo() was already compiled to native code outside of LTO.
> Wouldn't this then mean that its contents wouldn't be available for the WPA and
> LTRANS phases of the LTO code generation? It seems like the compiler wouldn't
> know that foo() might call bar(), and the presence of `__attribute__((leaf))`
> would cause it to assume that it doesn't call bar().

As said, GCC shouldn't assume this since leaf is defined at translation
unit level, not at LTO level.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2022-08-25  5:48 ` rguenther at suse dot de
@ 2022-08-25 16:26 ` dthorn at google dot com
  2022-08-26  7:15 ` rguenther at suse dot de
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: dthorn at google dot com @ 2022-08-25 16:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #4 from Daniel Thornburgh <dthorn at google dot com> ---
(In reply to rguenther@suse.de from comment #3)
> As said, GCC shouldn't assume this since leaf is defined at translation
> unit level, not at LTO level.

Sure, but what prevents GCC from making this assumption? Are all uses of leaf
evaluated before the TUs are merged? Does GCC have some provenance tracking for
which TU a given function came from in the merged view? Is there a pass I
missed to drop leaf after merging but before it's used?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2022-08-25 16:26 ` dthorn at google dot com
@ 2022-08-26  7:15 ` rguenther at suse dot de
  2022-11-01  1:32 ` dthorn at google dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: rguenther at suse dot de @ 2022-08-26  7:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #5 from rguenther at suse dot de <rguenther at suse dot de> ---
On Thu, 25 Aug 2022, dthorn at google dot com wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725
> 
> --- Comment #4 from Daniel Thornburgh <dthorn at google dot com> ---
> (In reply to rguenther@suse.de from comment #3)
> > As said, GCC shouldn't assume this since leaf is defined at translation
> > unit level, not at LTO level.
> 
> Sure, but what prevents GCC from making this assumption? Are all uses of leaf
> evaluated before the TUs are merged? Does GCC have some provenance tracking for
> which TU a given function came from in the merged view? Is there a pass I
> missed to drop leaf after merging but before it's used?

Honza should be able to answer this.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  2022-08-26  7:15 ` rguenther at suse dot de
@ 2022-11-01  1:32 ` dthorn at google dot com
  2022-11-01  1:50 ` dthorn at google dot com
  2022-11-05 14:09 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 8+ messages in thread
From: dthorn at google dot com @ 2022-11-01  1:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #6 from Daniel Thornburgh <dthorn at google dot com> ---
I spent a little more time on this, and here's a more concrete reproducer of
GCC's current behavior.

The setup again has 3 files: main.c, lto.c, and ext.c. lto.c is a simple
getter-setter interface wrapping a global int. main.c sets the value using this
interface, then makes an __attribute__((leaf)) call to ext.c. This sets the
value to 0. This should be legal, since the call doesn't call back to main.c,
it calls to lto.c.

$ tail -n+1 *.c

==> ext.c <==
void set_value(int v);

void external_call(void) {
  set_value(0);
}

==> lto.c <==
static int value;
void set_value(int v) { value = v; }
int get_value(void) { return value; }

==> main.c <==
#include <stdio.h>

void set_value(int v);
int get_value(void);
__attribute__((leaf)) void external_call(void);

int main(void) {
  set_value(42);
  external_call();
  printf("%d\n", get_value());
}


If we compile main.c and lto.c together using the pre-WHOPR module-merging
flow, the resulting binary assumes that the external call cannot clobber the
value, and it thus prints 42 rather than zero.

$ gcc -c -O2 ext.c
$ gcc -O2 -flto-partition=none main.o lto.o ext.o
$ ./a.out
42

If you instead use WHOPR, it looks like this optimization doesn't trigger:
$ gcc -O2 -flto main.o lto.o ext.o
$ ./a.out
0

At least in the unpartitioned case, it looks like the optimizer is considering
attribute((leaf)) to apply to the whole LTO unit. I'm unsure what WPA's
semantics are, since there may be other reasons why this optimization wasn't
taken there.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
                   ` (5 preceding siblings ...)
  2022-11-01  1:32 ` dthorn at google dot com
@ 2022-11-01  1:50 ` dthorn at google dot com
  2022-11-05 14:09 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 8+ messages in thread
From: dthorn at google dot com @ 2022-11-01  1:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

--- Comment #7 from Daniel Thornburgh <dthorn at google dot com> ---
Correction: A compilation line was missed:

+$ gcc -flto -c -O2 main.c lto.c
 $ gcc -c -O2 ext.c

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug middle-end/106725] LTO semantics for __attribute__((leaf))
       [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
                   ` (6 preceding siblings ...)
  2022-11-01  1:50 ` dthorn at google dot com
@ 2022-11-05 14:09 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-11-05 14:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106725

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-11-05
           Keywords|                            |wrong-code
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think that shows we have a correctness issue here (if 'leaf' should be
usable).

Honza - can you please investigate?

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-11-05 14:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-106725-4@http.gcc.gnu.org/bugzilla/>
2022-08-24  7:18 ` [Bug middle-end/106725] LTO semantics for __attribute__((leaf)) rguenth at gcc dot gnu.org
2022-08-24 16:42 ` dthorn at google dot com
2022-08-25  5:48 ` rguenther at suse dot de
2022-08-25 16:26 ` dthorn at google dot com
2022-08-26  7:15 ` rguenther at suse dot de
2022-11-01  1:32 ` dthorn at google dot com
2022-11-01  1:50 ` dthorn at google dot com
2022-11-05 14:09 ` rguenth at gcc dot gnu.org

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).