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

            Bug ID: 125332
           Summary: `flatten` attribute has no effect at -O0 optimization
                    level
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vittorio.romeo at outlook dot com
  Target Milestone: ---

Code:

    template<int N>
    [[gnu::flatten]]
    inline int f() {
        if constexpr (N == 0) {
            return 42;
        } else {
            return f<N-1>();
        }
    }

    [[gnu::flatten]]
    int main() {
        return f<100>();
    }

With either g++ trunk or g++ 16.1, using `-std=c++26 -O0`, no inlining happens
whatsoever. The generated assembly contains ~100 function calls:

        "main":
                push    rbp
                mov     rbp, rsp
                call    "int f<100>()"
                nop
                pop     rbp
                ret
        "int f<100>()":
                push    rbp
                mov     rbp, rsp
                call    "int f<99>()"
                pop     rbp
                ret
        "int f<99>()":
                push    rbp
                mov     rbp, rsp
                call    "int f<98>()"
                pop     rbp
                ret
        # etc...

On Compiler Explorer:
https://godbolt.org/z/xPW8nq6jE

If this is intended behavior, the documentation of `flatten` could be improved
that the attribute has no effect unless optimizations are enabled.
  • [Bug c++/125332] New: `flat... vittorio.romeo at outlook dot com via Gcc-bugs

Reply via email to