Skip to content

Change VM calling convention and generate shims #4736

Closed

Description

@benvanik

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

  1. added
    runtime/vm
    on Feb 5, 2021
  2. self-assigned this
    on Feb 5, 2021
  3. benvanik commented on Feb 5, 2021

    @benvanik
    CollaboratorAuthor

    I've started doing some cleanup in prep for this kind of change (it's useful cleanup anyway): #4743

  4. benvanik commented on Feb 5, 2021

    @benvanik
    CollaboratorAuthor

    A bit more iteration:

    // this could be generated from a list of interesting types
    
    IREE_VM_ABI_FIXED_STRUCT(
        r, struct { iree_vm_ref_t r0; });
    
    IREE_VM_ABI_FIXED_STRUCT(
        ri, struct {
          iree_vm_ref_t r0;
          int32_t i1;
        });
    
    IREE_VM_ABI_FIXED_STRUCT(
        ririi, struct {
          iree_vm_ref_t r0;
          int32_t i1;
          iree_vm_ref_t r2;
          int32_t i3;
          int32_t i4;
        });
    
    IREE_VM_ABI_VLA_STRUCT(
        riiCriD,
        struct {
          iree_vm_ref_t r0;
          int32_t i1;
          int32_t i2;
          iree_vm_size_t a3_count;
          iree_vm_abi_ri_t a3[0];
        },
        a3_count, a3);
    
    IREE_VM_ABI_DEFINE_SHIM(ririi, v);
    // this could be generated from the *.imports.mlir files
    EXPORT_FN("buffer.copy_data", ARGS(ririi), RETS(v),
              iree_hal_module_buffer_copy_data)
    // we have to unpack/check/deref the refs anyway, so just taking the struct
    // is even cleaner than trying to pass them as C function arguments
    static iree_status_t iree_hal_module_buffer_copy_data(
        iree_vm_stack_t* IREE_RESTRICT stack, iree_hal_module_t* IREE_RESTRICT module,
        iree_hal_module_state_t* IREE_RESTRICT module_state,
        const iree_vm_abi_ririi_t* IREE_RESTRICT args,
        iree_vm_abi_v_t* IREE_RESTRICT rets) {
      iree_hal_buffer_t* source_buffer = NULL;
      IREE_RETURN_IF_ERROR(iree_hal_buffer_check_deref(args->r0, &source_buffer));
      iree_vm_size_t source_offset = args->i1;
      iree_hal_buffer_t* target_buffer = NULL;
      IREE_RETURN_IF_ERROR(iree_hal_buffer_check_deref(args->r2, &target_buffer));
      iree_vm_size_t target_offset = args->i3;
      iree_vm_size_t byte_length = args->i4;
      ...

    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:

    // since the args/rets are declared it makes sense that there would be an args->r0
    // if it's ARGS(r...) - the rest of the args are rarely used or could be renamed -
    // for example `module_state` could be `self`.
    IREE_VM_ABI_EXPORT(iree_hal_module_buffer_copy_data, ARGS(ririi), RETS(v)) {
      iree_hal_buffer_t* source_buffer = NULL;
      IREE_RETURN_IF_ERROR(iree_hal_buffer_check_deref(args->r0, &source_buffer));
      iree_vm_size_t source_offset = args->i1;
      // ....
    }
  5. benvanik commented on Feb 5, 2021

    @benvanik
    CollaboratorAuthor

    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.

  6. benvanik commented on Apr 8, 2021

    @benvanik
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

runtime/vm

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions