1 =====================================
3 =====================================
10 This is a work in progress. Compatibility across LLVM releases is not
18 LLVM coroutines are functions that have one or more `suspend points`_.
19 When a suspend point is reached, the execution of a coroutine is suspended and
20 control is returned back to its caller. A suspended coroutine can be resumed
21 to continue execution from the last suspend point or it can be destroyed.
23 In the following example, we call function `f` (which may or may not be a
24 coroutine itself) that returns a handle to a suspended coroutine
25 (**coroutine handle**) that is used by `main` to resume the coroutine twice and
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)
41 In addition to the function stack frame which exists when a coroutine is
42 executing, there is an additional region of storage that contains objects that
43 keep the coroutine state when a coroutine is suspended. This region of storage
44 is called the **coroutine frame**. It is created when a coroutine is called
45 and destroyed when a coroutine either runs to completion or is destroyed
48 LLVM currently supports two styles of coroutine lowering. These styles
49 support substantially different sets of features, have substantially
50 different ABIs, and expect substantially different patterns of frontend
51 code generation. However, the styles also have a great deal in common.
53 In all cases, an LLVM coroutine is initially represented as an ordinary LLVM
54 function that has calls to `coroutine intrinsics`_ defining the structure of
55 the coroutine. The coroutine function is then, in the most general case,
56 rewritten by the coroutine lowering passes to become the "ramp function",
57 the initial entrypoint of the coroutine, which executes until a suspend point
58 is first reached. The remainder of the original coroutine function is split
59 out into some number of "resume functions". Any state which must persist
60 across suspensions is stored in the coroutine frame. The resume functions
61 must somehow be able to handle either a "normal" resumption, which continues
62 the normal execution of the coroutine, or an "abnormal" resumption, which
63 must unwind the coroutine without attempting to suspend it.
65 Switched-Resume Lowering
66 ------------------------
68 In LLVM's standard switched-resume lowering, signaled by the use of
69 `llvm.coro.id`, the coroutine frame is stored as part of a "coroutine
70 object" which represents a handle to a particular invocation of the
71 coroutine. All coroutine objects support a common ABI allowing certain
72 features to be used without knowing anything about the coroutine's
75 - A coroutine object can be queried to see if it has reached completion
76 with `llvm.coro.done`.
78 - A coroutine object can be resumed normally if it has not already reached
79 completion with `llvm.coro.resume`.
81 - A coroutine object can be destroyed, invalidating the coroutine object,
82 with `llvm.coro.destroy`. This must be done separately even if the
83 coroutine has reached completion normally.
85 - "Promise" storage, which is known to have a certain size and alignment,
86 can be projected out of the coroutine object with `llvm.coro.promise`.
87 The coroutine implementation must have been compiled to define a promise
88 of the same size and alignment.
90 In general, interacting with a coroutine object in any of these ways while
91 it is running has undefined behavior.
93 The coroutine function is split into three functions, representing three
94 different ways that control can enter the coroutine:
96 1. the ramp function that is initially invoked, which takes arbitrary
97 arguments and returns a pointer to the coroutine object;
99 2. a coroutine resume function that is invoked when the coroutine is resumed,
100 which takes a pointer to the coroutine object and returns `void`;
102 3. a coroutine destroy function that is invoked when the coroutine is
103 destroyed, which takes a pointer to the coroutine object and returns
106 Because the resume and destroy functions are shared across all suspend
107 points, suspend points must store the index of the active suspend in
108 the coroutine object, and the resume/destroy functions must switch over
109 that index to get back to the correct point. Hence the name of this
112 Pointers to the resume and destroy functions are stored in the coroutine
113 object at known offsets which are fixed for all coroutines. A completed
114 coroutine is represented with a null resume function.
116 There is a somewhat complex protocol of intrinsics for allocating and
117 deallocating the coroutine object. It is complex in order to allow the
118 allocation to be elided due to inlining. This protocol is discussed
119 in further detail below.
121 The frontend may generate code to call the coroutine function directly;
122 this will become a call to the ramp function and will return a pointer
123 to the coroutine object. The frontend should always resume or destroy
124 the coroutine using the corresponding intrinsics.
126 Returned-Continuation Lowering
127 ------------------------------
129 In returned-continuation lowering, signaled by the use of
130 `llvm.coro.id.retcon` or `llvm.coro.id.retcon.once`, some aspects of
131 the ABI must be handled more explicitly by the frontend.
133 In this lowering, every suspend point takes a list of "yielded values"
134 which are returned back to the caller along with a function pointer,
135 called the continuation function. The coroutine is resumed by simply
136 calling this continuation function pointer. The original coroutine
137 is divided into the ramp function and then an arbitrary number of
138 these continuation functions, one for each suspend point.
140 LLVM actually supports two closely-related returned-continuation
143 - In normal returned-continuation lowering, the coroutine may suspend
144 itself multiple times. This means that a continuation function
145 itself returns another continuation pointer, as well as a list of
148 The coroutine indicates that it has run to completion by returning
149 a null continuation pointer. Any yielded values will be `undef`
152 - In yield-once returned-continuation lowering, the coroutine must
153 suspend itself exactly once (or throw an exception). The ramp
154 function returns a continuation function pointer and yielded
155 values, but the continuation function simply returns `void`
156 when the coroutine has run to completion.
158 The coroutine frame is maintained in a fixed-size buffer that is
159 passed to the `coro.id` intrinsic, which guarantees a certain size
160 and alignment statically. The same buffer must be passed to the
161 continuation function(s). The coroutine will allocate memory if the
162 buffer is insufficient, in which case it will need to store at
163 least that pointer in the buffer; therefore the buffer must always
164 be at least pointer-sized. How the coroutine uses the buffer may
165 vary between suspend points.
167 In addition to the buffer pointer, continuation functions take an
168 argument indicating whether the coroutine is being resumed normally
169 (zero) or abnormally (non-zero).
171 LLVM is currently ineffective at statically eliminating allocations
172 after fully inlining returned-continuation coroutines into a caller.
173 This may be acceptable if LLVM's coroutine support is primarily being
174 used for low-level lowering and inlining is expected to be applied
175 earlier in the pipeline.
180 In async-continuation lowering, signaled by the use of `llvm.coro.id.async`,
181 handling of control-flow must be handled explicitly by the frontend.
183 In this lowering, a coroutine is assumed to take the current `async context` as
184 one of its arguments (the argument position is determined by
185 `llvm.coro.id.async`). It is used to marshal arguments and return values of the
186 coroutine. Therefore an async coroutine returns `void`.
190 define swiftcc void @async_coroutine(i8* %async.ctxt, i8*, i8*) {
193 Values live across a suspend point need to be stored in the coroutine frame to
194 be available in the continuation function. This frame is stored as a tail to the
197 Every suspend point takes an `context projection function` argument which
198 describes how-to obtain the continuations `async context` and every suspend
199 point has an associated `resume function` denoted by the
200 `llvm.coro.async.resume` intrinsic. The coroutine is resumed by calling this
201 `resume function` passing the `async context` as the one of its arguments
202 argument. The `resume function` can restore its (the caller's) `async context`
203 by applying a `context projection function` that is provided by the frontend as
204 a parameter to the `llvm.coro.suspend.async` intrinsic.
209 struct async_context {
210 struct async_context *caller_context;
214 char *context_projection_function(struct async_context *callee_ctxt) {
215 return callee_ctxt->caller_context;
220 %resume_func_ptr = call i8* @llvm.coro.async.resume()
221 call {i8*, i8*, i8*} (i8*, i8*, ...) @llvm.coro.suspend.async(
222 i8* %resume_func_ptr,
223 i8* %context_projection_function
225 The frontend should provide a `async function pointer` struct associated with
226 each async coroutine by `llvm.coro.id.async`'s argument. The initial size and
227 alignment of the `async context` must be provided as arguments to the
228 `llvm.coro.id.async` intrinsic. Lowering will update the size entry with the
229 coroutine frame requirements. The frontend is responsible for allocating the
230 memory for the `async context` but can use the `async function pointer` struct
231 to obtain the required size.
235 struct async_function_pointer {
236 uint32_t relative_function_pointer_to_async_impl;
237 uint32_t context_size;
240 Lowering will split an async coroutine into a ramp function and one resume
241 function per suspend point.
243 How control-flow is passed between caller, suspension point, and back to
244 resume function is left up to the frontend.
246 The suspend point takes a function and its arguments. The function is intended
247 to model the transfer to the callee function. It will be tail called by
248 lowering and therefore must have the same signature and calling convention as
253 call {i8*, i8*, i8*} (i8*, i8*, ...) @llvm.coro.suspend.async(
254 i8* %resume_func_ptr,
255 i8* %context_projection_function,
256 i8* (bitcast void (i8*, i8*, i8*)* to i8*) %suspend_function,
257 i8* %arg1, i8* %arg2, i8 %arg3)
259 Coroutines by Example
260 =====================
262 The examples below are all of switched-resume coroutines.
264 Coroutine Representation
265 ------------------------
267 Let's look at an example of an LLVM coroutine with the behavior sketched
268 by the following pseudo-code.
275 <suspend> // returns a coroutine handle on first suspend
279 This coroutine calls some function `print` with value `n` as an argument and
280 suspends 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
281 a `main` shown in the previous section. It will call `print` with values 4, 5
282 and 6 after which the coroutine will be destroyed.
284 The LLVM IR for this coroutine looks like this:
288 define i8* @f(i32 %n) {
290 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
291 %size = call i32 @llvm.coro.size.i32()
292 %alloc = call i8* @malloc(i32 %size)
293 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
296 %n.val = phi i32 [ %n, %entry ], [ %inc, %loop ]
297 %inc = add nsw i32 %n.val, 1
298 call void @print(i32 %n.val)
299 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
300 switch i8 %0, label %suspend [i8 0, label %loop
301 i8 1, label %cleanup]
303 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
304 call void @free(i8* %mem)
307 %unused = call i1 @llvm.coro.end(i8* %hdl, i1 false)
311 The `entry` block establishes the coroutine frame. The `coro.size`_ intrinsic is
312 lowered to a constant representing the size required for the coroutine frame.
313 The `coro.begin`_ intrinsic initializes the coroutine frame and returns the
314 coroutine handle. The second parameter of `coro.begin` is given a block of memory
315 to be used if the coroutine frame needs to be allocated dynamically.
316 The `coro.id`_ intrinsic serves as coroutine identity useful in cases when the
317 `coro.begin`_ intrinsic get duplicated by optimization passes such as
320 The `cleanup` block destroys the coroutine frame. The `coro.free`_ intrinsic,
321 given the coroutine handle, returns a pointer of the memory block to be freed or
322 `null` if the coroutine frame was not allocated dynamically. The `cleanup`
323 block is entered when coroutine runs to completion by itself or destroyed via
324 call to the `coro.destroy`_ intrinsic.
326 The `suspend` block contains code to be executed when coroutine runs to
327 completion or suspended. The `coro.end`_ intrinsic marks the point where
328 a coroutine needs to return control back to the caller if it is not an initial
329 invocation of the coroutine.
331 The `loop` blocks represents the body of the coroutine. The `coro.suspend`_
332 intrinsic in combination with the following switch indicates what happens to
333 control flow when a coroutine is suspended (default case), resumed (case 0) or
336 Coroutine Transformation
337 ------------------------
339 One of the steps of coroutine lowering is building the coroutine frame. The
340 def-use chains are analyzed to determine which objects need be kept alive across
341 suspend points. In the coroutine shown in the previous section, use of virtual register
342 `%inc` is separated from the definition by a suspend point, therefore, it
343 cannot reside on the stack frame since the latter goes away once the coroutine
344 is suspended and control is returned back to the caller. An i32 slot is
345 allocated in the coroutine frame and `%inc` is spilled and reloaded from that
348 We also store addresses of the resume and destroy functions so that the
349 `coro.resume` and `coro.destroy` intrinsics can resume and destroy the coroutine
350 when its identity cannot be determined statically at compile time. For our
351 example, the coroutine frame will be:
355 %f.frame = type { void (%f.frame*)*, void (%f.frame*)*, i32 }
357 After resume and destroy parts are outlined, function `f` will contain only the
358 code responsible for creation and initialization of the coroutine frame and
359 execution of the coroutine until a suspend point is reached:
363 define i8* @f(i32 %n) {
365 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
366 %alloc = call noalias i8* @malloc(i32 24)
367 %0 = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
368 %frame = bitcast i8* %0 to %f.frame*
369 %1 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 0
370 store void (%f.frame*)* @f.resume, void (%f.frame*)** %1
371 %2 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 1
372 store void (%f.frame*)* @f.destroy, void (%f.frame*)** %2
374 %inc = add nsw i32 %n, 1
375 %inc.spill.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 2
376 store i32 %inc, i32* %inc.spill.addr
377 call void @print(i32 %n)
382 Outlined resume part of the coroutine will reside in function `f.resume`:
386 define internal fastcc void @f.resume(%f.frame* %frame.ptr.resume) {
388 %inc.spill.addr = getelementptr %f.frame, %f.frame* %frame.ptr.resume, i64 0, i32 2
389 %inc.spill = load i32, i32* %inc.spill.addr, align 4
390 %inc = add i32 %n.val, 1
391 store i32 %inc, i32* %inc.spill.addr, align 4
392 tail call void @print(i32 %inc)
396 Whereas function `f.destroy` will contain the cleanup code for the coroutine:
400 define internal fastcc void @f.destroy(%f.frame* %frame.ptr.destroy) {
402 %0 = bitcast %f.frame* %frame.ptr.destroy to i8*
403 tail call void @free(i8* %0)
407 Avoiding Heap Allocations
408 -------------------------
410 A particular coroutine usage pattern, which is illustrated by the `main`
411 function in the overview section, where a coroutine is created, manipulated and
412 destroyed by the same calling function, is common for coroutines implementing
413 RAII idiom and is suitable for allocation elision optimization which avoid
414 dynamic allocation by storing the coroutine frame as a static `alloca` in its
417 In the entry block, we will call `coro.alloc`_ intrinsic that will return `true`
418 when dynamic allocation is required, and `false` if dynamic allocation is
424 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
425 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
426 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
428 %size = call i32 @llvm.coro.size.i32()
429 %alloc = call i8* @CustomAlloc(i32 %size)
432 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
433 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
435 In the cleanup block, we will make freeing the coroutine frame conditional on
436 `coro.free`_ intrinsic. If allocation is elided, `coro.free`_ returns `null`
437 thus skipping the deallocation code:
442 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
443 %need.dyn.free = icmp ne i8* %mem, null
444 br i1 %need.dyn.free, label %dyn.free, label %if.end
446 call void @CustomFree(i8* %mem)
451 With allocations and deallocations represented as described as above, after
452 coroutine heap allocation elision optimization, the resulting main will be:
458 call void @print(i32 4)
459 call void @print(i32 5)
460 call void @print(i32 6)
464 Multiple Suspend Points
465 -----------------------
467 Let's consider the coroutine that has more than one suspend point:
480 Matching LLVM code would look like (with the rest of the code remaining the same
481 as the code in the previous section):
486 %n.addr = phi i32 [ %n, %entry ], [ %inc, %loop.resume ]
487 call void @print(i32 %n.addr) #4
488 %2 = call i8 @llvm.coro.suspend(token none, i1 false)
489 switch i8 %2, label %suspend [i8 0, label %loop.resume
490 i8 1, label %cleanup]
492 %inc = add nsw i32 %n.addr, 1
493 %sub = xor i32 %n.addr, -1
494 call void @print(i32 %sub)
495 %3 = call i8 @llvm.coro.suspend(token none, i1 false)
496 switch i8 %3, label %suspend [i8 0, label %loop
497 i8 1, label %cleanup]
499 In this case, the coroutine frame would include a suspend index that will
500 indicate at which suspend point the coroutine needs to resume. The resume
501 function will use an index to jump to an appropriate basic block and will look
506 define internal fastcc void @f.Resume(%f.Frame* %FramePtr) {
508 %index.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 2
509 %index = load i8, i8* %index.addr, align 1
510 %switch = icmp eq i8 %index, 0
511 %n.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 3
512 %n = load i32, i32* %n.addr, align 4
513 br i1 %switch, label %loop.resume, label %loop
516 %sub = xor i32 %n, -1
517 call void @print(i32 %sub)
520 %inc = add nsw i32 %n, 1
521 store i32 %inc, i32* %n.addr, align 4
522 tail call void @print(i32 %inc)
526 %storemerge = phi i8 [ 0, %loop ], [ 1, %loop.resume ]
527 store i8 %storemerge, i8* %index.addr, align 1
531 If different cleanup code needs to get executed for different suspend points,
532 a similar switch will be in the `f.destroy` function.
536 Using suspend index in a coroutine state and having a switch in `f.resume` and
537 `f.destroy` is one of the possible implementation strategies. We explored
538 another option where a distinct `f.resume1`, `f.resume2`, etc. are created for
539 every suspend point, and instead of storing an index, the resume and destroy
540 function pointers are updated at every suspend. Early testing showed that the
541 current approach is easier on the optimizer than the latter so it is a
542 lowering strategy implemented at the moment.
544 Distinct Save and Suspend
545 -------------------------
547 In the previous example, setting a resume index (or some other state change that
548 needs to happen to prepare a coroutine for resumption) happens at the same time as
549 a suspension of a coroutine. However, in certain cases, it is necessary to control
550 when coroutine is prepared for resumption and when it is suspended.
552 In the following example, a coroutine represents some activity that is driven
553 by completions of asynchronous operations `async_op1` and `async_op2` which get
554 a coroutine handle as a parameter and resume the coroutine once async
555 operation is finished.
562 async_op1(<coroutine-handle>); // will resume once async_op1 completes
567 async_op2(<coroutine-handle>); // will resume once async_op2 completes
574 In this case, coroutine should be ready for resumption prior to a call to
575 `async_op1` and `async_op2`. The `coro.save`_ intrinsic is used to indicate a
576 point when coroutine should be ready for resumption (namely, when a resume index
577 should be stored in the coroutine frame, so that it can be resumed at the
578 correct resume point):
583 %save1 = call token @llvm.coro.save(i8* %hdl)
584 call void @async_op1(i8* %hdl)
585 %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
586 switch i8 %suspend1, label %suspend [i8 0, label %resume1
587 i8 1, label %cleanup]
589 %save2 = call token @llvm.coro.save(i8* %hdl)
590 call void @async_op2(i8* %hdl)
591 %suspend2 = call i1 @llvm.coro.suspend(token %save2, i1 false)
592 switch i8 %suspend1, label %suspend [i8 0, label %resume2
593 i8 1, label %cleanup]
595 .. _coroutine promise:
600 A coroutine author or a frontend may designate a distinguished `alloca` that can
601 be used to communicate with the coroutine. This distinguished alloca is called
602 **coroutine promise** and is provided as the second parameter to the
603 `coro.id`_ intrinsic.
605 The following coroutine designates a 32 bit integer `promise` and uses it to
606 store the current value produced by a coroutine.
610 define i8* @f(i32 %n) {
612 %promise = alloca i32
613 %pv = bitcast i32* %promise to i8*
614 %id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null)
615 %need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
616 br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
618 %size = call i32 @llvm.coro.size.i32()
619 %alloc = call i8* @malloc(i32 %size)
622 %phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
623 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
626 %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ]
627 %inc = add nsw i32 %n.val, 1
628 store i32 %n.val, i32* %promise
629 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
630 switch i8 %0, label %suspend [i8 0, label %loop
631 i8 1, label %cleanup]
633 %mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
634 call void @free(i8* %mem)
637 %unused = call i1 @llvm.coro.end(i8* %hdl, i1 false)
641 A coroutine consumer can rely on the `coro.promise`_ intrinsic to access the
648 %hdl = call i8* @f(i32 4)
649 %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false)
650 %promise.addr = bitcast i8* %promise.addr.raw to i32*
651 %val0 = load i32, i32* %promise.addr
652 call void @print(i32 %val0)
653 call void @llvm.coro.resume(i8* %hdl)
654 %val1 = load i32, i32* %promise.addr
655 call void @print(i32 %val1)
656 call void @llvm.coro.resume(i8* %hdl)
657 %val2 = load i32, i32* %promise.addr
658 call void @print(i32 %val2)
659 call void @llvm.coro.destroy(i8* %hdl)
663 After example in this section is compiled, result of the compilation will be:
669 tail call void @print(i32 4)
670 tail call void @print(i32 5)
671 tail call void @print(i32 6)
681 A coroutine author or a frontend may designate a particular suspend to be final,
682 by setting the second argument of the `coro.suspend`_ intrinsic to `true`.
683 Such a suspend point has two properties:
685 * it is possible to check whether a suspended coroutine is at the final suspend
686 point via `coro.done`_ intrinsic;
688 * a resumption of a coroutine stopped at the final suspend point leads to
689 undefined behavior. The only possible action for a coroutine at a final
690 suspend point is destroying it via `coro.destroy`_ intrinsic.
692 From the user perspective, the final suspend point represents an idea of a
693 coroutine reaching the end. From the compiler perspective, it is an optimization
694 opportunity for reducing number of resume points (and therefore switch cases) in
697 The following is an example of a function that keeps resuming the coroutine
698 until the final suspend point is reached after which point the coroutine is
705 %hdl = call i8* @f(i32 4)
708 call void @llvm.coro.resume(i8* %hdl)
709 %done = call i1 @llvm.coro.done(i8* %hdl)
710 br i1 %done, label %end, label %while
712 call void @llvm.coro.destroy(i8* %hdl)
716 Usually, final suspend point is a frontend injected suspend point that does not
717 correspond to any explicitly authored suspend point of the high level language.
718 For example, for a Python generator that has only one suspend point:
720 .. code-block:: python
726 Python frontend would inject two more suspend points, so that the actual code
731 void* coroutine(int n) {
733 <designate current_value to be coroutine promise>
734 <SUSPEND> // injected suspend point, so that the coroutine starts suspended
735 for (int i = 0; i < n; ++i) {
736 current_value = i; <SUSPEND>; // corresponds to "yield i"
738 <SUSPEND final=true> // injected final suspend point
741 and python iterator `__next__` would look like:
745 int __next__(void* hdl) {
747 if (coro.done(hdl)) throw StopIteration();
748 return *(int*)coro.promise(hdl, 4, false);
755 Coroutine Manipulation Intrinsics
756 ---------------------------------
758 Intrinsics described in this section are used to manipulate an existing
759 coroutine. They can be used in any function which happen to have a pointer
760 to a `coroutine frame`_ or a pointer to a `coroutine promise`_.
764 'llvm.coro.destroy' Intrinsic
765 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
772 declare void @llvm.coro.destroy(i8* <handle>)
777 The '``llvm.coro.destroy``' intrinsic destroys a suspended
778 switched-resume coroutine.
783 The argument is a coroutine handle to a suspended coroutine.
788 When possible, the `coro.destroy` intrinsic is replaced with a direct call to
789 the coroutine destroy function. Otherwise it is replaced with an indirect call
790 based on the function pointer for the destroy function stored in the coroutine
791 frame. Destroying a coroutine that is not suspended leads to undefined behavior.
795 'llvm.coro.resume' Intrinsic
796 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
800 declare void @llvm.coro.resume(i8* <handle>)
805 The '``llvm.coro.resume``' intrinsic resumes a suspended switched-resume coroutine.
810 The argument is a handle to a suspended coroutine.
815 When possible, the `coro.resume` intrinsic is replaced with a direct call to the
816 coroutine resume function. Otherwise it is replaced with an indirect call based
817 on the function pointer for the resume function stored in the coroutine frame.
818 Resuming a coroutine that is not suspended leads to undefined behavior.
822 'llvm.coro.done' Intrinsic
823 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
827 declare i1 @llvm.coro.done(i8* <handle>)
832 The '``llvm.coro.done``' intrinsic checks whether a suspended
833 switched-resume coroutine is at the final suspend point or not.
838 The argument is a handle to a suspended coroutine.
843 Using this intrinsic on a coroutine that does not have a `final suspend`_ point
844 or on a coroutine that is not suspended leads to undefined behavior.
848 'llvm.coro.promise' Intrinsic
849 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
853 declare i8* @llvm.coro.promise(i8* <ptr>, i32 <alignment>, i1 <from>)
858 The '``llvm.coro.promise``' intrinsic obtains a pointer to a
859 `coroutine promise`_ given a switched-resume coroutine handle and vice versa.
864 The first argument is a handle to a coroutine if `from` is false. Otherwise,
865 it is a pointer to a coroutine promise.
867 The second argument is an alignment requirements of the promise.
868 If a frontend designated `%promise = alloca i32` as a promise, the alignment
869 argument to `coro.promise` should be the alignment of `i32` on the target
870 platform. If a frontend designated `%promise = alloca i32, align 16` as a
871 promise, the alignment argument should be 16.
872 This argument only accepts constants.
874 The third argument is a boolean indicating a direction of the transformation.
875 If `from` is true, the intrinsic returns a coroutine handle given a pointer
876 to a promise. If `from` is false, the intrinsics return a pointer to a promise
877 from a coroutine handle. This argument only accepts constants.
882 Using this intrinsic on a coroutine that does not have a coroutine promise
883 leads to undefined behavior. It is possible to read and modify coroutine
884 promise of the coroutine which is currently executing. The coroutine author and
885 a coroutine user are responsible to makes sure there is no data races.
892 define i8* @f(i32 %n) {
894 %promise = alloca i32
895 %pv = bitcast i32* %promise to i8*
896 ; the second argument to coro.id points to the coroutine promise.
897 %id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null)
899 %hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %alloc)
901 store i32 42, i32* %promise ; store something into the promise
908 %hdl = call i8* @f(i32 4) ; starts the coroutine and returns its handle
909 %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false)
910 %promise.addr = bitcast i8* %promise.addr.raw to i32*
911 %val = load i32, i32* %promise.addr ; load a value from the promise
912 call void @print(i32 %val)
913 call void @llvm.coro.destroy(i8* %hdl)
917 .. _coroutine intrinsics:
919 Coroutine Structure Intrinsics
920 ------------------------------
921 Intrinsics described in this section are used within a coroutine to describe
922 the coroutine structure. They should not be used outside of a coroutine.
926 'llvm.coro.size' Intrinsic
927 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
930 declare i32 @llvm.coro.size.i32()
931 declare i64 @llvm.coro.size.i64()
936 The '``llvm.coro.size``' intrinsic returns the number of bytes
937 required to store a `coroutine frame`_. This is only supported for
938 switched-resume coroutines.
948 The `coro.size` intrinsic is lowered to a constant representing the size of
953 'llvm.coro.align' Intrinsic
954 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
957 declare i32 @llvm.coro.align.i32()
958 declare i64 @llvm.coro.align.i64()
963 The '``llvm.coro.align``' intrinsic returns the alignment of a `coroutine frame`_.
964 This is only supported for switched-resume coroutines.
974 The `coro.align` intrinsic is lowered to a constant representing the alignment of
979 'llvm.coro.begin' Intrinsic
980 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
983 declare i8* @llvm.coro.begin(token <id>, i8* <mem>)
988 The '``llvm.coro.begin``' intrinsic returns an address of the coroutine frame.
993 The first argument is a token returned by a call to '``llvm.coro.id``'
994 identifying the coroutine.
996 The second argument is a pointer to a block of memory where coroutine frame
997 will be stored if it is allocated dynamically. This pointer is ignored
998 for returned-continuation coroutines.
1003 Depending on the alignment requirements of the objects in the coroutine frame
1004 and/or on the codegen compactness reasons the pointer returned from `coro.begin`
1005 may be at offset to the `%mem` argument. (This could be beneficial if
1006 instructions that express relative access to data can be more compactly encoded
1007 with small positive and negative offsets).
1009 A frontend should emit exactly one `coro.begin` intrinsic per coroutine.
1013 'llvm.coro.free' Intrinsic
1014 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1017 declare i8* @llvm.coro.free(token %id, i8* <frame>)
1022 The '``llvm.coro.free``' intrinsic returns a pointer to a block of memory where
1023 coroutine frame is stored or `null` if this instance of a coroutine did not use
1024 dynamically allocated memory for its coroutine frame. This intrinsic is not
1025 supported for returned-continuation coroutines.
1030 The first argument is a token returned by a call to '``llvm.coro.id``'
1031 identifying the coroutine.
1033 The second argument is a pointer to the coroutine frame. This should be the same
1034 pointer that was returned by prior `coro.begin` call.
1036 Example (custom deallocation function):
1037 """""""""""""""""""""""""""""""""""""""
1039 .. code-block:: llvm
1042 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
1043 %mem_not_null = icmp ne i8* %mem, null
1044 br i1 %mem_not_null, label %if.then, label %if.end
1046 call void @CustomFree(i8* %mem)
1051 Example (standard deallocation functions):
1052 """"""""""""""""""""""""""""""""""""""""""
1054 .. code-block:: llvm
1057 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
1058 call void @free(i8* %mem)
1063 'llvm.coro.alloc' Intrinsic
1064 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1067 declare i1 @llvm.coro.alloc(token <id>)
1072 The '``llvm.coro.alloc``' intrinsic returns `true` if dynamic allocation is
1073 required to obtain a memory for the coroutine frame and `false` otherwise.
1074 This is not supported for returned-continuation coroutines.
1079 The first argument is a token returned by a call to '``llvm.coro.id``'
1080 identifying the coroutine.
1085 A frontend should emit at most one `coro.alloc` intrinsic per coroutine.
1086 The intrinsic is used to suppress dynamic allocation of the coroutine frame
1092 .. code-block:: llvm
1095 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
1096 %dyn.alloc.required = call i1 @llvm.coro.alloc(token %id)
1097 br i1 %dyn.alloc.required, label %coro.alloc, label %coro.begin
1100 %frame.size = call i32 @llvm.coro.size()
1101 %alloc = call i8* @MyAlloc(i32 %frame.size)
1102 br label %coro.begin
1105 %phi = phi i8* [ null, %entry ], [ %alloc, %coro.alloc ]
1106 %frame = call i8* @llvm.coro.begin(token %id, i8* %phi)
1110 'llvm.coro.noop' Intrinsic
1111 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1114 declare i8* @llvm.coro.noop()
1119 The '``llvm.coro.noop``' intrinsic returns an address of the coroutine frame of
1120 a coroutine that does nothing when resumed or destroyed.
1130 This intrinsic is lowered to refer to a private constant coroutine frame. The
1131 resume and destroy handlers for this frame are empty functions that do nothing.
1132 Note that in different translation units llvm.coro.noop may return different pointers.
1136 'llvm.coro.frame' Intrinsic
1137 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1140 declare i8* @llvm.coro.frame()
1145 The '``llvm.coro.frame``' intrinsic returns an address of the coroutine frame of
1146 the enclosing coroutine.
1156 This intrinsic is lowered to refer to the `coro.begin`_ instruction. This is
1157 a frontend convenience intrinsic that makes it easier to refer to the
1162 'llvm.coro.id' Intrinsic
1163 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1166 declare token @llvm.coro.id(i32 <align>, i8* <promise>, i8* <coroaddr>,
1172 The '``llvm.coro.id``' intrinsic returns a token identifying a
1173 switched-resume coroutine.
1178 The first argument provides information on the alignment of the memory returned
1179 by the allocation function and given to `coro.begin` by the first argument. If
1180 this argument is 0, the memory is assumed to be aligned to 2 * sizeof(i8*).
1181 This argument only accepts constants.
1183 The second argument, if not `null`, designates a particular alloca instruction
1184 to be a `coroutine promise`_.
1186 The third argument is `null` coming out of the frontend. The CoroEarly pass sets
1187 this argument to point to the function this coro.id belongs to.
1189 The fourth argument is `null` before coroutine is split, and later is replaced
1190 to point to a private global constant array containing function pointers to
1191 outlined resume and destroy parts of the coroutine.
1197 The purpose of this intrinsic is to tie together `coro.id`, `coro.alloc` and
1198 `coro.begin` belonging to the same coroutine to prevent optimization passes from
1199 duplicating any of these instructions unless entire body of the coroutine is
1202 A frontend should emit exactly one `coro.id` intrinsic per coroutine.
1204 A frontend should emit function attribute `presplitcoroutine` for the coroutine.
1208 'llvm.coro.id.async' Intrinsic
1209 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212 declare token @llvm.coro.id.async(i32 <context size>, i32 <align>,
1214 i8* <async function pointer>)
1219 The '``llvm.coro.id.async``' intrinsic returns a token identifying an async coroutine.
1224 The first argument provides the initial size of the `async context` as required
1225 from the frontend. Lowering will add to this size the size required by the frame
1226 storage and store that value to the `async function pointer`.
1228 The second argument, is the alignment guarantee of the memory of the
1229 `async context`. The frontend guarantees that the memory will be aligned by this
1232 The third argument is the `async context` argument in the current coroutine.
1234 The fourth argument is the address of the `async function pointer` struct.
1235 Lowering will update the context size requirement in this struct by adding the
1236 coroutine frame size requirement to the initial size requirement as specified by
1237 the first argument of this intrinsic.
1243 A frontend should emit exactly one `coro.id.async` intrinsic per coroutine.
1245 A frontend should emit function attribute `presplitcoroutine` for the coroutine.
1249 'llvm.coro.id.retcon' Intrinsic
1250 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1253 declare token @llvm.coro.id.retcon(i32 <size>, i32 <align>, i8* <buffer>,
1254 i8* <continuation prototype>,
1255 i8* <alloc>, i8* <dealloc>)
1260 The '``llvm.coro.id.retcon``' intrinsic returns a token identifying a
1261 multiple-suspend returned-continuation coroutine.
1263 The 'result-type sequence' of the coroutine is defined as follows:
1265 - if the return type of the coroutine function is ``void``, it is the
1268 - if the return type of the coroutine function is a ``struct``, it is the
1269 element types of that ``struct`` in order;
1271 - otherwise, it is just the return type of the coroutine function.
1273 The first element of the result-type sequence must be a pointer type;
1274 continuation functions will be coerced to this type. The rest of
1275 the sequence are the 'yield types', and any suspends in the coroutine
1276 must take arguments of these types.
1281 The first and second arguments are the expected size and alignment of
1282 the buffer provided as the third argument. They must be constant.
1284 The fourth argument must be a reference to a global function, called
1285 the 'continuation prototype function'. The type, calling convention,
1286 and attributes of any continuation functions will be taken from this
1287 declaration. The return type of the prototype function must match the
1288 return type of the current function. The first parameter type must be
1289 a pointer type. The second parameter type must be an integer type;
1290 it will be used only as a boolean flag.
1292 The fifth argument must be a reference to a global function that will
1293 be used to allocate memory. It may not fail, either by returning null
1294 or throwing an exception. It must take an integer and return a pointer.
1296 The sixth argument must be a reference to a global function that will
1297 be used to deallocate memory. It must take a pointer and return ``void``.
1302 A frontend should emit function attribute `presplitcoroutine` for the coroutine.
1304 'llvm.coro.id.retcon.once' Intrinsic
1305 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1308 declare token @llvm.coro.id.retcon.once(i32 <size>, i32 <align>, i8* <buffer>,
1310 i8* <alloc>, i8* <dealloc>)
1315 The '``llvm.coro.id.retcon.once``' intrinsic returns a token identifying a
1316 unique-suspend returned-continuation coroutine.
1321 As for ``llvm.core.id.retcon``, except that the return type of the
1322 continuation prototype must be `void` instead of matching the
1323 coroutine's return type.
1328 A frontend should emit function attribute `presplitcoroutine` for the coroutine.
1332 'llvm.coro.end' Intrinsic
1333 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1336 declare i1 @llvm.coro.end(i8* <handle>, i1 <unwind>)
1341 The '``llvm.coro.end``' marks the point where execution of the resume part of
1342 the coroutine should end and control should return to the caller.
1348 The first argument should refer to the coroutine handle of the enclosing
1349 coroutine. A frontend is allowed to supply null as the first parameter, in this
1350 case `coro-early` pass will replace the null with an appropriate coroutine
1353 The second argument should be `true` if this coro.end is in the block that is
1354 part of the unwind sequence leaving the coroutine body due to an exception and
1359 The purpose of this intrinsic is to allow frontends to mark the cleanup and
1360 other code that is only relevant during the initial invocation of the coroutine
1361 and should not be present in resume and destroy parts.
1363 In returned-continuation lowering, ``llvm.coro.end`` fully destroys the
1364 coroutine frame. If the second argument is `false`, it also returns from
1365 the coroutine with a null continuation pointer, and the next instruction
1366 will be unreachable. If the second argument is `true`, it falls through
1367 so that the following logic can resume unwinding. In a yield-once
1368 coroutine, reaching a non-unwind ``llvm.coro.end`` without having first
1369 reached a ``llvm.coro.suspend.retcon`` has undefined behavior.
1371 The remainder of this section describes the behavior under switched-resume
1374 This intrinsic is lowered when a coroutine is split into
1375 the start, resume and destroy parts. In the start part, it is a no-op,
1376 in resume and destroy parts, it is replaced with `ret void` instruction and
1377 the rest of the block containing `coro.end` instruction is discarded.
1378 In landing pads it is replaced with an appropriate instruction to unwind to
1379 caller. The handling of coro.end differs depending on whether the target is
1380 using landingpad or WinEH exception model.
1382 For landingpad based exception model, it is expected that frontend uses the
1383 `coro.end`_ intrinsic as follows:
1385 .. code-block:: llvm
1388 %InResumePart = call i1 @llvm.coro.end(i8* null, i1 true)
1389 br i1 %InResumePart, label %eh.resume, label %cleanup.cont
1392 ; rest of the cleanup
1395 %exn = load i8*, i8** %exn.slot, align 8
1396 %sel = load i32, i32* %ehselector.slot, align 4
1397 %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0
1398 %lpad.val29 = insertvalue { i8*, i32 } %lpad.val, i32 %sel, 1
1399 resume { i8*, i32 } %lpad.val29
1401 The `CoroSpit` pass replaces `coro.end` with ``True`` in the resume functions,
1402 thus leading to immediate unwind to the caller, whereas in start function it
1403 is replaced with ``False``, thus allowing to proceed to the rest of the cleanup
1404 code that is only needed during initial invocation of the coroutine.
1406 For Windows Exception handling model, a frontend should attach a funclet bundle
1407 referring to an enclosing cleanuppad as follows:
1409 .. code-block:: llvm
1412 %tok = cleanuppad within none []
1413 %unused = call i1 @llvm.coro.end(i8* null, i1 true) [ "funclet"(token %tok) ]
1414 cleanupret from %tok unwind label %RestOfTheCleanup
1416 The `CoroSplit` pass, if the funclet bundle is present, will insert
1417 ``cleanupret from %tok unwind to caller`` before
1418 the `coro.end`_ intrinsic and will remove the rest of the block.
1420 In the unwind path (when the argument is `true`), `coro.end` will mark the coroutine
1421 as done, making it undefined behavior to resume the coroutine again and causing
1422 `llvm.coro.done` to return `true`. This is not necessary in the normal path because
1423 the coroutine will already be marked as done by the final suspend.
1425 The following table summarizes the handling of `coro.end`_ intrinsic.
1427 +--------------------------+------------------------+---------------------------------+
1428 | | In Start Function | In Resume/Destroy Functions |
1429 +--------------------------+------------------------+---------------------------------+
1430 |unwind=false | nothing |``ret void`` |
1431 +------------+-------------+------------------------+---------------------------------+
1432 | | WinEH | mark coroutine as done || ``cleanupret unwind to caller``|
1433 | | | || mark coroutine done |
1434 |unwind=true +-------------+------------------------+---------------------------------+
1435 | | Landingpad | mark coroutine as done | mark coroutine done |
1436 +------------+-------------+------------------------+---------------------------------+
1439 'llvm.coro.end.async' Intrinsic
1440 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1443 declare i1 @llvm.coro.end.async(i8* <handle>, i1 <unwind>, ...)
1448 The '``llvm.coro.end.async``' marks the point where execution of the resume part
1449 of the coroutine should end and control should return to the caller. As part of
1450 its variable tail arguments this instruction allows to specify a function and
1451 the function's arguments that are to be tail called as the last action before
1458 The first argument should refer to the coroutine handle of the enclosing
1459 coroutine. A frontend is allowed to supply null as the first parameter, in this
1460 case `coro-early` pass will replace the null with an appropriate coroutine
1463 The second argument should be `true` if this coro.end is in the block that is
1464 part of the unwind sequence leaving the coroutine body due to an exception and
1467 The third argument if present should specify a function to be called.
1469 If the third argument is present, the remaining arguments are the arguments to
1472 .. code-block:: llvm
1474 call i1 (i8*, i1, ...) @llvm.coro.end.async(
1476 void (i8*, %async.task*, %async.actor*)* @must_tail_call_return,
1477 i8* %ctxt, %async.task* %task, %async.actor* %actor)
1483 'llvm.coro.suspend' Intrinsic
1484 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1487 declare i8 @llvm.coro.suspend(token <save>, i1 <final>)
1492 The '``llvm.coro.suspend``' marks the point where execution of a
1493 switched-resume coroutine is suspended and control is returned back
1494 to the caller. Conditional branches consuming the result of this
1495 intrinsic lead to basic blocks where coroutine should proceed when
1496 suspended (-1), resumed (0) or destroyed (1).
1501 The first argument refers to a token of `coro.save` intrinsic that marks the
1502 point when coroutine state is prepared for suspension. If `none` token is passed,
1503 the intrinsic behaves as if there were a `coro.save` immediately preceding
1504 the `coro.suspend` intrinsic.
1506 The second argument indicates whether this suspension point is `final`_.
1507 The second argument only accepts constants. If more than one suspend point is
1508 designated as final, the resume and destroy branches should lead to the same
1511 Example (normal suspend point):
1512 """""""""""""""""""""""""""""""
1514 .. code-block:: llvm
1516 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
1517 switch i8 %0, label %suspend [i8 0, label %resume
1518 i8 1, label %cleanup]
1520 Example (final suspend point):
1521 """"""""""""""""""""""""""""""
1523 .. code-block:: llvm
1526 %s.final = call i8 @llvm.coro.suspend(token none, i1 true)
1527 switch i8 %s.final, label %suspend [i8 0, label %trap
1528 i8 1, label %cleanup]
1530 call void @llvm.trap()
1536 If a coroutine that was suspended at the suspend point marked by this intrinsic
1537 is resumed via `coro.resume`_ the control will transfer to the basic block
1538 of the 0-case. If it is resumed via `coro.destroy`_, it will proceed to the
1539 basic block indicated by the 1-case. To suspend, coroutine proceed to the
1542 If suspend intrinsic is marked as final, it can consider the `true` branch
1543 unreachable and can perform optimizations that can take advantage of that fact.
1547 'llvm.coro.save' Intrinsic
1548 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1551 declare token @llvm.coro.save(i8* <handle>)
1556 The '``llvm.coro.save``' marks the point where a coroutine need to update its
1557 state to prepare for resumption to be considered suspended (and thus eligible
1558 for resumption). It is illegal to merge two '``llvm.coro.save``' calls unless their
1559 '``llvm.coro.suspend``' users are also merged. So '``llvm.coro.save``' is currently
1560 tagged with the `no_merge` function attribute.
1565 The first argument points to a coroutine handle of the enclosing coroutine.
1570 Whatever coroutine state changes are required to enable resumption of
1571 the coroutine from the corresponding suspend point should be done at the point
1572 of `coro.save` intrinsic.
1577 Separate save and suspend points are necessary when a coroutine is used to
1578 represent an asynchronous control flow driven by callbacks representing
1579 completions of asynchronous operations.
1581 In such a case, a coroutine should be ready for resumption prior to a call to
1582 `async_op` function that may trigger resumption of a coroutine from the same or
1583 a different thread possibly prior to `async_op` call returning control back
1586 .. code-block:: llvm
1588 %save1 = call token @llvm.coro.save(i8* %hdl)
1589 call void @async_op1(i8* %hdl)
1590 %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
1591 switch i8 %suspend1, label %suspend [i8 0, label %resume1
1592 i8 1, label %cleanup]
1594 .. _coro.suspend.async:
1596 'llvm.coro.suspend.async' Intrinsic
1597 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1600 declare {i8*, i8*, i8*} @llvm.coro.suspend.async(
1601 i8* <resume function>,
1602 i8* <context projection function>,
1603 ... <function to call>
1604 ... <arguments to function>)
1609 The '``llvm.coro.suspend.async``' intrinsic marks the point where
1610 execution of a async coroutine is suspended and control is passed to a callee.
1615 The first argument should be the result of the `llvm.coro.async.resume` intrinsic.
1616 Lowering will replace this intrinsic with the resume function for this suspend
1619 The second argument is the `context projection function`. It should describe
1620 how-to restore the `async context` in the continuation function from the first
1621 argument of the continuation function. Its type is `i8* (i8*)`.
1623 The third argument is the function that models transfer to the callee at the
1624 suspend point. It should take 3 arguments. Lowering will `musttail` call this
1627 The fourth to six argument are the arguments for the third argument.
1632 The result of the intrinsic are mapped to the arguments of the resume function.
1633 Execution is suspended at this intrinsic and resumed when the resume function is
1636 .. _coro.prepare.async:
1638 'llvm.coro.prepare.async' Intrinsic
1639 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1642 declare i8* @llvm.coro.prepare.async(i8* <coroutine function>)
1647 The '``llvm.coro.prepare.async``' intrinsic is used to block inlining of the
1648 async coroutine until after coroutine splitting.
1653 The first argument should be an async coroutine of type `void (i8*, i8*, i8*)`.
1654 Lowering will replace this intrinsic with its coroutine function argument.
1656 .. _coro.suspend.retcon:
1658 'llvm.coro.suspend.retcon' Intrinsic
1659 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1662 declare i1 @llvm.coro.suspend.retcon(...)
1667 The '``llvm.coro.suspend.retcon``' intrinsic marks the point where
1668 execution of a returned-continuation coroutine is suspended and control
1669 is returned back to the caller.
1671 `llvm.coro.suspend.retcon`` does not support separate save points;
1672 they are not useful when the continuation function is not locally
1673 accessible. That would be a more appropriate feature for a ``passcon``
1674 lowering that is not yet implemented.
1679 The types of the arguments must exactly match the yielded-types sequence
1680 of the coroutine. They will be turned into return values from the ramp
1681 and continuation functions, along with the next continuation function.
1686 The result of the intrinsic indicates whether the coroutine should resume
1687 abnormally (non-zero).
1689 In a normal coroutine, it is undefined behavior if the coroutine executes
1690 a call to ``llvm.coro.suspend.retcon`` after resuming abnormally.
1692 In a yield-once coroutine, it is undefined behavior if the coroutine
1693 executes a call to ``llvm.coro.suspend.retcon`` after resuming in any way.
1695 Coroutine Transformation Passes
1696 ===============================
1699 The pass CoroEarly lowers coroutine intrinsics that hide the details of the
1700 structure of the coroutine frame, but, otherwise not needed to be preserved to
1701 help later coroutine passes. This pass lowers `coro.frame`_, `coro.done`_,
1702 and `coro.promise`_ intrinsics.
1708 The pass CoroSplit buides coroutine frame and outlines resume and destroy parts
1709 into separate functions.
1713 The pass CoroElide examines if the inlined coroutine is eligible for heap
1714 allocation elision optimization. If so, it replaces
1715 `coro.begin` intrinsic with an address of a coroutine frame placed on its caller
1716 and replaces `coro.alloc` and `coro.free` intrinsics with `false` and `null`
1717 respectively to remove the deallocation code.
1718 This pass also replaces `coro.resume` and `coro.destroy` intrinsics with direct
1719 calls to resume and destroy functions for a particular coroutine where possible.
1723 This pass runs late to lower all coroutine related intrinsics not replaced by
1726 Areas Requiring Attention
1727 =========================
1728 #. When coro.suspend returns -1, the coroutine is suspended, and it's possible
1729 that the coroutine has already been destroyed (hence the frame has been freed).
1730 We cannot access anything on the frame on the suspend path.
1731 However there is nothing that prevents the compiler from moving instructions
1732 along that path (e.g. LICM), which can lead to use-after-free. At the moment
1733 we disabled LICM for loops that have coro.suspend, but the general problem still
1734 exists and requires a general solution.
1736 #. Take advantage of the lifetime intrinsics for the data that goes into the
1737 coroutine frame. Leave lifetime intrinsics as is for the data that stays in
1740 #. The CoroElide optimization pass relies on coroutine ramp function to be
1741 inlined. It would be beneficial to split the ramp function further to
1742 increase the chance that it will get inlined into its caller.
1744 #. Design a convention that would make it possible to apply coroutine heap
1745 elision optimization across ABI boundaries.
1747 #. Cannot handle coroutines with `inalloca` parameters (used in x86 on Windows).
1749 #. Alignment is ignored by coro.begin and coro.free intrinsics.
1751 #. Make required changes to make sure that coroutine optimizations work with
1754 #. More tests, more tests, more tests