1 // An end-to-end test to make sure things get processed correctly.
2 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s -O3 | \
5 #include "Inputs/coroutine.h"
8 // Resume the supplied handle once the awaitable becomes ready,
9 // returning a handle that should be resumed now for the sake of symmetric transfer.
10 // If the awaitable is already ready, return an empty handle without doing anything.
12 // Defined in another translation unit. Note that this may contain
13 // code that synchronizees with another thread.
14 std::coroutine_handle
<> Register(std::coroutine_handle
<>);
17 // Defined in another translation unit.
21 SomeAwaitable
&& awaitable
;
24 bool await_ready() { return false; }
26 std::coroutine_handle
<> await_suspend(const std::coroutine_handle
<> h
) {
27 // Assume we will suspend unless proven otherwise below. We must do
28 // this *before* calling Register, since we may be destroyed by another
29 // thread asynchronously as soon as we have registered.
32 // Attempt to hand off responsibility for resuming/destroying the coroutine.
33 const auto to_resume
= awaitable
.Register(h
);
36 // The awaitable is already ready. In this case we know that Register didn't
37 // hand off responsibility for the coroutine. So record the fact that we didn't
38 // actually suspend, and tell the compiler to resume us inline.
43 // Resume whatever Register wants us to resume.
48 // If we didn't suspend, make note of that fact.
57 MyTask
get_return_object() { return {}; }
58 std::suspend_never
initial_suspend() { return {}; }
59 std::suspend_always
final_suspend() noexcept
{ return {}; }
60 void unhandled_exception();
62 Awaiter
await_transform(SomeAwaitable
&& awaitable
) {
63 return Awaiter
{static_cast<SomeAwaitable
&&>(awaitable
)};
69 co_await
SomeAwaitable();
72 // CHECK-LABEL: @_Z6FooBarv
73 // CHECK: %[[to_resume:.*]] = {{.*}}call ptr @_ZN13SomeAwaitable8RegisterESt16coroutine_handleIvE
74 // CHECK-NEXT: %[[to_bool:.*]] = icmp eq ptr %[[to_resume]], null
75 // CHECK-NEXT: br i1 %[[to_bool]], label %[[then:.*]], label %[[else:.*]]
78 // We only access the coroutine frame conditionally as the sources did.
80 // CHECK-NEXT: br label %[[else]]
83 // No more access to the coroutine frame until suspended.