public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated.
@ 2022-02-15  4:34 cassio.neri at gmail dot com
  2022-02-15  4:35 ` [Bug tree-optimization/104539] " cassio.neri at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: cassio.neri at gmail dot com @ 2022-02-15  4:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

* [Bug tree-optimization/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
@ 2022-02-15  4:35 ` cassio.neri at gmail dot com
  2022-02-15  8:13 ` [Bug c++/104539] " rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cassio.neri at gmail dot com @ 2022-02-15  4:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Cassio Neri <cassio.neri at gmail dot com> ---
Sorry, the last snippet above should be

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

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
  2022-02-15  4:35 ` [Bug tree-optimization/104539] " cassio.neri at gmail dot com
@ 2022-02-15  8:13 ` rguenth at gcc dot gnu.org
  2022-02-17  5:11 ` jason at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-02-15  8:13 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2022-02-15
          Component|tree-optimization           |c++
     Ever confirmed|0                           |1
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |jason at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |12.0

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
t.ii:9:20: missed:   not inlinable: int g()/1 -> int f() [with int <anonymous>
= 0]/0, function body can be overwritten at link time

so the issue seems to be that explicitely instantiating changes

_Z1fILi0EEiv/1 (int f() [with int <anonymous> = 0]) @0x7ffff6697220
  Type: function definition analyzed
  Visibility: semantic_interposition public weak comdat
comdat_group:_Z1fILi0EEiv one_only
  Aux: @0x42d7d00
  References:
  Referring:
  Function flags: body
  Called by: _Z1gv/0
  Calls:

to

_Z1fILi0EEiv/0 (int f() [with int <anonymous> = 0]) @0x7ffff6697000
  Type: function definition analyzed
  Visibility: forced_by_abi semantic_interposition public weak
comdat_group:_Z1fILi0EEiv one_only
  Aux: @0x7ffff6697220
  References:
  Referring:
  Function flags: body
  Called by: _Z1gv/1 (can throw external)
  Calls:

the explicit instantiation lacks COMDAT (but has comdat_group) and it
has forced_by_abi.

I'm not sure the C++ standard calls out any semantic difference for
explicit vs. implicit instantiations but maybe the Itanium ABI does.

Jason?  Honza?

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
  2022-02-15  4:35 ` [Bug tree-optimization/104539] " cassio.neri at gmail dot com
  2022-02-15  8:13 ` [Bug c++/104539] " rguenth at gcc dot gnu.org
@ 2022-02-17  5:11 ` jason at gcc dot gnu.org
  2022-02-17 18:29 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jason at gcc dot gnu.org @ 2022-02-17  5:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #2)
> the explicit instantiation lacks COMDAT (but has comdat_group) and it
> has forced_by_abi.
> 
> I'm not sure the C++ standard calls out any semantic difference for
> explicit vs. implicit instantiations but maybe the Itanium ABI does.

The ABI says nothing about explicit vs. implicit instantiations.

mark_decl_instantiated cleared DECL_COMDAT to fix PR10968, where we failed to
emit an explicit instantiation.  But that was before mark_needed; now that we
have that, I'd think it makes sense to have DECL_COMDAT set on explicit
instantiations just like implicit ones.

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
                   ` (2 preceding siblings ...)
  2022-02-17  5:11 ` jason at gcc dot gnu.org
@ 2022-02-17 18:29 ` jason at gcc dot gnu.org
  2022-02-17 22:51 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jason at gcc dot gnu.org @ 2022-02-17 18:29 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
                   ` (3 preceding siblings ...)
  2022-02-17 18:29 ` jason at gcc dot gnu.org
@ 2022-02-17 22:51 ` cvs-commit at gcc dot gnu.org
  2022-02-17 22:52 ` jason at gcc dot gnu.org
  2024-03-18  7:08 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-02-17 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:2c9b7077b72529fbbe896212a0088bff6025c5e7

commit r12-7288-g2c9b7077b72529fbbe896212a0088bff6025c5e7
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Feb 17 00:04:21 2022 -0500

    c++: inlining explicit instantiations [PR104539]

    The PR10968 fix cleared DECL_COMDAT to force output of explicit
    instantiations.  Then the PR59469 fix added a call to mark_needed, after
    which we no longer need to clear DECL_COMDAT, and leaving it set allows us
    to inline explicit instantiations without worrying about symbol
    interposition.

    I suppose there's an argument to be made that an explicit instantiation
    declaration (extern template) should clear DECL_COMDAT, since that suggests
    that there will be only a single instantiation somewhere that could be
    subject to interposition, but that doesn't change the 'inline' semantics,
    and it seems cleaner to treat template instantiations uniformly.

            PR c++/104539

    gcc/cp/ChangeLog:

            * pt.cc (mark_decl_instantiated): Don't clear DECL_COMDAT.

    gcc/testsuite/ChangeLog:

            * g++.dg/ipa/inline-4.C: New test.

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
                   ` (4 preceding siblings ...)
  2022-02-17 22:51 ` cvs-commit at gcc dot gnu.org
@ 2022-02-17 22:52 ` jason at gcc dot gnu.org
  2024-03-18  7:08 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jason at gcc dot gnu.org @ 2022-02-17 22:52 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|12.0                        |
      Known to work|                            |12.0
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |12.0
         Resolution|---                         |FIXED

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for GCC 12.

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

* [Bug c++/104539] Failed to inline a very simple template function when it's explicit instantiated.
  2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
                   ` (5 preceding siblings ...)
  2022-02-17 22:52 ` jason at gcc dot gnu.org
@ 2024-03-18  7:08 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-18  7:08 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |programmerjake at gmail dot com

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 71587 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2024-03-18  7:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-15  4:34 [Bug tree-optimization/104539] New: Failed to inline a very simple template function when it's explicit instantiated cassio.neri at gmail dot com
2022-02-15  4:35 ` [Bug tree-optimization/104539] " cassio.neri at gmail dot com
2022-02-15  8:13 ` [Bug c++/104539] " rguenth at gcc dot gnu.org
2022-02-17  5:11 ` jason at gcc dot gnu.org
2022-02-17 18:29 ` jason at gcc dot gnu.org
2022-02-17 22:51 ` cvs-commit at gcc dot gnu.org
2022-02-17 22:52 ` jason at gcc dot gnu.org
2024-03-18  7:08 ` pinskia 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).