Skip to content

Enabling bytecode module coroutine begin/resume. - #9497

Merged
benvanik merged 1 commit into
mainfrom
benvanik-vm-resume
Jun 15, 2022
Merged

benvanik merged 1 commit into
mainfrom
benvanik-vm-resume

Conversation

@benvanik

@benvanik benvanik commented Jun 14, 2022 •

Copy link
Copy Markdown
Collaborator

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:
image
And two separate fibers:
image

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.

@benvanik benvanik added the runtime/vm IREE bytecode VM interpreter label Jun 14, 2022
@benvanik
benvanik requested a review from ScottTodd June 14, 2022 14:49
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.
@benvanik
benvanik force-pushed the benvanik-vm-resume branch from de232e2 to f3dce2d Compare June 14, 2022 15:06
@benvanik
benvanik marked this pull request as ready for review June 14, 2022 15:40
@benvanik
benvanik requested a review from GMNGeoffrey as a code owner June 14, 2022 15:40
@benvanik
benvanik enabled auto-merge (squash) June 14, 2022 16:44
@benvanik
benvanik requested review from stellaraccident and removed request for GMNGeoffrey June 14, 2022 23:15

@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.

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

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 assert should be a one-liner, no? Imo, would be better to have the assert if we think this is an invariant.

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.

➕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

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.

Can you say more (at least to me) about this scary looking admonition?

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.

"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) :)

@benvanik
benvanik merged commit b697e76 into main Jun 15, 2022
@benvanik
benvanik deleted the benvanik-vm-resume branch June 15, 2022 19:07
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.

2 participants