Repository navigation
Adding VM support for wait/resume and async invocations. - #9611
Conversation
9147c43 to
d2947b9
Compare
stellaraccident
left a comment
There was a problem hiding this comment.
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.
| // Asynchronous invocation | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // WARNING: this function cannot have any trace markers that span the begin |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // 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. |
There was a problem hiding this comment.
Ugh. (no better idea)
There was a problem hiding this comment.
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).
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).
No value, high overhead.
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.
d2947b9 to
91af04c
Compare
The two major additions are the low-level
iree_vm_begin/resume/end_invokemethods and the high-leveliree_vm_async_invokemethod. The existingiree_vm_invokehas been rewritten to reuse theiree_vm_*_invokemethods 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 theiree_loop_tinterface 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-moduleto perform concurrent execution viairee_vm_async_invokeand (when the TODOs are worked around) the HAL can yield: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.