Repository navigation
Enabling bytecode module coroutine begin/resume. - #9497
Conversation
iree_vm_bytecode_dispatch_begin now begins a call and if it does not complete prior to returning the caller can repeatedly issue iree_vm_bytecode_dispatch_resume until it does. Invocations are marked with fiber enter/leave to allow tracy to visualize them. Unfortunately due to proper nesting requirements we can't do the fiber management at a more common level and instead will need each implementation to manage it around their execution. Future changes will update iree/vm/invocation.h to support stateful coroutine-style invocations with a prettier API. This is just the internals to verify that the bytecode dispatch supports it. Progress on #8093.
de232e2 to
f3dce2d
Compare
stellaraccident
left a comment
There was a problem hiding this comment.
Pretty basic boilerplate. A few q's.
| iree_vm_stack_frame_t* current_frame = iree_vm_stack_current_frame(stack); | ||
| iree_vm_registers_t regs = | ||
| iree_vm_bytecode_get_register_storage(current_frame); | ||
| // TODO(benvanik): assert the module is at the top of the frame? We should |
There was a problem hiding this comment.
This assert should be a one-liner, no? Imo, would be better to have the assert if we think this is an invariant.
There was a problem hiding this comment.
➕1
follow on PR will have this
| // immediately the results will be written to |call|->results. | ||
| // | ||
| // Returns IREE_STATUS_DEFERRED if execution yielded and the call needs to be | ||
| // resumed. Depending on the program it may be unsafe to begin any other calls |
There was a problem hiding this comment.
Can you say more (at least to me) about this scary looking admonition?
There was a problem hiding this comment.
"unsafe" here is more about correctness than security/etc. It's mostly about there being no way for the runtime to enforce that the program being run uses coroutines properly - it has to be built to do the right thing based on the intent. An example would be global state - if a coroutine loads globals, yields, and then stores globals it'd not be safe (in the correctness sense) to launch other coroutines that touch those same globals. IOW the runtime supports an arbitrary number of simultaneously active coroutines but the program is what defines those and how they work.
For programs that come through the stream dialect we'll make sure things work mostly right (when I get semaphores plumbed through, Any Day Now(tm)) - you'll be able to invoke a function any number of times and the invocations will order themselves correctly based on the semaphores when everything is working. But if you hand-crafted a program or lowered from some other path it may not handle things right unless they accounted for the same. There's still some work to finish up in the compiler for this but it'll all be independent from the runtime.
Still some sketching to do for how this surfaces on the iree_vm_invocation_t (the coroutine/call state API you'll be wrapping) - my hope is that we don't end up with the runtime needing to serialize things based on module metadata but that's always an option ("export @foo cannot run unless the queue is empty" vs "export @bar is safe to execute under any circumstance"). To start I'll likely serialize everything as that's easiest pending the TBD compiler work which still allows for pipelining but not for concurrent execution within the same context (ala goroutines). Independent contexts will always be able to run concurrently, though (no globals or state hazards to worry about) - so two instances of model A can interleave as can an instance each of model A and model B. We may also decide here to say that every invocation in a context is serialized and leave the goroutine stuff for internals accessible by the compiler instead of a hosting user.
I'm realizing this may not help - but once I get this API baked I'll do a bigger writeup with diagrams (or tracy captures) :)
iree_vm_bytecode_dispatch_begin now begins a call and if it does not
complete prior to returning the caller can repeatedly issue
iree_vm_bytecode_dispatch_resume until it does.
Invocations are marked with fiber enter/leave to allow tracy to


visualize them. Unfortunately due to proper nesting requirements
we can't do the fiber management at a more common level and instead
will need each implementation to manage it around their execution.
Here's two invocations in the same fiber:
And two separate fibers:
Future changes will update iree/vm/invocation.h to support stateful
coroutine-style invocations with a prettier API. This is just the
internals to verify that the bytecode dispatch supports it.
Progress on #8093.