public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "antoshkka at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/115309] New: Simple coroutine based generator is not optimized well
Date: Fri, 31 May 2024 14:41:03 +0000	[thread overview]
Message-ID: <bug-115309-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 115309
           Summary: Simple coroutine based generator is not optimized well
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the following minimal C++ coroutine based generator: 

#include <coroutine>

namespace {
struct generator {
  struct promise_type {
    using handle = std::coroutine_handle<promise_type>;
    unsigned value{};

    generator get_return_object() noexcept {
      return generator{handle::from_promise(*this)};
    }

    std::suspend_never initial_suspend() noexcept { return {}; }
    std::suspend_always final_suspend() noexcept { return {}; }  
    void return_void() noexcept {}
    void unhandled_exception() { __builtin_abort(); }

    std::suspend_always yield_value(unsigned v) noexcept {
      value = v;
      return {};
    }    
  };

  ~generator() noexcept { m_coro.destroy(); }
  unsigned operator*() { return m_coro.promise().value; }
private:
  promise_type::handle m_coro;
  explicit generator(promise_type::handle coro) noexcept: m_coro{coro} {}
};

generator generate_1() { co_yield 1; }
}

unsigned test() {
    auto gen = generate_1();
    return *gen;
}



The expected assembly is:
test():
        mov     eax, 1
        ret

However, trunk GCC with `-O2 -std=c++23` flags generates 60+ instructions with
dynamic merory allocations and function calls.

Godbolt playground: https://godbolt.org/z/6PvfTfx9n


Looks that the main part of the problem is the missing allocation elision for
coroutine.

Note that the same problem arises with the Standard C++ std::generator:
https://godbolt.org/z/EvEPT7d1T

             reply	other threads:[~2024-05-31 14:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-31 14:41 antoshkka at gmail dot com [this message]
2024-05-31 14:59 ` [Bug ipa/115309] " pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-115309-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).