public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/115309] New: Simple coroutine based generator is not optimized well
@ 2024-05-31 14:41 antoshkka at gmail dot com
2024-05-31 14:59 ` [Bug ipa/115309] " pinskia at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: antoshkka at gmail dot com @ 2024-05-31 14:41 UTC (permalink / raw)
To: gcc-bugs
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug ipa/115309] Simple coroutine based generator is not optimized well
2024-05-31 14:41 [Bug middle-end/115309] New: Simple coroutine based generator is not optimized well antoshkka at gmail dot com
@ 2024-05-31 14:59 ` pinskia at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-31 14:59 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115309
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|middle-end |ipa
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Most of it is a missed inlining.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-05-31 14:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-31 14:41 [Bug middle-end/115309] New: Simple coroutine based generator is not optimized well antoshkka at gmail dot com
2024-05-31 14:59 ` [Bug ipa/115309] " 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).