Debian Codegraph @

clang/test/CIR/CodeGen/coro-task.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3
4 namespace std {
5
6 template<typename T> struct remove_reference { typedef T type; };
7 template<typename T> struct remove_reference<T &> { typedef T type; };
8 template<typename T> struct remove_reference<T &&> { typedef T type; };
9
10 template<typename T>
11 typename remove_reference<T>::type &&move(T &&t) noexcept;
12
13 template <class Ret, typename... T>
14 struct coroutine_traits { using promise_type = typename Ret::promise_type; };
15
16 template <class Promise = void>
17 struct coroutine_handle {
18 static coroutine_handle from_address(void *) noexcept;
19 };
20 template <>
21 struct coroutine_handle<void> {
22 template <class PromiseType>
23 coroutine_handle(coroutine_handle<PromiseType>) noexcept;
24 static coroutine_handle from_address(void *);
25 };
26
27 struct suspend_always {
28 bool await_ready() noexcept { return false; }
29 void await_suspend(coroutine_handle<>) noexcept {}
30 void await_resume() noexcept {}
31 };
32
33 struct suspend_never {
34 bool await_ready() noexcept { return true; }
35 void await_suspend(coroutine_handle<>) noexcept {}
36 void await_resume() noexcept {}
37 };
38
39 struct string {
40 int size() const;
41 string();
42 string(char const *s);
43 };
44
45 template<typename T>
46 struct optional {
47 optional();
48 optional(const T&);
49 T &operator*() &;
50 T &&operator*() &&;
51 T &value() &;
52 T &&value() &&;
53 };
54 } // namespace std
55
56 namespace folly {
57 namespace coro {
58
59 using std::suspend_always;
60 using std::suspend_never;
61 using std::coroutine_handle;
62
63 using SemiFuture = int;
64
65 template<class T>
66 struct Task {
67 struct promise_type {
68 Task<T> get_return_object() noexcept;
69 suspend_always initial_suspend() noexcept;
70 suspend_always final_suspend() noexcept;
71 void return_value(T);
72 void unhandled_exception();
73 auto yield_value(Task<T>) noexcept { return final_suspend(); }
74 };
75 bool await_ready() noexcept { return false; }
76 void await_suspend(coroutine_handle<>) noexcept {}
77 T await_resume();
78 };
79
80 template<>
81 struct Task<void> {
82 struct promise_type {
83 Task<void> get_return_object() noexcept;
84 suspend_always initial_suspend() noexcept;
85 suspend_always final_suspend() noexcept;
86 void return_void() noexcept;
87 void unhandled_exception() noexcept;
88 auto yield_value(Task<void>) noexcept { return final_suspend(); }
89 };
90 bool await_ready() noexcept { return false; }
91 void await_suspend(coroutine_handle<>) noexcept {}
92 void await_resume() noexcept {}
93 SemiFuture semi();
94 };
95
96 // FIXME: add CIRGen support here.
97 // struct blocking_wait_fn {
98 // template <typename T>
99 // T operator()(Task<T>&& awaitable) const {
100 // return T();
101 // }
102 // };
103
104 // inline constexpr blocking_wait_fn blocking_wait{};
105 // static constexpr blocking_wait_fn const& blockingWait = blocking_wait;
106 template <typename T>
107 T blockingWait(Task<T>&& awaitable) {
108 return T();
109 }
110
111 struct co_invoke_fn {
112 template <typename F, typename... A>
113 Task<void> operator()(F&& f, A&&... a) const {
114 return Task<void>();
115 }
116 };
117
118 co_invoke_fn co_invoke;
119
120 }} // namespace folly::coro
121
122 // CIR-DAG: ![[VoidTask:.*]] = !cir.record<struct "folly::coro::Task<void>" padded {!u8i}>
123 // CIR-DAG: ![[IntTask:.*]] = !cir.record<struct "folly::coro::Task<int>" padded {!u8i}>
124 // CIR-DAG: ![[VoidPromisse:.*]] = !cir.record<struct "folly::coro::Task<void>::promise_type" padded {!u8i}>
125 // CIR-DAG: ![[IntPromisse:.*]] = !cir.record<struct "folly::coro::Task<int>::promise_type" padded {!u8i}>
126 // CIR-DAG: ![[StdString:.*]] = !cir.record<struct "std::string" padded {!u8i}>
127 // CIR-DAG: ![[CoroHandleVoid:.*]] = !cir.record<struct "std::coroutine_handle<void>" padded {!u8i}>
128 // CIR-DAG: ![[CoroHandlePromiseVoid:rec_.*]] = !cir.record<struct "std::coroutine_handle<folly::coro::Task<void>::promise_type>" padded {!u8i}>
129 // CIR-DAG: ![[CoroHandlePromiseInt:rec_.*]] = !cir.record<struct "std::coroutine_handle<folly::coro::Task<int>::promise_type>" padded {!u8i}>
130 // CIR-DAG: ![[SuspendAlways:.*]] = !cir.record<struct "std::suspend_always" padded {!u8i}>
131
132 // CIR: module {{.*}} {
133 // CIR-NEXT: cir.global external @_ZN5folly4coro9co_invokeE = #cir.zero : !rec_folly3A3Acoro3A3Aco_invoke_fn
134
135 // CIR: cir.func builtin private @__builtin_coro_id(!u32i, !cir.ptr<!void>, !cir.ptr<!void>, !cir.ptr<!void>) -> !u32i
136 // CIR: cir.func builtin private @__builtin_coro_alloc(!u32i) -> !cir.bool
137 // CIR: cir.func builtin private @__builtin_coro_size() -> !u64i
138 // CIR: cir.func builtin private @__builtin_coro_begin(!u32i, !cir.ptr<!void>) -> !cir.ptr<!void>
139
140 using VoidTask = folly::coro::Task<void>;
141
142 VoidTask silly_task() {
143 co_await std::suspend_always();
144 }
145
146 // CIR: cir.func coroutine {{.*}} @_Z10silly_taskv() -> ![[VoidTask]]
147 // CIR: %[[VoidTaskAddr:.*]] = cir.alloca ![[VoidTask]], {{.*}}, ["__retval"]
148 // CIR: %[[SavedFrameAddr:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["__coro_frame_addr"]
149 // CIR: %[[VoidPromisseAddr:.*]] = cir.alloca ![[VoidPromisse]], {{.*}}, ["__promise"]
150
151 // Get coroutine id with __builtin_coro_id.
152
153 // CIR: %[[NullPtr:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!void>
154 // CIR: %[[Align:.*]] = cir.const #cir.int<16> : !u32i
155 // CIR: %[[CoroId:.*]] = cir.call @__builtin_coro_id(%[[Align]], %[[NullPtr]], %[[NullPtr]], %[[NullPtr]])
156
157 // Perform allocation calling operator 'new' depending on __builtin_coro_alloc and
158 // call __builtin_coro_begin for the final coroutine frame address.
159
160 // CIR: %[[ShouldAlloc:.*]] = cir.call @__builtin_coro_alloc(%[[CoroId]]) : (!u32i) -> !cir.bool
161 // CIR: cir.store{{.*}} %[[NullPtr]], %[[SavedFrameAddr]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
162 // CIR: cir.if %[[ShouldAlloc]] {
163 // CIR: %[[CoroSize:.*]] = cir.call @__builtin_coro_size() : () -> !u64i
164 // CIR: %[[AllocAddr:.*]] = cir.call @_Znwm(%[[CoroSize]]) : (!u64i) -> !cir.ptr<!void>
165 // CIR: cir.store{{.*}} %[[AllocAddr]], %[[SavedFrameAddr]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
166 // CIR: }
167 // CIR: %[[Load0:.*]] = cir.load{{.*}} %[[SavedFrameAddr]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
168 // CIR: %[[CoroFrameAddr:.*]] = cir.call @__builtin_coro_begin(%[[CoroId]], %[[Load0]])
169
170 // Call promise.get_return_object() to retrieve the task object.
171
172 // CIR: %[[RetObj:.*]] = cir.call @_ZN5folly4coro4TaskIvE12promise_type17get_return_objectEv(%[[VoidPromisseAddr]]) nothrow : {{.*}} -> ![[VoidTask]]
173 // CIR: cir.store{{.*}} %[[RetObj]], %[[VoidTaskAddr]] : ![[VoidTask]]
174 // Start a new scope for the actual codegen for co_await, create temporary allocas for
175 // holding coroutine handle and the suspend_always struct.
176
177 // CIR: cir.scope {
178 // CIR: %[[SuspendAlwaysAddr:.*]] = cir.alloca ![[SuspendAlways]], {{.*}} ["ref.tmp0"] {alignment = 1 : i64}
179 // CIR: %[[CoroHandleVoidAddr:.*]] = cir.alloca ![[CoroHandleVoid]], {{.*}} ["agg.tmp0"] {alignment = 1 : i64}
180 // CIR: %[[CoroHandlePromiseAddr:.*]] = cir.alloca ![[CoroHandlePromiseVoid]], {{.*}} ["agg.tmp1"] {alignment = 1 : i64}
181
182 // Effectively execute `coawait promise_type::initial_suspend()` by calling initial_suspend() and getting
183 // the suspend_always struct to use for cir.await. Note that we return by-value since we defer ABI lowering
184 // to later passes, same is done elsewhere.
185
186 // CIR: %[[Tmp0:.*]] = cir.call @_ZN5folly4coro4TaskIvE12promise_type15initial_suspendEv(%[[VoidPromisseAddr]])
187 // CIR: cir.store{{.*}} %[[Tmp0:.*]], %[[SuspendAlwaysAddr]]
188
189 //
190 // Here we start mapping co_await to cir.await.
191 //
192
193 // First regions `ready` has a special cir.yield code to veto suspension.
194
195 // CIR: cir.await(init, ready : {
196 // CIR: %[[ReadyVeto:.*]] = cir.scope {
197 // CIR: %[[TmpCallRes:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SuspendAlwaysAddr]])
198 // CIR: cir.yield %[[TmpCallRes:.*]] : !cir.bool
199 // CIR: }
200 // CIR: cir.condition(%[[ReadyVeto]])
201
202 // Second region `suspend` contains the actual suspend logic.
203 //
204 // - Start by getting the coroutine handle using from_address().
205 // - Implicit convert coroutine handle from task specific promisse
206 // specialization to a void one.
207 // - Call suspend_always::await_suspend() passing the handle.
208 //
209 // FIXME: add veto support for non-void await_suspends.
210
211 // CIR: }, suspend : {
212 // CIR: %[[FromAddrRes:.*]] = cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%[[CoroFrameAddr]])
213 // CIR: cir.store{{.*}} %[[FromAddrRes]], %[[CoroHandlePromiseAddr]] : ![[CoroHandlePromiseVoid]]
214 // CIR: %[[CoroHandlePromiseReload:.*]] = cir.load{{.*}} %[[CoroHandlePromiseAddr]]
215 // CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CoroHandleVoidAddr]], %[[CoroHandlePromiseReload]])
216 // CIR: %[[CoroHandleVoidReload:.*]] = cir.load{{.*}} %[[CoroHandleVoidAddr]] : !cir.ptr<![[CoroHandleVoid]]>, ![[CoroHandleVoid]]
217 // CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SuspendAlwaysAddr]], %[[CoroHandleVoidReload]])
218 // CIR: cir.yield
219
220 // Third region `resume` handles coroutine resuming logic.
221
222 // CIR: }, resume : {
223 // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SuspendAlwaysAddr]])
224 // CIR: cir.yield
225 // CIR: },)
226 // CIR: }
227
228 // Since we already tested cir.await guts above, the remaining checks for:
229 // - The actual user written co_await
230 // - The promise call
231 // - The final suspend co_await
232 // - Return
233
234 // The actual user written co_await
235 // CIR: cir.scope {
236 // CIR: cir.await(user, ready : {
237 // CIR: }, suspend : {
238 // CIR: }, resume : {
239 // CIR: },)
240 // CIR: }
241
242 // The promise call
243 // CHECK: cir.call @_ZN5folly4coro4TaskIvE12promise_type11return_voidEv(%[[VoidPromisseAddr]])
244
245 // The final suspend co_await
246 // CIR: cir.scope {
247 // CIR: cir.await(final, ready : {
248 // CIR: }, suspend : {
249 // CIR: }, resume : {
250 // CIR: },)
251 // CIR: }
252
253 folly::coro::Task<int> byRef(const std::string& s) {
254 co_return s.size();
255 }
256
257 // CIR: cir.func coroutine {{.*}} @_Z5byRefRKSt6string(%[[ARG:.*]]: !cir.ptr<![[StdString]]> {{.*}}) -> ![[IntTask]]
258 // CIR: %[[AllocaParam:.*]] = cir.alloca !cir.ptr<![[StdString]]>, {{.*}}, ["s", init, const]
259 // CIR: %[[IntTaskAddr:.*]] = cir.alloca ![[IntTask]], {{.*}}, ["__retval"]
260 // CIR: %[[SavedFrameAddr:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["__coro_frame_addr"]
261 // CIR: %[[AllocaFnUse:.*]] = cir.alloca !cir.ptr<![[StdString]]>, {{.*}}, ["s", init, const]
262 // CIR: %[[IntPromisseAddr:.*]] = cir.alloca ![[IntPromisse]], {{.*}}, ["__promise"]
263 // CIR: cir.store %[[ARG]], %[[AllocaParam]] : !cir.ptr<![[StdString]]>, {{.*}}
264
265 // Call promise.get_return_object() to retrieve the task object.
266
267 // CIR: %[[LOAD:.*]] = cir.load %[[AllocaParam]] : !cir.ptr<!cir.ptr<![[StdString]]>>, !cir.ptr<![[StdString]]>
268 // CIR: cir.store {{.*}} %[[LOAD]], %[[AllocaFnUse]] : !cir.ptr<![[StdString]]>, !cir.ptr<!cir.ptr<![[StdString]]>>
269 // CIR: %[[RetObj:.*]] = cir.call @_ZN5folly4coro4TaskIiE12promise_type17get_return_objectEv(%4) nothrow : {{.*}} -> ![[IntTask]]
270 // CIR: cir.store {{.*}} %[[RetObj]], %[[IntTaskAddr]] : ![[IntTask]]
271 // CIR: cir.scope {
272 // CIR: %[[SuspendAlwaysAddr:.*]] = cir.alloca ![[SuspendAlways]], {{.*}} ["ref.tmp0"] {alignment = 1 : i64}
273 // CIR: %[[CoroHandleVoidAddr:.*]] = cir.alloca ![[CoroHandleVoid]], {{.*}} ["agg.tmp0"] {alignment = 1 : i64}
274 // CIR: %[[CoroHandlePromiseAddr:.*]] = cir.alloca ![[CoroHandlePromiseInt]], {{.*}} ["agg.tmp1"] {alignment = 1 : i64}
275 // CIR: %[[Tmp0:.*]] = cir.call @_ZN5folly4coro4TaskIiE12promise_type15initial_suspendEv(%[[IntPromisseAddr]])
276 // CIR: cir.await(init, ready : {
277 // CIR: %[[ReadyVeto:.*]] = cir.scope {
278 // CIR: %[[TmpCallRes:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SuspendAlwaysAddr]])
279 // CIR: cir.yield %[[TmpCallRes:.*]] : !cir.bool
280 // CIR: }
281 // CIR: cir.condition(%[[ReadyVeto]])
282 // CIR: }, suspend : {
283 // CIR: %[[FromAddrRes:.*]] = cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIiE12promise_typeEE12from_addressEPv(%[[CoroFrameAddr:.*]])
284 // CIR: cir.store{{.*}} %[[FromAddrRes]], %[[CoroHandlePromiseAddr]] : ![[CoroHandlePromiseInt]]
285 // CIR: %[[CoroHandlePromiseReload:.*]] = cir.load{{.*}} %[[CoroHandlePromiseAddr]]
286 // CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIiE12promise_typeEEES_IT_E(%[[CoroHandleVoidAddr]], %[[CoroHandlePromiseReload]])
287 // CIR: %[[CoroHandleVoidReload:.*]] = cir.load{{.*}} %[[CoroHandleVoidAddr]] : !cir.ptr<![[CoroHandleVoid]]>, ![[CoroHandleVoid]]
288 // CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SuspendAlwaysAddr]], %[[CoroHandleVoidReload]])
289 // CIR: cir.yield
290 // CIR: }, resume : {
291 // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SuspendAlwaysAddr]])
292 // CIR: cir.yield
293 // CIR: },)
294 // CIR: }
295
296 // can't fallthrough
297 // CIR-NOT: cir.await(user
298
299 // The final suspend co_await
300 // CIR: cir.scope {
301 // CIR: cir.await(final, ready : {
302 // CIR: }, suspend : {
303 // CIR: }, resume : {
304 // CIR: },)
305 // CIR: }
306
307 folly::coro::Task<void> silly_coro() {
308 std::optional<folly::coro::Task<int>> task;
309 {
310 std::string s = "yolo";
311 task = byRef(s);
312 }
313 folly::coro::blockingWait(std::move(task.value()));
314 co_return;
315 }
316
317 // Make sure we properly handle OnFallthrough coro body sub stmt and
318 // check there are not multiple co_returns emitted.
319
320 // CIR: cir.func coroutine {{.*}} @_Z10silly_corov() {{.*}} ![[VoidTask]]
321 // CIR: cir.await(init, ready : {
322 // CIR: cir.call @_ZN5folly4coro4TaskIvE12promise_type11return_voidEv
323 // CIR-NOT: cir.call @_ZN5folly4coro4TaskIvE12promise_type11return_voidEv
324 // CIR: cir.await(final, ready : {