Skip to content

Adding VM support for wait/resume and async invocations. - #9611

Merged
benvanik merged 15 commits into
mainfrom
benvanik-vm-wait
Jun 27, 2022
Merged

benvanik merged 15 commits into
mainfrom
benvanik-vm-wait

Conversation

@benvanik

@benvanik benvanik commented Jun 24, 2022 •

Copy link
Copy Markdown
Collaborator

The two major additions are the low-level iree_vm_begin/resume/end_invoke methods and the high-level iree_vm_async_invoke method. The existing iree_vm_invoke has been rewritten to reuse the iree_vm_*_invoke methods such that all invocations are handled in a consistent way. Bindings can choose to use either set of methods. The low-level methods don't dictate a scheduling mechanism and can tightly integrate with external schedulers. The high-level method uses the iree_loop_t interface which can be mapped to normal event loop mechanisms and will handle state tracking. In either case the binding layer provides the storage and lifetime management and both sets of methods don't allocate any additional memory.

This PR only contains the VM portions of the async scheduling work: the stream->HAL compiler lowering and runtime module need to be adapted to use semaphores and there are some TODOs around semaphore exports to wait handles that need to be done. Experiments show that about ~100 LOC is all that's needed to adapt iree-run-module to perform concurrent execution via iree_vm_async_invoke and (when the TODOs are worked around) the HAL can yield:
image

Most of the implementation work here was done years ago by having the VM module interface expose the begin_call and resume_call. This set of changes completes that work for bytecode and native modules, but emitc is going to need additional work to handle yielding. Future changes that enable the yielding behavior in the compiler and HAL module will have flags to disable them for when emitc is used. Documented in #9612.

Progress on #8093 (phase 1 complete).
Fixes #9503.

@benvanik benvanik added the runtime/vm IREE bytecode VM interpreter label Jun 24, 2022
@benvanik
benvanik force-pushed the benvanik-vm-wait branch 2 times, most recently from 9147c43 to d2947b9 Compare June 24, 2022 21:29

@stellaraccident stellaraccident left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks approximately like I expected. Glad you wrote it and not me: I've done this before and it is some hard programming. A handful of comments, but I didn't see any flaws. Just generally would recommend some asserts/something about invariants: these things can be really hard to debug when they go wrong.

Comment thread runtime/src/iree/base/tracing.h Resolved
Comment thread runtime/src/iree/vm/invocation.c Resolved
// Asynchronous invocation
//===----------------------------------------------------------------------===//

// WARNING: this function cannot have any trace markers that span the begin

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way at all in debug mode or something to assert this? I can almost guarantee I'm going to mess this up... and I know roughly what I am doing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortuantely not though perhaps we could build something. Tracy's server (recording) side does not track zone nesting so the only thing that can reveal incorrect behavior is the client (profiler) that does the analysis. It'll pop up an error indicating which zone was incorrectly nested but it does require that a user is actually tracing to see it.

We could have an expensive asserts check that would keep a local zone stack but we'd need to support tracking fiber lifetimes and such (as each thread and fiber have their own stacks) - I'm not quite sure how to do that yet but maybe it's something that could be done easier in the tracy server code. Probably worth an upstream suggestion.

Comment thread runtime/src/iree/vm/stack.c Resolved
// iree_vm_stack_suspend_trace_zones for more information.

IREE_API_EXPORT void iree_vm_stack_suspend_trace_zones(iree_vm_stack_t* stack) {
// Walk top->bottom to unwind the trace zones.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. (no better idea)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ;( Best path is to make fiber tracing fast enough. This mostly restores zone stacks but without a lot of side-channel storage it's not full fidelity. It also messes up counts and such in statistics. Turns out having proper fiber tracing for tracing fibers is useful :P
At least in most benchmarks/etc there won't be multiple fibers and things will still be fairly understandable (the things taking times are not the dozen or so events related to the invocation function today, so it being two dozen events doesn't hurt much besides readability).

benvanik added 15 commits June 27, 2022 07:45
Passing the entire call was overkill.
The native module needs to support resuming; there's no magic here.
These special frames sit on the stack during a wait operation. The
code that wants to wait will push a wait frame to the stack, populate it
with the wait information, and return as deferred. The scheduler can
peek at the wait frame and perform the wait before then resuming the
waiting code, which can pop the wait frame and get back the wait status.
Since we ref count everything on the stack it's safe to yield back to
the scheduler with wait sources dependent on program resources.
Fibers are managed by the scheduler.
We can also use the wait result to pass back more detailed information
like which wait sources were woken in a multi-wait.
This makes it easier to debug unbalanced trace zones (by breakpoint or
printf).
When not using tracy's fiber support these are required to pop all
zones off the tracy zone stack when yielding from a fiber-like construct.
Upon re-entering the context the trace zones can be pushed back on the
tracy zone stack in the same order for continuity (albeit with a bit of
noise, like doubling the zone count).
This new low-level API allows for user scheduling of invocations with
(relatively) low overhead (no allocations, non-blocking, etc).

iree_vm_invoke has been reworked to use this API and future changes will
add more user-friendly higher-level APIs by using iree_loop_t. Some
integrations may prefer to use this API even for synchronous-like
invocations in order to deal with enforced scheduling (like main thread
wasm, which cannot block).
This reuses the low-level iree_vm_*_invoke APIs to provide a
fire-and-forget callback-style interface. Storage is externalized
such that it can be embedded in higher-level binding data structures
and avoid allocations.

The API here is unopinionated with respect to overlapping invocations
within the same context. Callers are expected to set the
IREE_VM_CONTEXT_FLAG_CONCURRENT if they will overlap (mostly just to make
tracing work) but otherwise there's no difference in the implementation
between sequential and concurrent invocation execution.
This never worked quite right and the coming submission list work will
probably be a better way of doing it - and if not the implementation will
at least be different from what's here today.
It's significantly slower when running multithreaded due to all thread
local trace queues being disabled.
Really don't want to be blocking a worker in this case.
@benvanik
benvanik marked this pull request as ready for review June 27, 2022 15:20
@benvanik
benvanik requested a review from ThomasRaoux as a code owner June 27, 2022 15:20
@benvanik
benvanik removed the request for review from ThomasRaoux June 27, 2022 15:22
@benvanik
benvanik merged commit 96e7a93 into main Jun 27, 2022
@benvanik
benvanik deleted the benvanik-vm-wait branch June 27, 2022 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

runtime/vm IREE bytecode VM interpreter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support manual management of VM stack tracing zones.

2 participants