The LLVM IR for this coroutine looks like this:
-.. code-block:: none
+.. code-block:: llvm
define i8* @f(i32 %n) {
entry:
when its identity cannot be determined statically at compile time. For our
example, the coroutine frame will be:
-.. code-block:: text
+.. code-block:: llvm
%f.frame = type { void (%f.frame*)*, void (%f.frame*)*, i32 }
code responsible for creation and initialization of the coroutine frame and
execution of the coroutine until a suspend point is reached:
-.. code-block:: none
+.. code-block:: llvm
define i8* @f(i32 %n) {
entry:
when dynamic allocation is required, and `false` if dynamic allocation is
elided.
-.. code-block:: none
+.. code-block:: llvm
entry:
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
`coro.free`_ intrinsic. If allocation is elided, `coro.free`_ returns `null`
thus skipping the deallocation code:
-.. code-block:: text
+.. code-block:: llvm
cleanup:
%mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
Matching LLVM code would look like (with the rest of the code remaining the same
as the code in the previous section):
-.. code-block:: text
+.. code-block:: llvm
loop:
%n.addr = phi i32 [ %n, %entry ], [ %inc, %loop.resume ]
should be stored in the coroutine frame, so that it can be resumed at the
correct resume point):
-.. code-block:: text
+.. code-block:: llvm
if.true:
%save1 = call token @llvm.coro.save(i8* %hdl)
- call void async_op1(i8* %hdl)
+ call void @async_op1(i8* %hdl)
%suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
switch i8 %suspend1, label %suspend [i8 0, label %resume1
i8 1, label %cleanup]
if.false:
%save2 = call token @llvm.coro.save(i8* %hdl)
- call void async_op2(i8* %hdl)
+ call void @async_op2(i8* %hdl)
%suspend2 = call i1 @llvm.coro.suspend(token %save2, i1 false)
switch i8 %suspend1, label %suspend [i8 0, label %resume2
i8 1, label %cleanup]
The following coroutine designates a 32 bit integer `promise` and uses it to
store the current value produced by a coroutine.
-.. code-block:: text
+.. code-block:: llvm
define i8* @f(i32 %n) {
entry:
Example:
""""""""
-.. code-block:: text
+.. code-block:: llvm
define i8* @f(i32 %n) {
entry:
Example (custom deallocation function):
"""""""""""""""""""""""""""""""""""""""
-.. code-block:: text
+.. code-block:: llvm
cleanup:
%mem = call i8* @llvm.coro.free(token %id, i8* %frame)
Example (standard deallocation functions):
""""""""""""""""""""""""""""""""""""""""""
-.. code-block:: text
+.. code-block:: llvm
cleanup:
%mem = call i8* @llvm.coro.free(token %id, i8* %frame)
Example:
""""""""
-.. code-block:: text
+.. code-block:: llvm
entry:
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
For Windows Exception handling model, a frontend should attach a funclet bundle
referring to an enclosing cleanuppad as follows:
-.. code-block:: text
+.. code-block:: llvm
ehcleanup:
%tok = cleanuppad within none []
Example (normal suspend point):
"""""""""""""""""""""""""""""""
-.. code-block:: text
+.. code-block:: llvm
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %resume
Example (final suspend point):
""""""""""""""""""""""""""""""
-.. code-block:: text
+.. code-block:: llvm
while.end:
%s.final = call i8 @llvm.coro.suspend(token none, i1 true)
a different thread possibly prior to `async_op` call returning control back
to the coroutine:
-.. code-block:: text
+.. code-block:: llvm
%save1 = call token @llvm.coro.save(i8* %hdl)
- call void async_op1(i8* %hdl)
+ call void @async_op1(i8* %hdl)
%suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
switch i8 %suspend1, label %suspend [i8 0, label %resume1
i8 1, label %cleanup]