Repository navigation
Description
The current convention (rii[ri].r) was chosen to be nice to read but is unable to be directly represented by C identifiers. Instead if the syntax is identifier compatible we can avoid redundant declarations and do more fun macro-fu. For example, changing [] to CD (because they kind of look like parens 😊) and the . to _ lets us use riiCriD_r as both the string and the shim name (shim_riiCdiR_r).
Once we have this it becomes possible to start composing shims. Here's my current rough sketch:
// Generated structs for each unique arg or result pair.
// By splitting by ins/outs we reduce combinatorial explosion as each is only defined once
// and nested tuples are also reused. Note here that it restricts calls to having a single
// variadic argument - which could be a tuple - this isn't something required today but
// it is a nice simplification.
//
// These can be generated by iree-tblgen if we want.
typedef struct {
iree_vm_ref_t r0;
} iree_vm_io_r_t;
typedef struct {
iree_vm_ref_t r0;
int32_t i1;
} iree_vm_io_ri_t;
typedef struct {
iree_vm_ref_t r0;
int32_t i1;
int32_t i2;
iree_vm_size_t a3_count;
iree_vm_io_ri_t[] a3; // variable length based on a3_count
} iree_vm_io_riiCriD_t;
// Each struct has a helper function that verifies that it falls within the stack bounds.
// This could be no-oped in trusted environments, but is required in untrusted ones when
// calling from non-bytecode modules (which does bounds checking prior to calling calling).
iree_vm_io_r_t* iree_vm_io_r_checked_deref(iree_byte_span_t buffer) {
return buffer.size == sizeof(iree_vm_io_r_t) ? (iree_vm_io_r_t*)buffer.data : NULL;
}
iree_vm_io_riiCriD_t* iree_vm_io_riiCriD_checked_deref(iree_byte_span_t buffer) {
return buffer.size >= sizeof(iree_vm_io_riiCriD_t) &&
buffer.size == sizeof(iree_vm_io_riiCriD_t) +
((const iree_vm_io_riiCriD_t*)buffer.data)->a3_count * sizeof(iree_vm_io_ri_t) ?
(iree_vm_io_riiCriD_t*)buffer.data : NULL;
}
// Combined in+out shim function generated by a macro:
IREE_VM_IO_DECLARE_SHIM(riiCriD, r);
// ...
iree_status_t iree_vm_shim_riiCriD_r(..., iree_vm_stack_t* stack, ...) {
// Check the stack is in bounds and get the I/O:
const iree_vm_io_riiCriD_t* args = iree_vm_io_riiCriD_checked_deref(stack->arguments);
iree_vm_io_r_t* rets = iree_vm_io_r_checked_deref(stack->results);
if (IREE_UNLIKELY(!args || !rets)) return iree_make_status(whoops);
// Initialize outputs to 0 (safe for all types):
memset(rets, 0, sizeof(*rets));
// Tail-call the target function:
return target_fn(...., args, rets);
}This gets us the smallest amount of code/highest sharing, avoids function pointer casts by having a fixed target_fn calling convention (args and rets are always structs), and avoids packing/unpacking as we are directly referencing the VM stack frame with the pointers.
I'm prototyping this now as I port the HAL module (our most complex example) to C - will post more as I have more examples but wanted to hear what you thought @marbre / @simon-camp. It should strictly make what you're doing in emitc-land easier, hopefully. I was inspired by your comment here and also running into the same exact issue :)
Activity
Reordering params in iree_hal_executable_layout_create.
benvanik commentedon Feb 5, 2021
I've started doing some cleanup in prep for this kind of change (it's useful cleanup anyway): #4743
benvanik commentedon Feb 5, 2021
A bit more iteration:
One could alias the structs if they wanted names for their args but I'm not sure it's worth it as it's just more to potentially hurt the compiler later on (the above can be optimized quite well).
Still noodling on ways to make the signatures smaller so half the file isn't just the same boilerplate. Hiding things hurts readability but so does a ton of boilerplate. Thinking:
benvanik commentedon Feb 5, 2021
Coming along well - the hal_module changed to use this style of mechanism is almost exactly the same number of lines as the C++ template magic - but without any magic. I've still got a few things to figure out with tuples and then I can get some size measurements.
Reordering params in iree_hal_executable_layout_create.
Merge main -> google (#4800)
Merge main -> google (#4800)
Swapping `[]` -> `CD` and `.` -> `_` in the VM calling convention. (#…
benvanik commentedon Apr 8, 2021
Still not generating shims but calling this good for now. We may need to collaborate with emitc on shims to see how to share it between the core modules, external custom modules, and emitc.