blob: 0e7cde7aa38ba9457f90477128eae0d2b8fb51c4 [file] [log] [blame]
David Majnemera6539272016-07-23 04:05:08 +00001=====================================
2Coroutines in LLVM
3=====================================
4
5.. contents::
6 :local:
7 :depth: 3
8
9.. warning::
10 This is a work in progress. Compatibility across LLVM releases is not
11 guaranteed.
12
13Introduction
14============
15
16.. _coroutine handle:
17
18LLVM coroutines are functions that have one or more `suspend points`_.
19When a suspend point is reached, the execution of a coroutine is suspended and
20control is returned back to its caller. A suspended coroutine can be resumed
21to continue execution from the last suspend point or it can be destroyed.
22
23In the following example, we call function `f` (which may or may not be a
24coroutine itself) that returns a handle to a suspended coroutine
25(**coroutine handle**) that is used by `main` to resume the coroutine twice and
26then destroy it:
27
28.. code-block:: llvm
29
30 define i32 @main() {
31 entry:
32 %hdl = call i8* @f(i32 4)
33 call void @llvm.coro.resume(i8* %hdl)
34 call void @llvm.coro.resume(i8* %hdl)
35 call void @llvm.coro.destroy(i8* %hdl)
36 ret i32 0
37 }
38
39.. _coroutine frame:
40
41In addition to the function stack frame which exists when a coroutine is
42executing, there is an additional region of storage that contains objects that
43keep the coroutine state when a coroutine is suspended. This region of storage
44is called **coroutine frame**. It is created when a coroutine is called and
45destroyed when a coroutine runs to completion or destroyed by a call to
46the `coro.destroy`_ intrinsic.
47
48An LLVM coroutine is represented as an LLVM function that has calls to
49`coroutine intrinsics`_ defining the structure of the coroutine.
50After lowering, a coroutine is split into several
51functions that represent three different ways of how control can enter the
52coroutine:
53
541. a ramp function, which represents an initial invocation of the coroutine that
55 creates the coroutine frame and executes the coroutine code until it
56 encounters a suspend point or reaches the end of the function;
57
582. a coroutine resume function that is invoked when the coroutine is resumed;
59
603. a coroutine destroy function that is invoked when the coroutine is destroyed.
61
62.. note:: Splitting out resume and destroy functions are just one of the
63 possible ways of lowering the coroutine. We chose it for initial
64 implementation as it matches closely the mental model and results in
65 reasonably nice code.
66
67Coroutines by Example
68=====================
69
70Coroutine Representation
71------------------------
72
73Let's look at an example of an LLVM coroutine with the behavior sketched
74by the following pseudo-code.
75
Sanjoy Das77a9c792016-07-26 21:03:41 +000076.. code-block:: c++
David Majnemera6539272016-07-23 04:05:08 +000077
78 void *f(int n) {
79 for(;;) {
80 print(n++);
81 <suspend> // returns a coroutine handle on first suspend
82 }
83 }
84
85This coroutine calls some function `print` with value `n` as an argument and
86suspends execution. Every time this coroutine resumes, it calls `print` again with an argument one bigger than the last time. This coroutine never completes by itself and must be destroyed explicitly. If we use this coroutine with
87a `main` shown in the previous section. It will call `print` with values 4, 5
88and 6 after which the coroutine will be destroyed.
89
90The LLVM IR for this coroutine looks like this:
91
Aaron Ballman378ac7e2016-07-23 18:53:35 +000092.. code-block:: none
David Majnemera6539272016-07-23 04:05:08 +000093
94 define i8* @f(i32 %n) {
95 entry:
Gor Nishanovdce9b022016-08-29 14:34:12 +000096 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
David Majnemera6539272016-07-23 04:05:08 +000097 %size = call i32 @llvm.coro.size.i32()
98 %alloc = call i8* @malloc(i32 %size)
Gor Nishanov0f303ac2016-08-12 05:45:49 +000099 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
David Majnemera6539272016-07-23 04:05:08 +0000100 br label %loop
101 loop:
102 %n.val = phi i32 [ %n, %entry ], [ %inc, %loop ]
103 %inc = add nsw i32 %n.val, 1
104 call void @print(i32 %n.val)
105 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
106 switch i8 %0, label %suspend [i8 0, label %loop
107 i8 1, label %cleanup]
108 cleanup:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000109 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
David Majnemera6539272016-07-23 04:05:08 +0000110 call void @free(i8* %mem)
111 br label %suspend
112 suspend:
113 call void @llvm.coro.end(i8* %hdl, i1 false)
114 ret i8* %hdl
115 }
116
117The `entry` block establishes the coroutine frame. The `coro.size`_ intrinsic is
118lowered to a constant representing the size required for the coroutine frame.
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000119The `coro.begin`_ intrinsic initializes the coroutine frame and returns the
120coroutine handle. The second parameter of `coro.begin` is given a block of memory
121to be used if the coroutine frame needs to be allocated dynamically.
122The `coro.id`_ intrinsic serves as coroutine identity useful in cases when the
123`coro.begin`_ intrinsic get duplicated by optimization passes such as
124jump-threading.
David Majnemera6539272016-07-23 04:05:08 +0000125
126The `cleanup` block destroys the coroutine frame. The `coro.free`_ intrinsic,
127given the coroutine handle, returns a pointer of the memory block to be freed or
128`null` if the coroutine frame was not allocated dynamically. The `cleanup`
129block is entered when coroutine runs to completion by itself or destroyed via
130call to the `coro.destroy`_ intrinsic.
131
132The `suspend` block contains code to be executed when coroutine runs to
133completion or suspended. The `coro.end`_ intrinsic marks the point where
134a coroutine needs to return control back to the caller if it is not an initial
135invocation of the coroutine.
136
137The `loop` blocks represents the body of the coroutine. The `coro.suspend`_
138intrinsic in combination with the following switch indicates what happens to
139control flow when a coroutine is suspended (default case), resumed (case 0) or
140destroyed (case 1).
141
142Coroutine Transformation
143------------------------
144
145One of the steps of coroutine lowering is building the coroutine frame. The
146def-use chains are analyzed to determine which objects need be kept alive across
147suspend points. In the coroutine shown in the previous section, use of virtual register
148`%n.val` is separated from the definition by a suspend point, therefore, it
149cannot reside on the stack frame since the latter goes away once the coroutine
150is suspended and control is returned back to the caller. An i32 slot is
151allocated in the coroutine frame and `%n.val` is spilled and reloaded from that
152slot as needed.
153
154We also store addresses of the resume and destroy functions so that the
155`coro.resume` and `coro.destroy` intrinsics can resume and destroy the coroutine
156when its identity cannot be determined statically at compile time. For our
157example, the coroutine frame will be:
158
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +0000159.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000160
161 %f.frame = type { void (%f.frame*)*, void (%f.frame*)*, i32 }
162
163After resume and destroy parts are outlined, function `f` will contain only the
164code responsible for creation and initialization of the coroutine frame and
165execution of the coroutine until a suspend point is reached:
166
Gor Nishanovb2a9c022016-08-10 16:40:39 +0000167.. code-block:: none
David Majnemera6539272016-07-23 04:05:08 +0000168
169 define i8* @f(i32 %n) {
170 entry:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000171 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
David Majnemera6539272016-07-23 04:05:08 +0000172 %alloc = call noalias i8* @malloc(i32 24)
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000173 %0 = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
Mehdi Aminibe1cb222016-07-27 06:03:47 +0000174 %frame = bitcast i8* %0 to %f.frame*
David Majnemera6539272016-07-23 04:05:08 +0000175 %1 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 0
176 store void (%f.frame*)* @f.resume, void (%f.frame*)** %1
177 %2 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 1
178 store void (%f.frame*)* @f.destroy, void (%f.frame*)** %2
179
180 %inc = add nsw i32 %n, 1
181 %inc.spill.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 2
182 store i32 %inc, i32* %inc.spill.addr
183 call void @print(i32 %n)
184
185 ret i8* %frame
186 }
187
188Outlined resume part of the coroutine will reside in function `f.resume`:
189
190.. code-block:: llvm
191
192 define internal fastcc void @f.resume(%f.frame* %frame.ptr.resume) {
193 entry:
194 %inc.spill.addr = getelementptr %f.frame, %f.frame* %frame.ptr.resume, i64 0, i32 2
195 %inc.spill = load i32, i32* %inc.spill.addr, align 4
196 %inc = add i32 %n.val, 1
197 store i32 %inc, i32* %inc.spill.addr, align 4
198 tail call void @print(i32 %inc)
199 ret void
200 }
201
202Whereas function `f.destroy` will contain the cleanup code for the coroutine:
203
204.. code-block:: llvm
205
206 define internal fastcc void @f.destroy(%f.frame* %frame.ptr.destroy) {
207 entry:
208 %0 = bitcast %f.frame* %frame.ptr.destroy to i8*
209 tail call void @free(i8* %0)
210 ret void
211 }
212
213Avoiding Heap Allocations
214-------------------------
215
216A particular coroutine usage pattern, which is illustrated by the `main`
217function in the overview section, where a coroutine is created, manipulated and
218destroyed by the same calling function, is common for coroutines implementing
219RAII idiom and is suitable for allocation elision optimization which avoid
220dynamic allocation by storing the coroutine frame as a static `alloca` in its
221caller.
222
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000223In the entry block, we will call `coro.alloc`_ intrinsic that will return `true`
224when dynamic allocation is required, and `false` if dynamic allocation is
225elided.
David Majnemera6539272016-07-23 04:05:08 +0000226
Gor Nishanovb2a9c022016-08-10 16:40:39 +0000227.. code-block:: none
David Majnemera6539272016-07-23 04:05:08 +0000228
229 entry:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000230 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000231 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
232 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
David Majnemera6539272016-07-23 04:05:08 +0000233 dyn.alloc:
234 %size = call i32 @llvm.coro.size.i32()
235 %alloc = call i8* @CustomAlloc(i32 %size)
236 br label %coro.begin
237 coro.begin:
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000238 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
239 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
David Majnemera6539272016-07-23 04:05:08 +0000240
241In the cleanup block, we will make freeing the coroutine frame conditional on
242`coro.free`_ intrinsic. If allocation is elided, `coro.free`_ returns `null`
243thus skipping the deallocation code:
244
Aaron Ballman655e5a42016-08-31 13:29:23 +0000245.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000246
247 cleanup:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000248 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
David Majnemera6539272016-07-23 04:05:08 +0000249 %need.dyn.free = icmp ne i8* %mem, null
250 br i1 %need.dyn.free, label %dyn.free, label %if.end
251 dyn.free:
252 call void @CustomFree(i8* %mem)
253 br label %if.end
254 if.end:
255 ...
256
257With allocations and deallocations represented as described as above, after
David Majnemer78557192016-07-27 05:12:35 +0000258coroutine heap allocation elision optimization, the resulting main will be:
David Majnemera6539272016-07-23 04:05:08 +0000259
260.. code-block:: llvm
261
262 define i32 @main() {
263 entry:
264 call void @print(i32 4)
265 call void @print(i32 5)
266 call void @print(i32 6)
267 ret i32 0
268 }
269
270Multiple Suspend Points
271-----------------------
272
273Let's consider the coroutine that has more than one suspend point:
274
Sanjoy Das77a9c792016-07-26 21:03:41 +0000275.. code-block:: c++
David Majnemera6539272016-07-23 04:05:08 +0000276
277 void *f(int n) {
278 for(;;) {
279 print(n++);
280 <suspend>
281 print(-n);
282 <suspend>
283 }
284 }
285
286Matching LLVM code would look like (with the rest of the code remaining the same
287as the code in the previous section):
288
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +0000289.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000290
291 loop:
292 %n.addr = phi i32 [ %n, %entry ], [ %inc, %loop.resume ]
293 call void @print(i32 %n.addr) #4
294 %2 = call i8 @llvm.coro.suspend(token none, i1 false)
295 switch i8 %2, label %suspend [i8 0, label %loop.resume
296 i8 1, label %cleanup]
297 loop.resume:
298 %inc = add nsw i32 %n.addr, 1
299 %sub = xor i32 %n.addr, -1
300 call void @print(i32 %sub)
301 %3 = call i8 @llvm.coro.suspend(token none, i1 false)
302 switch i8 %3, label %suspend [i8 0, label %loop
303 i8 1, label %cleanup]
304
305In this case, the coroutine frame would include a suspend index that will
306indicate at which suspend point the coroutine needs to resume. The resume
307function will use an index to jump to an appropriate basic block and will look
308as follows:
309
310.. code-block:: llvm
311
312 define internal fastcc void @f.Resume(%f.Frame* %FramePtr) {
313 entry.Resume:
314 %index.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 2
315 %index = load i8, i8* %index.addr, align 1
316 %switch = icmp eq i8 %index, 0
317 %n.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 3
318 %n = load i32, i32* %n.addr, align 4
319 br i1 %switch, label %loop.resume, label %loop
320
321 loop.resume:
322 %sub = xor i32 %n, -1
323 call void @print(i32 %sub)
324 br label %suspend
325 loop:
326 %inc = add nsw i32 %n, 1
327 store i32 %inc, i32* %n.addr, align 4
328 tail call void @print(i32 %inc)
329 br label %suspend
330
331 suspend:
332 %storemerge = phi i8 [ 0, %loop ], [ 1, %loop.resume ]
333 store i8 %storemerge, i8* %index.addr, align 1
334 ret void
335 }
336
337If different cleanup code needs to get executed for different suspend points,
338a similar switch will be in the `f.destroy` function.
339
340.. note ::
341
342 Using suspend index in a coroutine state and having a switch in `f.resume` and
343 `f.destroy` is one of the possible implementation strategies. We explored
344 another option where a distinct `f.resume1`, `f.resume2`, etc. are created for
345 every suspend point, and instead of storing an index, the resume and destroy
346 function pointers are updated at every suspend. Early testing showed that the
347 current approach is easier on the optimizer than the latter so it is a
348 lowering strategy implemented at the moment.
349
350Distinct Save and Suspend
351-------------------------
352
353In the previous example, setting a resume index (or some other state change that
354needs to happen to prepare a coroutine for resumption) happens at the same time as
355a suspension of a coroutine. However, in certain cases, it is necessary to control
356when coroutine is prepared for resumption and when it is suspended.
357
358In the following example, a coroutine represents some activity that is driven
359by completions of asynchronous operations `async_op1` and `async_op2` which get
360a coroutine handle as a parameter and resume the coroutine once async
361operation is finished.
362
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +0000363.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000364
365 void g() {
366 for (;;)
367 if (cond()) {
368 async_op1(<coroutine-handle>); // will resume once async_op1 completes
369 <suspend>
370 do_one();
371 }
372 else {
373 async_op2(<coroutine-handle>); // will resume once async_op2 completes
374 <suspend>
375 do_two();
376 }
377 }
378 }
379
380In this case, coroutine should be ready for resumption prior to a call to
381`async_op1` and `async_op2`. The `coro.save`_ intrinsic is used to indicate a
382point when coroutine should be ready for resumption (namely, when a resume index
383should be stored in the coroutine frame, so that it can be resumed at the
384correct resume point):
385
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +0000386.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000387
388 if.true:
389 %save1 = call token @llvm.coro.save(i8* %hdl)
390 call void async_op1(i8* %hdl)
391 %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
392 switch i8 %suspend1, label %suspend [i8 0, label %resume1
393 i8 1, label %cleanup]
394 if.false:
395 %save2 = call token @llvm.coro.save(i8* %hdl)
396 call void async_op2(i8* %hdl)
397 %suspend2 = call i1 @llvm.coro.suspend(token %save2, i1 false)
398 switch i8 %suspend1, label %suspend [i8 0, label %resume2
399 i8 1, label %cleanup]
400
401.. _coroutine promise:
402
403Coroutine Promise
404-----------------
405
406A coroutine author or a frontend may designate a distinguished `alloca` that can
407be used to communicate with the coroutine. This distinguished alloca is called
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000408**coroutine promise** and is provided as the second parameter to the
409`coro.id`_ intrinsic.
David Majnemera6539272016-07-23 04:05:08 +0000410
411The following coroutine designates a 32 bit integer `promise` and uses it to
412store the current value produced by a coroutine.
413
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +0000414.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000415
416 define i8* @f(i32 %n) {
417 entry:
418 %promise = alloca i32
419 %pv = bitcast i32* %promise to i8*
Gor Nishanovdce9b022016-08-29 14:34:12 +0000420 %id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null)
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000421 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
422 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
David Majnemer78557192016-07-27 05:12:35 +0000423 dyn.alloc:
David Majnemera6539272016-07-23 04:05:08 +0000424 %size = call i32 @llvm.coro.size.i32()
425 %alloc = call i8* @malloc(i32 %size)
David Majnemer78557192016-07-27 05:12:35 +0000426 br label %coro.begin
427 coro.begin:
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000428 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
429 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
David Majnemera6539272016-07-23 04:05:08 +0000430 br label %loop
431 loop:
David Majnemer78557192016-07-27 05:12:35 +0000432 %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ]
David Majnemera6539272016-07-23 04:05:08 +0000433 %inc = add nsw i32 %n.val, 1
434 store i32 %n.val, i32* %promise
435 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
436 switch i8 %0, label %suspend [i8 0, label %loop
437 i8 1, label %cleanup]
438 cleanup:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000439 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
David Majnemera6539272016-07-23 04:05:08 +0000440 call void @free(i8* %mem)
441 br label %suspend
442 suspend:
443 call void @llvm.coro.end(i8* %hdl, i1 false)
444 ret i8* %hdl
445 }
446
447A coroutine consumer can rely on the `coro.promise`_ intrinsic to access the
448coroutine promise.
449
450.. code-block:: llvm
451
452 define i32 @main() {
453 entry:
454 %hdl = call i8* @f(i32 4)
455 %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false)
456 %promise.addr = bitcast i8* %promise.addr.raw to i32*
457 %val0 = load i32, i32* %promise.addr
458 call void @print(i32 %val0)
459 call void @llvm.coro.resume(i8* %hdl)
460 %val1 = load i32, i32* %promise.addr
461 call void @print(i32 %val1)
462 call void @llvm.coro.resume(i8* %hdl)
463 %val2 = load i32, i32* %promise.addr
464 call void @print(i32 %val2)
465 call void @llvm.coro.destroy(i8* %hdl)
466 ret i32 0
467 }
468
David Majnemer78557192016-07-27 05:12:35 +0000469After example in this section is compiled, result of the compilation will be:
David Majnemera6539272016-07-23 04:05:08 +0000470
471.. code-block:: llvm
472
473 define i32 @main() {
474 entry:
475 tail call void @print(i32 4)
476 tail call void @print(i32 5)
477 tail call void @print(i32 6)
478 ret i32 0
479 }
480
481.. _final:
482.. _final suspend:
483
484Final Suspend
485-------------
486
487A coroutine author or a frontend may designate a particular suspend to be final,
488by setting the second argument of the `coro.suspend`_ intrinsic to `true`.
489Such a suspend point has two properties:
490
491* it is possible to check whether a suspended coroutine is at the final suspend
492 point via `coro.done`_ intrinsic;
493
494* a resumption of a coroutine stopped at the final suspend point leads to
495 undefined behavior. The only possible action for a coroutine at a final
496 suspend point is destroying it via `coro.destroy`_ intrinsic.
497
498From the user perspective, the final suspend point represents an idea of a
499coroutine reaching the end. From the compiler perspective, it is an optimization
500opportunity for reducing number of resume points (and therefore switch cases) in
501the resume function.
502
503The following is an example of a function that keeps resuming the coroutine
504until the final suspend point is reached after which point the coroutine is
505destroyed:
506
507.. code-block:: llvm
508
509 define i32 @main() {
510 entry:
511 %hdl = call i8* @f(i32 4)
512 br label %while
513 while:
514 call void @llvm.coro.resume(i8* %hdl)
515 %done = call i1 @llvm.coro.done(i8* %hdl)
516 br i1 %done, label %end, label %while
517 end:
518 call void @llvm.coro.destroy(i8* %hdl)
519 ret i32 0
520 }
521
522Usually, final suspend point is a frontend injected suspend point that does not
523correspond to any explicitly authored suspend point of the high level language.
524For example, for a Python generator that has only one suspend point:
525
526.. code-block:: python
527
528 def coroutine(n):
529 for i in range(n):
530 yield i
531
532Python frontend would inject two more suspend points, so that the actual code
533looks like this:
534
Sanjoy Das77a9c792016-07-26 21:03:41 +0000535.. code-block:: c
David Majnemera6539272016-07-23 04:05:08 +0000536
537 void* coroutine(int n) {
538 int current_value;
539 <designate current_value to be coroutine promise>
540 <SUSPEND> // injected suspend point, so that the coroutine starts suspended
541 for (int i = 0; i < n; ++i) {
542 current_value = i; <SUSPEND>; // corresponds to "yield i"
543 }
544 <SUSPEND final=true> // injected final suspend point
545 }
546
547and python iterator `__next__` would look like:
548
Sanjoy Das77a9c792016-07-26 21:03:41 +0000549.. code-block:: c++
David Majnemera6539272016-07-23 04:05:08 +0000550
551 int __next__(void* hdl) {
552 coro.resume(hdl);
553 if (coro.done(hdl)) throw StopIteration();
554 return *(int*)coro.promise(hdl, 4, false);
555 }
556
557Intrinsics
558==========
559
560Coroutine Manipulation Intrinsics
561---------------------------------
562
563Intrinsics described in this section are used to manipulate an existing
564coroutine. They can be used in any function which happen to have a pointer
565to a `coroutine frame`_ or a pointer to a `coroutine promise`_.
566
567.. _coro.destroy:
568
569'llvm.coro.destroy' Intrinsic
570^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
571
572Syntax:
573"""""""
574
575::
576
577 declare void @llvm.coro.destroy(i8* <handle>)
578
579Overview:
580"""""""""
581
582The '``llvm.coro.destroy``' intrinsic destroys a suspended
583coroutine.
584
585Arguments:
586""""""""""
587
588The argument is a coroutine handle to a suspended coroutine.
589
590Semantics:
591""""""""""
592
593When possible, the `coro.destroy` intrinsic is replaced with a direct call to
594the coroutine destroy function. Otherwise it is replaced with an indirect call
595based on the function pointer for the destroy function stored in the coroutine
596frame. Destroying a coroutine that is not suspended leads to undefined behavior.
597
598.. _coro.resume:
599
600'llvm.coro.resume' Intrinsic
601^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
602
603::
604
605 declare void @llvm.coro.resume(i8* <handle>)
606
607Overview:
608"""""""""
609
610The '``llvm.coro.resume``' intrinsic resumes a suspended coroutine.
611
612Arguments:
613""""""""""
614
615The argument is a handle to a suspended coroutine.
616
617Semantics:
618""""""""""
619
620When possible, the `coro.resume` intrinsic is replaced with a direct call to the
621coroutine resume function. Otherwise it is replaced with an indirect call based
622on the function pointer for the resume function stored in the coroutine frame.
623Resuming a coroutine that is not suspended leads to undefined behavior.
624
625.. _coro.done:
626
627'llvm.coro.done' Intrinsic
628^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
629
630::
631
632 declare i1 @llvm.coro.done(i8* <handle>)
633
634Overview:
635"""""""""
636
637The '``llvm.coro.done``' intrinsic checks whether a suspended coroutine is at
638the final suspend point or not.
639
640Arguments:
641""""""""""
642
643The argument is a handle to a suspended coroutine.
644
645Semantics:
646""""""""""
647
648Using this intrinsic on a coroutine that does not have a `final suspend`_ point
649or on a coroutine that is not suspended leads to undefined behavior.
650
651.. _coro.promise:
652
653'llvm.coro.promise' Intrinsic
654^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
655
656::
657
658 declare i8* @llvm.coro.promise(i8* <ptr>, i32 <alignment>, i1 <from>)
659
660Overview:
661"""""""""
662
663The '``llvm.coro.promise``' intrinsic obtains a pointer to a
664`coroutine promise`_ given a coroutine handle and vice versa.
665
666Arguments:
667""""""""""
668
669The first argument is a handle to a coroutine if `from` is false. Otherwise,
670it is a pointer to a coroutine promise.
671
672The second argument is an alignment requirements of the promise.
673If a frontend designated `%promise = alloca i32` as a promise, the alignment
674argument to `coro.promise` should be the alignment of `i32` on the target
675platform. If a frontend designated `%promise = alloca i32, align 16` as a
676promise, the alignment argument should be 16.
677This argument only accepts constants.
678
679The third argument is a boolean indicating a direction of the transformation.
680If `from` is true, the intrinsic returns a coroutine handle given a pointer
681to a promise. If `from` is false, the intrinsics return a pointer to a promise
682from a coroutine handle. This argument only accepts constants.
683
684Semantics:
685""""""""""
686
687Using this intrinsic on a coroutine that does not have a coroutine promise
688leads to undefined behavior. It is possible to read and modify coroutine
689promise of the coroutine which is currently executing. The coroutine author and
690a coroutine user are responsible to makes sure there is no data races.
691
692Example:
693""""""""
694
Gor Nishanovb2a9c022016-08-10 16:40:39 +0000695.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000696
697 define i8* @f(i32 %n) {
698 entry:
699 %promise = alloca i32
700 %pv = bitcast i32* %promise to i8*
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000701 ; the second argument to coro.id points to the coroutine promise.
Gor Nishanovdce9b022016-08-29 14:34:12 +0000702 %id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null)
David Majnemera6539272016-07-23 04:05:08 +0000703 ...
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000704 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
David Majnemera6539272016-07-23 04:05:08 +0000705 ...
706 store i32 42, i32* %promise ; store something into the promise
707 ...
708 ret i8* %hdl
709 }
710
711 define i32 @main() {
712 entry:
713 %hdl = call i8* @f(i32 4) ; starts the coroutine and returns its handle
714 %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false)
715 %promise.addr = bitcast i8* %promise.addr.raw to i32*
716 %val = load i32, i32* %promise.addr ; load a value from the promise
717 call void @print(i32 %val)
718 call void @llvm.coro.destroy(i8* %hdl)
719 ret i32 0
720 }
721
722.. _coroutine intrinsics:
723
724Coroutine Structure Intrinsics
725------------------------------
726Intrinsics described in this section are used within a coroutine to describe
727the coroutine structure. They should not be used outside of a coroutine.
728
729.. _coro.size:
730
731'llvm.coro.size' Intrinsic
732^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
733::
734
735 declare i32 @llvm.coro.size.i32()
736 declare i64 @llvm.coro.size.i64()
737
738Overview:
739"""""""""
740
741The '``llvm.coro.size``' intrinsic returns the number of bytes
742required to store a `coroutine frame`_.
743
744Arguments:
745""""""""""
746
747None
748
749Semantics:
750""""""""""
751
752The `coro.size` intrinsic is lowered to a constant representing the size of
753the coroutine frame.
754
755.. _coro.begin:
756
757'llvm.coro.begin' Intrinsic
758^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
759::
760
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000761 declare i8* @llvm.coro.begin(token <id>, i8* <mem>)
David Majnemera6539272016-07-23 04:05:08 +0000762
763Overview:
764"""""""""
765
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000766The '``llvm.coro.begin``' intrinsic returns an address of the coroutine frame.
David Majnemera6539272016-07-23 04:05:08 +0000767
768Arguments:
769""""""""""
770
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000771The first argument is a token returned by a call to '``llvm.coro.id``'
772identifying the coroutine.
David Majnemera6539272016-07-23 04:05:08 +0000773
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000774The second argument is a pointer to a block of memory where coroutine frame
775will be stored if it is allocated dynamically.
David Majnemera6539272016-07-23 04:05:08 +0000776
777Semantics:
778""""""""""
779
780Depending on the alignment requirements of the objects in the coroutine frame
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000781and/or on the codegen compactness reasons the pointer returned from `coro.begin`
782may be at offset to the `%mem` argument. (This could be beneficial if
783instructions that express relative access to data can be more compactly encoded
784with small positive and negative offsets).
David Majnemera6539272016-07-23 04:05:08 +0000785
David Majnemer78557192016-07-27 05:12:35 +0000786A frontend should emit exactly one `coro.begin` intrinsic per coroutine.
David Majnemera6539272016-07-23 04:05:08 +0000787
788.. _coro.free:
789
790'llvm.coro.free' Intrinsic
791^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
792::
793
Gor Nishanovdce9b022016-08-29 14:34:12 +0000794 declare i8* @llvm.coro.free(token %id, i8* <frame>)
David Majnemera6539272016-07-23 04:05:08 +0000795
796Overview:
797"""""""""
798
799The '``llvm.coro.free``' intrinsic returns a pointer to a block of memory where
800coroutine frame is stored or `null` if this instance of a coroutine did not use
801dynamically allocated memory for its coroutine frame.
802
803Arguments:
804""""""""""
805
Gor Nishanovdce9b022016-08-29 14:34:12 +0000806The first argument is a token returned by a call to '``llvm.coro.id``'
807identifying the coroutine.
808
809The second argument is a pointer to the coroutine frame. This should be the same
810pointer that was returned by prior `coro.begin` call.
David Majnemera6539272016-07-23 04:05:08 +0000811
812Example (custom deallocation function):
813"""""""""""""""""""""""""""""""""""""""
814
Aaron Ballman15b978a2016-08-31 14:37:20 +0000815.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000816
817 cleanup:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000818 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
David Majnemera6539272016-07-23 04:05:08 +0000819 %mem_not_null = icmp ne i8* %mem, null
820 br i1 %mem_not_null, label %if.then, label %if.end
821 if.then:
822 call void @CustomFree(i8* %mem)
823 br label %if.end
824 if.end:
825 ret void
826
827Example (standard deallocation functions):
828""""""""""""""""""""""""""""""""""""""""""
829
Aaron Ballman15b978a2016-08-31 14:37:20 +0000830.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000831
832 cleanup:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000833 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
David Majnemera6539272016-07-23 04:05:08 +0000834 call void @free(i8* %mem)
835 ret void
836
837.. _coro.alloc:
838
839'llvm.coro.alloc' Intrinsic
840^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
841::
842
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000843 declare i1 @llvm.coro.alloc(token <id>)
David Majnemera6539272016-07-23 04:05:08 +0000844
845Overview:
846"""""""""
847
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000848The '``llvm.coro.alloc``' intrinsic returns `true` if dynamic allocation is
849required to obtain a memory for the corutine frame and `false` otherwise.
David Majnemera6539272016-07-23 04:05:08 +0000850
851Arguments:
852""""""""""
853
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000854The first argument is a token returned by a call to '``llvm.coro.id``'
855identifying the coroutine.
David Majnemera6539272016-07-23 04:05:08 +0000856
857Semantics:
858""""""""""
859
David Majnemer78557192016-07-27 05:12:35 +0000860A frontend should emit at most one `coro.alloc` intrinsic per coroutine.
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000861The intrinsic is used to suppress dynamic allocation of the coroutine frame
862when possible.
Gor Nishanovb2a9c022016-08-10 16:40:39 +0000863
David Majnemera6539272016-07-23 04:05:08 +0000864Example:
865""""""""
866
Gor Nishanovb2a9c022016-08-10 16:40:39 +0000867.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +0000868
869 entry:
Gor Nishanovdce9b022016-08-29 14:34:12 +0000870 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000871 %dyn.alloc.required = call i1 @llvm.coro.alloc(token %id)
872 br i1 %dyn.alloc.required, label %coro.alloc, label %coro.begin
David Majnemera6539272016-07-23 04:05:08 +0000873
874 coro.alloc:
875 %frame.size = call i32 @llvm.coro.size()
876 %alloc = call i8* @MyAlloc(i32 %frame.size)
877 br label %coro.begin
878
879 coro.begin:
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000880 %phi = phi i8* [ null, %entry ], [ %alloc, %coro.alloc ]
881 %frame = call i8* @llvm.coro.begin(token %id, i8* %phi)
David Majnemera6539272016-07-23 04:05:08 +0000882
883.. _coro.frame:
884
885'llvm.coro.frame' Intrinsic
886^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
887::
888
889 declare i8* @llvm.coro.frame()
890
891Overview:
892"""""""""
893
894The '``llvm.coro.frame``' intrinsic returns an address of the coroutine frame of
895the enclosing coroutine.
896
897Arguments:
898""""""""""
899
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000900None
David Majnemera6539272016-07-23 04:05:08 +0000901
902Semantics:
903""""""""""
904
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000905This intrinsic is lowered to refer to the `coro.begin`_ instruction. This is
906a frontend convenience intrinsic that makes it easier to refer to the
907coroutine frame.
908
909.. _coro.id:
910
911'llvm.coro.id' Intrinsic
912^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
913::
914
Gor Nishanovdce9b022016-08-29 14:34:12 +0000915 declare token @llvm.coro.id(i32 <align>, i8* <promise>, i8* <coroaddr>,
916 i8* <fnaddrs>)
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000917
918Overview:
919"""""""""
920
921The '``llvm.coro.id``' intrinsic returns a token identifying a coroutine.
922
923Arguments:
924""""""""""
925
926The first argument provides information on the alignment of the memory returned
927by the allocation function and given to `coro.begin` by the first argument. If
928this argument is 0, the memory is assumed to be aligned to 2 * sizeof(i8*).
929This argument only accepts constants.
930
931The second argument, if not `null`, designates a particular alloca instruction
932to be a `coroutine promise`_.
933
Gor Nishanovdce9b022016-08-29 14:34:12 +0000934The third argument is `null` coming out of the frontend. The CoroEarly pass sets
935this argument to point to the function this coro.id belongs to.
936
937The fourth argument is `null` before coroutine is split, and later is replaced
Gor Nishanov0f303ac2016-08-12 05:45:49 +0000938to point to a private global constant array containing function pointers to
939outlined resume and destroy parts of the coroutine.
940
941
942Semantics:
943""""""""""
944
945The purpose of this intrinsic is to tie together `coro.id`, `coro.alloc` and
946`coro.begin` belonging to the same coroutine to prevent optimization passes from
947duplicating any of these instructions unless entire body of the coroutine is
948duplicated.
949
950A frontend should emit exactly one `coro.id` intrinsic per coroutine.
David Majnemera6539272016-07-23 04:05:08 +0000951
952.. _coro.end:
953
954'llvm.coro.end' Intrinsic
955^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
956::
957
958 declare void @llvm.coro.end(i8* <handle>, i1 <unwind>)
959
960Overview:
961"""""""""
962
963The '``llvm.coro.end``' marks the point where execution of the resume part of
964the coroutine should end and control returns back to the caller.
965
966
967Arguments:
968""""""""""
969
970The first argument should refer to the coroutine handle of the enclosing coroutine.
971
972The second argument should be `true` if this coro.end is in the block that is
973part of the unwind sequence leaving the coroutine body due to exception prior to
974the first reaching any suspend points, and `false` otherwise.
975
976Semantics:
977""""""""""
978The `coro.end`_ intrinsic is a no-op during an initial invocation of the
979coroutine. When the coroutine resumes, the intrinsic marks the point when
980coroutine need to return control back to the caller.
981
982This intrinsic is removed by the CoroSplit pass when a coroutine is split into
983the start, resume and destroy parts. In start part, the intrinsic is removed,
984in resume and destroy parts, it is replaced with `ret void` instructions and
985the rest of the block containing `coro.end` instruction is discarded.
986
987In landing pads it is replaced with an appropriate instruction to unwind to
988caller.
989
990A frontend is allowed to supply null as the first parameter, in this case
991`coro-early` pass will replace the null with an appropriate coroutine handle
992value.
993
994.. _coro.suspend:
995.. _suspend points:
996
997'llvm.coro.suspend' Intrinsic
998^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
999::
1000
1001 declare i8 @llvm.coro.suspend(token <save>, i1 <final>)
1002
1003Overview:
1004"""""""""
1005
1006The '``llvm.coro.suspend``' marks the point where execution of the coroutine
1007need to get suspended and control returned back to the caller.
1008Conditional branches consuming the result of this intrinsic lead to basic blocks
1009where coroutine should proceed when suspended (-1), resumed (0) or destroyed
1010(1).
1011
1012Arguments:
1013""""""""""
1014
1015The first argument refers to a token of `coro.save` intrinsic that marks the
1016point when coroutine state is prepared for suspension. If `none` token is passed,
1017the intrinsic behaves as if there were a `coro.save` immediately preceding
1018the `coro.suspend` intrinsic.
1019
1020The second argument indicates whether this suspension point is `final`_.
1021The second argument only accepts constants. If more than one suspend point is
1022designated as final, the resume and destroy branches should lead to the same
1023basic blocks.
1024
1025Example (normal suspend point):
1026"""""""""""""""""""""""""""""""
1027
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +00001028.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +00001029
1030 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
1031 switch i8 %0, label %suspend [i8 0, label %resume
1032 i8 1, label %cleanup]
1033
1034Example (final suspend point):
1035""""""""""""""""""""""""""""""
1036
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +00001037.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +00001038
1039 while.end:
1040 %s.final = call i8 @llvm.coro.suspend(token none, i1 true)
1041 switch i8 %s.final, label %suspend [i8 0, label %trap
1042 i8 1, label %cleanup]
1043 trap:
1044 call void @llvm.trap()
1045 unreachable
1046
1047Semantics:
1048""""""""""
1049
1050If a coroutine that was suspended at the suspend point marked by this intrinsic
1051is resumed via `coro.resume`_ the control will transfer to the basic block
1052of the 0-case. If it is resumed via `coro.destroy`_, it will proceed to the
1053basic block indicated by the 1-case. To suspend, coroutine proceed to the
1054default label.
1055
1056If suspend intrinsic is marked as final, it can consider the `true` branch
1057unreachable and can perform optimizations that can take advantage of that fact.
1058
1059.. _coro.save:
1060
1061'llvm.coro.save' Intrinsic
1062^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1063::
1064
1065 declare token @llvm.coro.save(i8* <handle>)
1066
1067Overview:
1068"""""""""
1069
1070The '``llvm.coro.save``' marks the point where a coroutine need to update its
1071state to prepare for resumption to be considered suspended (and thus eligible
1072for resumption).
1073
1074Arguments:
1075""""""""""
1076
1077The first argument points to a coroutine handle of the enclosing coroutine.
1078
1079Semantics:
1080""""""""""
1081
1082Whatever coroutine state changes are required to enable resumption of
1083the coroutine from the corresponding suspend point should be done at the point
1084of `coro.save` intrinsic.
1085
1086Example:
1087""""""""
1088
1089Separate save and suspend points are necessary when a coroutine is used to
1090represent an asynchronous control flow driven by callbacks representing
1091completions of asynchronous operations.
1092
1093In such a case, a coroutine should be ready for resumption prior to a call to
1094`async_op` function that may trigger resumption of a coroutine from the same or
1095a different thread possibly prior to `async_op` call returning control back
1096to the coroutine:
1097
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +00001098.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +00001099
1100 %save1 = call token @llvm.coro.save(i8* %hdl)
1101 call void async_op1(i8* %hdl)
1102 %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
1103 switch i8 %suspend1, label %suspend [i8 0, label %resume1
1104 i8 1, label %cleanup]
1105
1106.. _coro.param:
1107
1108'llvm.coro.param' Intrinsic
1109^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1110::
1111
1112 declare i1 @llvm.coro.param(i8* <original>, i8* <copy>)
1113
1114Overview:
1115"""""""""
1116
David Majnemer78557192016-07-27 05:12:35 +00001117The '``llvm.coro.param``' is used by a frontend to mark up the code used to
David Majnemera6539272016-07-23 04:05:08 +00001118construct and destruct copies of the parameters. If the optimizer discovers that
1119a particular parameter copy is not used after any suspends, it can remove the
1120construction and destruction of the copy by replacing corresponding coro.param
1121with `i1 false` and replacing any use of the `copy` with the `original`.
1122
1123Arguments:
1124""""""""""
1125
1126The first argument points to an `alloca` storing the value of a parameter to a
1127coroutine.
1128
1129The second argument points to an `alloca` storing the value of the copy of that
1130parameter.
1131
1132Semantics:
1133""""""""""
1134
1135The optimizer is free to always replace this intrinsic with `i1 true`.
1136
1137The optimizer is also allowed to replace it with `i1 false` provided that the
1138parameter copy is only used prior to control flow reaching any of the suspend
1139points. The code that would be DCE'd if the `coro.param` is replaced with
1140`i1 false` is not considered to be a use of the parameter copy.
1141
1142The frontend can emit this intrinsic if its language rules allow for this
1143optimization.
1144
1145Example:
1146""""""""
1147Consider the following example. A coroutine takes two parameters `a` and `b`
1148that has a destructor and a move constructor.
1149
Sanjoy Das77a9c792016-07-26 21:03:41 +00001150.. code-block:: c++
David Majnemera6539272016-07-23 04:05:08 +00001151
1152 struct A { ~A(); A(A&&); bool foo(); void bar(); };
1153
1154 task<int> f(A a, A b) {
1155 if (a.foo())
1156 return 42;
1157
1158 a.bar();
1159 co_await read_async(); // introduces suspend point
1160 b.bar();
1161 }
1162
1163Note that, uses of `b` is used after a suspend point and thus must be copied
1164into a coroutine frame, whereas `a` does not have to, since it never used
1165after suspend.
1166
1167A frontend can create parameter copies for `a` and `b` as follows:
1168
Aaron Ballmanbc7c2d02016-07-23 20:11:21 +00001169.. code-block:: text
David Majnemera6539272016-07-23 04:05:08 +00001170
1171 task<int> f(A a', A b') {
1172 a = alloca A;
1173 b = alloca A;
1174 // move parameters to its copies
1175 if (coro.param(a', a)) A::A(a, A&& a');
1176 if (coro.param(b', b)) A::A(b, A&& b');
1177 ...
1178 // destroy parameters copies
1179 if (coro.param(a', a)) A::~A(a);
1180 if (coro.param(b', b)) A::~A(b);
1181 }
1182
1183The optimizer can replace coro.param(a',a) with `i1 false` and replace all uses
1184of `a` with `a'`, since it is not used after suspend.
1185
1186The optimizer must replace coro.param(b', b) with `i1 true`, since `b` is used
1187after suspend and therefore, it has to reside in the coroutine frame.
1188
1189Coroutine Transformation Passes
1190===============================
1191CoroEarly
1192---------
1193The pass CoroEarly lowers coroutine intrinsics that hide the details of the
1194structure of the coroutine frame, but, otherwise not needed to be preserved to
1195help later coroutine passes. This pass lowers `coro.frame`_, `coro.done`_,
1196and `coro.promise`_ intrinsics.
1197
1198.. _CoroSplit:
1199
1200CoroSplit
1201---------
1202The pass CoroSplit buides coroutine frame and outlines resume and destroy parts
1203into separate functions.
1204
1205CoroElide
1206---------
1207The pass CoroElide examines if the inlined coroutine is eligible for heap
Gor Nishanov0f303ac2016-08-12 05:45:49 +00001208allocation elision optimization. If so, it replaces
1209`coro.begin` intrinsic with an address of a coroutine frame placed on its caller
1210and replaces `coro.alloc` and `coro.free` intrinsics with `false` and `null`
1211respectively to remove the deallocation code.
David Majnemera6539272016-07-23 04:05:08 +00001212This pass also replaces `coro.resume` and `coro.destroy` intrinsics with direct
1213calls to resume and destroy functions for a particular coroutine where possible.
1214
1215CoroCleanup
1216-----------
1217This pass runs late to lower all coroutine related intrinsics not replaced by
1218earlier passes.
1219
David Majnemera6539272016-07-23 04:05:08 +00001220Areas Requiring Attention
1221=========================
1222#. A coroutine frame is bigger than it could be. Adding stack packing and stack
1223 coloring like optimization on the coroutine frame will result in tighter
1224 coroutine frames.
1225
1226#. Take advantage of the lifetime intrinsics for the data that goes into the
1227 coroutine frame. Leave lifetime intrinsics as is for the data that stays in
1228 allocas.
1229
1230#. The CoroElide optimization pass relies on coroutine ramp function to be
1231 inlined. It would be beneficial to split the ramp function further to
1232 increase the chance that it will get inlined into its caller.
1233
1234#. Design a convention that would make it possible to apply coroutine heap
1235 elision optimization across ABI boundaries.
1236
1237#. Cannot handle coroutines with `inalloca` parameters (used in x86 on Windows).
1238
1239#. Alignment is ignored by coro.begin and coro.free intrinsics.
1240
1241#. Make required changes to make sure that coroutine optimizations work with
1242 LTO.
1243
1244#. More tests, more tests, more tests