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

            Bug ID: 100579
           Summary: [coroutines] Poor codegen using an optional-like type
                    with co_await
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rs2740 at gmail dot com
  Target Milestone: ---

#ifdef __clang__
#include <experimental/coroutine>
namespace stdx = std::experimental;
#else
#include <coroutine>
namespace stdx = std;
#endif
struct O {
 ~O() {}
 struct promise_type {
     O get_return_object() { return O{this};}
     stdx::suspend_never initial_suspend() { return {}; }
     stdx::suspend_never final_suspend() noexcept { return {}; }
     void return_void(){ value->b = true; }
     void unhandled_exception(){ throw; }

     auto await_transform(O o) {
         struct A {
             bool await_ready() {
                 return o_.b;
             }
             void await_resume() {}
             void await_suspend(stdx::coroutine_handle<> h){
                 h.destroy();
             }
             O o_;
         };
         return A{o};
     }
     O* value;
 };

 explicit O(promise_type* p){ p->value = this; }
 explicit O(bool b) : b(b) {}

 bool b = false;
};

O g();

O f() { co_await g();  co_return; }

This is basically a reduced version of
https://github.com/facebook/folly/blob/99f856ae2009a80b157b5121e44b1f70f61bd7c9/folly/Optional.h#L613-L678

GCC does not appear to be able to optimize away the coroutine frame allocation,
while Clang does: https://gcc.godbolt.org/z/7981o81ne

Reply via email to