1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O1 -emit-llvm %s -o - -disable-llvm-passes | FileCheck %s
3 #include "Inputs/coroutine.h"
7 Task
get_return_object() noexcept
{
8 return Task
{std::coroutine_handle
<promise_type
>::from_promise(*this)};
11 void return_void() noexcept
{}
13 struct final_awaiter
{
14 bool await_ready() noexcept
{ return false; }
15 std::coroutine_handle
<> await_suspend(std::coroutine_handle
<promise_type
> h
) noexcept
{
19 void await_resume() noexcept
{}
22 void unhandled_exception() noexcept
{}
24 final_awaiter
final_suspend() noexcept
{ return {}; }
26 std::suspend_always
initial_suspend() noexcept
{ return {}; }
28 template <typename Awaitable
>
29 auto await_transform(Awaitable
&&awaitable
) {
30 return awaitable
.co_viaIfAsync();
34 using handle_t
= std::coroutine_handle
<promise_type
>;
38 explicit Awaiter(handle_t coro
) noexcept
;
39 Awaiter(Awaiter
&&other
) noexcept
;
40 Awaiter(const Awaiter
&) = delete;
43 bool await_ready() noexcept
{ return false; }
44 handle_t
await_suspend(std::coroutine_handle
<> continuation
) noexcept
;
51 Task(handle_t coro
) noexcept
: coro_(coro
) {}
55 Task(const Task
&t
) = delete;
56 Task(Task
&&t
) noexcept
;
58 Task
&operator=(Task t
) noexcept
;
60 Awaiter
co_viaIfAsync();
81 // CHECK-LABEL: define{{.*}} void @_Z3barv
82 // CHECK: %[[MODE:.+]] = load i32, ptr %mode
83 // CHECK-NEXT: switch i32 %[[MODE]], label %{{.+}} [
84 // CHECK-NEXT: i32 1, label %[[CASE1:.+]]
85 // CHECK-NEXT: i32 2, label %[[CASE2:.+]]
89 // CHECK: br i1 %{{.+}}, label %[[CASE1_AWAIT_READY:.+]], label %[[CASE1_AWAIT_SUSPEND:.+]]
90 // CHECK: [[CASE1_AWAIT_SUSPEND]]:
91 // CHECK-NEXT: %{{.+}} = call token @llvm.coro.save(ptr null)
92 // CHECK-NEXT: call void @llvm.coro.await.suspend.handle
93 // CHECK-NEXT: %{{.+}} = call i8 @llvm.coro.suspend
94 // CHECK-NEXT: switch i8 %{{.+}}, label %coro.ret [
95 // CHECK-NEXT: i8 0, label %[[CASE1_AWAIT_READY]]
96 // CHECK-NEXT: i8 1, label %[[CASE1_AWAIT_CLEANUP:.+]]
98 // CHECK: [[CASE1_AWAIT_CLEANUP]]:
99 // make sure that the awaiter eventually gets cleaned up.
100 // CHECK: call void @{{.+Awaiter.+}}
103 // CHECK: br i1 %{{.+}}, label %[[CASE2_AWAIT_READY:.+]], label %[[CASE2_AWAIT_SUSPEND:.+]]
104 // CHECK: [[CASE2_AWAIT_SUSPEND]]:
105 // CHECK-NEXT: %{{.+}} = call token @llvm.coro.save(ptr null)
106 // CHECK-NEXT: call void @llvm.coro.await.suspend.handle
107 // CHECK-NEXT: %{{.+}} = call i8 @llvm.coro.suspend
108 // CHECK-NEXT: switch i8 %{{.+}}, label %coro.ret [
109 // CHECK-NEXT: i8 0, label %[[CASE2_AWAIT_READY]]
110 // CHECK-NEXT: i8 1, label %[[CASE2_AWAIT_CLEANUP:.+]]
112 // CHECK: [[CASE2_AWAIT_CLEANUP]]:
113 // make sure that the awaiter eventually gets cleaned up.
114 // CHECK: call void @{{.+Awaiter.+}}