<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [coroutines] Bad codegen that causes a coroutine to resume at the wrong suspend-point under -O2 and above"
   href="https://bugs.llvm.org/show_bug.cgi?id=45321">45321</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[coroutines] Bad codegen that causes a coroutine to resume at the wrong suspend-point under -O2 and above
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lewissbaker@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following godbolt contains a sample that shows correct output under clang
10 with -O1 but incorrect output under -O2 (also observed under -O3).

<a href="https://godbolt.org/z/dUn2-p">https://godbolt.org/z/dUn2-p</a>

Note that the issue does not seem to occur under current clang trunk so may
have already been fixed. Reporting here in case a fix needs to be backported to
clang 10 branch. Feel free to close if a backport to clang 10 is not required.


The code generated for the coroutine:

immediate_task task() {
    awaitable a(0);
    awaitable b(1);
    std::printf("Expected 0: %i\n", co_await a);
    std::printf("Expected 1: %i\n", co_await b);
}

When compiled with: -std=c++2a -stdlib=libc++ -O2

Suspends first at `co_await a', writing its coroutine_handle to a global var
which is then used to resume it.
However, when it suspends at `co_await b` and is subsequently resumed it
resumes at the `co_await a` expression instead of the `co_await b` expression.

Note that minor changes to the code can make it generate the correct output.
eg. changing immediate_task::promise_type::final_suspend() to return
suspend_never instead of returning an awaitable that calls .destroy() inside
the await_suspend() method.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>