1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
|
=====================================
Coroutines in LLVM
=====================================
.. contents::
:local:
:depth: 3
.. warning::
Compatibility across LLVM releases is not guaranteed.
Introduction
============
.. _coroutine handle:
LLVM coroutines are functions that have one or more `suspend points`_.
When a suspend point is reached, the execution of a coroutine is suspended and
control is returned back to its caller. A suspended coroutine can be resumed
to continue execution from the last suspend point or it can be destroyed.
In the following example, we call function `f` (which may or may not be a
coroutine itself) that returns a handle to a suspended coroutine
(**coroutine handle**) that is used by `main` to resume the coroutine twice and
then destroy it:
.. code-block:: llvm
define i32 @main() {
entry:
%hdl = call ptr @f(i32 4)
call void @llvm.coro.resume(ptr %hdl)
call void @llvm.coro.resume(ptr %hdl)
call void @llvm.coro.destroy(ptr %hdl)
ret i32 0
}
.. _coroutine frame:
In addition to the function stack frame which exists when a coroutine is
executing, there is an additional region of storage that contains objects that
keep the coroutine state when a coroutine is suspended. This region of storage
is called the **coroutine frame**. It is created when a coroutine is called
and destroyed when a coroutine either runs to completion or is destroyed
while suspended.
LLVM currently supports two styles of coroutine lowering. These styles
support substantially different sets of features, have substantially
different ABIs, and expect substantially different patterns of frontend
code generation. However, the styles also have a great deal in common.
In all cases, an LLVM coroutine is initially represented as an ordinary LLVM
function that has calls to `coroutine intrinsics`_ defining the structure of
the coroutine. The coroutine function is then, in the most general case,
rewritten by the coroutine lowering passes to become the "ramp function",
the initial entrypoint of the coroutine, which executes until a suspend point
is first reached. The remainder of the original coroutine function is split
out into some number of "resume functions". Any state which must persist
across suspensions is stored in the coroutine frame. The resume functions
must somehow be able to handle either a "normal" resumption, which continues
the normal execution of the coroutine, or an "abnormal" resumption, which
must unwind the coroutine without attempting to suspend it.
Switched-Resume Lowering
------------------------
In LLVM's standard switched-resume lowering, signaled by the use of
`llvm.coro.id`, the coroutine frame is stored as part of a "coroutine
object" which represents a handle to a particular invocation of the
coroutine. All coroutine objects support a common ABI allowing certain
features to be used without knowing anything about the coroutine's
implementation:
- A coroutine object can be queried to see if it has reached completion
with `llvm.coro.done`.
- A coroutine object can be resumed normally if it has not already reached
completion with `llvm.coro.resume`.
- A coroutine object can be destroyed, invalidating the coroutine object,
with `llvm.coro.destroy`. This must be done separately even if the
coroutine has reached completion normally.
- "Promise" storage, which is known to have a certain size and alignment,
can be projected out of the coroutine object with `llvm.coro.promise`.
The coroutine implementation must have been compiled to define a promise
of the same size and alignment.
In general, interacting with a coroutine object in any of these ways while
it is running has undefined behavior.
The coroutine function is split into three functions, representing three
different ways that control can enter the coroutine:
1. the ramp function that is initially invoked, which takes arbitrary
arguments and returns a pointer to the coroutine object;
2. a coroutine resume function that is invoked when the coroutine is resumed,
which takes a pointer to the coroutine object and returns `void`;
3. a coroutine destroy function that is invoked when the coroutine is
destroyed, which takes a pointer to the coroutine object and returns
`void`.
Because the resume and destroy functions are shared across all suspend
points, suspend points must store the index of the active suspend in
the coroutine object, and the resume/destroy functions must switch over
that index to get back to the correct point. Hence the name of this
lowering.
Pointers to the resume and destroy functions are stored in the coroutine
object at known offsets which are fixed for all coroutines. A completed
coroutine is represented with a null resume function.
There is a somewhat complex protocol of intrinsics for allocating and
deallocating the coroutine object. It is complex in order to allow the
allocation to be elided due to inlining. This protocol is discussed
in further detail below.
The frontend may generate code to call the coroutine function directly;
this will become a call to the ramp function and will return a pointer
to the coroutine object. The frontend should always resume or destroy
the coroutine using the corresponding intrinsics.
Returned-Continuation Lowering
------------------------------
In returned-continuation lowering, signaled by the use of
`llvm.coro.id.retcon` or `llvm.coro.id.retcon.once`, some aspects of
the ABI must be handled more explicitly by the frontend.
In this lowering, every suspend point takes a list of "yielded values"
which are returned back to the caller along with a function pointer,
called the continuation function. The coroutine is resumed by simply
calling this continuation function pointer. The original coroutine
is divided into the ramp function and then an arbitrary number of
these continuation functions, one for each suspend point.
LLVM actually supports two closely-related returned-continuation
lowerings:
- In normal returned-continuation lowering, the coroutine may suspend
itself multiple times. This means that a continuation function
itself returns another continuation pointer, as well as a list of
yielded values.
The coroutine indicates that it has run to completion by returning
a null continuation pointer. Any yielded values will be `undef`
should be ignored.
- In yield-once returned-continuation lowering, the coroutine must
suspend itself exactly once (or throw an exception). The ramp
function returns a continuation function pointer and yielded
values, the continuation function may optionally return ordinary
results when the coroutine has run to completion.
The coroutine frame is maintained in a fixed-size buffer that is
passed to the `coro.id` intrinsic, which guarantees a certain size
and alignment statically. The same buffer must be passed to the
continuation function(s). The coroutine will allocate memory if the
buffer is insufficient, in which case it will need to store at
least that pointer in the buffer; therefore the buffer must always
be at least pointer-sized. How the coroutine uses the buffer may
vary between suspend points.
In addition to the buffer pointer, continuation functions take an
argument indicating whether the coroutine is being resumed normally
(zero) or abnormally (non-zero).
LLVM is currently ineffective at statically eliminating allocations
after fully inlining returned-continuation coroutines into a caller.
This may be acceptable if LLVM's coroutine support is primarily being
used for low-level lowering and inlining is expected to be applied
earlier in the pipeline.
Async Lowering
--------------
In async-continuation lowering, signaled by the use of `llvm.coro.id.async`,
handling of control-flow must be handled explicitly by the frontend.
In this lowering, a coroutine is assumed to take the current `async context` as
one of its arguments (the argument position is determined by
`llvm.coro.id.async`). It is used to marshal arguments and return values of the
coroutine. Therefore an async coroutine returns `void`.
.. code-block:: llvm
define swiftcc void @async_coroutine(ptr %async.ctxt, ptr, ptr) {
}
Values live across a suspend point need to be stored in the coroutine frame to
be available in the continuation function. This frame is stored as a tail to the
`async context`.
Every suspend point takes an `context projection function` argument which
describes how-to obtain the continuations `async context` and every suspend
point has an associated `resume function` denoted by the
`llvm.coro.async.resume` intrinsic. The coroutine is resumed by calling this
`resume function` passing the `async context` as the one of its arguments
argument. The `resume function` can restore its (the caller's) `async context`
by applying a `context projection function` that is provided by the frontend as
a parameter to the `llvm.coro.suspend.async` intrinsic.
.. code-block:: c
// For example:
struct async_context {
struct async_context *caller_context;
...
}
char *context_projection_function(struct async_context *callee_ctxt) {
return callee_ctxt->caller_context;
}
.. code-block:: llvm
%resume_func_ptr = call ptr @llvm.coro.async.resume()
call {ptr, ptr, ptr} (ptr, ptr, ...) @llvm.coro.suspend.async(
ptr %resume_func_ptr,
ptr %context_projection_function
The frontend should provide a `async function pointer` struct associated with
each async coroutine by `llvm.coro.id.async`'s argument. The initial size and
alignment of the `async context` must be provided as arguments to the
`llvm.coro.id.async` intrinsic. Lowering will update the size entry with the
coroutine frame requirements. The frontend is responsible for allocating the
memory for the `async context` but can use the `async function pointer` struct
to obtain the required size.
.. code-block:: c
struct async_function_pointer {
uint32_t relative_function_pointer_to_async_impl;
uint32_t context_size;
}
Lowering will split an async coroutine into a ramp function and one resume
function per suspend point.
How control-flow is passed between caller, suspension point, and back to
resume function is left up to the frontend.
The suspend point takes a function and its arguments. The function is intended
to model the transfer to the callee function. It will be tail called by
lowering and therefore must have the same signature and calling convention as
the async coroutine.
.. code-block:: llvm
call {ptr, ptr, ptr} (ptr, ptr, ...) @llvm.coro.suspend.async(
ptr %resume_func_ptr,
ptr %context_projection_function,
ptr %suspend_function,
ptr %arg1, ptr %arg2, i8 %arg3)
Coroutines by Example
=====================
The examples below are all of switched-resume coroutines.
Coroutine Representation
------------------------
Let's look at an example of an LLVM coroutine with the behavior sketched
by the following pseudo-code.
.. code-block:: c++
void *f(int n) {
for(;;) {
print(n++);
<suspend> // returns a coroutine handle on first suspend
}
}
This coroutine calls some function `print` with value `n` as an argument and
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
a `main` shown in the previous section. It will call `print` with values 4, 5
and 6 after which the coroutine will be destroyed.
The LLVM IR for this coroutine looks like this:
.. code-block:: llvm
define ptr @f(i32 %n) presplitcoroutine {
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%size = call i32 @llvm.coro.size.i32()
%alloc = call ptr @malloc(i32 %size)
%hdl = call noalias ptr @llvm.coro.begin(token %id, ptr %alloc)
br label %loop
loop:
%n.val = phi i32 [ %n, %entry ], [ %inc, %loop ]
%inc = add nsw i32 %n.val, 1
call void @print(i32 %n.val)
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %loop
i8 1, label %cleanup]
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
call void @free(ptr %mem)
br label %suspend
suspend:
%unused = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
ret ptr %hdl
}
The `entry` block establishes the coroutine frame. The `coro.size`_ intrinsic is
lowered to a constant representing the size required for the coroutine frame.
The `coro.begin`_ intrinsic initializes the coroutine frame and returns the
coroutine handle. The second parameter of `coro.begin` is given a block of memory
to be used if the coroutine frame needs to be allocated dynamically.
The `coro.id`_ intrinsic serves as coroutine identity useful in cases when the
`coro.begin`_ intrinsic get duplicated by optimization passes such as
jump-threading.
The `cleanup` block destroys the coroutine frame. The `coro.free`_ intrinsic,
given the coroutine handle, returns a pointer of the memory block to be freed or
`null` if the coroutine frame was not allocated dynamically. The `cleanup`
block is entered when coroutine runs to completion by itself or destroyed via
call to the `coro.destroy`_ intrinsic.
The `suspend` block contains code to be executed when coroutine runs to
completion or suspended. The `coro.end`_ intrinsic marks the point where
a coroutine needs to return control back to the caller if it is not an initial
invocation of the coroutine.
The `loop` blocks represents the body of the coroutine. The `coro.suspend`_
intrinsic in combination with the following switch indicates what happens to
control flow when a coroutine is suspended (default case), resumed (case 0) or
destroyed (case 1).
Coroutine Transformation
------------------------
One of the steps of coroutine lowering is building the coroutine frame. The
def-use chains are analyzed to determine which objects need be kept alive across
suspend points. In the coroutine shown in the previous section, use of virtual register
`%inc` is separated from the definition by a suspend point, therefore, it
cannot reside on the stack frame since the latter goes away once the coroutine
is suspended and control is returned back to the caller. An i32 slot is
allocated in the coroutine frame and `%inc` is spilled and reloaded from that
slot as needed.
We also store addresses of the resume and destroy functions so that the
`coro.resume` and `coro.destroy` intrinsics can resume and destroy the coroutine
when its identity cannot be determined statically at compile time. For our
example, the coroutine frame will be:
.. code-block:: llvm
%f.frame = type { ptr, ptr, i32 }
After resume and destroy parts are outlined, function `f` will contain only the
code responsible for creation and initialization of the coroutine frame and
execution of the coroutine until a suspend point is reached:
.. code-block:: llvm
define ptr @f(i32 %n) {
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%alloc = call noalias ptr @malloc(i32 24)
%frame = call noalias ptr @llvm.coro.begin(token %id, ptr %alloc)
%1 = getelementptr %f.frame, ptr %frame, i32 0, i32 0
store ptr @f.resume, ptr %1
%2 = getelementptr %f.frame, ptr %frame, i32 0, i32 1
store ptr @f.destroy, ptr %2
%inc = add nsw i32 %n, 1
%inc.spill.addr = getelementptr inbounds %f.Frame, ptr %FramePtr, i32 0, i32 2
store i32 %inc, ptr %inc.spill.addr
call void @print(i32 %n)
ret ptr %frame
}
Outlined resume part of the coroutine will reside in function `f.resume`:
.. code-block:: llvm
define internal fastcc void @f.resume(ptr %frame.ptr.resume) {
entry:
%inc.spill.addr = getelementptr %f.frame, ptr %frame.ptr.resume, i64 0, i32 2
%inc.spill = load i32, ptr %inc.spill.addr, align 4
%inc = add i32 %inc.spill, 1
store i32 %inc, ptr %inc.spill.addr, align 4
tail call void @print(i32 %inc)
ret void
}
Whereas function `f.destroy` will contain the cleanup code for the coroutine:
.. code-block:: llvm
define internal fastcc void @f.destroy(ptr %frame.ptr.destroy) {
entry:
tail call void @free(ptr %frame.ptr.destroy)
ret void
}
Avoiding Heap Allocations
-------------------------
A particular coroutine usage pattern, which is illustrated by the `main`
function in the overview section, where a coroutine is created, manipulated and
destroyed by the same calling function, is common for coroutines implementing
RAII idiom and is suitable for allocation elision optimization which avoid
dynamic allocation by storing the coroutine frame as a static `alloca` in its
caller.
In the entry block, we will call `coro.alloc`_ intrinsic that will return `true`
when dynamic allocation is required, and `false` if dynamic allocation is
elided.
.. code-block:: llvm
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
dyn.alloc:
%size = call i32 @llvm.coro.size.i32()
%alloc = call ptr @CustomAlloc(i32 %size)
br label %coro.begin
coro.begin:
%phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
%hdl = call noalias ptr @llvm.coro.begin(token %id, ptr %phi)
In the cleanup block, we will make freeing the coroutine frame conditional on
`coro.free`_ intrinsic. If allocation is elided, `coro.free`_ returns `null`
thus skipping the deallocation code:
.. code-block:: llvm
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
%need.dyn.free = icmp ne ptr %mem, null
br i1 %need.dyn.free, label %dyn.free, label %if.end
dyn.free:
call void @CustomFree(ptr %mem)
br label %if.end
if.end:
...
With allocations and deallocations represented as described as above, after
coroutine heap allocation elision optimization, the resulting main will be:
.. code-block:: llvm
define i32 @main() {
entry:
call void @print(i32 4)
call void @print(i32 5)
call void @print(i32 6)
ret i32 0
}
Multiple Suspend Points
-----------------------
Let's consider the coroutine that has more than one suspend point:
.. code-block:: c++
void *f(int n) {
for(;;) {
print(n++);
<suspend>
print(-n);
<suspend>
}
}
Matching LLVM code would look like (with the rest of the code remaining the same
as the code in the previous section):
.. code-block:: llvm
loop:
%n.addr = phi i32 [ %n, %entry ], [ %inc, %loop.resume ]
call void @print(i32 %n.addr) #4
%2 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %2, label %suspend [i8 0, label %loop.resume
i8 1, label %cleanup]
loop.resume:
%inc = add nsw i32 %n.addr, 1
%sub = xor i32 %n.addr, -1
call void @print(i32 %sub)
%3 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %3, label %suspend [i8 0, label %loop
i8 1, label %cleanup]
In this case, the coroutine frame would include a suspend index that will
indicate at which suspend point the coroutine needs to resume.
.. code-block:: llvm
%f.frame = type { ptr, ptr, i32, i32 }
The resume function will use an index to jump to an appropriate basic block and will look
as follows:
.. code-block:: llvm
define internal fastcc void @f.Resume(ptr %FramePtr) {
entry.Resume:
%index.addr = getelementptr inbounds %f.Frame, ptr %FramePtr, i64 0, i32 2
%index = load i8, ptr %index.addr, align 1
%switch = icmp eq i8 %index, 0
%n.addr = getelementptr inbounds %f.Frame, ptr %FramePtr, i64 0, i32 3
%n = load i32, ptr %n.addr, align 4
br i1 %switch, label %loop.resume, label %loop
loop.resume:
%sub = sub nsw i32 0, %n
call void @print(i32 %sub)
br label %suspend
loop:
%inc = add nsw i32 %n, 1
store i32 %inc, ptr %n.addr, align 4
tail call void @print(i32 %inc)
br label %suspend
suspend:
%storemerge = phi i8 [ 0, %loop ], [ 1, %loop.resume ]
store i8 %storemerge, ptr %index.addr, align 1
ret void
}
If different cleanup code needs to get executed for different suspend points,
a similar switch will be in the `f.destroy` function.
.. note ::
Using suspend index in a coroutine state and having a switch in `f.resume` and
`f.destroy` is one of the possible implementation strategies. We explored
another option where a distinct `f.resume1`, `f.resume2`, etc. are created for
every suspend point, and instead of storing an index, the resume and destroy
function pointers are updated at every suspend. Early testing showed that the
current approach is easier on the optimizer than the latter so it is a
lowering strategy implemented at the moment.
Distinct Save and Suspend
-------------------------
In the previous example, setting a resume index (or some other state change that
needs to happen to prepare a coroutine for resumption) happens at the same time as
a suspension of a coroutine. However, in certain cases, it is necessary to control
when coroutine is prepared for resumption and when it is suspended.
In the following example, a coroutine represents some activity that is driven
by completions of asynchronous operations `async_op1` and `async_op2` which get
a coroutine handle as a parameter and resume the coroutine once async
operation is finished.
.. code-block:: text
void g() {
for (;;)
if (cond()) {
async_op1(<coroutine-handle>); // will resume once async_op1 completes
<suspend>
do_one();
}
else {
async_op2(<coroutine-handle>); // will resume once async_op2 completes
<suspend>
do_two();
}
}
}
In this case, coroutine should be ready for resumption prior to a call to
`async_op1` and `async_op2`. The `coro.save`_ intrinsic is used to indicate a
point when coroutine should be ready for resumption (namely, when a resume index
should be stored in the coroutine frame, so that it can be resumed at the
correct resume point):
.. code-block:: llvm
if.true:
%save1 = call token @llvm.coro.save(ptr %hdl)
call void @async_op1(ptr %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(ptr %hdl)
call void @async_op2(ptr %hdl)
%suspend2 = call i1 @llvm.coro.suspend(token %save2, i1 false)
switch i8 %suspend2, label %suspend [i8 0, label %resume2
i8 1, label %cleanup]
.. _coroutine promise:
Coroutine Promise
-----------------
A coroutine author or a frontend may designate a distinguished `alloca` that can
be used to communicate with the coroutine. This distinguished alloca is called
**coroutine promise** and is provided as the second parameter to the
`coro.id`_ intrinsic.
The following coroutine designates a 32-bit integer `promise` and uses it to
store the current value produced by a coroutine.
.. code-block:: llvm
define ptr @f(i32 %n) {
entry:
%promise = alloca i32
%id = call token @llvm.coro.id(i32 0, ptr %promise, ptr null, ptr null)
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
dyn.alloc:
%size = call i32 @llvm.coro.size.i32()
%alloc = call ptr @malloc(i32 %size)
br label %coro.begin
coro.begin:
%phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
%hdl = call noalias ptr @llvm.coro.begin(token %id, ptr %phi)
br label %loop
loop:
%n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ]
%inc = add nsw i32 %n.val, 1
store i32 %n.val, ptr %promise
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %loop
i8 1, label %cleanup]
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
call void @free(ptr %mem)
br label %suspend
suspend:
%unused = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
ret ptr %hdl
}
A coroutine consumer can rely on the `coro.promise`_ intrinsic to access the
coroutine promise.
.. code-block:: llvm
define i32 @main() {
entry:
%hdl = call ptr @f(i32 4)
%promise.addr = call ptr @llvm.coro.promise(ptr %hdl, i32 4, i1 false)
%val0 = load i32, ptr %promise.addr
call void @print(i32 %val0)
call void @llvm.coro.resume(ptr %hdl)
%val1 = load i32, ptr %promise.addr
call void @print(i32 %val1)
call void @llvm.coro.resume(ptr %hdl)
%val2 = load i32, ptr %promise.addr
call void @print(i32 %val2)
call void @llvm.coro.destroy(ptr %hdl)
ret i32 0
}
After example in this section is compiled, result of the compilation will be:
.. code-block:: llvm
define i32 @main() {
entry:
tail call void @print(i32 4)
tail call void @print(i32 5)
tail call void @print(i32 6)
ret i32 0
}
.. _final:
.. _final suspend:
Final Suspend
-------------
A coroutine author or a frontend may designate a particular suspend to be final,
by setting the second argument of the `coro.suspend`_ intrinsic to `true`.
Such a suspend point has two properties:
* it is possible to check whether a suspended coroutine is at the final suspend
point via `coro.done`_ intrinsic;
* a resumption of a coroutine stopped at the final suspend point leads to
undefined behavior. The only possible action for a coroutine at a final
suspend point is destroying it via `coro.destroy`_ intrinsic.
From the user perspective, the final suspend point represents an idea of a
coroutine reaching the end. From the compiler perspective, it is an optimization
opportunity for reducing number of resume points (and therefore switch cases) in
the resume function.
The following is an example of a function that keeps resuming the coroutine
until the final suspend point is reached after which point the coroutine is
destroyed:
.. code-block:: llvm
define i32 @main() {
entry:
%hdl = call ptr @f(i32 4)
br label %while
while:
call void @llvm.coro.resume(ptr %hdl)
%done = call i1 @llvm.coro.done(ptr %hdl)
br i1 %done, label %end, label %while
end:
call void @llvm.coro.destroy(ptr %hdl)
ret i32 0
}
Usually, final suspend point is a frontend injected suspend point that does not
correspond to any explicitly authored suspend point of the high level language.
For example, for a Python generator that has only one suspend point:
.. code-block:: python
def coroutine(n):
for i in range(n):
yield i
Python frontend would inject two more suspend points, so that the actual code
looks like this:
.. code-block:: c
void* coroutine(int n) {
int current_value;
<designate current_value to be coroutine promise>
<SUSPEND> // injected suspend point, so that the coroutine starts suspended
for (int i = 0; i < n; ++i) {
current_value = i; <SUSPEND>; // corresponds to "yield i"
}
<SUSPEND final=true> // injected final suspend point
}
and python iterator `__next__` would look like:
.. code-block:: c++
int __next__(void* hdl) {
coro.resume(hdl);
if (coro.done(hdl)) throw StopIteration();
return *(int*)coro.promise(hdl, 4, false);
}
Custom ABIs and Plugin Libraries
--------------------------------
Plugin libraries can extend coroutine lowering enabling a wide variety of users
to utilize the coroutine transformation passes. An existing coroutine lowering
is extended by:
#. defining custom ABIs that inherit from the existing ABIs,
#. give a list of generators for the custom ABIs when constructing the `CoroSplit`_ pass, and
#. use `coro.begin.custom.abi`_ in place of `coro.begin`_ that has an additional parameter for the index of the generator/ABI to be used for the coroutine.
A custom ABI overriding the SwitchABI's materialization looks like:
.. code-block:: c++
class CustomSwitchABI : public coro::SwitchABI {
public:
CustomSwitchABI(Function &F, coro::Shape &S)
: coro::SwitchABI(F, S, ExtraMaterializable) {}
};
Giving a list of custom ABI generators while constructing the `CoroSplit`
pass looks like:
.. code-block:: c++
CoroSplitPass::BaseABITy GenCustomABI = [](Function &F, coro::Shape &S) {
return std::make_unique<CustomSwitchABI>(F, S);
};
CGSCCPassManager CGPM;
CGPM.addPass(CoroSplitPass({GenCustomABI}));
The LLVM IR for a coroutine using a Coroutine with a custom ABI looks like:
.. code-block:: llvm
define ptr @f(i32 %n) presplitcoroutine_custom_abi {
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%size = call i32 @llvm.coro.size.i32()
%alloc = call ptr @malloc(i32 %size)
%hdl = call noalias ptr @llvm.coro.begin.custom.abi(token %id, ptr %alloc, i32 0)
br label %loop
loop:
%n.val = phi i32 [ %n, %entry ], [ %inc, %loop ]
%inc = add nsw i32 %n.val, 1
call void @print(i32 %n.val)
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %loop
i8 1, label %cleanup]
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
call void @free(ptr %mem)
br label %suspend
suspend:
%unused = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
ret ptr %hdl
}
Parameter Attributes
====================
Some parameter attributes, used to communicate additional information about the result or parameters of a function, require special handling.
ByVal
-----
A ByVal parameter on an argument indicates that the pointee should be treated as being passed by value to the function.
Prior to the coroutine transforms loads and stores to/from the pointer are generated where the value is needed.
Consequently, a ByVal argument is treated much like an alloca.
Space is allocated for it on the coroutine frame and the uses of the argument pointer are replaced with a pointer to the coroutine frame.
Swift Error
-----------
Clang supports the swiftcall calling convention in many common targets, and a user could call a function that takes a swifterror argument from a C++ coroutine.
The swifterror parameter attribute exists to model and optimize Swift error handling.
A swifterror alloca or parameter can only be loaded, stored, or passed as a swifterror call argument, and a swifterror call argument can only be a direct reference to a swifterror alloca or parameter.
These rules, not coincidentally, mean that you can always perfectly model the data flow in the alloca, and LLVM CodeGen actually has to do that in order to emit code.
For coroutine lowering the default treatment of allocas breaks those rules — splitting will try to replace the alloca with an entry in the coro frame, which can lead to trying to pass that as a swifterror argument.
To pass a swifterror argument in a split function, we need to still have the alloca around; but we also potentially need the coro frame slot, since useful data can (in theory) be stored in the swifterror alloca slot across suspensions in the presplit coroutine.
When split a coroutine it is consequently necessary to keep both the frame slot as well as the alloca itself and then keep them in sync.
Intrinsics
==========
Coroutine Manipulation Intrinsics
---------------------------------
Intrinsics described in this section are used to manipulate an existing
coroutine. They can be used in any function which happen to have a pointer
to a `coroutine frame`_ or a pointer to a `coroutine promise`_.
.. _coro.destroy:
'llvm.coro.destroy' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Syntax:
"""""""
::
declare void @llvm.coro.destroy(ptr <handle>)
Overview:
"""""""""
The '``llvm.coro.destroy``' intrinsic destroys a suspended
switched-resume coroutine.
Arguments:
""""""""""
The argument is a coroutine handle to a suspended coroutine.
Semantics:
""""""""""
When possible, the `coro.destroy` intrinsic is replaced with a direct call to
the coroutine destroy function. Otherwise it is replaced with an indirect call
based on the function pointer for the destroy function stored in the coroutine
frame. Destroying a coroutine that is not suspended leads to undefined behavior.
.. _coro.resume:
'llvm.coro.resume' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare void @llvm.coro.resume(ptr <handle>)
Overview:
"""""""""
The '``llvm.coro.resume``' intrinsic resumes a suspended switched-resume coroutine.
Arguments:
""""""""""
The argument is a handle to a suspended coroutine.
Semantics:
""""""""""
When possible, the `coro.resume` intrinsic is replaced with a direct call to the
coroutine resume function. Otherwise it is replaced with an indirect call based
on the function pointer for the resume function stored in the coroutine frame.
Resuming a coroutine that is not suspended leads to undefined behavior.
.. _coro.done:
'llvm.coro.done' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.done(ptr <handle>)
Overview:
"""""""""
The '``llvm.coro.done``' intrinsic checks whether a suspended
switched-resume coroutine is at the final suspend point or not.
Arguments:
""""""""""
The argument is a handle to a suspended coroutine.
Semantics:
""""""""""
Using this intrinsic on a coroutine that does not have a `final suspend`_ point
or on a coroutine that is not suspended leads to undefined behavior.
.. _coro.promise:
'llvm.coro.promise' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.promise(ptr <ptr>, i32 <alignment>, i1 <from>)
Overview:
"""""""""
The '``llvm.coro.promise``' intrinsic obtains a pointer to a
`coroutine promise`_ given a switched-resume coroutine handle and vice versa.
Arguments:
""""""""""
The first argument is a handle to a coroutine if `from` is false. Otherwise,
it is a pointer to a coroutine promise.
The second argument is an alignment requirements of the promise.
If a frontend designated `%promise = alloca i32` as a promise, the alignment
argument to `coro.promise` should be the alignment of `i32` on the target
platform. If a frontend designated `%promise = alloca i32, align 16` as a
promise, the alignment argument should be 16.
This argument only accepts constants.
The third argument is a boolean indicating a direction of the transformation.
If `from` is true, the intrinsic returns a coroutine handle given a pointer
to a promise. If `from` is false, the intrinsics return a pointer to a promise
from a coroutine handle. This argument only accepts constants.
Semantics:
""""""""""
Using this intrinsic on a coroutine that does not have a coroutine promise
leads to undefined behavior. It is possible to read and modify coroutine
promise of the coroutine which is currently executing. The coroutine author and
a coroutine user are responsible to makes sure there is no data races.
Example:
""""""""
.. code-block:: llvm
define ptr @f(i32 %n) {
entry:
%promise = alloca i32
; the second argument to coro.id points to the coroutine promise.
%id = call token @llvm.coro.id(i32 0, ptr %promise, ptr null, ptr null)
...
%hdl = call noalias ptr @llvm.coro.begin(token %id, ptr %alloc)
...
store i32 42, ptr %promise ; store something into the promise
...
ret ptr %hdl
}
define i32 @main() {
entry:
%hdl = call ptr @f(i32 4) ; starts the coroutine and returns its handle
%promise.addr = call ptr @llvm.coro.promise(ptr %hdl, i32 4, i1 false)
%val = load i32, ptr %promise.addr ; load a value from the promise
call void @print(i32 %val)
call void @llvm.coro.destroy(ptr %hdl)
ret i32 0
}
.. _coroutine intrinsics:
Coroutine Structure Intrinsics
------------------------------
Intrinsics described in this section are used within a coroutine to describe
the coroutine structure. They should not be used outside of a coroutine.
.. _coro.size:
'llvm.coro.size' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i32 @llvm.coro.size.i32()
declare i64 @llvm.coro.size.i64()
Overview:
"""""""""
The '``llvm.coro.size``' intrinsic returns the number of bytes
required to store a `coroutine frame`_. This is only supported for
switched-resume coroutines.
Arguments:
""""""""""
None
Semantics:
""""""""""
The `coro.size` intrinsic is lowered to a constant representing the size of
the coroutine frame.
.. _coro.align:
'llvm.coro.align' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i32 @llvm.coro.align.i32()
declare i64 @llvm.coro.align.i64()
Overview:
"""""""""
The '``llvm.coro.align``' intrinsic returns the alignment of a `coroutine frame`_.
This is only supported for switched-resume coroutines.
Arguments:
""""""""""
None
Semantics:
""""""""""
The `coro.align` intrinsic is lowered to a constant representing the alignment of
the coroutine frame.
.. _coro.begin:
'llvm.coro.begin' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.begin(token <id>, ptr <mem>)
Overview:
"""""""""
The '``llvm.coro.begin``' intrinsic returns an address of the coroutine frame.
Arguments:
""""""""""
The first argument is a token returned by a call to '``llvm.coro.id``'
identifying the coroutine.
The second argument is a pointer to a block of memory where coroutine frame
will be stored if it is allocated dynamically. This pointer is ignored
for returned-continuation coroutines.
Semantics:
""""""""""
Depending on the alignment requirements of the objects in the coroutine frame
and/or on the codegen compactness reasons the pointer returned from `coro.begin`
may be at offset to the `%mem` argument. (This could be beneficial if
instructions that express relative access to data can be more compactly encoded
with small positive and negative offsets).
A frontend should emit exactly one `coro.begin` intrinsic per coroutine.
.. _coro.begin.custom.abi:
'llvm.coro.begin.custom.abi' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.begin.custom.abi(token <id>, ptr <mem>, i32)
Overview:
"""""""""
The '``llvm.coro.begin.custom.abi``' intrinsic is used in place of the
`coro.begin` intrinsic that has an additional parameter to specify the custom
ABI for the coroutine. The return is identical to that of the `coro.begin`
intrinsic.
Arguments:
""""""""""
The first and second arguments are identical to those of the `coro.begin`
intrinsic.
The third argument is an i32 index of the generator list given to the
`CoroSplit` pass specifying the custom ABI generator for this coroutine.
Semantics:
""""""""""
The semantics are identical to those of the `coro.begin` intrinsic.
.. _coro.free:
'llvm.coro.free' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.free(token %id, ptr <frame>)
Overview:
"""""""""
The '``llvm.coro.free``' intrinsic returns a pointer to a block of memory where
coroutine frame is stored or `null` if this instance of a coroutine did not use
dynamically allocated memory for its coroutine frame. This intrinsic is not
supported for returned-continuation coroutines.
Arguments:
""""""""""
The first argument is a token returned by a call to '``llvm.coro.id``'
identifying the coroutine.
The second argument is a pointer to the coroutine frame. This should be the same
pointer that was returned by prior `coro.begin` call.
Example (custom deallocation function):
"""""""""""""""""""""""""""""""""""""""
.. code-block:: llvm
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %frame)
%mem_not_null = icmp ne ptr %mem, null
br i1 %mem_not_null, label %if.then, label %if.end
if.then:
call void @CustomFree(ptr %mem)
br label %if.end
if.end:
ret void
Example (standard deallocation functions):
""""""""""""""""""""""""""""""""""""""""""
.. code-block:: llvm
cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %frame)
call void @free(ptr %mem)
ret void
.. _coro.alloc:
'llvm.coro.alloc' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.alloc(token <id>)
Overview:
"""""""""
The '``llvm.coro.alloc``' intrinsic returns `true` if dynamic allocation is
required to obtain a memory for the coroutine frame and `false` otherwise.
This is not supported for returned-continuation coroutines.
Arguments:
""""""""""
The first argument is a token returned by a call to '``llvm.coro.id``'
identifying the coroutine.
Semantics:
""""""""""
A frontend should emit at most one `coro.alloc` intrinsic per coroutine.
The intrinsic is used to suppress dynamic allocation of the coroutine frame
when possible.
Example:
""""""""
.. code-block:: llvm
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%dyn.alloc.required = call i1 @llvm.coro.alloc(token %id)
br i1 %dyn.alloc.required, label %coro.alloc, label %coro.begin
coro.alloc:
%frame.size = call i32 @llvm.coro.size()
%alloc = call ptr @MyAlloc(i32 %frame.size)
br label %coro.begin
coro.begin:
%phi = phi ptr [ null, %entry ], [ %alloc, %coro.alloc ]
%frame = call ptr @llvm.coro.begin(token %id, ptr %phi)
.. _coro.noop:
'llvm.coro.noop' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.noop()
Overview:
"""""""""
The '``llvm.coro.noop``' intrinsic returns an address of the coroutine frame of
a coroutine that does nothing when resumed or destroyed.
Arguments:
""""""""""
None
Semantics:
""""""""""
This intrinsic is lowered to refer to a private constant coroutine frame. The
resume and destroy handlers for this frame are empty functions that do nothing.
Note that in different translation units llvm.coro.noop may return different pointers.
.. _coro.frame:
'llvm.coro.frame' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.frame()
Overview:
"""""""""
The '``llvm.coro.frame``' intrinsic returns an address of the coroutine frame of
the enclosing coroutine.
Arguments:
""""""""""
None
Semantics:
""""""""""
This intrinsic is lowered to refer to the `coro.begin`_ instruction. This is
a frontend convenience intrinsic that makes it easier to refer to the
coroutine frame.
.. _coro.id:
'llvm.coro.id' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.id(i32 <align>, ptr <promise>, ptr <coroaddr>,
ptr <fnaddrs>)
Overview:
"""""""""
The '``llvm.coro.id``' intrinsic returns a token identifying a
switched-resume coroutine.
Arguments:
""""""""""
The first argument provides information on the alignment of the memory returned
by the allocation function and given to `coro.begin` by the first argument. If
this argument is 0, the memory is assumed to be aligned to 2 * sizeof(ptr).
This argument only accepts constants.
The second argument, if not `null`, designates a particular alloca instruction
to be a `coroutine promise`_.
The third argument is `null` coming out of the frontend. The CoroEarly pass sets
this argument to point to the function this coro.id belongs to.
The fourth argument is `null` before coroutine is split, and later is replaced
to point to a private global constant array containing function pointers to
outlined resume and destroy parts of the coroutine.
Semantics:
""""""""""
The purpose of this intrinsic is to tie together `coro.id`, `coro.alloc` and
`coro.begin` belonging to the same coroutine to prevent optimization passes from
duplicating any of these instructions unless entire body of the coroutine is
duplicated.
A frontend should emit exactly one `coro.id` intrinsic per coroutine.
A frontend should emit function attribute `presplitcoroutine` for the coroutine.
.. _coro.id.async:
'llvm.coro.id.async' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.id.async(i32 <context size>, i32 <align>,
ptr <context arg>,
ptr <async function pointer>)
Overview:
"""""""""
The '``llvm.coro.id.async``' intrinsic returns a token identifying an async coroutine.
Arguments:
""""""""""
The first argument provides the initial size of the `async context` as required
from the frontend. Lowering will add to this size the size required by the frame
storage and store that value to the `async function pointer`.
The second argument, is the alignment guarantee of the memory of the
`async context`. The frontend guarantees that the memory will be aligned by this
value.
The third argument is the `async context` argument in the current coroutine.
The fourth argument is the address of the `async function pointer` struct.
Lowering will update the context size requirement in this struct by adding the
coroutine frame size requirement to the initial size requirement as specified by
the first argument of this intrinsic.
Semantics:
""""""""""
A frontend should emit exactly one `coro.id.async` intrinsic per coroutine.
A frontend should emit function attribute `presplitcoroutine` for the coroutine.
.. _coro.id.retcon:
'llvm.coro.id.retcon' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.id.retcon(i32 <size>, i32 <align>, ptr <buffer>,
ptr <continuation prototype>,
ptr <alloc>, ptr <dealloc>)
Overview:
"""""""""
The '``llvm.coro.id.retcon``' intrinsic returns a token identifying a
multiple-suspend returned-continuation coroutine.
The 'result-type sequence' of the coroutine is defined as follows:
- if the return type of the coroutine function is ``void``, it is the
empty sequence;
- if the return type of the coroutine function is a ``struct``, it is the
element types of that ``struct`` in order;
- otherwise, it is just the return type of the coroutine function.
The first element of the result-type sequence must be a pointer type;
continuation functions will be coerced to this type. The rest of
the sequence are the 'yield types', and any suspends in the coroutine
must take arguments of these types.
Arguments:
""""""""""
The first and second arguments are the expected size and alignment of
the buffer provided as the third argument. They must be constant.
The fourth argument must be a reference to a global function, called
the 'continuation prototype function'. The type, calling convention,
and attributes of any continuation functions will be taken from this
declaration. The return type of the prototype function must match the
return type of the current function. The first parameter type must be
a pointer type. The second parameter type must be an integer type;
it will be used only as a boolean flag.
The fifth argument must be a reference to a global function that will
be used to allocate memory. It may not fail, either by returning null
or throwing an exception. It must take an integer and return a pointer.
The sixth argument must be a reference to a global function that will
be used to deallocate memory. It must take a pointer and return ``void``.
Semantics:
""""""""""
A frontend should emit function attribute `presplitcoroutine` for the coroutine.
'llvm.coro.id.retcon.once' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.id.retcon.once(i32 <size>, i32 <align>, ptr <buffer>,
ptr <prototype>,
ptr <alloc>, ptr <dealloc>)
Overview:
"""""""""
The '``llvm.coro.id.retcon.once``' intrinsic returns a token identifying a
unique-suspend returned-continuation coroutine.
Arguments:
""""""""""
As for ``llvm.core.id.retcon``, except that the return type of the
continuation prototype must represent the normal return type of the continuation
(instead of matching the coroutine's return type).
Semantics:
""""""""""
A frontend should emit function attribute `presplitcoroutine` for the coroutine.
.. _coro.end:
'llvm.coro.end' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.end(ptr <handle>, i1 <unwind>, token <result.token>)
Overview:
"""""""""
The '``llvm.coro.end``' marks the point where execution of the resume part of
the coroutine should end and control should return to the caller.
Arguments:
""""""""""
The first argument should refer to the coroutine handle of the enclosing
coroutine. A frontend is allowed to supply null as the first parameter, in this
case `coro-early` pass will replace the null with an appropriate coroutine
handle value.
The second argument should be `true` if this coro.end is in the block that is
part of the unwind sequence leaving the coroutine body due to an exception and
`false` otherwise.
Non-trivial (non-none) token argument can only be specified for unique-suspend
returned-continuation coroutines where it must be a token value produced by
'``llvm.coro.end.results``' intrinsic.
Only none token is allowed for coro.end calls in unwind sections
Semantics:
""""""""""
The purpose of this intrinsic is to allow frontends to mark the cleanup and
other code that is only relevant during the initial invocation of the coroutine
and should not be present in resume and destroy parts.
In returned-continuation lowering, ``llvm.coro.end`` fully destroys the
coroutine frame. If the second argument is `false`, it also returns from
the coroutine with a null continuation pointer, and the next instruction
will be unreachable. If the second argument is `true`, it falls through
so that the following logic can resume unwinding. In a yield-once
coroutine, reaching a non-unwind ``llvm.coro.end`` without having first
reached a ``llvm.coro.suspend.retcon`` has undefined behavior.
The remainder of this section describes the behavior under switched-resume
lowering.
This intrinsic is lowered when a coroutine is split into
the start, resume and destroy parts. In the start part, it is a no-op,
in resume and destroy parts, it is replaced with `ret void` instruction and
the rest of the block containing `coro.end` instruction is discarded.
In landing pads it is replaced with an appropriate instruction to unwind to
caller. The handling of coro.end differs depending on whether the target is
using landingpad or WinEH exception model.
For landingpad based exception model, it is expected that frontend uses the
`coro.end`_ intrinsic as follows:
.. code-block:: llvm
ehcleanup:
%InResumePart = call i1 @llvm.coro.end(ptr null, i1 true, token none)
br i1 %InResumePart, label %eh.resume, label %cleanup.cont
cleanup.cont:
; rest of the cleanup
eh.resume:
%exn = load ptr, ptr %exn.slot, align 8
%sel = load i32, ptr %ehselector.slot, align 4
%lpad.val = insertvalue { ptr, i32 } undef, ptr %exn, 0
%lpad.val29 = insertvalue { ptr, i32 } %lpad.val, i32 %sel, 1
resume { ptr, i32 } %lpad.val29
The `CoroSpit` pass replaces `coro.end` with ``True`` in the resume functions,
thus leading to immediate unwind to the caller, whereas in start function it
is replaced with ``False``, thus allowing to proceed to the rest of the cleanup
code that is only needed during initial invocation of the coroutine.
For Windows Exception handling model, a frontend should attach a funclet bundle
referring to an enclosing cleanuppad as follows:
.. code-block:: llvm
ehcleanup:
%tok = cleanuppad within none []
%unused = call i1 @llvm.coro.end(ptr null, i1 true, token none) [ "funclet"(token %tok) ]
cleanupret from %tok unwind label %RestOfTheCleanup
The `CoroSplit` pass, if the funclet bundle is present, will insert
``cleanupret from %tok unwind to caller`` before
the `coro.end`_ intrinsic and will remove the rest of the block.
In the unwind path (when the argument is `true`), `coro.end` will mark the coroutine
as done, making it undefined behavior to resume the coroutine again and causing
`llvm.coro.done` to return `true`. This is not necessary in the normal path because
the coroutine will already be marked as done by the final suspend.
The following table summarizes the handling of `coro.end`_ intrinsic.
+--------------------------+------------------------+---------------------------------+
| | In Start Function | In Resume/Destroy Functions |
+--------------------------+------------------------+---------------------------------+
|unwind=false | nothing |``ret void`` |
+------------+-------------+------------------------+---------------------------------+
| | WinEH | mark coroutine as done || ``cleanupret unwind to caller``|
| | | || mark coroutine done |
|unwind=true +-------------+------------------------+---------------------------------+
| | Landingpad | mark coroutine as done | mark coroutine done |
+------------+-------------+------------------------+---------------------------------+
.. _coro.end.results:
'llvm.coro.end.results' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.end.results(...)
Overview:
"""""""""
The '``llvm.coro.end.results``' intrinsic captures values to be returned from
unique-suspend returned-continuation coroutines.
Arguments:
""""""""""
The number of arguments must match the return type of the continuation function:
- if the return type of the continuation function is ``void`` there must be no
arguments
- if the return type of the continuation function is a ``struct``, the arguments
will be of element types of that ``struct`` in order;
- otherwise, it is just the return value of the continuation function.
.. code-block:: llvm
define {ptr, ptr} @g(ptr %buffer, ptr %ptr, i8 %val) presplitcoroutine {
entry:
%id = call token @llvm.coro.id.retcon.once(i32 8, i32 8, ptr %buffer,
ptr @prototype,
ptr @allocate, ptr @deallocate)
%hdl = call ptr @llvm.coro.begin(token %id, ptr null)
...
cleanup:
%tok = call token (...) @llvm.coro.end.results(i8 %val)
call i1 @llvm.coro.end(ptr %hdl, i1 0, token %tok)
unreachable
...
declare i8 @prototype(ptr, i1 zeroext)
'llvm.coro.end.async' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.end.async(ptr <handle>, i1 <unwind>, ...)
Overview:
"""""""""
The '``llvm.coro.end.async``' marks the point where execution of the resume part
of the coroutine should end and control should return to the caller. As part of
its variable tail arguments this instruction allows to specify a function and
the function's arguments that are to be tail called as the last action before
returning.
Arguments:
""""""""""
The first argument should refer to the coroutine handle of the enclosing
coroutine. A frontend is allowed to supply null as the first parameter, in this
case `coro-early` pass will replace the null with an appropriate coroutine
handle value.
The second argument should be `true` if this coro.end is in the block that is
part of the unwind sequence leaving the coroutine body due to an exception and
`false` otherwise.
The third argument if present should specify a function to be called.
If the third argument is present, the remaining arguments are the arguments to
the function call.
.. code-block:: llvm
call i1 (ptr, i1, ...) @llvm.coro.end.async(
ptr %hdl, i1 0,
ptr @must_tail_call_return,
ptr %ctxt, ptr %task, ptr %actor)
unreachable
.. _coro.suspend:
.. _suspend points:
'llvm.coro.suspend' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i8 @llvm.coro.suspend(token <save>, i1 <final>)
Overview:
"""""""""
The '``llvm.coro.suspend``' marks the point where execution of a
switched-resume coroutine is suspended and control is returned back
to the caller. Conditional branches consuming the result of this
intrinsic lead to basic blocks where coroutine should proceed when
suspended (-1), resumed (0) or destroyed (1).
Arguments:
""""""""""
The first argument refers to a token of `coro.save` intrinsic that marks the
point when coroutine state is prepared for suspension. If `none` token is passed,
the intrinsic behaves as if there were a `coro.save` immediately preceding
the `coro.suspend` intrinsic.
The second argument indicates whether this suspension point is `final`_.
The second argument only accepts constants. If more than one suspend point is
designated as final, the resume and destroy branches should lead to the same
basic blocks.
Example (normal suspend point):
"""""""""""""""""""""""""""""""
.. code-block:: llvm
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %resume
i8 1, label %cleanup]
Example (final suspend point):
""""""""""""""""""""""""""""""
.. code-block:: llvm
while.end:
%s.final = call i8 @llvm.coro.suspend(token none, i1 true)
switch i8 %s.final, label %suspend [i8 0, label %trap
i8 1, label %cleanup]
trap:
call void @llvm.trap()
unreachable
Semantics:
""""""""""
If a coroutine that was suspended at the suspend point marked by this intrinsic
is resumed via `coro.resume`_ the control will transfer to the basic block
of the 0-case. If it is resumed via `coro.destroy`_, it will proceed to the
basic block indicated by the 1-case. To suspend, coroutine proceed to the
default label.
If suspend intrinsic is marked as final, it can consider the `true` branch
unreachable and can perform optimizations that can take advantage of that fact.
.. _coro.save:
'llvm.coro.save' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare token @llvm.coro.save(ptr <handle>)
Overview:
"""""""""
The '``llvm.coro.save``' marks the point where a coroutine need to update its
state to prepare for resumption to be considered suspended (and thus eligible
for resumption). It is illegal to merge two '``llvm.coro.save``' calls unless their
'``llvm.coro.suspend``' users are also merged. So '``llvm.coro.save``' is currently
tagged with the `no_merge` function attribute.
Arguments:
""""""""""
The first argument points to a coroutine handle of the enclosing coroutine.
Semantics:
""""""""""
Whatever coroutine state changes are required to enable resumption of
the coroutine from the corresponding suspend point should be done at the point
of `coro.save` intrinsic.
Example:
""""""""
Separate save and suspend points are necessary when a coroutine is used to
represent an asynchronous control flow driven by callbacks representing
completions of asynchronous operations.
In such a case, a coroutine should be ready for resumption prior to a call to
`async_op` function that may trigger resumption of a coroutine from the same or
a different thread possibly prior to `async_op` call returning control back
to the coroutine:
.. code-block:: llvm
%save1 = call token @llvm.coro.save(ptr %hdl)
call void @async_op1(ptr %hdl)
%suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false)
switch i8 %suspend1, label %suspend [i8 0, label %resume1
i8 1, label %cleanup]
.. _coro.suspend.async:
'llvm.coro.suspend.async' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare {ptr, ptr, ptr} @llvm.coro.suspend.async(
ptr <resume function>,
ptr <context projection function>,
... <function to call>
... <arguments to function>)
Overview:
"""""""""
The '``llvm.coro.suspend.async``' intrinsic marks the point where
execution of an async coroutine is suspended and control is passed to a callee.
Arguments:
""""""""""
The first argument should be the result of the `llvm.coro.async.resume` intrinsic.
Lowering will replace this intrinsic with the resume function for this suspend
point.
The second argument is the `context projection function`. It should describe
how-to restore the `async context` in the continuation function from the first
argument of the continuation function. Its type is `ptr (ptr)`.
The third argument is the function that models transfer to the callee at the
suspend point. It should take 3 arguments. Lowering will `musttail` call this
function.
The fourth to six argument are the arguments for the third argument.
Semantics:
""""""""""
The result of the intrinsic are mapped to the arguments of the resume function.
Execution is suspended at this intrinsic and resumed when the resume function is
called.
.. _coro.prepare.async:
'llvm.coro.prepare.async' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare ptr @llvm.coro.prepare.async(ptr <coroutine function>)
Overview:
"""""""""
The '``llvm.coro.prepare.async``' intrinsic is used to block inlining of the
async coroutine until after coroutine splitting.
Arguments:
""""""""""
The first argument should be an async coroutine of type `void (ptr, ptr, ptr)`.
Lowering will replace this intrinsic with its coroutine function argument.
.. _coro.suspend.retcon:
'llvm.coro.suspend.retcon' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.suspend.retcon(...)
Overview:
"""""""""
The '``llvm.coro.suspend.retcon``' intrinsic marks the point where
execution of a returned-continuation coroutine is suspended and control
is returned back to the caller.
`llvm.coro.suspend.retcon`` does not support separate save points;
they are not useful when the continuation function is not locally
accessible. That would be a more appropriate feature for a ``passcon``
lowering that is not yet implemented.
Arguments:
""""""""""
The types of the arguments must exactly match the yielded-types sequence
of the coroutine. They will be turned into return values from the ramp
and continuation functions, along with the next continuation function.
Semantics:
""""""""""
The result of the intrinsic indicates whether the coroutine should resume
abnormally (non-zero).
In a normal coroutine, it is undefined behavior if the coroutine executes
a call to ``llvm.coro.suspend.retcon`` after resuming abnormally.
In a yield-once coroutine, it is undefined behavior if the coroutine
executes a call to ``llvm.coro.suspend.retcon`` after resuming in any way.
.. _coro.await.suspend.void:
'llvm.coro.await.suspend.void' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare void @llvm.coro.await.suspend.void(
ptr <awaiter>,
ptr <handle>,
ptr <await_suspend_function>)
Overview:
"""""""""
The '``llvm.coro.await.suspend.void``' intrinsic encapsulates C++
`await-suspend` block until it can't interfere with coroutine transform.
The `await_suspend` block of `co_await` is essentially asynchronous
to the execution of the coroutine. Inlining it normally into an unsplit
coroutine can cause miscompilation because the coroutine CFG misrepresents
the true control flow of the program: things that happen in the
await_suspend are not guaranteed to happen prior to the resumption of the
coroutine, and things that happen after the resumption of the coroutine
(including its exit and the potential deallocation of the coroutine frame)
are not guaranteed to happen only after the end of `await_suspend`.
This version of intrinsic corresponds to
'``void awaiter.await_suspend(...)``' variant.
Arguments:
""""""""""
The first argument is a pointer to `awaiter` object.
The second argument is a pointer to the current coroutine's frame.
The third argument is a pointer to the wrapper function encapsulating
`await-suspend` logic. Its signature must be
.. code-block:: llvm
declare void @await_suspend_function(ptr %awaiter, ptr %hdl)
Semantics:
""""""""""
The intrinsic must be used between corresponding `coro.save`_ and
`coro.suspend`_ calls. It is lowered to a direct
`await_suspend_function` call during `CoroSplit`_ pass.
Example:
""""""""
.. code-block:: llvm
; before lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
call void @llvm.coro.await.suspend.void(
ptr %awaiter,
ptr %hdl,
ptr @await_suspend_function)
%suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
...
; after lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
; the call to await_suspend_function can be inlined
call void @await_suspend_function(
ptr %awaiter,
ptr %hdl)
%suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
...
; wrapper function example
define void @await_suspend_function(ptr %awaiter, ptr %hdl)
entry:
%hdl.arg = ... ; construct std::coroutine_handle from %hdl
call void @"Awaiter::await_suspend"(ptr %awaiter, ptr %hdl.arg)
ret void
.. _coro.await.suspend.bool:
'llvm.coro.await.suspend.bool' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare i1 @llvm.coro.await.suspend.bool(
ptr <awaiter>,
ptr <handle>,
ptr <await_suspend_function>)
Overview:
"""""""""
The '``llvm.coro.await.suspend.bool``' intrinsic encapsulates C++
`await-suspend` block until it can't interfere with coroutine transform.
The `await_suspend` block of `co_await` is essentially asynchronous
to the execution of the coroutine. Inlining it normally into an unsplit
coroutine can cause miscompilation because the coroutine CFG misrepresents
the true control flow of the program: things that happen in the
await_suspend are not guaranteed to happen prior to the resumption of the
coroutine, and things that happen after the resumption of the coroutine
(including its exit and the potential deallocation of the coroutine frame)
are not guaranteed to happen only after the end of `await_suspend`.
This version of intrinsic corresponds to
'``bool awaiter.await_suspend(...)``' variant.
Arguments:
""""""""""
The first argument is a pointer to `awaiter` object.
The second argument is a pointer to the current coroutine's frame.
The third argument is a pointer to the wrapper function encapsulating
`await-suspend` logic. Its signature must be
.. code-block:: llvm
declare i1 @await_suspend_function(ptr %awaiter, ptr %hdl)
Semantics:
""""""""""
The intrinsic must be used between corresponding `coro.save`_ and
`coro.suspend`_ calls. It is lowered to a direct
`await_suspend_function` call during `CoroSplit`_ pass.
If `await_suspend_function` call returns `true`, the current coroutine is
immediately resumed.
Example:
""""""""
.. code-block:: llvm
; before lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
%resume = call i1 @llvm.coro.await.suspend.bool(
ptr %awaiter,
ptr %hdl,
ptr @await_suspend_function)
br i1 %resume, %await.suspend.bool, %await.ready
await.suspend.bool:
%suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
...
await.ready:
call void @"Awaiter::await_resume"(ptr %awaiter)
...
; after lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
; the call to await_suspend_function can inlined
%resume = call i1 @await_suspend_function(
ptr %awaiter,
ptr %hdl)
br i1 %resume, %await.suspend.bool, %await.ready
...
; wrapper function example
define i1 @await_suspend_function(ptr %awaiter, ptr %hdl)
entry:
%hdl.arg = ... ; construct std::coroutine_handle from %hdl
%resume = call i1 @"Awaiter::await_suspend"(ptr %awaiter, ptr %hdl.arg)
ret i1 %resume
.. _coro.await.suspend.handle:
'llvm.coro.await.suspend.handle' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
declare void @llvm.coro.await.suspend.handle(
ptr <awaiter>,
ptr <handle>,
ptr <await_suspend_function>)
Overview:
"""""""""
The '``llvm.coro.await.suspend.handle``' intrinsic encapsulates C++
`await-suspend` block until it can't interfere with coroutine transform.
The `await_suspend` block of `co_await` is essentially asynchronous
to the execution of the coroutine. Inlining it normally into an unsplit
coroutine can cause miscompilation because the coroutine CFG misrepresents
the true control flow of the program: things that happen in the
await_suspend are not guaranteed to happen prior to the resumption of the
coroutine, and things that happen after the resumption of the coroutine
(including its exit and the potential deallocation of the coroutine frame)
are not guaranteed to happen only after the end of `await_suspend`.
This version of intrinsic corresponds to
'``std::coroutine_handle<> awaiter.await_suspend(...)``' variant.
Arguments:
""""""""""
The first argument is a pointer to `awaiter` object.
The second argument is a pointer to the current coroutine's frame.
The third argument is a pointer to the wrapper function encapsulating
`await-suspend` logic. Its signature must be
.. code-block:: llvm
declare ptr @await_suspend_function(ptr %awaiter, ptr %hdl)
Semantics:
""""""""""
The intrinsic must be used between corresponding `coro.save`_ and
`coro.suspend`_ calls. It is lowered to a direct
`await_suspend_function` call during `CoroSplit`_ pass.
`await_suspend_function` must return a pointer to a valid
coroutine frame. The intrinsic will be lowered to a tail call resuming the
returned coroutine frame. It will be marked `musttail` on targets that support
that. Instructions following the intrinsic will become unreachable.
Example:
""""""""
.. code-block:: llvm
; before lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
call void @llvm.coro.await.suspend.handle(
ptr %awaiter,
ptr %hdl,
ptr @await_suspend_function)
%suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
...
; after lowering
await.suspend:
%save = call token @llvm.coro.save(ptr %hdl)
; the call to await_suspend_function can be inlined
%next = call ptr @await_suspend_function(
ptr %awaiter,
ptr %hdl)
musttail call void @llvm.coro.resume(%next)
ret void
...
; wrapper function example
define ptr @await_suspend_function(ptr %awaiter, ptr %hdl)
entry:
%hdl.arg = ... ; construct std::coroutine_handle from %hdl
%hdl.raw = call ptr @"Awaiter::await_suspend"(ptr %awaiter, ptr %hdl.arg)
%hdl.result = ... ; get address of returned coroutine handle
ret ptr %hdl.result
Coroutine Transformation Passes
===============================
CoroEarly
---------
The CoroEarly pass ensures later middle end passes correctly interpret coroutine
semantics and lowers coroutine intrinsics that not needed to be preserved to
help later coroutine passes. This pass lowers `coro.promise`_, `coro.frame`_ and
`coro.done`_ intrinsics. Afterwards, it replace uses of promise alloca with
`coro.promise`_ intrinsic.
.. _CoroSplit:
CoroSplit
---------
The pass CoroSplit builds coroutine frame and outlines resume and destroy parts
into separate functions. This pass also lowers `coro.await.suspend.void`_,
`coro.await.suspend.bool`_ and `coro.await.suspend.handle`_ intrinsics.
CoroAnnotationElide
-------------------
This pass finds all usages of coroutines that are "must elide" and replaces
`coro.begin` intrinsic with an address of a coroutine frame placed on its caller
and replaces `coro.alloc` and `coro.free` intrinsics with `false` and `null`
respectively to remove the deallocation code.
CoroElide
---------
The pass CoroElide examines if the inlined coroutine is eligible for heap
allocation elision optimization. If so, it replaces
`coro.begin` intrinsic with an address of a coroutine frame placed on its caller
and replaces `coro.alloc` and `coro.free` intrinsics with `false` and `null`
respectively to remove the deallocation code.
This pass also replaces `coro.resume` and `coro.destroy` intrinsics with direct
calls to resume and destroy functions for a particular coroutine where possible.
CoroCleanup
-----------
This pass runs late to lower all coroutine related intrinsics not replaced by
earlier passes.
Attributes
==========
coro_only_destroy_when_complete
-------------------------------
When the coroutine are marked with coro_only_destroy_when_complete, it indicates
the coroutine must reach the final suspend point when it get destroyed.
This attribute only works for switched-resume coroutines now.
coro_elide_safe
---------------
When a Call or Invoke instruction to switch ABI coroutine `f` is marked with
`coro_elide_safe`, CoroSplitPass generates a `f.noalloc` ramp function.
`f.noalloc` has one more argument than its original ramp function `f`, which is
the pointer to the allocated frame. `f.noalloc` also suppressed any allocations
or deallocations that may be guarded by `@llvm.coro.alloc` and `@llvm.coro.free`.
CoroAnnotationElidePass performs the heap elision when possible. Note that for
recursive or mutually recursive functions this elision is usually not possible.
Metadata
========
'``coro.outside.frame``' Metadata
---------------------------------
``coro.outside.frame`` metadata may be attached to an alloca instruction to
to signify that it shouldn't be promoted to the coroutine frame, useful for
filtering allocas out by the frontend when emitting internal control mechanisms.
Additionally, this metadata is only used as a flag, so the associated
node must be empty.
.. code-block:: text
%__coro_gro = alloca %struct.GroType, align 1, !coro.outside.frame !0
...
!0 = !{}
Areas Requiring Attention
=========================
#. When coro.suspend returns -1, the coroutine is suspended, and it's possible
that the coroutine has already been destroyed (hence the frame has been freed).
We cannot access anything on the frame on the suspend path.
However there is nothing that prevents the compiler from moving instructions
along that path (e.g. LICM), which can lead to use-after-free. At the moment
we disabled LICM for loops that have coro.suspend, but the general problem still
exists and requires a general solution.
#. Take advantage of the lifetime intrinsics for the data that goes into the
coroutine frame. Leave lifetime intrinsics as is for the data that stays in
allocas.
#. The CoroElide optimization pass relies on coroutine ramp function to be
inlined. It would be beneficial to split the ramp function further to
increase the chance that it will get inlined into its caller.
#. Design a convention that would make it possible to apply coroutine heap
elision optimization across ABI boundaries.
#. Cannot handle coroutines with `inalloca` parameters (used in x86 on Windows).
#. Alignment is ignored by coro.begin and coro.free intrinsics.
#. Make required changes to make sure that coroutine optimizations work with
LTO.
#. More tests, more tests, more tests
|