Skip to content

Implement coroutine support in emitc. #9612

Open

Description

@benvanik

#9497 added the basic support in bytecode modules for begin/resume and the simple async test in there can be used as a starting point; the follow-on work in #9611 enables full wait support but it's built on the same foundation and shouldn't need much from emitc itself.

emitc will need extensions to support the yielding behavior of the VM by using the iree_vm_stack_t to manage the stack across yield points. emitc functions that can yield (or call ones that can) will need to reserve VM stack storage with iree_vm_stack_function_enter for all locals that are live across yield boundaries. When a yield point is reached (ops like vm.yield and imports will have a yield trait on them) the generated function will need to store all live locals to their stack frame and set a resume PC before returning IREE_STATUS_DEFERRED all the way up the call chain. On resume the generated function will need to jump to the resume PC and unpack the locals from the stack frame back into native stack variables. Recovering the call stack after a nested resume is handled by the caller and emitc functions only need to know how to handle themselves. Upon completion a function returns as normal with iree_vm_stack_function_leave and a return OK.

Since all ops that can yield are known at compile time an analysis can be performed to identify which functions need to handle suspend/resume and leaf functions not involved can remain as they are today. The transformations required are similar to those done by LLVM for their coroutine impl: https://llvm.org/docs/Coroutines.html#distinct-save-and-suspend

  vm.func @yield_sequence(%arg0: i32) -> i32 {
    %c1 = vm.const.i32 1
    %y0 = vm.add.i32 %arg0, %c1 : i32
    %y0_dno = util.do_not_optimize(%y0) : i32
    vm.yield ^bb1
  ^bb1:
    %y1 = vm.add.i32 %y0_dno, %c1 : i32
    %y1_dno = util.do_not_optimize(%y1) : i32
    vm.yield ^bb2
  ^bb2:
    %y2 = vm.add.i32 %y1_dno, %c1 : i32
    %y2_dno = util.do_not_optimize(%y2) : i32
    vm.yield ^bb3
  ^bb3:
    vm.return %y2_dno : i32
  }

->

struct frame_storage_t {
  // int32_t arg0;  not needed here, but if the arg was used outside of the entry block we'd have to store it
  // some of these could alias as their lifetimes do not overlap; basic lifetime analysis in the compiler can provide that:
  int32_t c1;
  int32_t y0;
  int32_t y1;
  int32_t y2;
};
iree_status_t yield_sequence(iree_vm_stack_t* stack, iree_vm_stack_frame_t* frame, int32_t arg0, int32_t* out_ret0) {
  frame_storage_t* storage = iree_vm_stack_frame_storage(frame);
  switch (frame->pc) {
   case 0:  // entry
    int32_t c1 = 1;
    int32_t y0 = arg0 + c1;
    frame->pc = 1;  // resume at bb1
    storage->c1 = c1;  // used again
    storage->y0 = y0;
    return IREE_STATUS_DEFERRED;  // yield
   case 1:  // ^bb1:
    int32_t y0 = storage->y0;
    int32_t c1 = storage->c1;
    int32_t y1 = y0 + c1;
    frame->pc = 2;  // resume at bb2
    storage->y1 = y1;
    return IREE_STATUS_DEFERRED;  // yield
   case 2:  // ^bb2:
    int32_t y1 = storage->y1;
    int32_t c1 = storage->c1;
    int32_t y2 = y1 + c1;
    frame->pc = 3;  // resume at bb3
    storage->y2 = y2;
    return IREE_STATUS_DEFERRED;  // yield
   case 3:  // ^bb3:
    int32_t y2 = storage->y2;
    *out_ret0 = y2;
    return IREE_STATUS_OK;  // done
  }

There are a few routes to get to this point; one may be to add special MLIR ops that pack/unpack coroutine storage and then those are lowered to emitc ops. The upstream async dialect has some coroutine ops that could also be used, however they are unfortunately tied to other async dialect ops and may not be a good intermediate state. Extracting those async.coro.* ops into a core coro dialect would be useful. The ref count tracking it does may also be useful but is again not quite generalizable as-is. Probably best to use it as inspiration instead.

Currently the native module does not allocate storage as it enters frames:
https://github.com/google/iree/blob/c8fb99b5322fcd28370e21dc57c3a193f3cec565/runtime/src/iree/vm/native_module.c#L303-L310
We'll need to fix that such that we can declare frame storage space and a cleanup function that releases any refs that are stored. This would probably just be an optional table provided alongside the function pointers with the stack size required (this is what the bytecode modules do).

Until this work is done emitc will only be usable in fully synchronous mode and if a program attempts to yield it will fail at runtime.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions