public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103429] New: Optimization of Auto-generated condition chain is not giving good lookup tables.
@ 2021-11-25 14:55 ed at edwardrosten dot com
  2021-11-25 15:10 ` [Bug tree-optimization/103429] " rguenth at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: ed at edwardrosten dot com @ 2021-11-25 14:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103429
           Summary: Optimization of Auto-generated condition chain is not
                    giving good lookup tables.
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at edwardrosten dot com
  Target Milestone: ---

I've got come generated condition chains (using recursive templates) and am
getting some odd/suboptimal optimization results. Code is provided below and
with a godbolt link.

In the first case (without a force inline), the compiler inlines the functions
but does not perform condition chain optimization. In the second case
(identical code but with force inline), it will optimize condition chains but
only with exactly 5 elements. Otherwise it will end up with an if-else
structure indexing optimized 5 element condition chains, and an if-else chain
for anything spare.

It only attempts the optimization from gcc 11 onwards, I checked on trunk too.


Example:
https://godbolt.org/z/c9xbPqq7r

Here's the code:
template<int I> void f();

constexpr int N=5;

template<int I=0> 
static inline void f_dispatch(int i){
    if constexpr (I == N)
        return;
    else if(i == I)
        f<I>();
    else
        f_dispatch<I+1>(i);
}

template<int I=0> __attribute__((always_inline)) 
static inline void f_dispatch_always_inline(int i){
    if constexpr (I == N)
        return;
    else if(i == I)
        f<I>();
    else
        f_dispatch_always_inline<I+1>(i);
}

void run(int i){
    f_dispatch<>(i);
}

void run_inline(int i){
    f_dispatch_always_inline<>(i);
}

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

end of thread, other threads:[~2024-05-21  9:10 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-25 14:55 [Bug c++/103429] New: Optimization of Auto-generated condition chain is not giving good lookup tables ed at edwardrosten dot com
2021-11-25 15:10 ` [Bug tree-optimization/103429] " rguenth at gcc dot gnu.org
2021-11-25 15:50 ` ed at edwardrosten dot com
2021-11-25 16:18 ` marxin at gcc dot gnu.org
2021-11-25 18:19 ` marxin at gcc dot gnu.org
2021-11-25 19:03 ` pinskia at gcc dot gnu.org
2021-11-26  8:23 ` marxin at gcc dot gnu.org
2023-04-03  9:03 ` marxin at gcc dot gnu.org
2023-04-26  6:55 ` rguenth at gcc dot gnu.org
2023-07-27  9:22 ` rguenth at gcc dot gnu.org
2024-05-21  9:10 ` jakub 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).