Skip to content

Add EmitC support for direct HAL API calls. #6645

Open

Description

@benvanik

The VM HAL module is what lets us perform versioning, safety checks, and dynamic loading. In tiny scenarios where everything is linked in to the application binary and everything is rebuilt together this isn't needed.

Instead, we could have patterns in the compiler that when lowering VM calls into EmitC we can instead replace them with direct calls. These patterns would take vm.call ops that would be routed to imports and normally go through the dynamic import call pattern and instead lower them to emitc ops directly calling the C functions.

For example:

vm.call @hal.command_buffer.dispatch(%ref_931, %_executable_vmla_linked_1, %c28, %c1, %c1, %c1) : (!vm.ref<!hal.command_buffer>, !vm.ref<!hal.executable>, i32, i32, i32, i32) -> ()

that then ends up making the dynamic call through the HAL module and could instead directly call the iree_hal_command_buffer_dispatch HAL C function.

Combined with devirtualizing the HAL (#6641) this would allow for full LTO across the entire program. Optimizations that may take place on the EmitC ops - such as avoiding packing/unpacking things into iree_vm_ref_t - also become possible, reducing the amount of reference counting done at runtime.

Part of this work would be making sure the HAL API is easy to call into like this by possibly designing an alternative to iree/modules/hal/module.c that was intended for calling from EmitC similar to how we have ops_emitc.h to make it easier to emit the code. There's not much logic in the HAL module but some of the things marshaling arrays of pointers and such may be easier to emit as macros/inline functions in an iree/module/hal/module_emitc.h header.

Activity

  1. added
    hal/api
    performance ⚡
    runtime/vm
    on Aug 4, 2021
  2. benvanik commented on Aug 4, 2021

    @benvanik
    CollaboratorAuthor

    As a rough estimate, the HAL module is ~10-20KB, though it's hard to tell as some of that is inlined HAL code. Safe to guess that we save at least 10KB, and make things faster at runtime (a lot less packing/unpacking/calling convention round tripping, etc).

  3. benvanik commented on Jun 30, 2022

    @benvanik
    CollaboratorAuthor

    Backlogging as #9549 would cut down sizes in constrained scenarios but it would still benefit from this approach.

  4. moved this to Available in IREEon Jun 30, 2022
  5. moved this from Available to Backlog in (Deprecated) IREEon Apr 5, 2023
  6. moved this from Backlog to Inbox in (Deprecated) IREEon Apr 5, 2023
  7. moved this from Inbox to Backlog in (Deprecated) IREEon Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/emitchal/apiperformance ⚡runtime/vm

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions