@@ -789,13 +789,16 @@ coroutine, where a normal function return would need to transfer ownership of
789
789
its return value, since a normal function's context ceases to exist and be able
790
790
to maintain ownership of the value after it returns.
791
791
792
- To support these concepts, SIL supports two kinds of coroutine:
793
- ``@yield_many `` and ``@yield_once ``. Either of these attributes may be
792
+ To support these concepts, SIL supports two flavors: single-yield and
793
+ multi-yield. These two flavors correspond to three kinds. A multi-yield
794
+ coroutine is of kind ``@yield_many ``. A single-yield coroutine is of kind
795
+ either ``@yield_once `` or ``@yield_once_2 ``. Any of these attributes may be
794
796
written before a function type to indicate that it is a coroutine type.
795
- ``@yield_many `` and ``@yield_once `` coroutines are allowed to also be
796
- ``@async ``. (Note that ``@async `` functions are not themselves modeled
797
- explicitly as coroutines in SIL, although the implementation may use a coroutine
798
- lowering strategy.)
797
+
798
+ Both single-yield and multi-yield coroutines are allowed to also be ``@async ``.
799
+ (Note that ``@async `` functions are not themselves modeled explicitly as
800
+ coroutines in SIL, although the implementation may use a coroutine lowering
801
+ strategy.)
799
802
800
803
A coroutine type may declare any number of *yielded values *, which is to
801
804
say, values which are provided to the caller at a yield point. Yielded
@@ -816,9 +819,10 @@ calling a yield-many coroutine of any kind.
816
819
Coroutines may contain the special ``yield `` and ``unwind ``
817
820
instructions.
818
821
819
- A ``@yield_many `` coroutine may yield as many times as it desires.
820
- A ``@yield_once `` coroutine may yield exactly once before returning,
821
- although it may also ``throw `` before reaching that point.
822
+ A multi-yield (``@yield_many ``) coroutine may yield as many times as it desires.
823
+ A single-yield (``@yield_once `` or ``@yield_once_2 ``) coroutine may yield
824
+ exactly once before returning, although it may also ``throw `` before reaching
825
+ that point.
822
826
823
827
Variadic Generics
824
828
`````````````````
@@ -6128,23 +6132,36 @@ begin_apply
6128
6132
// %float : $Float
6129
6133
// %token is a token
6130
6134
6135
+ (%anyAddr, %float, %token, %allocation) = begin_apply %0() : $@yield_once_2 () -> (@yields @inout %Any, @yields Float)
6136
+ // %anyAddr : $*Any
6137
+ // %float : $Float
6138
+ // %token is a token
6139
+ // %allocation is a pointer to a token
6140
+
6131
6141
Transfers control to coroutine ``%0 ``, passing it the given arguments.
6132
6142
The rules for the application generally follow the rules for ``apply ``,
6133
6143
except:
6134
6144
6135
- - the callee value must have a ``yield_once `` coroutine type,
6145
+ - the callee value must have be of single-yield coroutine type (``yield_once ``
6146
+ or ``yield_once_2 ``)
6136
6147
6137
6148
- control returns to this function not when the coroutine performs a
6138
6149
``return ``, but when it performs a ``yield ``, and
6139
6150
6140
6151
- the instruction results are derived from the yields of the coroutine
6141
6152
instead of its normal results.
6142
6153
6143
- The final result of a ``begin_apply `` is a "token", a special value which
6144
- can only be used as the operand of an ``end_apply `` or ``abort_apply ``
6145
- instruction. Before this second instruction is executed, the coroutine
6146
- is said to be "suspended", and the token represents a reference to its
6147
- suspended activation record.
6154
+ The final (in the case of ``@yield_once ``) or penultimate (in the case of
6155
+ ``@yield_once_2 ``) result of a ``begin_apply `` is a "token", a special value
6156
+ which can only be used as the operand of an ``end_apply `` or ``abort_apply ``
6157
+ instruction. Before this second instruction is executed, the coroutine is said
6158
+ to be "suspended", and the token represents a reference to its suspended
6159
+ activation record.
6160
+
6161
+ If the coroutine's kind ``yield_once_2 ``, its final result is an address of a
6162
+ "token", representing the allocation done by the callee coroutine. It can only
6163
+ be used as the operand of a ``dealloc_stack `` which must appear after the
6164
+ coroutine is resumed.
6148
6165
6149
6166
The other results of the instruction correspond to the yields in the
6150
6167
coroutine type. In general, the rules of a yield are similar to the rules
@@ -8376,9 +8393,10 @@ the current function was invoked with a ``try_apply`` instruction, control
8376
8393
resumes at the normal destination, and the value of the basic block argument
8377
8394
will be the operand of this ``return `` instruction.
8378
8395
8379
- If the current function is a ``yield_once `` coroutine, there must not be
8380
- a path from the entry block to a ``return `` which does not pass through
8381
- a ``yield `` instruction. This rule does not apply in the ``raw `` SIL stage.
8396
+ If the current function is a single-yield coroutine (``yield_once `` or
8397
+ ``yield_once_2 ``), there must not be a path from the entry block to a
8398
+ ``return `` which does not pass through a ``yield `` instruction. This rule does
8399
+ not apply in the ``raw `` SIL stage.
8382
8400
8383
8401
``return `` does not retain or release its operand or any other values.
8384
8402
@@ -8446,9 +8464,10 @@ The ``resume`` and ``unwind`` destination blocks must be uniquely
8446
8464
referenced by the ``yield `` instruction. This prevents them from becoming
8447
8465
critical edges.
8448
8466
8449
- In a ``yield_once `` coroutine, there must not be a control flow path leading
8450
- from the ``resume `` edge to another ``yield `` instruction in this function.
8451
- This rule does not apply in the ``raw `` SIL stage.
8467
+ In a single-yield coroutine (``yield_once `` or ``yield_once_2 ``), there must
8468
+ not be a control flow path leading from the ``resume `` edge to another
8469
+ ``yield `` instruction in this function. This rule does not apply in the ``raw ``
8470
+ SIL stage.
8452
8471
8453
8472
There must not be a control flow path leading from the ``unwind `` edge to
8454
8473
a ``return `` instruction, to a ``throw `` instruction, or to any block
0 commit comments