David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1 | ===================================== |
| 2 | Coroutines in LLVM |
| 3 | ===================================== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | :depth: 3 |
| 8 | |
| 9 | .. warning:: |
| 10 | This is a work in progress. Compatibility across LLVM releases is not |
| 11 | guaranteed. |
| 12 | |
| 13 | Introduction |
| 14 | ============ |
| 15 | |
| 16 | .. _coroutine handle: |
| 17 | |
| 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. |
| 22 | |
| 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 |
| 26 | then destroy it: |
| 27 | |
| 28 | .. code-block:: llvm |
| 29 | |
| 30 | define i32 @main() { |
| 31 | entry: |
| 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) |
| 36 | ret i32 0 |
| 37 | } |
| 38 | |
| 39 | .. _coroutine frame: |
| 40 | |
| 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 **coroutine frame**. It is created when a coroutine is called and |
| 45 | destroyed when a coroutine runs to completion or destroyed by a call to |
| 46 | the `coro.destroy`_ intrinsic. |
| 47 | |
| 48 | An LLVM coroutine is represented as an LLVM function that has calls to |
| 49 | `coroutine intrinsics`_ defining the structure of the coroutine. |
| 50 | After lowering, a coroutine is split into several |
| 51 | functions that represent three different ways of how control can enter the |
| 52 | coroutine: |
| 53 | |
| 54 | 1. a ramp function, which represents an initial invocation of the coroutine that |
| 55 | creates the coroutine frame and executes the coroutine code until it |
| 56 | encounters a suspend point or reaches the end of the function; |
| 57 | |
| 58 | 2. a coroutine resume function that is invoked when the coroutine is resumed; |
| 59 | |
| 60 | 3. a coroutine destroy function that is invoked when the coroutine is destroyed. |
| 61 | |
| 62 | .. note:: Splitting out resume and destroy functions are just one of the |
| 63 | possible ways of lowering the coroutine. We chose it for initial |
| 64 | implementation as it matches closely the mental model and results in |
| 65 | reasonably nice code. |
| 66 | |
| 67 | Coroutines by Example |
| 68 | ===================== |
| 69 | |
| 70 | Coroutine Representation |
| 71 | ------------------------ |
| 72 | |
| 73 | Let's look at an example of an LLVM coroutine with the behavior sketched |
| 74 | by the following pseudo-code. |
| 75 | |
Sanjoy Das | 77a9c79 | 2016-07-26 21:03:41 +0000 | [diff] [blame] | 76 | .. code-block:: c++ |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 77 | |
| 78 | void *f(int n) { |
| 79 | for(;;) { |
| 80 | print(n++); |
| 81 | <suspend> // returns a coroutine handle on first suspend |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | This coroutine calls some function `print` with value `n` as an argument and |
| 86 | 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 |
| 87 | a `main` shown in the previous section. It will call `print` with values 4, 5 |
| 88 | and 6 after which the coroutine will be destroyed. |
| 89 | |
| 90 | The LLVM IR for this coroutine looks like this: |
| 91 | |
Aaron Ballman | 378ac7e | 2016-07-23 18:53:35 +0000 | [diff] [blame] | 92 | .. code-block:: none |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 93 | |
| 94 | define i8* @f(i32 %n) { |
| 95 | entry: |
| 96 | %size = call i32 @llvm.coro.size.i32() |
| 97 | %alloc = call i8* @malloc(i32 %size) |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 98 | %beg = call token @llvm.coro.begin(i8* %alloc, i8* null, i32 0, i8* null, i8* null) |
| 99 | %hdl = call noalias i8* @llvm.coro.frame(token %beg) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 100 | br label %loop |
| 101 | loop: |
| 102 | %n.val = phi i32 [ %n, %entry ], [ %inc, %loop ] |
| 103 | %inc = add nsw i32 %n.val, 1 |
| 104 | call void @print(i32 %n.val) |
| 105 | %0 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 106 | switch i8 %0, label %suspend [i8 0, label %loop |
| 107 | i8 1, label %cleanup] |
| 108 | cleanup: |
| 109 | %mem = call i8* @llvm.coro.free(i8* %hdl) |
| 110 | call void @free(i8* %mem) |
| 111 | br label %suspend |
| 112 | suspend: |
| 113 | call void @llvm.coro.end(i8* %hdl, i1 false) |
| 114 | ret i8* %hdl |
| 115 | } |
| 116 | |
| 117 | The `entry` block establishes the coroutine frame. The `coro.size`_ intrinsic is |
| 118 | lowered to a constant representing the size required for the coroutine frame. |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 119 | The `coro.begin`_ intrinsic initializes the coroutine frame and returns the a |
| 120 | token that is used to obtain the coroutine handle via `coro.frame` intrinsic. |
| 121 | The first parameter of `coro.begin` is given a block of memory to be used if the |
| 122 | coroutine frame needs to be allocated dynamically. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 123 | |
| 124 | The `cleanup` block destroys the coroutine frame. The `coro.free`_ intrinsic, |
| 125 | given the coroutine handle, returns a pointer of the memory block to be freed or |
| 126 | `null` if the coroutine frame was not allocated dynamically. The `cleanup` |
| 127 | block is entered when coroutine runs to completion by itself or destroyed via |
| 128 | call to the `coro.destroy`_ intrinsic. |
| 129 | |
| 130 | The `suspend` block contains code to be executed when coroutine runs to |
| 131 | completion or suspended. The `coro.end`_ intrinsic marks the point where |
| 132 | a coroutine needs to return control back to the caller if it is not an initial |
| 133 | invocation of the coroutine. |
| 134 | |
| 135 | The `loop` blocks represents the body of the coroutine. The `coro.suspend`_ |
| 136 | intrinsic in combination with the following switch indicates what happens to |
| 137 | control flow when a coroutine is suspended (default case), resumed (case 0) or |
| 138 | destroyed (case 1). |
| 139 | |
| 140 | Coroutine Transformation |
| 141 | ------------------------ |
| 142 | |
| 143 | One of the steps of coroutine lowering is building the coroutine frame. The |
| 144 | def-use chains are analyzed to determine which objects need be kept alive across |
| 145 | suspend points. In the coroutine shown in the previous section, use of virtual register |
| 146 | `%n.val` is separated from the definition by a suspend point, therefore, it |
| 147 | cannot reside on the stack frame since the latter goes away once the coroutine |
| 148 | is suspended and control is returned back to the caller. An i32 slot is |
| 149 | allocated in the coroutine frame and `%n.val` is spilled and reloaded from that |
| 150 | slot as needed. |
| 151 | |
| 152 | We also store addresses of the resume and destroy functions so that the |
| 153 | `coro.resume` and `coro.destroy` intrinsics can resume and destroy the coroutine |
| 154 | when its identity cannot be determined statically at compile time. For our |
| 155 | example, the coroutine frame will be: |
| 156 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 157 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 158 | |
| 159 | %f.frame = type { void (%f.frame*)*, void (%f.frame*)*, i32 } |
| 160 | |
| 161 | After resume and destroy parts are outlined, function `f` will contain only the |
| 162 | code responsible for creation and initialization of the coroutine frame and |
| 163 | execution of the coroutine until a suspend point is reached: |
| 164 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 165 | .. code-block:: none |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 166 | |
| 167 | define i8* @f(i32 %n) { |
| 168 | entry: |
| 169 | %alloc = call noalias i8* @malloc(i32 24) |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 170 | %beg = call token @llvm.coro.begin(i8* %alloc, i8* null, i32 0, i8* null, i8* null) |
| 171 | %0 = call i8* @llvm.coro.frame(token %beg) |
Mehdi Amini | be1cb22 | 2016-07-27 06:03:47 +0000 | [diff] [blame] | 172 | %frame = bitcast i8* %0 to %f.frame* |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 173 | %1 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 0 |
| 174 | store void (%f.frame*)* @f.resume, void (%f.frame*)** %1 |
| 175 | %2 = getelementptr %f.frame, %f.frame* %frame, i32 0, i32 1 |
| 176 | store void (%f.frame*)* @f.destroy, void (%f.frame*)** %2 |
| 177 | |
| 178 | %inc = add nsw i32 %n, 1 |
| 179 | %inc.spill.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 2 |
| 180 | store i32 %inc, i32* %inc.spill.addr |
| 181 | call void @print(i32 %n) |
| 182 | |
| 183 | ret i8* %frame |
| 184 | } |
| 185 | |
| 186 | Outlined resume part of the coroutine will reside in function `f.resume`: |
| 187 | |
| 188 | .. code-block:: llvm |
| 189 | |
| 190 | define internal fastcc void @f.resume(%f.frame* %frame.ptr.resume) { |
| 191 | entry: |
| 192 | %inc.spill.addr = getelementptr %f.frame, %f.frame* %frame.ptr.resume, i64 0, i32 2 |
| 193 | %inc.spill = load i32, i32* %inc.spill.addr, align 4 |
| 194 | %inc = add i32 %n.val, 1 |
| 195 | store i32 %inc, i32* %inc.spill.addr, align 4 |
| 196 | tail call void @print(i32 %inc) |
| 197 | ret void |
| 198 | } |
| 199 | |
| 200 | Whereas function `f.destroy` will contain the cleanup code for the coroutine: |
| 201 | |
| 202 | .. code-block:: llvm |
| 203 | |
| 204 | define internal fastcc void @f.destroy(%f.frame* %frame.ptr.destroy) { |
| 205 | entry: |
| 206 | %0 = bitcast %f.frame* %frame.ptr.destroy to i8* |
| 207 | tail call void @free(i8* %0) |
| 208 | ret void |
| 209 | } |
| 210 | |
| 211 | Avoiding Heap Allocations |
| 212 | ------------------------- |
| 213 | |
| 214 | A particular coroutine usage pattern, which is illustrated by the `main` |
| 215 | function in the overview section, where a coroutine is created, manipulated and |
| 216 | destroyed by the same calling function, is common for coroutines implementing |
| 217 | RAII idiom and is suitable for allocation elision optimization which avoid |
| 218 | dynamic allocation by storing the coroutine frame as a static `alloca` in its |
| 219 | caller. |
| 220 | |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 221 | In the entry block, we will call `coro.alloc`_ intrinsic that will return `null` |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 222 | when dynamic allocation is required, and an address of an alloca on the caller's |
| 223 | frame where coroutine frame can be stored if dynamic allocation is elided. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 224 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 225 | .. code-block:: none |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 226 | |
| 227 | entry: |
| 228 | %elide = call i8* @llvm.coro.alloc() |
| 229 | %need.dyn.alloc = icmp ne i8* %elide, null |
| 230 | br i1 %need.dyn.alloc, label %coro.begin, label %dyn.alloc |
| 231 | dyn.alloc: |
| 232 | %size = call i32 @llvm.coro.size.i32() |
| 233 | %alloc = call i8* @CustomAlloc(i32 %size) |
| 234 | br label %coro.begin |
| 235 | coro.begin: |
| 236 | %phi = phi i8* [ %elide, %entry ], [ %alloc, %dyn.alloc ] |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 237 | %beg = call token @llvm.coro.begin(i8* %phi, i8* null, i32 0, i8* null, i8* null) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 238 | |
| 239 | In the cleanup block, we will make freeing the coroutine frame conditional on |
| 240 | `coro.free`_ intrinsic. If allocation is elided, `coro.free`_ returns `null` |
| 241 | thus skipping the deallocation code: |
| 242 | |
| 243 | .. code-block:: llvm |
| 244 | |
| 245 | cleanup: |
| 246 | %mem = call i8* @llvm.coro.free(i8* %hdl) |
| 247 | %need.dyn.free = icmp ne i8* %mem, null |
| 248 | br i1 %need.dyn.free, label %dyn.free, label %if.end |
| 249 | dyn.free: |
| 250 | call void @CustomFree(i8* %mem) |
| 251 | br label %if.end |
| 252 | if.end: |
| 253 | ... |
| 254 | |
| 255 | With allocations and deallocations represented as described as above, after |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 256 | coroutine heap allocation elision optimization, the resulting main will be: |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 257 | |
| 258 | .. code-block:: llvm |
| 259 | |
| 260 | define i32 @main() { |
| 261 | entry: |
| 262 | call void @print(i32 4) |
| 263 | call void @print(i32 5) |
| 264 | call void @print(i32 6) |
| 265 | ret i32 0 |
| 266 | } |
| 267 | |
| 268 | Multiple Suspend Points |
| 269 | ----------------------- |
| 270 | |
| 271 | Let's consider the coroutine that has more than one suspend point: |
| 272 | |
Sanjoy Das | 77a9c79 | 2016-07-26 21:03:41 +0000 | [diff] [blame] | 273 | .. code-block:: c++ |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 274 | |
| 275 | void *f(int n) { |
| 276 | for(;;) { |
| 277 | print(n++); |
| 278 | <suspend> |
| 279 | print(-n); |
| 280 | <suspend> |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | Matching LLVM code would look like (with the rest of the code remaining the same |
| 285 | as the code in the previous section): |
| 286 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 287 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 288 | |
| 289 | loop: |
| 290 | %n.addr = phi i32 [ %n, %entry ], [ %inc, %loop.resume ] |
| 291 | call void @print(i32 %n.addr) #4 |
| 292 | %2 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 293 | switch i8 %2, label %suspend [i8 0, label %loop.resume |
| 294 | i8 1, label %cleanup] |
| 295 | loop.resume: |
| 296 | %inc = add nsw i32 %n.addr, 1 |
| 297 | %sub = xor i32 %n.addr, -1 |
| 298 | call void @print(i32 %sub) |
| 299 | %3 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 300 | switch i8 %3, label %suspend [i8 0, label %loop |
| 301 | i8 1, label %cleanup] |
| 302 | |
| 303 | In this case, the coroutine frame would include a suspend index that will |
| 304 | indicate at which suspend point the coroutine needs to resume. The resume |
| 305 | function will use an index to jump to an appropriate basic block and will look |
| 306 | as follows: |
| 307 | |
| 308 | .. code-block:: llvm |
| 309 | |
| 310 | define internal fastcc void @f.Resume(%f.Frame* %FramePtr) { |
| 311 | entry.Resume: |
| 312 | %index.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 2 |
| 313 | %index = load i8, i8* %index.addr, align 1 |
| 314 | %switch = icmp eq i8 %index, 0 |
| 315 | %n.addr = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i64 0, i32 3 |
| 316 | %n = load i32, i32* %n.addr, align 4 |
| 317 | br i1 %switch, label %loop.resume, label %loop |
| 318 | |
| 319 | loop.resume: |
| 320 | %sub = xor i32 %n, -1 |
| 321 | call void @print(i32 %sub) |
| 322 | br label %suspend |
| 323 | loop: |
| 324 | %inc = add nsw i32 %n, 1 |
| 325 | store i32 %inc, i32* %n.addr, align 4 |
| 326 | tail call void @print(i32 %inc) |
| 327 | br label %suspend |
| 328 | |
| 329 | suspend: |
| 330 | %storemerge = phi i8 [ 0, %loop ], [ 1, %loop.resume ] |
| 331 | store i8 %storemerge, i8* %index.addr, align 1 |
| 332 | ret void |
| 333 | } |
| 334 | |
| 335 | If different cleanup code needs to get executed for different suspend points, |
| 336 | a similar switch will be in the `f.destroy` function. |
| 337 | |
| 338 | .. note :: |
| 339 | |
| 340 | Using suspend index in a coroutine state and having a switch in `f.resume` and |
| 341 | `f.destroy` is one of the possible implementation strategies. We explored |
| 342 | another option where a distinct `f.resume1`, `f.resume2`, etc. are created for |
| 343 | every suspend point, and instead of storing an index, the resume and destroy |
| 344 | function pointers are updated at every suspend. Early testing showed that the |
| 345 | current approach is easier on the optimizer than the latter so it is a |
| 346 | lowering strategy implemented at the moment. |
| 347 | |
| 348 | Distinct Save and Suspend |
| 349 | ------------------------- |
| 350 | |
| 351 | In the previous example, setting a resume index (or some other state change that |
| 352 | needs to happen to prepare a coroutine for resumption) happens at the same time as |
| 353 | a suspension of a coroutine. However, in certain cases, it is necessary to control |
| 354 | when coroutine is prepared for resumption and when it is suspended. |
| 355 | |
| 356 | In the following example, a coroutine represents some activity that is driven |
| 357 | by completions of asynchronous operations `async_op1` and `async_op2` which get |
| 358 | a coroutine handle as a parameter and resume the coroutine once async |
| 359 | operation is finished. |
| 360 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 361 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 362 | |
| 363 | void g() { |
| 364 | for (;;) |
| 365 | if (cond()) { |
| 366 | async_op1(<coroutine-handle>); // will resume once async_op1 completes |
| 367 | <suspend> |
| 368 | do_one(); |
| 369 | } |
| 370 | else { |
| 371 | async_op2(<coroutine-handle>); // will resume once async_op2 completes |
| 372 | <suspend> |
| 373 | do_two(); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | In this case, coroutine should be ready for resumption prior to a call to |
| 379 | `async_op1` and `async_op2`. The `coro.save`_ intrinsic is used to indicate a |
| 380 | point when coroutine should be ready for resumption (namely, when a resume index |
| 381 | should be stored in the coroutine frame, so that it can be resumed at the |
| 382 | correct resume point): |
| 383 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 384 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 385 | |
| 386 | if.true: |
| 387 | %save1 = call token @llvm.coro.save(i8* %hdl) |
| 388 | call void async_op1(i8* %hdl) |
| 389 | %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false) |
| 390 | switch i8 %suspend1, label %suspend [i8 0, label %resume1 |
| 391 | i8 1, label %cleanup] |
| 392 | if.false: |
| 393 | %save2 = call token @llvm.coro.save(i8* %hdl) |
| 394 | call void async_op2(i8* %hdl) |
| 395 | %suspend2 = call i1 @llvm.coro.suspend(token %save2, i1 false) |
| 396 | switch i8 %suspend1, label %suspend [i8 0, label %resume2 |
| 397 | i8 1, label %cleanup] |
| 398 | |
| 399 | .. _coroutine promise: |
| 400 | |
| 401 | Coroutine Promise |
| 402 | ----------------- |
| 403 | |
| 404 | A coroutine author or a frontend may designate a distinguished `alloca` that can |
| 405 | be used to communicate with the coroutine. This distinguished alloca is called |
| 406 | **coroutine promise** and is provided as a third parameter to the `coro.begin`_ |
| 407 | intrinsic. |
| 408 | |
| 409 | The following coroutine designates a 32 bit integer `promise` and uses it to |
| 410 | store the current value produced by a coroutine. |
| 411 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 412 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 413 | |
| 414 | define i8* @f(i32 %n) { |
| 415 | entry: |
| 416 | %promise = alloca i32 |
| 417 | %pv = bitcast i32* %promise to i8* |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 418 | %elide = call i8* @llvm.coro.alloc() |
| 419 | %need.dyn.alloc = icmp ne i8* %elide, null |
| 420 | br i1 %need.dyn.alloc, label %coro.begin, label %dyn.alloc |
| 421 | dyn.alloc: |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 422 | %size = call i32 @llvm.coro.size.i32() |
| 423 | %alloc = call i8* @malloc(i32 %size) |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 424 | br label %coro.begin |
| 425 | coro.begin: |
| 426 | %phi = phi i8* [ %elide, %entry ], [ %alloc, %dyn.alloc ] |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 427 | %beg = call token @llvm.coro.begin(i8* %phi, i8* %elide, i32 0, i8* %pv, i8* null) |
| 428 | %hdl = call i8* @llvm.coro.frame(token %beg) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 429 | br label %loop |
| 430 | loop: |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 431 | %n.val = phi i32 [ %n, %coro.begin ], [ %inc, %loop ] |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 432 | %inc = add nsw i32 %n.val, 1 |
| 433 | store i32 %n.val, i32* %promise |
| 434 | %0 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 435 | switch i8 %0, label %suspend [i8 0, label %loop |
| 436 | i8 1, label %cleanup] |
| 437 | cleanup: |
| 438 | %mem = call i8* @llvm.coro.free(i8* %hdl) |
| 439 | call void @free(i8* %mem) |
| 440 | br label %suspend |
| 441 | suspend: |
| 442 | call void @llvm.coro.end(i8* %hdl, i1 false) |
| 443 | ret i8* %hdl |
| 444 | } |
| 445 | |
| 446 | A coroutine consumer can rely on the `coro.promise`_ intrinsic to access the |
| 447 | coroutine promise. |
| 448 | |
| 449 | .. code-block:: llvm |
| 450 | |
| 451 | define i32 @main() { |
| 452 | entry: |
| 453 | %hdl = call i8* @f(i32 4) |
| 454 | %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false) |
| 455 | %promise.addr = bitcast i8* %promise.addr.raw to i32* |
| 456 | %val0 = load i32, i32* %promise.addr |
| 457 | call void @print(i32 %val0) |
| 458 | call void @llvm.coro.resume(i8* %hdl) |
| 459 | %val1 = load i32, i32* %promise.addr |
| 460 | call void @print(i32 %val1) |
| 461 | call void @llvm.coro.resume(i8* %hdl) |
| 462 | %val2 = load i32, i32* %promise.addr |
| 463 | call void @print(i32 %val2) |
| 464 | call void @llvm.coro.destroy(i8* %hdl) |
| 465 | ret i32 0 |
| 466 | } |
| 467 | |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 468 | After example in this section is compiled, result of the compilation will be: |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 469 | |
| 470 | .. code-block:: llvm |
| 471 | |
| 472 | define i32 @main() { |
| 473 | entry: |
| 474 | tail call void @print(i32 4) |
| 475 | tail call void @print(i32 5) |
| 476 | tail call void @print(i32 6) |
| 477 | ret i32 0 |
| 478 | } |
| 479 | |
| 480 | .. _final: |
| 481 | .. _final suspend: |
| 482 | |
| 483 | Final Suspend |
| 484 | ------------- |
| 485 | |
| 486 | A coroutine author or a frontend may designate a particular suspend to be final, |
| 487 | by setting the second argument of the `coro.suspend`_ intrinsic to `true`. |
| 488 | Such a suspend point has two properties: |
| 489 | |
| 490 | * it is possible to check whether a suspended coroutine is at the final suspend |
| 491 | point via `coro.done`_ intrinsic; |
| 492 | |
| 493 | * a resumption of a coroutine stopped at the final suspend point leads to |
| 494 | undefined behavior. The only possible action for a coroutine at a final |
| 495 | suspend point is destroying it via `coro.destroy`_ intrinsic. |
| 496 | |
| 497 | From the user perspective, the final suspend point represents an idea of a |
| 498 | coroutine reaching the end. From the compiler perspective, it is an optimization |
| 499 | opportunity for reducing number of resume points (and therefore switch cases) in |
| 500 | the resume function. |
| 501 | |
| 502 | The following is an example of a function that keeps resuming the coroutine |
| 503 | until the final suspend point is reached after which point the coroutine is |
| 504 | destroyed: |
| 505 | |
| 506 | .. code-block:: llvm |
| 507 | |
| 508 | define i32 @main() { |
| 509 | entry: |
| 510 | %hdl = call i8* @f(i32 4) |
| 511 | br label %while |
| 512 | while: |
| 513 | call void @llvm.coro.resume(i8* %hdl) |
| 514 | %done = call i1 @llvm.coro.done(i8* %hdl) |
| 515 | br i1 %done, label %end, label %while |
| 516 | end: |
| 517 | call void @llvm.coro.destroy(i8* %hdl) |
| 518 | ret i32 0 |
| 519 | } |
| 520 | |
| 521 | Usually, final suspend point is a frontend injected suspend point that does not |
| 522 | correspond to any explicitly authored suspend point of the high level language. |
| 523 | For example, for a Python generator that has only one suspend point: |
| 524 | |
| 525 | .. code-block:: python |
| 526 | |
| 527 | def coroutine(n): |
| 528 | for i in range(n): |
| 529 | yield i |
| 530 | |
| 531 | Python frontend would inject two more suspend points, so that the actual code |
| 532 | looks like this: |
| 533 | |
Sanjoy Das | 77a9c79 | 2016-07-26 21:03:41 +0000 | [diff] [blame] | 534 | .. code-block:: c |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 535 | |
| 536 | void* coroutine(int n) { |
| 537 | int current_value; |
| 538 | <designate current_value to be coroutine promise> |
| 539 | <SUSPEND> // injected suspend point, so that the coroutine starts suspended |
| 540 | for (int i = 0; i < n; ++i) { |
| 541 | current_value = i; <SUSPEND>; // corresponds to "yield i" |
| 542 | } |
| 543 | <SUSPEND final=true> // injected final suspend point |
| 544 | } |
| 545 | |
| 546 | and python iterator `__next__` would look like: |
| 547 | |
Sanjoy Das | 77a9c79 | 2016-07-26 21:03:41 +0000 | [diff] [blame] | 548 | .. code-block:: c++ |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 549 | |
| 550 | int __next__(void* hdl) { |
| 551 | coro.resume(hdl); |
| 552 | if (coro.done(hdl)) throw StopIteration(); |
| 553 | return *(int*)coro.promise(hdl, 4, false); |
| 554 | } |
| 555 | |
| 556 | Intrinsics |
| 557 | ========== |
| 558 | |
| 559 | Coroutine Manipulation Intrinsics |
| 560 | --------------------------------- |
| 561 | |
| 562 | Intrinsics described in this section are used to manipulate an existing |
| 563 | coroutine. They can be used in any function which happen to have a pointer |
| 564 | to a `coroutine frame`_ or a pointer to a `coroutine promise`_. |
| 565 | |
| 566 | .. _coro.destroy: |
| 567 | |
| 568 | 'llvm.coro.destroy' Intrinsic |
| 569 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 570 | |
| 571 | Syntax: |
| 572 | """"""" |
| 573 | |
| 574 | :: |
| 575 | |
| 576 | declare void @llvm.coro.destroy(i8* <handle>) |
| 577 | |
| 578 | Overview: |
| 579 | """"""""" |
| 580 | |
| 581 | The '``llvm.coro.destroy``' intrinsic destroys a suspended |
| 582 | coroutine. |
| 583 | |
| 584 | Arguments: |
| 585 | """""""""" |
| 586 | |
| 587 | The argument is a coroutine handle to a suspended coroutine. |
| 588 | |
| 589 | Semantics: |
| 590 | """""""""" |
| 591 | |
| 592 | When possible, the `coro.destroy` intrinsic is replaced with a direct call to |
| 593 | the coroutine destroy function. Otherwise it is replaced with an indirect call |
| 594 | based on the function pointer for the destroy function stored in the coroutine |
| 595 | frame. Destroying a coroutine that is not suspended leads to undefined behavior. |
| 596 | |
| 597 | .. _coro.resume: |
| 598 | |
| 599 | 'llvm.coro.resume' Intrinsic |
| 600 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 601 | |
| 602 | :: |
| 603 | |
| 604 | declare void @llvm.coro.resume(i8* <handle>) |
| 605 | |
| 606 | Overview: |
| 607 | """"""""" |
| 608 | |
| 609 | The '``llvm.coro.resume``' intrinsic resumes a suspended coroutine. |
| 610 | |
| 611 | Arguments: |
| 612 | """""""""" |
| 613 | |
| 614 | The argument is a handle to a suspended coroutine. |
| 615 | |
| 616 | Semantics: |
| 617 | """""""""" |
| 618 | |
| 619 | When possible, the `coro.resume` intrinsic is replaced with a direct call to the |
| 620 | coroutine resume function. Otherwise it is replaced with an indirect call based |
| 621 | on the function pointer for the resume function stored in the coroutine frame. |
| 622 | Resuming a coroutine that is not suspended leads to undefined behavior. |
| 623 | |
| 624 | .. _coro.done: |
| 625 | |
| 626 | 'llvm.coro.done' Intrinsic |
| 627 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 628 | |
| 629 | :: |
| 630 | |
| 631 | declare i1 @llvm.coro.done(i8* <handle>) |
| 632 | |
| 633 | Overview: |
| 634 | """"""""" |
| 635 | |
| 636 | The '``llvm.coro.done``' intrinsic checks whether a suspended coroutine is at |
| 637 | the final suspend point or not. |
| 638 | |
| 639 | Arguments: |
| 640 | """""""""" |
| 641 | |
| 642 | The argument is a handle to a suspended coroutine. |
| 643 | |
| 644 | Semantics: |
| 645 | """""""""" |
| 646 | |
| 647 | Using this intrinsic on a coroutine that does not have a `final suspend`_ point |
| 648 | or on a coroutine that is not suspended leads to undefined behavior. |
| 649 | |
| 650 | .. _coro.promise: |
| 651 | |
| 652 | 'llvm.coro.promise' Intrinsic |
| 653 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 654 | |
| 655 | :: |
| 656 | |
| 657 | declare i8* @llvm.coro.promise(i8* <ptr>, i32 <alignment>, i1 <from>) |
| 658 | |
| 659 | Overview: |
| 660 | """"""""" |
| 661 | |
| 662 | The '``llvm.coro.promise``' intrinsic obtains a pointer to a |
| 663 | `coroutine promise`_ given a coroutine handle and vice versa. |
| 664 | |
| 665 | Arguments: |
| 666 | """""""""" |
| 667 | |
| 668 | The first argument is a handle to a coroutine if `from` is false. Otherwise, |
| 669 | it is a pointer to a coroutine promise. |
| 670 | |
| 671 | The second argument is an alignment requirements of the promise. |
| 672 | If a frontend designated `%promise = alloca i32` as a promise, the alignment |
| 673 | argument to `coro.promise` should be the alignment of `i32` on the target |
| 674 | platform. If a frontend designated `%promise = alloca i32, align 16` as a |
| 675 | promise, the alignment argument should be 16. |
| 676 | This argument only accepts constants. |
| 677 | |
| 678 | The third argument is a boolean indicating a direction of the transformation. |
| 679 | If `from` is true, the intrinsic returns a coroutine handle given a pointer |
| 680 | to a promise. If `from` is false, the intrinsics return a pointer to a promise |
| 681 | from a coroutine handle. This argument only accepts constants. |
| 682 | |
| 683 | Semantics: |
| 684 | """""""""" |
| 685 | |
| 686 | Using this intrinsic on a coroutine that does not have a coroutine promise |
| 687 | leads to undefined behavior. It is possible to read and modify coroutine |
| 688 | promise of the coroutine which is currently executing. The coroutine author and |
| 689 | a coroutine user are responsible to makes sure there is no data races. |
| 690 | |
| 691 | Example: |
| 692 | """""""" |
| 693 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 694 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 695 | |
| 696 | define i8* @f(i32 %n) { |
| 697 | entry: |
| 698 | %promise = alloca i32 |
| 699 | %pv = bitcast i32* %promise to i8* |
| 700 | ... |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 701 | ; the fourth argument to coro.begin points to the coroutine promise. |
| 702 | %beg = call token @llvm.coro.begin(i8* %alloc, i8* null, i32 0, i8* %pv, i8* null) |
| 703 | %hdl = call noalias i8* @llvm.coro.frame(token %beg) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 704 | ... |
| 705 | store i32 42, i32* %promise ; store something into the promise |
| 706 | ... |
| 707 | ret i8* %hdl |
| 708 | } |
| 709 | |
| 710 | define i32 @main() { |
| 711 | entry: |
| 712 | %hdl = call i8* @f(i32 4) ; starts the coroutine and returns its handle |
| 713 | %promise.addr.raw = call i8* @llvm.coro.promise(i8* %hdl, i32 4, i1 false) |
| 714 | %promise.addr = bitcast i8* %promise.addr.raw to i32* |
| 715 | %val = load i32, i32* %promise.addr ; load a value from the promise |
| 716 | call void @print(i32 %val) |
| 717 | call void @llvm.coro.destroy(i8* %hdl) |
| 718 | ret i32 0 |
| 719 | } |
| 720 | |
| 721 | .. _coroutine intrinsics: |
| 722 | |
| 723 | Coroutine Structure Intrinsics |
| 724 | ------------------------------ |
| 725 | Intrinsics described in this section are used within a coroutine to describe |
| 726 | the coroutine structure. They should not be used outside of a coroutine. |
| 727 | |
| 728 | .. _coro.size: |
| 729 | |
| 730 | 'llvm.coro.size' Intrinsic |
| 731 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 732 | :: |
| 733 | |
| 734 | declare i32 @llvm.coro.size.i32() |
| 735 | declare i64 @llvm.coro.size.i64() |
| 736 | |
| 737 | Overview: |
| 738 | """"""""" |
| 739 | |
| 740 | The '``llvm.coro.size``' intrinsic returns the number of bytes |
| 741 | required to store a `coroutine frame`_. |
| 742 | |
| 743 | Arguments: |
| 744 | """""""""" |
| 745 | |
| 746 | None |
| 747 | |
| 748 | Semantics: |
| 749 | """""""""" |
| 750 | |
| 751 | The `coro.size` intrinsic is lowered to a constant representing the size of |
| 752 | the coroutine frame. |
| 753 | |
| 754 | .. _coro.begin: |
| 755 | |
| 756 | 'llvm.coro.begin' Intrinsic |
| 757 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 758 | :: |
| 759 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 760 | declare i8* @llvm.coro.begin(i8* <mem>, i8* <elide>, i32 <align>, i8* <promise>, i8* <fnaddr>) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 761 | |
| 762 | Overview: |
| 763 | """"""""" |
| 764 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 765 | The '``llvm.coro.begin``' intrinsic captures coroutine initialization |
| 766 | information and returns a token that can be used by `coro.frame` intrinsic to |
| 767 | return an address of the coroutine frame. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 768 | |
| 769 | Arguments: |
| 770 | """""""""" |
| 771 | |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 772 | The first argument is a pointer to a block of memory where coroutine frame |
| 773 | will be stored. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 774 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 775 | The second argument is either null or an SSA value of `coro.alloc` intrinsic. |
| 776 | |
| 777 | The third argument provides information on the alignment of the memory returned |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 778 | by the allocation function and given to `coro.begin` by the first argument. If |
| 779 | this argument is 0, the memory is assumed to be aligned to 2 * sizeof(i8*). |
| 780 | This argument only accepts constants. |
| 781 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 782 | The fourth argument, if not `null`, designates a particular alloca instruction to |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 783 | be a `coroutine promise`_. |
| 784 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 785 | The fifth argument is `null` before coroutine is split, and later is replaced |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 786 | to point to a private global constant array containing function pointers to |
| 787 | outlined resume and destroy parts of the coroutine. |
| 788 | |
| 789 | Semantics: |
| 790 | """""""""" |
| 791 | |
| 792 | Depending on the alignment requirements of the objects in the coroutine frame |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 793 | and/or on the codegen compactness reasons the pointer returned from `coro.frame` |
| 794 | associated with a particular `coro.begin` may be at offset to the `%mem` |
| 795 | argument. (This could be beneficial if instructions that express relative access |
| 796 | to data can be more compactly encoded with small positive and negative offsets). |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 797 | |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 798 | A frontend should emit exactly one `coro.begin` intrinsic per coroutine. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 799 | |
| 800 | .. _coro.free: |
| 801 | |
| 802 | 'llvm.coro.free' Intrinsic |
| 803 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 804 | :: |
| 805 | |
| 806 | declare i8* @llvm.coro.free(i8* <frame>) |
| 807 | |
| 808 | Overview: |
| 809 | """"""""" |
| 810 | |
| 811 | The '``llvm.coro.free``' intrinsic returns a pointer to a block of memory where |
| 812 | coroutine frame is stored or `null` if this instance of a coroutine did not use |
| 813 | dynamically allocated memory for its coroutine frame. |
| 814 | |
| 815 | Arguments: |
| 816 | """""""""" |
| 817 | |
| 818 | A pointer to the coroutine frame. This should be the same pointer that was |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 819 | returned by prior `coro.frame` call. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 820 | |
| 821 | Example (custom deallocation function): |
| 822 | """"""""""""""""""""""""""""""""""""""" |
| 823 | |
| 824 | .. code-block:: llvm |
| 825 | |
| 826 | cleanup: |
| 827 | %mem = call i8* @llvm.coro.free(i8* %frame) |
| 828 | %mem_not_null = icmp ne i8* %mem, null |
| 829 | br i1 %mem_not_null, label %if.then, label %if.end |
| 830 | if.then: |
| 831 | call void @CustomFree(i8* %mem) |
| 832 | br label %if.end |
| 833 | if.end: |
| 834 | ret void |
| 835 | |
| 836 | Example (standard deallocation functions): |
| 837 | """""""""""""""""""""""""""""""""""""""""" |
| 838 | |
| 839 | .. code-block:: llvm |
| 840 | |
| 841 | cleanup: |
| 842 | %mem = call i8* @llvm.coro.free(i8* %frame) |
| 843 | call void @free(i8* %mem) |
| 844 | ret void |
| 845 | |
| 846 | .. _coro.alloc: |
| 847 | |
| 848 | 'llvm.coro.alloc' Intrinsic |
| 849 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 850 | :: |
| 851 | |
| 852 | declare i8* @llvm.coro.alloc() |
| 853 | |
| 854 | Overview: |
| 855 | """"""""" |
| 856 | |
| 857 | The '``llvm.coro.alloc``' intrinsic returns an address of the memory on the |
| 858 | callers frame where coroutine frame of this coroutine can be placed or `null` |
| 859 | otherwise. |
| 860 | |
| 861 | Arguments: |
| 862 | """""""""" |
| 863 | |
| 864 | None |
| 865 | |
| 866 | Semantics: |
| 867 | """""""""" |
| 868 | |
| 869 | If the coroutine is eligible for heap elision, this intrinsic is lowered to an |
| 870 | alloca storing the coroutine frame. Otherwise, it is lowered to constant `null`. |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 871 | |
| 872 | A frontend should emit at most one `coro.alloc` intrinsic per coroutine. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 873 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 874 | If `coro.alloc` is present, the second parameter to `coro.begin` should refer |
| 875 | to it. |
| 876 | |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 877 | Example: |
| 878 | """""""" |
| 879 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 880 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 881 | |
| 882 | entry: |
| 883 | %elide = call i8* @llvm.coro.alloc() |
| 884 | %0 = icmp ne i8* %elide, null |
| 885 | br i1 %0, label %coro.begin, label %coro.alloc |
| 886 | |
| 887 | coro.alloc: |
| 888 | %frame.size = call i32 @llvm.coro.size() |
| 889 | %alloc = call i8* @MyAlloc(i32 %frame.size) |
| 890 | br label %coro.begin |
| 891 | |
| 892 | coro.begin: |
| 893 | %phi = phi i8* [ %elide, %entry ], [ %alloc, %coro.alloc ] |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 894 | %beg = call token @llvm.coro.begin(i8* %phi, i8* %elide, i32 0, i8* null, i8* null) |
| 895 | %frame = call i8* @llvm.coro.frame(token %beg) |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 896 | |
| 897 | .. _coro.frame: |
| 898 | |
| 899 | 'llvm.coro.frame' Intrinsic |
| 900 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 901 | :: |
| 902 | |
| 903 | declare i8* @llvm.coro.frame() |
| 904 | |
| 905 | Overview: |
| 906 | """"""""" |
| 907 | |
| 908 | The '``llvm.coro.frame``' intrinsic returns an address of the coroutine frame of |
| 909 | the enclosing coroutine. |
| 910 | |
| 911 | Arguments: |
| 912 | """""""""" |
| 913 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 914 | A token that refers to `coro.begin` instruction. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 915 | |
| 916 | Semantics: |
| 917 | """""""""" |
| 918 | |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 919 | This intrinsic is lowered to refer to address of the coroutine frame. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 920 | |
| 921 | .. _coro.end: |
| 922 | |
| 923 | 'llvm.coro.end' Intrinsic |
| 924 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 925 | :: |
| 926 | |
| 927 | declare void @llvm.coro.end(i8* <handle>, i1 <unwind>) |
| 928 | |
| 929 | Overview: |
| 930 | """"""""" |
| 931 | |
| 932 | The '``llvm.coro.end``' marks the point where execution of the resume part of |
| 933 | the coroutine should end and control returns back to the caller. |
| 934 | |
| 935 | |
| 936 | Arguments: |
| 937 | """""""""" |
| 938 | |
| 939 | The first argument should refer to the coroutine handle of the enclosing coroutine. |
| 940 | |
| 941 | The second argument should be `true` if this coro.end is in the block that is |
| 942 | part of the unwind sequence leaving the coroutine body due to exception prior to |
| 943 | the first reaching any suspend points, and `false` otherwise. |
| 944 | |
| 945 | Semantics: |
| 946 | """""""""" |
| 947 | The `coro.end`_ intrinsic is a no-op during an initial invocation of the |
| 948 | coroutine. When the coroutine resumes, the intrinsic marks the point when |
| 949 | coroutine need to return control back to the caller. |
| 950 | |
| 951 | This intrinsic is removed by the CoroSplit pass when a coroutine is split into |
| 952 | the start, resume and destroy parts. In start part, the intrinsic is removed, |
| 953 | in resume and destroy parts, it is replaced with `ret void` instructions and |
| 954 | the rest of the block containing `coro.end` instruction is discarded. |
| 955 | |
| 956 | In landing pads it is replaced with an appropriate instruction to unwind to |
| 957 | caller. |
| 958 | |
| 959 | A frontend is allowed to supply null as the first parameter, in this case |
| 960 | `coro-early` pass will replace the null with an appropriate coroutine handle |
| 961 | value. |
| 962 | |
| 963 | .. _coro.suspend: |
| 964 | .. _suspend points: |
| 965 | |
| 966 | 'llvm.coro.suspend' Intrinsic |
| 967 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 968 | :: |
| 969 | |
| 970 | declare i8 @llvm.coro.suspend(token <save>, i1 <final>) |
| 971 | |
| 972 | Overview: |
| 973 | """"""""" |
| 974 | |
| 975 | The '``llvm.coro.suspend``' marks the point where execution of the coroutine |
| 976 | need to get suspended and control returned back to the caller. |
| 977 | Conditional branches consuming the result of this intrinsic lead to basic blocks |
| 978 | where coroutine should proceed when suspended (-1), resumed (0) or destroyed |
| 979 | (1). |
| 980 | |
| 981 | Arguments: |
| 982 | """""""""" |
| 983 | |
| 984 | The first argument refers to a token of `coro.save` intrinsic that marks the |
| 985 | point when coroutine state is prepared for suspension. If `none` token is passed, |
| 986 | the intrinsic behaves as if there were a `coro.save` immediately preceding |
| 987 | the `coro.suspend` intrinsic. |
| 988 | |
| 989 | The second argument indicates whether this suspension point is `final`_. |
| 990 | The second argument only accepts constants. If more than one suspend point is |
| 991 | designated as final, the resume and destroy branches should lead to the same |
| 992 | basic blocks. |
| 993 | |
| 994 | Example (normal suspend point): |
| 995 | """"""""""""""""""""""""""""""" |
| 996 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 997 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 998 | |
| 999 | %0 = call i8 @llvm.coro.suspend(token none, i1 false) |
| 1000 | switch i8 %0, label %suspend [i8 0, label %resume |
| 1001 | i8 1, label %cleanup] |
| 1002 | |
| 1003 | Example (final suspend point): |
| 1004 | """""""""""""""""""""""""""""" |
| 1005 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 1006 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1007 | |
| 1008 | while.end: |
| 1009 | %s.final = call i8 @llvm.coro.suspend(token none, i1 true) |
| 1010 | switch i8 %s.final, label %suspend [i8 0, label %trap |
| 1011 | i8 1, label %cleanup] |
| 1012 | trap: |
| 1013 | call void @llvm.trap() |
| 1014 | unreachable |
| 1015 | |
| 1016 | Semantics: |
| 1017 | """""""""" |
| 1018 | |
| 1019 | If a coroutine that was suspended at the suspend point marked by this intrinsic |
| 1020 | is resumed via `coro.resume`_ the control will transfer to the basic block |
| 1021 | of the 0-case. If it is resumed via `coro.destroy`_, it will proceed to the |
| 1022 | basic block indicated by the 1-case. To suspend, coroutine proceed to the |
| 1023 | default label. |
| 1024 | |
| 1025 | If suspend intrinsic is marked as final, it can consider the `true` branch |
| 1026 | unreachable and can perform optimizations that can take advantage of that fact. |
| 1027 | |
| 1028 | .. _coro.save: |
| 1029 | |
| 1030 | 'llvm.coro.save' Intrinsic |
| 1031 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1032 | :: |
| 1033 | |
| 1034 | declare token @llvm.coro.save(i8* <handle>) |
| 1035 | |
| 1036 | Overview: |
| 1037 | """"""""" |
| 1038 | |
| 1039 | The '``llvm.coro.save``' marks the point where a coroutine need to update its |
| 1040 | state to prepare for resumption to be considered suspended (and thus eligible |
| 1041 | for resumption). |
| 1042 | |
| 1043 | Arguments: |
| 1044 | """""""""" |
| 1045 | |
| 1046 | The first argument points to a coroutine handle of the enclosing coroutine. |
| 1047 | |
| 1048 | Semantics: |
| 1049 | """""""""" |
| 1050 | |
| 1051 | Whatever coroutine state changes are required to enable resumption of |
| 1052 | the coroutine from the corresponding suspend point should be done at the point |
| 1053 | of `coro.save` intrinsic. |
| 1054 | |
| 1055 | Example: |
| 1056 | """""""" |
| 1057 | |
| 1058 | Separate save and suspend points are necessary when a coroutine is used to |
| 1059 | represent an asynchronous control flow driven by callbacks representing |
| 1060 | completions of asynchronous operations. |
| 1061 | |
| 1062 | In such a case, a coroutine should be ready for resumption prior to a call to |
| 1063 | `async_op` function that may trigger resumption of a coroutine from the same or |
| 1064 | a different thread possibly prior to `async_op` call returning control back |
| 1065 | to the coroutine: |
| 1066 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 1067 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1068 | |
| 1069 | %save1 = call token @llvm.coro.save(i8* %hdl) |
| 1070 | call void async_op1(i8* %hdl) |
| 1071 | %suspend1 = call i1 @llvm.coro.suspend(token %save1, i1 false) |
| 1072 | switch i8 %suspend1, label %suspend [i8 0, label %resume1 |
| 1073 | i8 1, label %cleanup] |
| 1074 | |
| 1075 | .. _coro.param: |
| 1076 | |
| 1077 | 'llvm.coro.param' Intrinsic |
| 1078 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1079 | :: |
| 1080 | |
| 1081 | declare i1 @llvm.coro.param(i8* <original>, i8* <copy>) |
| 1082 | |
| 1083 | Overview: |
| 1084 | """"""""" |
| 1085 | |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 1086 | The '``llvm.coro.param``' is used by a frontend to mark up the code used to |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1087 | construct and destruct copies of the parameters. If the optimizer discovers that |
| 1088 | a particular parameter copy is not used after any suspends, it can remove the |
| 1089 | construction and destruction of the copy by replacing corresponding coro.param |
| 1090 | with `i1 false` and replacing any use of the `copy` with the `original`. |
| 1091 | |
| 1092 | Arguments: |
| 1093 | """""""""" |
| 1094 | |
| 1095 | The first argument points to an `alloca` storing the value of a parameter to a |
| 1096 | coroutine. |
| 1097 | |
| 1098 | The second argument points to an `alloca` storing the value of the copy of that |
| 1099 | parameter. |
| 1100 | |
| 1101 | Semantics: |
| 1102 | """""""""" |
| 1103 | |
| 1104 | The optimizer is free to always replace this intrinsic with `i1 true`. |
| 1105 | |
| 1106 | The optimizer is also allowed to replace it with `i1 false` provided that the |
| 1107 | parameter copy is only used prior to control flow reaching any of the suspend |
| 1108 | points. The code that would be DCE'd if the `coro.param` is replaced with |
| 1109 | `i1 false` is not considered to be a use of the parameter copy. |
| 1110 | |
| 1111 | The frontend can emit this intrinsic if its language rules allow for this |
| 1112 | optimization. |
| 1113 | |
| 1114 | Example: |
| 1115 | """""""" |
| 1116 | Consider the following example. A coroutine takes two parameters `a` and `b` |
| 1117 | that has a destructor and a move constructor. |
| 1118 | |
Sanjoy Das | 77a9c79 | 2016-07-26 21:03:41 +0000 | [diff] [blame] | 1119 | .. code-block:: c++ |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1120 | |
| 1121 | struct A { ~A(); A(A&&); bool foo(); void bar(); }; |
| 1122 | |
| 1123 | task<int> f(A a, A b) { |
| 1124 | if (a.foo()) |
| 1125 | return 42; |
| 1126 | |
| 1127 | a.bar(); |
| 1128 | co_await read_async(); // introduces suspend point |
| 1129 | b.bar(); |
| 1130 | } |
| 1131 | |
| 1132 | Note that, uses of `b` is used after a suspend point and thus must be copied |
| 1133 | into a coroutine frame, whereas `a` does not have to, since it never used |
| 1134 | after suspend. |
| 1135 | |
| 1136 | A frontend can create parameter copies for `a` and `b` as follows: |
| 1137 | |
Aaron Ballman | bc7c2d0 | 2016-07-23 20:11:21 +0000 | [diff] [blame] | 1138 | .. code-block:: text |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1139 | |
| 1140 | task<int> f(A a', A b') { |
| 1141 | a = alloca A; |
| 1142 | b = alloca A; |
| 1143 | // move parameters to its copies |
| 1144 | if (coro.param(a', a)) A::A(a, A&& a'); |
| 1145 | if (coro.param(b', b)) A::A(b, A&& b'); |
| 1146 | ... |
| 1147 | // destroy parameters copies |
| 1148 | if (coro.param(a', a)) A::~A(a); |
| 1149 | if (coro.param(b', b)) A::~A(b); |
| 1150 | } |
| 1151 | |
| 1152 | The optimizer can replace coro.param(a',a) with `i1 false` and replace all uses |
| 1153 | of `a` with `a'`, since it is not used after suspend. |
| 1154 | |
| 1155 | The optimizer must replace coro.param(b', b) with `i1 true`, since `b` is used |
| 1156 | after suspend and therefore, it has to reside in the coroutine frame. |
| 1157 | |
| 1158 | Coroutine Transformation Passes |
| 1159 | =============================== |
| 1160 | CoroEarly |
| 1161 | --------- |
| 1162 | The pass CoroEarly lowers coroutine intrinsics that hide the details of the |
| 1163 | structure of the coroutine frame, but, otherwise not needed to be preserved to |
| 1164 | help later coroutine passes. This pass lowers `coro.frame`_, `coro.done`_, |
| 1165 | and `coro.promise`_ intrinsics. |
| 1166 | |
| 1167 | .. _CoroSplit: |
| 1168 | |
| 1169 | CoroSplit |
| 1170 | --------- |
| 1171 | The pass CoroSplit buides coroutine frame and outlines resume and destroy parts |
| 1172 | into separate functions. |
| 1173 | |
| 1174 | CoroElide |
| 1175 | --------- |
| 1176 | The pass CoroElide examines if the inlined coroutine is eligible for heap |
| 1177 | allocation elision optimization. If so, it replaces `coro.alloc` and |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 1178 | `coro.frame` intrinsic with an address of a coroutine frame placed on its caller |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1179 | and replaces `coro.free` intrinsics with `null` to remove the deallocation code. |
| 1180 | This pass also replaces `coro.resume` and `coro.destroy` intrinsics with direct |
| 1181 | calls to resume and destroy functions for a particular coroutine where possible. |
| 1182 | |
| 1183 | CoroCleanup |
| 1184 | ----------- |
| 1185 | This pass runs late to lower all coroutine related intrinsics not replaced by |
| 1186 | earlier passes. |
| 1187 | |
| 1188 | Upstreaming sequence (rough plan) |
| 1189 | ================================= |
David Majnemer | 7855719 | 2016-07-27 05:12:35 +0000 | [diff] [blame] | 1190 | #. Add documentation. |
David Majnemer | 3d32b7e | 2016-07-28 21:04:31 +0000 | [diff] [blame] | 1191 | #. Add coroutine intrinsics. |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 1192 | #. Add empty coroutine passes. |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1193 | #. Add coroutine devirtualization + tests. |
| 1194 | #. Add CGSCC restart trigger + tests. |
| 1195 | #. Add coroutine heap elision + tests. |
Gor Nishanov | b2a9c02 | 2016-08-10 16:40:39 +0000 | [diff] [blame^] | 1196 | #. Add custom allocation heap elision + tests. <== we are here |
David Majnemer | a653927 | 2016-07-23 04:05:08 +0000 | [diff] [blame] | 1197 | #. Add coroutine splitting logic + tests. |
| 1198 | #. Add simple coroutine frame builder + tests. |
| 1199 | #. Add the rest of the logic + tests. (Maybe split further as needed). |
| 1200 | |
| 1201 | Areas Requiring Attention |
| 1202 | ========================= |
| 1203 | #. A coroutine frame is bigger than it could be. Adding stack packing and stack |
| 1204 | coloring like optimization on the coroutine frame will result in tighter |
| 1205 | coroutine frames. |
| 1206 | |
| 1207 | #. Take advantage of the lifetime intrinsics for the data that goes into the |
| 1208 | coroutine frame. Leave lifetime intrinsics as is for the data that stays in |
| 1209 | allocas. |
| 1210 | |
| 1211 | #. The CoroElide optimization pass relies on coroutine ramp function to be |
| 1212 | inlined. It would be beneficial to split the ramp function further to |
| 1213 | increase the chance that it will get inlined into its caller. |
| 1214 | |
| 1215 | #. Design a convention that would make it possible to apply coroutine heap |
| 1216 | elision optimization across ABI boundaries. |
| 1217 | |
| 1218 | #. Cannot handle coroutines with `inalloca` parameters (used in x86 on Windows). |
| 1219 | |
| 1220 | #. Alignment is ignored by coro.begin and coro.free intrinsics. |
| 1221 | |
| 1222 | #. Make required changes to make sure that coroutine optimizations work with |
| 1223 | LTO. |
| 1224 | |
| 1225 | #. More tests, more tests, more tests |