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