[Bug libstdc++/104223] New: GCC unable to inline trivial functions passed to views::filter and transform unless lifted into types

redbeard0531 at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Jan 25 13:11:22 GMT 2022


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

            Bug ID: 104223
           Summary: GCC unable to inline trivial functions passed to
                    views::filter and transform unless lifted into types
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

I'm not sure if this is an issue with the optimizer or the way that the library
code is written, or some combination of the two, but the end result seems
unfortunate. with() and without() are logically the same function, the only
difference is that with() lifts the function pointers into types using a
templated lambda variable, while without() just passes the function names
directly to the library. It seems interesting that the optimizer knows that
they are "constant enough" to emit direct rather than indirect calls to t() and
f(), however, it isn't constant enough to inline those calls.


https://godbolt.org/z/EqWzzh916
Flags: -std=c++2b -O3
Reproduces on at least 11.2 and trunk

#include <ranges>

namespace views = std::views;

void trace() noexcept;
inline int f(int i) {
    trace();
    return i;
}

inline bool t(int) { return true; }

// for some reason gcc needs this in order to inline f() and t()
template <auto f>
auto typify = [] (int i) { return f(i); };

void with() {
    for (auto&& x : views::single(1) | views::transform(typify<f>) |
views::filter(typify<t>)) {}
}

void without() {
    for (auto&& x : views::single(1) | views::transform(f) | views::filter(t))
{}
}

with():
        sub     rsp, 8
        call    trace()
        add     rsp, 8
        jmp     trace()
without():
        sub     rsp, 8
        mov     edi, 1
        call    f(int)
        mov     edi, eax
        call    t(int)
        test    al, al
        jne     .L10
        add     rsp, 8
        ret
.L10:
        mov     edi, 1
        add     rsp, 8
        jmp     f(int)


More information about the Gcc-bugs mailing list