public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110845] New: Function call when it should inline?
@ 2023-07-28 14:27 deco33000 at yandex dot com
  2023-07-28 15:19 ` [Bug c++/110845] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: deco33000 at yandex dot com @ 2023-07-28 14:27 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110845
           Summary: Function call when it should inline?
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deco33000 at yandex dot com
  Target Milestone: ---

Hi,

The below code is inlined in clang (trunk) but not in gcc (13.2).

The code:

#include <format>
#include <iostream>

using namespace std;

constexpr auto f(bool a) -> void {

    if (a) {

        cout << format("hello 1\n");

    } else {

        cout << format("hello 2\n");
    }

}

template <bool T> 
constexpr auto g() -> void {

    if constexpr (T) {

        cout << format("hello 3\n");

    } else {

        cout << format("hello 4\n");
    }
}


int main() {
    f(true);
    f(false);
    g<true>();
    g<false>();

}

flags = -std=c++23 -O3

You can see/test the Godbolt here:
https://godbolt.org/z/aK86nKzYs

Maybe it is related to "format" because without it, it inlines properly.

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

* [Bug c++/110845] Function call when it should inline?
  2023-07-28 14:27 [Bug c++/110845] New: Function call when it should inline? deco33000 at yandex dot com
@ 2023-07-28 15:19 ` pinskia at gcc dot gnu.org
  2023-07-28 15:34 ` deco33000 at yandex dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-28 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Gcc has an heuristic for main where gcc knows that main is called only once and
does not inline as much into a function that will ever be called exactly once.

My bet if you Rename main to foo, gcc will inline f and g into that.

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

* [Bug c++/110845] Function call when it should inline?
  2023-07-28 14:27 [Bug c++/110845] New: Function call when it should inline? deco33000 at yandex dot com
  2023-07-28 15:19 ` [Bug c++/110845] " pinskia at gcc dot gnu.org
@ 2023-07-28 15:34 ` deco33000 at yandex dot com
  2023-07-29  7:32 ` xry111 at gcc dot gnu.org
  2023-07-29 13:03 ` deco33000 at yandex dot com
  3 siblings, 0 replies; 5+ messages in thread
From: deco33000 at yandex dot com @ 2023-07-28 15:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from KL <deco33000 at yandex dot com> ---
Changed main to foo:
same behavior

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

* [Bug c++/110845] Function call when it should inline?
  2023-07-28 14:27 [Bug c++/110845] New: Function call when it should inline? deco33000 at yandex dot com
  2023-07-28 15:19 ` [Bug c++/110845] " pinskia at gcc dot gnu.org
  2023-07-28 15:34 ` deco33000 at yandex dot com
@ 2023-07-29  7:32 ` xry111 at gcc dot gnu.org
  2023-07-29 13:03 ` deco33000 at yandex dot com
  3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-07-29  7:32 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to KL from comment #2)
> Changed main to foo:
> same behavior

It's because you don't have a return statement in foo, causing an undefined
behavior.  And GCC considers undefined behaviors highly improbable to be
executed, so the same logic (not to inline too much into "cold" code paths)
still applies.

If you add the return statement they are inlined:

https://godbolt.org/z/Ko9r4fn3d

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

* [Bug c++/110845] Function call when it should inline?
  2023-07-28 14:27 [Bug c++/110845] New: Function call when it should inline? deco33000 at yandex dot com
                   ` (2 preceding siblings ...)
  2023-07-29  7:32 ` xry111 at gcc dot gnu.org
@ 2023-07-29 13:03 ` deco33000 at yandex dot com
  3 siblings, 0 replies; 5+ messages in thread
From: deco33000 at yandex dot com @ 2023-07-29 13:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from KL <deco33000 at yandex dot com> ---
My mistake indeed,

You are right everything is OK :+1

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

end of thread, other threads:[~2023-07-29 13:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-28 14:27 [Bug c++/110845] New: Function call when it should inline? deco33000 at yandex dot com
2023-07-28 15:19 ` [Bug c++/110845] " pinskia at gcc dot gnu.org
2023-07-28 15:34 ` deco33000 at yandex dot com
2023-07-29  7:32 ` xry111 at gcc dot gnu.org
2023-07-29 13:03 ` deco33000 at yandex dot com

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