public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Code size explosion with -O2
@ 2023-10-06 22:47 Jakub Svojgr
  2023-10-06 23:08 ` David Edelsohn
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Svojgr @ 2023-10-06 22:47 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 1227 bytes --]

Hello,
this is an extremely specific scenario - however while compiling computer-generated code for a fibonnacci function which looks like this:

I32 GF_fib_I_I(I32 L_0_0)
{
    I32 L_2_0; I32 L_5_0; I32 L_6_0;
bb0:;
    bool L_0_1 = ((I32)L_0_0) == ((I32)0);
    if (L_0_1)
    {
        goto bb1;
    }
    L_2_0 = L_0_0;
    goto bb2;
bb1:;
    return ((I32)1);
bb2:;
    bool L_2_1 = ((I32)L_2_0) == ((I32)(0));
    if (L_2_1)
    {
        goto bb4;
    }
    L_5_0 = L_2_0;
    goto bb5;
bb4:;
    return ((I32)1);
bb5:;
    I32 L_5_1 = ((I32)L_5_0) - ((I32)1);
    I32 L_5_2 = GF_fib_I_I(L_5_1);
    I32 L_5_3 = ((I32)L_5_0) - ((I32)2);
    I32 L_5_4 = GF_fib_I_I(L_5_3);
    I32 L_5_5 = ((I32)L_5_2) + ((I32)L_5_4);
    return L_5_5;
}

Now, when compiling this with x86-64 gcc version 13.2 on GodBolt (https://godbolt.org/z/5sj7hPchs)
using the O0 option generates not-so-bad, slightly naive code with 52 lines of assembly
using O1 generates good code with 20 lines of assembly
however when O2 is turned on the generated assembly blows up to 230 lines for this function!
I am not sure if this is classified as a bug because the code does behave correctly however it is strange.

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

* Re: Code size explosion with -O2
  2023-10-06 22:47 Code size explosion with -O2 Jakub Svojgr
@ 2023-10-06 23:08 ` David Edelsohn
  0 siblings, 0 replies; 2+ messages in thread
From: David Edelsohn @ 2023-10-06 23:08 UTC (permalink / raw)
  To: Jakub Svojgr; +Cc: gcc

[-- Attachment #1: Type: text/plain, Size: 1879 bytes --]

On Fri, Oct 6, 2023 at 6:49 PM Jakub Svojgr via Gcc <gcc@gcc.gnu.org> wrote:

> Hello,
> this is an extremely specific scenario - however while compiling
> computer-generated code for a fibonnacci function which looks like this:
>
> I32 GF_fib_I_I(I32 L_0_0)
> {
>     I32 L_2_0; I32 L_5_0; I32 L_6_0;
> bb0:;
>     bool L_0_1 = ((I32)L_0_0) == ((I32)0);
>     if (L_0_1)
>     {
>         goto bb1;
>     }
>     L_2_0 = L_0_0;
>     goto bb2;
> bb1:;
>     return ((I32)1);
> bb2:;
>     bool L_2_1 = ((I32)L_2_0) == ((I32)(0));
>     if (L_2_1)
>     {
>         goto bb4;
>     }
>     L_5_0 = L_2_0;
>     goto bb5;
> bb4:;
>     return ((I32)1);
> bb5:;
>     I32 L_5_1 = ((I32)L_5_0) - ((I32)1);
>     I32 L_5_2 = GF_fib_I_I(L_5_1);
>     I32 L_5_3 = ((I32)L_5_0) - ((I32)2);
>     I32 L_5_4 = GF_fib_I_I(L_5_3);
>     I32 L_5_5 = ((I32)L_5_2) + ((I32)L_5_4);
>     return L_5_5;
> }
>
> Now, when compiling this with x86-64 gcc version 13.2 on GodBolt (
> https://godbolt.org/z/5sj7hPchs)
> using the O0 option generates not-so-bad, slightly naive code with 52
> lines of assembly
> using O1 generates good code with 20 lines of assembly
> however when O2 is turned on the generated assembly blows up to 230 lines
> for this function!
> I am not sure if this is classified as a bug because the code does behave
> correctly however it is strange.
>

Have you measured the performance?

GCC -O2 does not target minimum code size and one should not assume that
smaller code is faster code given the details of processor
microarchitectures.

If you are reporting that -O2 is producing worse performance, then that is
a regression.  Large code size with -O2 is not a regression and may
actually be correct and beneficial because the option is intended to
improve performance, not reduce code size.

Thanks, David

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

end of thread, other threads:[~2023-10-06 23:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-06 22:47 Code size explosion with -O2 Jakub Svojgr
2023-10-06 23:08 ` David Edelsohn

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