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.begin' Intrinsic
954 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
957 declare i8* @llvm.coro.begin(token <id>, i8* <mem>)
962 The '``llvm.coro.begin``' intrinsic returns an address of the coroutine frame.
967 The first argument is a token returned by a call to '``llvm.coro.id``'
968 identifying the coroutine.
970 The second argument is a pointer to a block of memory where coroutine frame
971 will be stored if it is allocated dynamically. This pointer is ignored
972 for returned-continuation coroutines.
977 Depending on the alignment requirements of the objects in the coroutine frame
978 and/or on the codegen compactness reasons the pointer returned from `coro.begin`
979 may be at offset to the `%mem` argument. (This could be beneficial if
980 instructions that express relative access to data can be more compactly encoded
981 with small positive and negative offsets).
983 A frontend should emit exactly one `coro.begin` intrinsic per coroutine.
987 'llvm.coro.free' Intrinsic
988 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
991 declare i8* @llvm.coro.free(token %id, i8* <frame>)
996 The '``llvm.coro.free``' intrinsic returns a pointer to a block of memory where
997 coroutine frame is stored or `null` if this instance of a coroutine did not use
998 dynamically allocated memory for its coroutine frame. This intrinsic is not
999 supported for returned-continuation coroutines.
1004 The first argument is a token returned by a call to '``llvm.coro.id``'
1005 identifying the coroutine.
1007 The second argument is a pointer to the coroutine frame. This should be the same
1008 pointer that was returned by prior `coro.begin` call.
1010 Example (custom deallocation function):
1011 """""""""""""""""""""""""""""""""""""""
1013 .. code-block:: llvm
1016 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
1017 %mem_not_null = icmp ne i8* %mem, null
1018 br i1 %mem_not_null, label %if.then, label %if.end
1020 call void @CustomFree(i8* %mem)
1025 Example (standard deallocation functions):
1026 """"""""""""""""""""""""""""""""""""""""""
1028 .. code-block:: llvm
1031 %mem = call i8* @llvm.coro.free(token %id, i8* %frame)
1032 call void @free(i8* %mem)
1037 'llvm.coro.alloc' Intrinsic
1038 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1041 declare i1 @llvm.coro.alloc(token <id>)
1046 The '``llvm.coro.alloc``' intrinsic returns `true` if dynamic allocation is
1047 required to obtain a memory for the coroutine frame and `false` otherwise.
1048 This is not supported for returned-continuation coroutines.
1053 The first argument is a token returned by a call to '``llvm.coro.id``'
1054 identifying the coroutine.
1059 A frontend should emit at most one `coro.alloc` intrinsic per coroutine.
1060 The intrinsic is used to suppress dynamic allocation of the coroutine frame
1066 .. code-block:: llvm
1069 %id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
1070 %dyn.alloc.required = call i1 @llvm.coro.alloc(token %id)
1071 br i1 %dyn.alloc.required, label %coro.alloc, label %coro.begin
1074 %frame.size = call i32 @llvm.coro.size()
1075 %alloc = call i8* @MyAlloc(i32 %frame.size)
1076 br label %coro.begin
1079 %phi = phi i8* [ null, %entry ], [ %alloc, %coro.alloc ]
1080 %frame = call i8* @llvm.coro.begin(token %id, i8* %phi)
1084 'llvm.coro.noop' Intrinsic
1085 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1088 declare i8* @llvm.coro.noop()
1093 The '``llvm.coro.noop``' intrinsic returns an address of the coroutine frame of
1094 a coroutine that does nothing when resumed or destroyed.
1104 This intrinsic is lowered to refer to a private constant coroutine frame. The
1105 resume and destroy handlers for this frame are empty functions that do nothing.
1106 Note that in different translation units llvm.coro.noop may return different pointers.
1110 'llvm.coro.frame' Intrinsic
1111 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1114 declare i8* @llvm.coro.frame()
1119 The '``llvm.coro.frame``' intrinsic returns an address of the coroutine frame of
1120 the enclosing coroutine.
1130 This intrinsic is lowered to refer to the `coro.begin`_ instruction. This is
1131 a frontend convenience intrinsic that makes it easier to refer to the
1136 'llvm.coro.id' Intrinsic
1137 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1140 declare token @llvm.coro.id(i32 <align>, i8* <promise>, i8* <coroaddr>,
1146 The '``llvm.coro.id``' intrinsic returns a token identifying a
1147 switched-resume coroutine.
1152 The first argument provides information on the alignment of the memory returned
1153 by the allocation function and given to `coro.begin` by the first argument. If
1154 this argument is 0, the memory is assumed to be aligned to 2 * sizeof(i8*).
1155 This argument only accepts constants.
1157 The second argument, if not `null`, designates a particular alloca instruction
1158 to be a `coroutine promise`_.
1160 The third argument is `null` coming out of the frontend. The CoroEarly pass sets
1161 this argument to point to the function this coro.id belongs to.
1163 The fourth argument is `null` before coroutine is split, and later is replaced
1164 to point to a private global constant array containing function pointers to
1165 outlined resume and destroy parts of the coroutine.
1171 The purpose of this intrinsic is to tie together `coro.id`, `coro.alloc` and
1172 `coro.begin` belonging to the same coroutine to prevent optimization passes from
1173 duplicating any of these instructions unless entire body of the coroutine is
1176 A frontend should emit exactly one `coro.id` intrinsic per coroutine.
1178 A frontend should emit function attribute `"coroutine.presplit"` for the coroutine.
1182 'llvm.coro.id.async' Intrinsic
1183 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1186 declare token @llvm.coro.id.async(i32 <context size>, i32 <align>,
1188 i8* <async function pointer>)
1193 The '``llvm.coro.id.async``' intrinsic returns a token identifying an async coroutine.
1198 The first argument provides the initial size of the `async context` as required
1199 from the frontend. Lowering will add to this size the size required by the frame
1200 storage and store that value to the `async function pointer`.
1202 The second argument, is the alignment guarantee of the memory of the
1203 `async context`. The frontend guarantees that the memory will be aligned by this
1206 The third argument is the `async context` argument in the current coroutine.
1208 The fourth argument is the address of the `async function pointer` struct.
1209 Lowering will update the context size requirement in this struct by adding the
1210 coroutine frame size requirement to the initial size requirement as specified by
1211 the first argument of this intrinsic.
1217 A frontend should emit exactly one `coro.id.async` intrinsic per coroutine.
1219 A frontend should emit function attribute `"coroutine.presplit"` for the coroutine.
1223 'llvm.coro.id.retcon' Intrinsic
1224 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1227 declare token @llvm.coro.id.retcon(i32 <size>, i32 <align>, i8* <buffer>,
1228 i8* <continuation prototype>,
1229 i8* <alloc>, i8* <dealloc>)
1234 The '``llvm.coro.id.retcon``' intrinsic returns a token identifying a
1235 multiple-suspend returned-continuation coroutine.
1237 The 'result-type sequence' of the coroutine is defined as follows:
1239 - if the return type of the coroutine function is ``void``, it is the
1242 - if the return type of the coroutine function is a ``struct``, it is the
1243 element types of that ``struct`` in order;
1245 - otherwise, it is just the return type of the coroutine function.
1247 The first element of the result-type sequence must be a pointer type;
1248 continuation functions will be coerced to this type. The rest of
1249 the sequence are the 'yield types', and any suspends in the coroutine
1250 must take arguments of these types.
1255 The first and second arguments are the expected size and alignment of
1256 the buffer provided as the third argument. They must be constant.
1258 The fourth argument must be a reference to a global function, called
1259 the 'continuation prototype function'. The type, calling convention,
1260 and attributes of any continuation functions will be taken from this
1261 declaration. The return type of the prototype function must match the
1262 return type of the current function. The first parameter type must be
1263 a pointer type. The second parameter type must be an integer type;
1264 it will be used only as a boolean flag.
1266 The fifth argument must be a reference to a global function that will
1267 be used to allocate memory. It may not fail, either by returning null
1268 or throwing an exception. It must take an integer and return a pointer.
1270 The sixth argument must be a reference to a global function that will
1271 be used to deallocate memory. It must take a pointer and return ``void``.
1276 A frontend should emit function attribute `"coroutine.presplit"` for the coroutine.
1278 'llvm.coro.id.retcon.once' Intrinsic
1279 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1282 declare token @llvm.coro.id.retcon.once(i32 <size>, i32 <align>, i8* <buffer>,
1284 i8* <alloc>, i8* <dealloc>)
1289 The '``llvm.coro.id.retcon.once``' intrinsic returns a token identifying a
1290 unique-suspend returned-continuation coroutine.
1295 As for ``llvm.core.id.retcon``, except that the return type of the
1296 continuation prototype must be `void` instead of matching the
1297 coroutine's return type.
1302 A frontend should emit function attribute `"coroutine.presplit"` for the coroutine.
1306 'llvm.coro.end' Intrinsic
1307 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1310 declare i1 @llvm.coro.end(i8* <handle>, i1 <unwind>)
1315 The '``llvm.coro.end``' marks the point where execution of the resume part of
1316 the coroutine should end and control should return to the caller.
1322 The first argument should refer to the coroutine handle of the enclosing
1323 coroutine. A frontend is allowed to supply null as the first parameter, in this
1324 case `coro-early` pass will replace the null with an appropriate coroutine
1327 The second argument should be `true` if this coro.end is in the block that is
1328 part of the unwind sequence leaving the coroutine body due to an exception and
1333 The purpose of this intrinsic is to allow frontends to mark the cleanup and
1334 other code that is only relevant during the initial invocation of the coroutine
1335 and should not be present in resume and destroy parts.
1337 In returned-continuation lowering, ``llvm.coro.end`` fully destroys the
1338 coroutine frame. If the second argument is `false`, it also returns from
1339 the coroutine with a null continuation pointer, and the next instruction
1340 will be unreachable. If the second argument is `true`, it falls through
1341 so that the following logic can resume unwinding. In a yield-once
1342 coroutine, reaching a non-unwind ``llvm.coro.end`` without having first
1343 reached a ``llvm.coro.suspend.retcon`` has undefined behavior.
1345 The remainder of this section describes the behavior under switched-resume
1348 This intrinsic is lowered when a coroutine is split into
1349 the start, resume and destroy parts. In the start part, it is a no-op,
1350 in resume and destroy parts, it is replaced with `ret void` instruction and
1351 the rest of the block containing `coro.end` instruction is discarded.
1352 In landing pads it is replaced with an appropriate instruction to unwind to
1353 caller. The handling of coro.end differs depending on whether the target is
1354 using landingpad or WinEH exception model.
1356 For landingpad based exception model, it is expected that frontend uses the
1357 `coro.end`_ intrinsic as follows:
1359 .. code-block:: llvm
1362 %InResumePart = call i1 @llvm.coro.end(i8* null, i1 true)
1363 br i1 %InResumePart, label %eh.resume, label %cleanup.cont
1366 ; rest of the cleanup
1369 %exn = load i8*, i8** %exn.slot, align 8
1370 %sel = load i32, i32* %ehselector.slot, align 4
1371 %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0
1372 %lpad.val29 = insertvalue { i8*, i32 } %lpad.val, i32 %sel, 1
1373 resume { i8*, i32 } %lpad.val29
1375 The `CoroSpit` pass replaces `coro.end` with ``True`` in the resume functions,
1376 thus leading to immediate unwind to the caller, whereas in start function it
1377 is replaced with ``False``, thus allowing to proceed to the rest of the cleanup
1378 code that is only needed during initial invocation of the coroutine.
1380 For Windows Exception handling model, a frontend should attach a funclet bundle
1381 referring to an enclosing cleanuppad as follows:
1383 .. code-block:: llvm
1386 %tok = cleanuppad within none []
1387 %unused = call i1 @llvm.coro.end(i8* null, i1 true) [ "funclet"(token %tok) ]
1388 cleanupret from %tok unwind label %RestOfTheCleanup
1390 The `CoroSplit` pass, if the funclet bundle is present, will insert
1391 ``cleanupret from %tok unwind to caller`` before
1392 the `coro.end`_ intrinsic and will remove the rest of the block.
1394 In the unwind path (when the argument is `true`), `coro.end` will mark the coroutine
1395 as done, making it undefined behavior to resume the coroutine again and causing
1396 `llvm.coro.done` to return `true`. This is not necessary in the normal path because
1397 the coroutine will already be marked as done by the final suspend.
1399 The following table summarizes the handling of `coro.end`_ intrinsic.
1401 +--------------------------+------------------------+---------------------------------+
1402 | | In Start Function | In Resume/Destroy Functions |
1403 +--------------------------+------------------------+---------------------------------+
1404 |unwind=false | nothing |``ret void`` |
1405 +------------+-------------+------------------------+---------------------------------+
1406 | | WinEH | mark coroutine as done || ``cleanupret unwind to caller``|
1407 | | | || mark coroutine done |
1408 |unwind=true +-------------+------------------------+---------------------------------+
1409 | | Landingpad | mark coroutine as done | mark coroutine done |
1410 +------------+-------------+------------------------+---------------------------------+
1413 'llvm.coro.end.async' Intrinsic
1414 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1417 declare i1 @llvm.coro.end.async(i8* <handle>, i1 <unwind>, ...)
1422 The '``llvm.coro.end.async``' marks the point where execution of the resume part
1423 of the coroutine should end and control should return to the caller. As part of
1424 its variable tail arguments this instruction allows to specify a function and
1425 the function's arguments that are to be tail called as the last action before
1432 The first argument should refer to the coroutine handle of the enclosing
1433 coroutine. A frontend is allowed to supply null as the first parameter, in this
1434 case `coro-early` pass will replace the null with an appropriate coroutine
1437 The second argument should be `true` if this coro.end is in the block that is
1438 part of the unwind sequence leaving the coroutine body due to an exception and
1441 The third argument if present should specify a function to be called.
1443 If the third argument is present, the remaining arguments are the arguments to
1446 .. code-block:: llvm
1448 call i1 (i8*, i1, ...) @llvm.coro.end.async(
1450 void (i8*, %async.task*, %async.actor*)* @must_tail_call_return,
1451 i8* %ctxt, %async.task* %task, %async.actor* %actor)
1457 'llvm.coro.suspend' Intrinsic
1458 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1461 declare i8 @llvm.coro.suspend(token <save>, i1 <final>)
1466 The '``llvm.coro.suspend``' marks the point where execution of a
1467 switched-resume coroutine is suspended and control is returned back
1468 to the caller. Conditional branches consuming the result of this
1469 intrinsic lead to basic blocks where coroutine should proceed when
1470 suspended (-1), resumed (0) or destroyed (1).
1475 The first argument refers to a token of `coro.save` intrinsic that marks the
1476 point when coroutine state is prepared for suspension. If `none` token is passed,
1477 the intrinsic behaves as if there were a `coro.save` immediately preceding
1478 the `coro.suspend` intrinsic.
1480 The second argument indicates whether this suspension point is `final`_.
1481 The second argument only accepts constants. If more than one suspend point is
1482 designated as final, the resume and destroy branches should lead to the same
1485 Example (normal suspend point):
1486 """""""""""""""""""""""""""""""
1488 .. code-block:: llvm
1490 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
1491 switch i8 %0, label %suspend [i8 0, label %resume
1492 i8 1, label %cleanup]
1494 Example (final suspend point):
1495 """"""""""""""""""""""""""""""
1497 .. code-block:: llvm
1500 %s.final = call i8 @llvm.coro.suspend(token none, i1 true)
1501 switch i8 %s.final, label %suspend [i8 0, label %trap
1502 i8 1, label %cleanup]
1504 call void @llvm.trap()
1510 If a coroutine that was suspended at the suspend point marked by this intrinsic
1511 is resumed via `coro.resume`_ the control will transfer to the basic block
1512 of the 0-case. If it is resumed via `coro.destroy`_, it will proceed to the
1513 basic block indicated by the 1-case. To suspend, coroutine proceed to the
1516 If suspend intrinsic is marked as final, it can consider the `true` branch
1517 unreachable and can perform optimizations that can take advantage of that fact.
1521 'llvm.coro.save' Intrinsic
1522 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1525 declare token @llvm.coro.save(i8* <handle>)
1530 The '``llvm.coro.save``' marks the point where a coroutine need to update its
1531 state to prepare for resumption to be considered suspended (and thus eligible
1537 The first argument points to a coroutine handle of the enclosing coroutine.
1542 Whatever coroutine state changes are required to enable resumption of
1543 the coroutine from the corresponding suspend point should be done at the point
1544 of `coro.save` intrinsic.
1549 Separate save and suspend points are necessary when a coroutine is used to
1550 represent an asynchronous control flow driven by callbacks representing
1551 completions of asynchronous operations.
1553 In such a case, a coroutine should be ready for resumption prior to a call to
1554 `async_op` function that may trigger resumption of a coroutine from the same or
1555 a different thread possibly prior to `async_op` call returning control back
1558 .. code-block:: llvm
1560 %save1 = call token @llvm.coro.save(i8* %hdl)
1561 call void @async_op1(i8* %hdl)
1562 %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
1563 switch i8 %suspend1, label %suspend [i8 0, label %resume1
1564 i8 1, label %cleanup]
1566 .. _coro.suspend.async:
1568 'llvm.coro.suspend.async' Intrinsic
1569 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1572 declare {i8*, i8*, i8*} @llvm.coro.suspend.async(
1573 i8* <resume function>,
1574 i8* <context projection function>,
1575 ... <function to call>
1576 ... <arguments to function>)
1581 The '``llvm.coro.suspend.async``' intrinsic marks the point where
1582 execution of a async coroutine is suspended and control is passed to a callee.
1587 The first argument should be the result of the `llvm.coro.async.resume` intrinsic.
1588 Lowering will replace this intrinsic with the resume function for this suspend
1591 The second argument is the `context projection function`. It should describe
1592 how-to restore the `async context` in the continuation function from the first
1593 argument of the continuation function. Its type is `i8* (i8*)`.
1595 The third argument is the function that models transfer to the callee at the
1596 suspend point. It should take 3 arguments. Lowering will `musttail` call this
1599 The fourth to six argument are the arguments for the third argument.
1604 The result of the intrinsic are mapped to the arguments of the resume function.
1605 Execution is suspended at this intrinsic and resumed when the resume function is
1608 .. _coro.prepare.async:
1610 'llvm.coro.prepare.async' Intrinsic
1611 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1614 declare i8* @llvm.coro.prepare.async(i8* <coroutine function>)
1619 The '``llvm.coro.prepare.async``' intrinsic is used to block inlining of the
1620 async coroutine until after coroutine splitting.
1625 The first argument should be an async coroutine of type `void (i8*, i8*, i8*)`.
1626 Lowering will replace this intrinsic with its coroutine function argument.
1628 .. _coro.suspend.retcon:
1630 'llvm.coro.suspend.retcon' Intrinsic
1631 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1634 declare i1 @llvm.coro.suspend.retcon(...)
1639 The '``llvm.coro.suspend.retcon``' intrinsic marks the point where
1640 execution of a returned-continuation coroutine is suspended and control
1641 is returned back to the caller.
1643 `llvm.coro.suspend.retcon`` does not support separate save points;
1644 they are not useful when the continuation function is not locally
1645 accessible. That would be a more appropriate feature for a ``passcon``
1646 lowering that is not yet implemented.
1651 The types of the arguments must exactly match the yielded-types sequence
1652 of the coroutine. They will be turned into return values from the ramp
1653 and continuation functions, along with the next continuation function.
1658 The result of the intrinsic indicates whether the coroutine should resume
1659 abnormally (non-zero).
1661 In a normal coroutine, it is undefined behavior if the coroutine executes
1662 a call to ``llvm.coro.suspend.retcon`` after resuming abnormally.
1664 In a yield-once coroutine, it is undefined behavior if the coroutine
1665 executes a call to ``llvm.coro.suspend.retcon`` after resuming in any way.
1667 Coroutine Transformation Passes
1668 ===============================
1671 The pass CoroEarly lowers coroutine intrinsics that hide the details of the
1672 structure of the coroutine frame, but, otherwise not needed to be preserved to
1673 help later coroutine passes. This pass lowers `coro.frame`_, `coro.done`_,
1674 and `coro.promise`_ intrinsics.
1680 The pass CoroSplit buides coroutine frame and outlines resume and destroy parts
1681 into separate functions.
1685 The pass CoroElide examines if the inlined coroutine is eligible for heap
1686 allocation elision optimization. If so, it replaces
1687 `coro.begin` intrinsic with an address of a coroutine frame placed on its caller
1688 and replaces `coro.alloc` and `coro.free` intrinsics with `false` and `null`
1689 respectively to remove the deallocation code.
1690 This pass also replaces `coro.resume` and `coro.destroy` intrinsics with direct
1691 calls to resume and destroy functions for a particular coroutine where possible.
1695 This pass runs late to lower all coroutine related intrinsics not replaced by
1698 Areas Requiring Attention
1699 =========================
1700 #. When coro.suspend returns -1, the coroutine is suspended, and it's possible
1701 that the coroutine has already been destroyed (hence the frame has been freed).
1702 We cannot access anything on the frame on the suspend path.
1703 However there is nothing that prevents the compiler from moving instructions
1704 along that path (e.g. LICM), which can lead to use-after-free. At the moment
1705 we disabled LICM for loops that have coro.suspend, but the general problem still
1706 exists and requires a general solution.
1708 #. Take advantage of the lifetime intrinsics for the data that goes into the
1709 coroutine frame. Leave lifetime intrinsics as is for the data that stays in
1712 #. The CoroElide optimization pass relies on coroutine ramp function to be
1713 inlined. It would be beneficial to split the ramp function further to
1714 increase the chance that it will get inlined into its caller.
1716 #. Design a convention that would make it possible to apply coroutine heap
1717 elision optimization across ABI boundaries.
1719 #. Cannot handle coroutines with `inalloca` parameters (used in x86 on Windows).
1721 #. Alignment is ignored by coro.begin and coro.free intrinsics.
1723 #. Make required changes to make sure that coroutine optimizations work with
1726 #. More tests, more tests, more tests