public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
@ 2020-10-16  1:40 lewissbaker.opensource at gmail dot com
  2020-10-16  6:55 ` [Bug c++/97452] " iains at gcc dot gnu.org
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: lewissbaker.opensource at gmail dot com @ 2020-10-16  1:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97452
           Summary: [coroutines] incorrect sequencing of await_resume()
                    when multiple co_await expressions occur in a single
                    statement
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lewissbaker.opensource at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/9Kj9o8

Compile the following program with GCC trunk and flags: -std=c++20

----
#include <coroutine>
#include <exception>
#include <utility>
#include <cassert>
#include <cstdio>

struct task {
    struct promise_type {
        task get_return_object() noexcept {
            return
task{std::coroutine_handle<promise_type>::from_promise(*this)};
        }
        std::suspend_always initial_suspend() noexcept {
            std::puts("task initial_suspend");
            return {};
        }

        struct yield_awaiter {
            bool await_ready() noexcept { return false; }
            auto await_suspend(std::coroutine_handle<promise_type> h) noexcept
{
                std::puts("task yielding/returning value");
                return std::exchange(h.promise().continuation, {});
            }
            void await_resume() noexcept {
                std::puts("task resumed");
            }
        };

        yield_awaiter yield_value(int x) noexcept {
            value = x;
            return {};
        }

        void return_value(int x) noexcept {
            value = x;
        }

        yield_awaiter final_suspend() noexcept {
            return {};
        }

        [[noreturn]] void unhandled_exception() noexcept {
            std::terminate();
        }

        int value;
        std::coroutine_handle<> continuation;
    };

    explicit task(std::coroutine_handle<promise_type> coro) noexcept
    : coro(coro)
    {}

    task(task&& t) noexcept
    : coro(std::exchange(t.coro, {}))
    {}

    ~task() {
        if (coro) coro.destroy();
    }

    __attribute__((noinline)) bool await_ready() {
        std::puts("task::await_ready");
        return false;
    }

    __attribute__((noinline)) auto await_suspend(std::coroutine_handle<> h)
noexcept {
        std::puts("task::await_suspend");
        coro.promise().continuation = h;
        return coro;
    }

    __attribute__((noinline)) int await_resume() {
        std::puts("task::await_resume");
        return coro.promise().value;
    }

    std::coroutine_handle<promise_type> coro;
};

task two_values() {
    co_yield 1;
    co_return 2;
}

task example() {
    task t = two_values();
    int result = co_await t + co_await t;
    std::printf("result = %i (should be 3)\n", result);
    std::fflush(stdout);
    co_return result;
}

int main() {
    task t = example();
    t.coro.resume();
    assert(t.coro.done());
}
----

Expected output:
----
task initial_suspend
task initial_suspend
task::await_ready
task::await_suspend
task yielding/returning value
task::await_resume
task::await_ready
task::await_suspend
task resumed
task yielding/returning value
task::await_resume
result = 3 (should be 3)

Actual output:
----
task initial_suspend
task initial_suspend
task::await_ready
task::await_suspend
task yielding/returning value
task::await_ready
task::await_suspend
task resumed
task yielding/returning value
task::await_resume
task::await_resume
result = 4 (should be 3)


Note that the output indicates the the compiler is only calling await_resume()
on both awaiters immediately before evaluating the plus-expression rather than
evaluating await_resume() on the first awaiter immediately upon resuming from
the first suspend-point and before evaluating await_ready() for the second
awaiter.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
@ 2020-10-16  6:55 ` iains at gcc dot gnu.org
  2020-10-16  7:04 ` iains at gcc dot gnu.org
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: iains at gcc dot gnu.org @ 2020-10-16  6:55 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |10.3
           Assignee|unassigned at gcc dot gnu.org      |iains at gcc dot gnu.org
   Last reconfirmed|                            |2020-10-16

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
  2020-10-16  6:55 ` [Bug c++/97452] " iains at gcc dot gnu.org
@ 2020-10-16  7:04 ` iains at gcc dot gnu.org
  2020-10-17  3:15 ` davidledger at live dot com.au
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: iains at gcc dot gnu.org @ 2020-10-16  7:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Iain Sandoe <iains at gcc dot gnu.org> ---
probably a dup of 97433.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
  2020-10-16  6:55 ` [Bug c++/97452] " iains at gcc dot gnu.org
  2020-10-16  7:04 ` iains at gcc dot gnu.org
@ 2020-10-17  3:15 ` davidledger at live dot com.au
  2020-10-17  7:30 ` iains at gcc dot gnu.org
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: davidledger at live dot com.au @ 2020-10-17  3:15 UTC (permalink / raw)
  To: gcc-bugs

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

David Ledger <davidledger at live dot com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |davidledger at live dot com.au

--- Comment #2 from David Ledger <davidledger at live dot com.au> ---
I'm happy to use this thread for the issue, I can just repost my link to the
same issue here.

My reporting of the issue is here, but Lewis Bakers example is seperate.

https://stackoverflow.com/questions/64348125/c20-coroutines-unexpected-reordering-of-await-resume-return-value-and-yield

Is there anything I can do to help this issue get resolved? I've been throwing
things at the wall trying to get something to stick (for a work around) but so
far nothing helps with this particular case.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (2 preceding siblings ...)
  2020-10-17  3:15 ` davidledger at live dot com.au
@ 2020-10-17  7:30 ` iains at gcc dot gnu.org
  2020-10-29  6:11 ` davidledger at live dot com.au
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: iains at gcc dot gnu.org @ 2020-10-17  7:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to David Ledger from comment #2)
> I'm happy to use this thread for the issue, I can just repost my link to the
> same issue here.
> 
> My reporting of the issue is here, but Lewis Bakers example is seperate.
> 
> https://stackoverflow.com/questions/64348125/c20-coroutines-unexpected-
> reordering-of-await-resume-return-value-and-yield
> 
> Is there anything I can do to help this issue get resolved? I've been
> throwing things at the wall trying to get something to stick (for a work
> around) but so far nothing helps with this particular case.

I have reproduced the problem(s) - the next thing is to do some analysis of the
generated code and try to figure out where the bug is.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (3 preceding siblings ...)
  2020-10-17  7:30 ` iains at gcc dot gnu.org
@ 2020-10-29  6:11 ` davidledger at live dot com.au
  2020-10-29  7:52 ` iains at gcc dot gnu.org
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: davidledger at live dot com.au @ 2020-10-29  6:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from David Ledger <davidledger at live dot com.au> ---
@Iain Sandoe
In terms of the standard do you think this is technically undefined behaviour?
I tried bring this up with Std-Proposals but got no response at all.

I believe either I'm very bad at writing (likely), or nobody knows whether or
not this is undefined behaviour and therefore the conversation didn't start. If
this is undefined behaviour I believe a defect report is in order.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (4 preceding siblings ...)
  2020-10-29  6:11 ` davidledger at live dot com.au
@ 2020-10-29  7:52 ` iains at gcc dot gnu.org
  2020-12-02  9:01 ` davidledger at live dot com.au
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: iains at gcc dot gnu.org @ 2020-10-29  7:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to David Ledger from comment #4)
> @Iain Sandoe
> In terms of the standard do you think this is technically undefined
> behaviour?

no, AFAICT, it's just a regular bug in the implementation.
(it's just a question of finding resources to deal it).

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (5 preceding siblings ...)
  2020-10-29  7:52 ` iains at gcc dot gnu.org
@ 2020-12-02  9:01 ` davidledger at live dot com.au
  2020-12-02  9:02 ` iains at gcc dot gnu.org
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: davidledger at live dot com.au @ 2020-12-02  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from David Ledger <davidledger at live dot com.au> ---
Is this the right place for me to track this bug?

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (6 preceding siblings ...)
  2020-12-02  9:01 ` davidledger at live dot com.au
@ 2020-12-02  9:02 ` iains at gcc dot gnu.org
  2021-04-08 12:02 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: iains at gcc dot gnu.org @ 2020-12-02  9:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to David Ledger from comment #6)
> Is this the right place for me to track this bug?

yes - it's just waiting for someone to have time to address it.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (7 preceding siblings ...)
  2020-12-02  9:02 ` iains at gcc dot gnu.org
@ 2021-04-08 12:02 ` rguenth at gcc dot gnu.org
  2021-04-09 10:23 ` lewissbaker.opensource at gmail dot com
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-08 12:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.3                        |10.4

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 10.3 is being released, retargeting bugs to GCC 10.4.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (8 preceding siblings ...)
  2021-04-08 12:02 ` rguenth at gcc dot gnu.org
@ 2021-04-09 10:23 ` lewissbaker.opensource at gmail dot com
  2021-05-04 12:31 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: lewissbaker.opensource at gmail dot com @ 2021-04-09 10:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Lewis Baker <lewissbaker.opensource at gmail dot com> ---
> In terms of the standard do you think this is technically undefined behaviour?

Yes, I think this is something that Gor was looking into as a wording issue
that could do with some clarification.

I think the suggestion was something along the lines of adding some wording to
ensure that the evaluation of a an await-expression was sequenced atomically
with respect to the evaluation of other expressions in the statement.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (9 preceding siblings ...)
  2021-04-09 10:23 ` lewissbaker.opensource at gmail dot com
@ 2021-05-04 12:31 ` rguenth at gcc dot gnu.org
  2022-06-28 10:42 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-05-04 12:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (10 preceding siblings ...)
  2021-05-04 12:31 ` rguenth at gcc dot gnu.org
@ 2022-06-28 10:42 ` jakub at gcc dot gnu.org
  2022-11-12  3:31 ` davidledger at live dot com.au
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-28 10:42 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |10.5

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 10.4 is being released, retargeting bugs to GCC 10.5.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (11 preceding siblings ...)
  2022-06-28 10:42 ` jakub at gcc dot gnu.org
@ 2022-11-12  3:31 ` davidledger at live dot com.au
  2023-01-19 14:08 ` davidledger at live dot com.au
  2023-07-07  9:10 ` rguenth at gcc dot gnu.org
  14 siblings, 0 replies; 16+ messages in thread
From: davidledger at live dot com.au @ 2022-11-12  3:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from David Ledger <davidledger at live dot com.au> ---
This did not occur with GCC 10.2, it started in GCC 10.3:

10.3 (https://godbolt.org/z/jrdv31M17):
```
0x15d1ed3 A
0x15d1ed2 ~A
0x15d1ed3 ~A
```

10.2 (https://godbolt.org/z/rrvKh9h6K):
```
0x2322ed1 A
0x2322ed1 ~A
```

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (12 preceding siblings ...)
  2022-11-12  3:31 ` davidledger at live dot com.au
@ 2023-01-19 14:08 ` davidledger at live dot com.au
  2023-07-07  9:10 ` rguenth at gcc dot gnu.org
  14 siblings, 0 replies; 16+ messages in thread
From: davidledger at live dot com.au @ 2023-01-19 14:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from David Ledger <davidledger at live dot com.au> ---
(In reply to David Ledger from comment #11)
> This did not occur with GCC 10.2, it started in GCC 10.3:
> 
> 10.3 (https://godbolt.org/z/jrdv31M17):
> ```
> 0x15d1ed3 A
> 0x15d1ed2 ~A
> 0x15d1ed3 ~A
> ```
> 
> 10.2 (https://godbolt.org/z/rrvKh9h6K):
> ```
> 0x2322ed1 A
> 0x2322ed1 ~A
> ```

Please ignore, responded to incorrect thread.

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

* [Bug c++/97452] [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
  2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
                   ` (13 preceding siblings ...)
  2023-01-19 14:08 ` davidledger at live dot com.au
@ 2023-07-07  9:10 ` rguenth at gcc dot gnu.org
  14 siblings, 0 replies; 16+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07  9:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.5                        |---

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

end of thread, other threads:[~2023-07-07  9:10 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16  1:40 [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement lewissbaker.opensource at gmail dot com
2020-10-16  6:55 ` [Bug c++/97452] " iains at gcc dot gnu.org
2020-10-16  7:04 ` iains at gcc dot gnu.org
2020-10-17  3:15 ` davidledger at live dot com.au
2020-10-17  7:30 ` iains at gcc dot gnu.org
2020-10-29  6:11 ` davidledger at live dot com.au
2020-10-29  7:52 ` iains at gcc dot gnu.org
2020-12-02  9:01 ` davidledger at live dot com.au
2020-12-02  9:02 ` iains at gcc dot gnu.org
2021-04-08 12:02 ` rguenth at gcc dot gnu.org
2021-04-09 10:23 ` lewissbaker.opensource at gmail dot com
2021-05-04 12:31 ` rguenth at gcc dot gnu.org
2022-06-28 10:42 ` jakub at gcc dot gnu.org
2022-11-12  3:31 ` davidledger at live dot com.au
2023-01-19 14:08 ` davidledger at live dot com.au
2023-07-07  9:10 ` rguenth 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).