[Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated.

cassio.neri at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Feb 15 04:34:33 GMT 2022


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

            Bug ID: 104539
           Summary: Failed to inline a very simple template function when
                    it's explicit instantiated.
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cassio.neri at gmail dot com
  Target Milestone: ---

Consider:

    template <int>
    //inline
    int f() {
        return 0;
    }

    int g() {
        return f<0>() + 1;
    }

Using -O3, I'd expect f to be inlined in g and this is indeed the case:

    g():
      mov eax, 1
      ret

However, if f is explicit instantiated:

    template unsigned f<0>();

then we get a function call (or a jmp if tail call optimisation is possible)

    g():
      sub rsp, 8
      call int f<0>()
      add rsp, 8
      add eax, 1
      ret

A (quite unusual, IMHO) workaround is declaring f as inline:

    template <unsigned n>
    inline
    unsigned f() {
        return n;
    }

https://godbolt.org/z/TarsTY3zb


More information about the Gcc-bugs mailing list