Skip to content

Implement initial coroutine support in the VM. #8093

Closed

Description

@benvanik

There's some important overlap with the HAL work required for semaphores and it would be good to get it nailed down in one go, mostly focused on how to best return wait handles that would be backed by HAL semaphores.

Getting a wait handle can be expensive and we want to avoid needing to materialize them until a system-level wait is requested. Idea is to have a ref-counted waitable token that can be polled via a vtable method or used to materialize an iree_wait_handle_t. HAL semaphores would implement this interface and use the underlying APIs for polling without needing to materialize the handle and perform API-specific exporting if required. Single waits could also be done via the vtable and allow for more efficient single-wait behavior (no need to export and multi-wait on a single handle).

Once we have this waitable type we can return that on the execution result with a special IREE_STATUS_DEFERRED (or something) status indicating that the caller needs to perform the wait. We already have resume_call but VM stack management and reconstructing the stack on resume is required. In this first pass we could ignore compiler-generated coroutines and focus only on native ones from the HAL letting us keep the resume behavior isolated to the HAL module. An idea would be to have an IREE_VM_STACK_FRAME_WAIT that retains the wait token for as long as the stack is live - a HAL method that wants to wait would push the wait frame and return all the way up to the caller. On resume the wait frame would be checked and if satisfied would pop the frame and resume execution in the caller. The bytecode dispatch needs to be reworked to allow the main dispatch method to run without external_enter. Native modules that call back into bytecode will need special handling but we don't do that anywhere today and can punt. The wait frame retains the wait token and so the execution result doesn't need to.

Phase 1 (no waiting, just suspend/resume):

  • Make vm.yield a terminator.
  • Add IREE_STATUS_* for indicating deferral/suspension/etc.
  • Implement bytecode vm.yield to allow for testing.
  • Externalize stack entry from iree_vm_bytecode_dispatch to allow for resume_call.
  • Add wait stack frame and iree_vm_execution_result_t plumbing to surface wait sources.
  • Implement iree_vm_invocation_t to manage the stack across yield points.
  • Plumb through execution result handling during invocation and checks.

Phase 2a (wait frame):

  • HAL implementation of the wait frame entry for submit-and-wait (to start).
  • Some tests with HAL integration.

Phase 2b (waitable tokens):

  • Add iree_vm_waitable_t in iree/vm/waitable.h.
  • Add !vm.waitable (or something) and vm.await.all/any ops.
  • HAL implementation of the wait token routing to semaphores.
  • Some tests with HAL integration.

Phase 3 (cleanup):

Activity

  1. added
    runtime
    on Jan 12, 2022
  2. self-assigned this
    on Jan 12, 2022
  3. moved this from TODO to Active in @benvanik 👻on Jan 12, 2022
  4. benvanik commented on Jan 27, 2022

    @benvanik
    CollaboratorAuthor

    Depends on #4026.

  5. benvanik commented on May 27, 2022

    @benvanik
    CollaboratorAuthor

    Taking another look at this after iree_wait_source_t and iree_loop_t landed.

  6. moved this from Available to In Progress in IREE - ARCHIVED (do not update)on Jun 14, 2022
  7. moved this to In Progress in IREEon Jun 28, 2022
  8. added a commit that references this issue on Aug 2, 2022
  9. benvanik commented on Oct 27, 2022

    @benvanik
    CollaboratorAuthor

    Calling this done for now.

  10. Repository owner moved this from In Progress to Done in IREEon Oct 27, 2022
  11. Repository owner moved this from In Progress to Done in IREE - ARCHIVED (do not update)on Oct 27, 2022
  12. Repository owner moved this from Active to Done in @benvanik 👻on Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

runtime

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions