public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined
@ 2024-03-25 10:09 chfast at gmail dot com
  2024-03-25 10:35 ` [Bug rtl-optimization/114452] " xry111 at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: chfast at gmail dot com @ 2024-03-25 10:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114452
           Summary: Functions invoked through compile-time table of
                    function pointers not inlined
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chfast at gmail dot com
  Target Milestone: ---

In the following example there is a compile-time table of pointers to simple
functions. When the table is used in a simple unrolled loop with constant trip
count the functions invoked by pointers are not inlined.

using F = int (*)(int) noexcept;

void test(int z[2]) noexcept {
    static constexpr F fs[]{
        [](int x) noexcept { return x; },
        [](int x) noexcept { return x; },
    };

    for (int i = 0; i < 2; ++i) {
        z[i] = fs[i](z[i]);
    }
}

Generated assembly:

test(int*)::{lambda(int)#1}::_FUN(int):
        mov     eax, edi
        ret
test(int*)::{lambda(int)#2}::_FUN(int):
        mov     eax, edi
        ret
test(int*):
        mov     rdx, rdi
        mov     edi, DWORD PTR [rdi]
        call    test(int*)::{lambda(int)#1}::_FUN(int)
        mov     edi, DWORD PTR [rdx+4]
        mov     DWORD PTR [rdx], eax
        call    test(int*)::{lambda(int)#2}::_FUN(int)
        mov     DWORD PTR [rdx+4], eax
        ret


https://godbolt.org/z/fGqPKh81j

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
@ 2024-03-25 10:35 ` xry111 at gcc dot gnu.org
  2024-03-25 10:40 ` chfast at gmail dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-25 10:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Dup.

*** This bug has been marked as a duplicate of bug 111573 ***

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
  2024-03-25 10:35 ` [Bug rtl-optimization/114452] " xry111 at gcc dot gnu.org
@ 2024-03-25 10:40 ` chfast at gmail dot com
  2024-03-25 10:41 ` xry111 at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: chfast at gmail dot com @ 2024-03-25 10:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Paweł Bylica <chfast at gmail dot com> ---
I don't think this is related to lambdas. The following is also not optimized:


using F = int (*)(int) noexcept;

inline int impl(int x) noexcept { return x; }

void test(int z[2]) noexcept {
    static constexpr F fs[]{
        impl,
        impl,
    };

    for (int i = 0; i < 2; ++i) {
        z[i] = fs[i](z[i]);
    }
}

https://godbolt.org/z/9hPbzo4Px

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
  2024-03-25 10:35 ` [Bug rtl-optimization/114452] " xry111 at gcc dot gnu.org
  2024-03-25 10:40 ` chfast at gmail dot com
@ 2024-03-25 10:41 ` xry111 at gcc dot gnu.org
  2024-03-27 17:01 ` jamborm at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-25 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Paweł Bylica from comment #2)
> I don't think this is related to lambdas. The following is also not
> optimized:
> 
> 
> using F = int (*)(int) noexcept;
> 
> inline int impl(int x) noexcept { return x; }
> 
> void test(int z[2]) noexcept {
>     static constexpr F fs[]{
>         impl,
>         impl,
>     };
> 
>     for (int i = 0; i < 2; ++i) {
>         z[i] = fs[i](z[i]);
>     }
> }
> 
> https://godbolt.org/z/9hPbzo4Px

The analysis of PR111573 says:

"So we fail to inline since ipa-prop fails to track the constant function
address.  I think this is really common in typical lambda function usage"

"fails to track the constant function address" applies for normal functions
too.

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
                   ` (2 preceding siblings ...)
  2024-03-25 10:41 ` xry111 at gcc dot gnu.org
@ 2024-03-27 17:01 ` jamborm at gcc dot gnu.org
  2024-03-27 17:03 ` xry111 at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jamborm at gcc dot gnu.org @ 2024-03-27 17:01 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Jambor <jamborm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|DUPLICATE                   |---
   Last reconfirmed|                            |2024-03-27
     Ever confirmed|0                           |1

--- Comment #4 from Martin Jambor <jamborm at gcc dot gnu.org> ---
This does not look like a duplicate of PR 111573.

Nevertheless, it is not quite obvious what to do here.  Inlining
happens before unrolling and I am not sure we'd consider unrolling in
early optimizations.  And without unrolling, the load from the array
is not easy to fold.

In this testcase all (well, both) functions referenced from the array
are semantically equivalent which is recognized by ICF but making it
be able to pass this information to the inliner would be
non-trivial... and is this the common case worth optimizing for?

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
                   ` (3 preceding siblings ...)
  2024-03-27 17:01 ` jamborm at gcc dot gnu.org
@ 2024-03-27 17:03 ` xry111 at gcc dot gnu.org
  2024-04-11  8:07 ` chfast at gmail dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-27 17:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |NEW
           Keywords|                            |missed-optimization

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
                   ` (4 preceding siblings ...)
  2024-03-27 17:03 ` xry111 at gcc dot gnu.org
@ 2024-04-11  8:07 ` chfast at gmail dot com
  2024-04-11  9:33 ` jamborm at gcc dot gnu.org
  2024-04-11  9:41 ` chfast at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: chfast at gmail dot com @ 2024-04-11  8:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Paweł Bylica <chfast at gmail dot com> ---
(In reply to Martin Jambor from comment #4)
> In this testcase all (well, both) functions referenced from the array
> are semantically equivalent which is recognized by ICF but making it
> be able to pass this information to the inliner would be
> non-trivial... and is this the common case worth optimizing for?

I reduced the original code to the array of two identical functions.
Originally, there weren't identical. I can update the test case if this make
more sense.

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
                   ` (5 preceding siblings ...)
  2024-04-11  8:07 ` chfast at gmail dot com
@ 2024-04-11  9:33 ` jamborm at gcc dot gnu.org
  2024-04-11  9:41 ` chfast at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: jamborm at gcc dot gnu.org @ 2024-04-11  9:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Martin Jambor <jamborm at gcc dot gnu.org> ---
(In reply to Paweł Bylica from comment #5)
> (In reply to Martin Jambor from comment #4)
> > In this testcase all (well, both) functions referenced from the array
> > are semantically equivalent which is recognized by ICF but making it
> > be able to pass this information to the inliner would be
> > non-trivial... and is this the common case worth optimizing for?
> 
> I reduced the original code to the array of two identical functions.
> Originally, there weren't identical. I can update the test case if this make
> more sense.

Probably not.  But how many elements does the array have in the original code? 
Perhaps we could speculatively inline them if there are only few.

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

* [Bug rtl-optimization/114452] Functions invoked through compile-time table of function pointers not inlined
  2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
                   ` (6 preceding siblings ...)
  2024-04-11  9:33 ` jamborm at gcc dot gnu.org
@ 2024-04-11  9:41 ` chfast at gmail dot com
  7 siblings, 0 replies; 9+ messages in thread
From: chfast at gmail dot com @ 2024-04-11  9:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Paweł Bylica <chfast at gmail dot com> ---
(In reply to Martin Jambor from comment #6)
> (In reply to Paweł Bylica from comment #5)
> > (In reply to Martin Jambor from comment #4)
> > > In this testcase all (well, both) functions referenced from the array
> > > are semantically equivalent which is recognized by ICF but making it
> > > be able to pass this information to the inliner would be
> > > non-trivial... and is this the common case worth optimizing for?
> > 
> > I reduced the original code to the array of two identical functions.
> > Originally, there weren't identical. I can update the test case if this make
> > more sense.
> 
> Probably not.  But how many elements does the array have in the original
> code?  Perhaps we could speculatively inline them if there are only few.

5. These are boolean functions from RIPEMD160.

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

end of thread, other threads:[~2024-04-11  9:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-25 10:09 [Bug rtl-optimization/114452] New: Functions invoked through compile-time table of function pointers not inlined chfast at gmail dot com
2024-03-25 10:35 ` [Bug rtl-optimization/114452] " xry111 at gcc dot gnu.org
2024-03-25 10:40 ` chfast at gmail dot com
2024-03-25 10:41 ` xry111 at gcc dot gnu.org
2024-03-27 17:01 ` jamborm at gcc dot gnu.org
2024-03-27 17:03 ` xry111 at gcc dot gnu.org
2024-04-11  8:07 ` chfast at gmail dot com
2024-04-11  9:33 ` jamborm at gcc dot gnu.org
2024-04-11  9:41 ` chfast at gmail 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).