<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 - Coroutine suspend/resume in loop not always optimized away"
   href="https://bugs.llvm.org/show_bug.cgi?id=35405">35405</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Coroutine suspend/resume in loop not always optimized away
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>strager@fb.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>It seems like Clang+LLVM has some trouble optimizing my code. In a loop, I am
suspending a coroutine then calling
std::experimental::coroutine_handle::resume. See main_v0_loop in the program
below.

I expect Clang+LLVM to optimize the loop away completely, but the generated
code repeatedly suspends and resumes the coroutine.

Clang+LLVM *is* able to properly optimize similar loops. It also is able to
properly optimize the loop if I unroll the loop, but not always. See the other
main_* functions in the program below.

Let me know if (and how) I should reduce the program further.

Program:
<a href="https://godbolt.org/g/m2YSsW">https://godbolt.org/g/m2YSsW</a>

main_v0_loop: gen_pop-push 1000 times, then gen_pop once (suspending the
coroutine forever). Conceptually, the gen_pop-s are done asynchronously on a
different "thread".
main_v1_loop: push-gen_pop 1000 times. Conceptually, the gen_pop-s are done
synchronously on the main "thread".
main_v2_loop: push-pop 1000 times.
main_v3_loop: push-gen_pop 1000 times, then gen_pop once (suspending the
coroutine forever). Conceptually, the gen_pop-s are done asynchronously on a
different "thread".

main_v0_single: Like main_v0_loop, but iterate only once and manually unroll
the loop.
main_v1_single: Like main_v1_loop, but iterate only once and manually unroll
the loop.
main_v2_single: Like main_v2_loop, but iterate only once and manually unroll
the loop.
main_v3_single: Like main_v3_loop, but iterate only once and manually unroll
the loop.

main_v0_triple: Like main_v0_loop, but iterate only 3 times and manually unroll
the loop.
main_v1_triple: Like main_v1_loop, but iterate only 3 times and manually unroll
the loop.
main_v2_triple: Like main_v2_loop, but iterate only 3 times and manually unroll
the loop.
main_v3_triple: Like main_v3_loop, but iterate only 3 times and manually unroll
the loop.

Clang+LLVM version: 6.0.0 trunk 318899 (according to Godbolt's Compiler
Explorer <<a href="https://godbolt.org/">https://godbolt.org/</a>>)

Clang driver flags: -std=c++17 -fcoroutines-ts -stdlib=libc++ -O2 -DNDEBUG=1
-fno-exceptions

Platform: Linux x86_64 (I think; I used Godbolt's Compiler Explorer
<<a href="https://godbolt.org/">https://godbolt.org/</a>>)

Expected results:
X: The generated code for main_v0_*, main_v1_*, and main_v3_* has no calls to
operator new or operator delete (due to the coroutine created by pop_once or
keep_popping).
Y: The generated code for all functions has no indirect jumps (due to the call
to coroutine_handle::resume).
Z: The generated code for all functions is just 'ret', since the function has
no side effects.

Observed results:
X: As expected, the generated code for all functions has no calls to operator
new or operator delete.
Y: The generated code for main_v0_loop, main_v0_triple, main_v3_loop, and
main_v3_triple has indirect jumps. The generated code for all other functions
has no indirect jumps as expected.
Z: The generated code for main_v0_loop, main_v0_triple, main_v3_loop, and
main_v3_triple is non-trivial. The generated code for all other functions is
just 'ret' as expected.</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>