Repository navigation
Description
Similar to #34 (lowering VM dialect to LLVM IR dialect), the idea here is to emit the VM IR as C/C++. This would allow us to - without needing to involve LLVM IR and deal with target compilation/linkage issues - ahead-of-time compile the scheduler side of the VM operations (or the reference VMLA backend). The benefit here is that when targeting weird environments (which either don't have or may have incomplete or practically binary-only LLVM distributions) we aren't doing anything fancy. This makes debugging easier (we can emit #line directives to get source mapping), profiling easier (as we can use normal platform tools like vtune without special hacks), and the whole process more seamless for initial bringup (gcc module.c should be close to the scope of the required configuration).
The design of the VM C API should make this fairly straightforward (and if not we should fix the API :). The emitted code would implement the iree_vm_module_t interface and then could be plugged into an iree_vm_context_t just as any other custom module or the interpreter bytecode_module is plugged in.
This means that when walking a vm.module with vm.funcs you could emit the module struct and C functions and then emit the lookup functions (like get_function/lookup_function). It'd look similar to what the bytecode_module does (as I did that before writing all the C++ goo that we should avoid because it only makes it easier to do handwritten stuff and adds additional dependencies).
Some example IR snippets exist in the compiler/Dialect/VM/**/tests/ paths, like:
https://github.com/google/iree/blob/master/iree/compiler/Dialect/VM/Transforms/test/global_initialization.mlir
As an example, an input like this:
vm.module @my_module {
vm.func @sub_i32(%arg0 : i32, %arg1 : i32) -> i32 {
%0 = vm.sub.i32 %arg0, %arg1 : i32
vm.return %0 : i32
}
}may end up as something like:
typedef struct {
iree_vm_module_t interface;
} my_module_t;
typedef struct {
// global variable storage, iree_vm_function-t import table, etc
} my_module_state_t;
iree_status_t my_module_alloc_state(...) {
// allocate my_module_state_t, initialize variables, etc
}
// vm.func @sub_i32(%arg0 : i32, %arg1 : i32) -> i32
iree_status_t my_module_sub_i32(int32_t arg0, int32_t arg1, int32_t* out_ret0) {
// vm.sub.i32 %arg0, %arg1 : i32
int32_t v0 = arg0 - arg1;
// vm.return %0 : i32
*out_ret0 = v0;
return IREE_STATUS_OK;
}
iree_status_t my_module_lookup_function(...) {
if (function_name == "sub_i32") return ...;
}
iree_status_t my_module_create(iree_allocator_t allocator, iree_vm_module_t** out_module) {
// alloc iree_vm_module_t
// set function pointers
module->interface.alloc_state = my_module_alloc_state;
module->interface.lookup_function = my_module_lookup_function;
...
}Because we are lowering from SSA IR we can simply emit the registers as is. vm.ref types in the IR can use the iree_vm_ref_* methods to manage the refs, and branches can be implemented with gotos (as we can emit the proper ref management when needed). Things like globals can be implemented with named values in the state struct (so vm.global.i32 @g2 in the IR becomes i32_t g2; as a struct member, etc).
The intent was to mirror the VM stack (iree_vm_stack_t) such that it is valid during execution. This will let us get nice stack walking on errors, implement a debugger for modules that can inspect module state, etc. It's not strictly required, though, except for coming into/out of the execute calls. I've got a bit of cleanup to do there and can try to make it lighter for this use case (#1172).
Imports (calls to vm.import ops) can be resolved through iree_vm_function_t. If the imports are listed in the lookup_function method (like hal.allocator.allocate, which would be generated from the list of vm.import ops in the IR) the runtime will call resolve_import for each one and then the iree_vm_module_t::execute method on the target function can be used to call it (ala https://github.com/google/iree/blob/master/iree/vm/bytecode_dispatch.c#L735-L736).
We can use this to clean up the interface, reduce rough edges, add more tests, etc. The nice thing about this is that the same interface is used by the HAL module and any custom module added (tensor lists, etc), so improvements to docs/performance/etc help everything.
To enable this all we can add a new translation registration like -iree-mlir-to-vm-c-module or something here: https://github.com/google/iree/blob/master/iree/compiler/Translation/IREEVM.cpp#L107 - note that the only difference between going to a flatbuffer with bytecode or C (or LLVM IR/etc) would be the final translation call: https://github.com/google/iree/blob/master/iree/compiler/Translation/IREEVM.cpp#L93
The translation then lives under VM/Target/:
https://github.com/google/iree/tree/master/iree/compiler/Dialect/VM/Target
Activity
benvanik commentedon Mar 20, 2020
Let me know if this all sounds crazy or how it matches up with what you were thinking :) The intent is to make things faster/leaner/simpler vs. the flatbuffers+bytecode approach (still needed for dynamic module loading/deployment) and make our lives easier in testing/debugging/etc - if it starts to feel like that's not the case we can figure out if it's worth it.
Add mlir-emitc submodule and extend CMake configuration
Add a VM to EmitC conversion and CModule Target (#2536)
Add a VM to EmitC conversion and CModule Target (iree-org#2536)
Add a VM to EmitC conversion and CModule Target (#2536)
Add a VM to EmitC conversion and CModule Target (iree-org#2536)
Add a VM to EmitC conversion and CModule Target (iree-org#2536)
simon-camp commentedon Aug 12, 2020
I've looked into the bytecode module and put something together. Some pieces are missing / unclear to me though. If this goes in the right direction I'll update and cleanup PR #2842 to emit the corresponding code.
I think we should get something simple like the above example running end to end first so we can test this and add the missing ops afterwards.
benvanik commentedon Aug 12, 2020
Oo a timely question! I was just looking at something similar for revamping the native modules (like the HAL and custom modules) so that they shared more code. We may be able to share the same thing with this, too, which will keep things easier to modify and reduce total binary size.
My plan was to have an iree/vm/native_module.h+.c that had all of the general stuff for reflection and method dispatch and then generate an inline include file containing a few tables of information that were used per module. This is similar to how the current C++ wrapper in module_abi_cc.h is done (with the table here https://github.com/google/iree/blob/e734f9a8a4d7f17f23dd0d838f964f89a32f73b4/iree/vm/module_abi_cc.h#L240 and the rest of the code in that file using that table for information).
For purely native modules (custom/HAL/etc) they'd then just need to define the functions they export and put them in one or more tables. Here's a super rough sketch I did last night: https://gist.github.com/benvanik/2d7929373181e23511319aa0ed7b1ad9 - of note is that users provide the
fnand then thefn_callshims are generated that marshal arguments into C arguments. Users can have any other additional C functions that use whatever convention they want internally but the exported ones are derived from thevm.importops for example in https://github.com/google/iree/blob/9624bbbcaa1f491f067aad500d6679b538db3403/iree/compiler/Dialect/HAL/hal.imports.mlir#L28-L33.What I'm thinking is that you could generate the same kind of thing here; if you emitted your C functions then all you'd need to do is also emit the dispatch table and some module metadata (like its name) and leave the rest like function lookup to the hand-written native_module code. I was thinking that the emitter for the tables would take in an IREE::VM::ModuleOp with the ImportOps and then emit the tables, so if you took your exported IREE::VM::FuncOps and turned them into ImportOps you'd be able to just pass those along. Then we have just one place taking the ops and producing the function signatures so they won't get out of sync.
If this sounds reasonable, I can get the first pass on this done today (PST) - or at least the header with the table types - and then we can iterate from there. Or, you can do what you have above and we could always reconcile it later - I don't want to block you, but this may help take care of a lot of goo for you that isn't core to what the actual emitc work is doing :)
benvanik commentedon Aug 12, 2020
Here's a rough sketch of the header:
So you could generate some readonly tables like:
And provide them when you create your module:
Besides a lot of the common code (like a binary search on exported function names in function lookup, reflection attribute queries and error validation, etc) being shared, which is nice, the only thing your generated module code has to do is have some compile-time generated tables and provide your state alloc, free, and function call implementations.
benvanik commentedon Aug 13, 2020
Here's a WIP PR: #2866
native_module_test.h shows what some modules look like and is probably the most interesting. In this model you'd be emitting a .h/.c with:
The rest of the code for reflection/function lookups/etc are in native_module.c and shared. It's not a lot of code, but it is nice to have a consistent implementation and when 10-20+ modules are built into a binary not duplicating it all across all emit-c modules, custom user modules, etc.
The thing still missing in this PR is the stuff from https://gist.github.com/benvanik/2d7929373181e23511319aa0ed7b1ad9 for marshaling args nicely, which I'll get finished tomorrow. Exports are easy (demonstrated in module_b.entry as an i32->i32), but the imports (module_a.*) require some nicer macros.
Hopefully this is useful and gives us something to iterate on!
simon-camp commentedon Aug 14, 2020
Thank you :)
I've adapted your test case to the VM module from PR #2842 and got it working. https://gist.github.com/simon-camp/cb63d7d434c586e6552e28fd5bd07317 is what I've come up with.
So add_module_test.h needs to be generated by the CModuleTarget, right?
benvanik commentedon Aug 14, 2020
Nice! That's the idea - then you'd just have an extern module_a_create (or whatever your module name is) and call that to create the module (vs iree_vm_bytecode_module_create or iree_hal_module_create). The nice thing about the way you have it separated there with add_mlir_generated.h and the add_module_test.h is that the goo setting up modules is isolated from the actual code that was translated, so as a user I could ignore the module setup and see that nice mapping from add.mlir -> add_mlir_generated.h.
Now I just need to hide all the boilerplate to marshal function args and it'll be even better :)
I didn't get to that today, but I think with what you have it shouldn't be blocking so I can land the native_module PR as-is if that helps!
simon-camp commentedon Aug 18, 2020
Yeah landing your PR as-is would already be really useful. I'll then update my PR. I should be able to get something up soon.
Once this is done we can get to the more interesting stuff :)
benvanik commentedon Aug 18, 2020
Cool - I'll get that landed today!
simon-camp commentedon Aug 19, 2020
The code generation is now hooked up in the build system so things are tested properly. The generated code looks like this
The rest of the setup is written by hand for now. I'd like to set up one or two more tests to run the translation on. Supporting vm.call would allow us to test imports I guess. Not sure how the translation would look like though.
benvanik commentedon Aug 19, 2020
That's awesome @simon-camp!
For vm.call, you could handle only internal calls to start. These would be only to other functions within the same module so you don't have to worry about cross-module calling conventions. In that case,
%1 = vm.call @foo(%0)would simply beIREE_RETURN_IF_ERROR(foo(v0, &v1));Of other things to start testing next (building up to running full modules using the HAL):
ref<...>typesmarbre commentedon Jan 20, 2022
With what we already have landed, this is basically done! 🥳