Repository navigation
Description
The current HAL design follows Vulkan fairly closely around descriptor sets where descriptor sets are defined and then bound to a command buffer at recording time. Vulkan grew the ability to update descriptor sets after binding but it's not super great (can't do it once the command buffer has been submitted, or have multiple updates pending, has tricky semantics around partial binding, etc) and not well-supported on mobile. It also doesn't have any matching equivalent in WebGPU and that has an even more restrictive binding model as its baseline.
Much of the current HAL design predates the stream dialect; one goal of building it was to simplify the binding story to enable reusable command buffers. The stream dialect now allows us to easily identify all resources used within a batch of commands using captures on stream.cmd.execute and analyze which resources will change over time in order to partition the batches into command buffers by update frequency. This becomes critical when thinking about command buffer reusability both for latency improvements and mapping to driver APIs that support/require such concepts (e.g. remote systems/sandboxes and CUDA graphs).
After some thinking on what we have today, what we can easily do in the compiler, and where we want to go I'm going to start working on some major changes (and a few cosmetic ones) towards simplifying things. The goal is to make CUDA graphs and indirect command buffers as in Metal/D3D12 possible/efficient and then let Vulkan performance lag behind until the API catches up: Vulkan usage won't be any worse than it is today but potential performance improvements from reusability will be difficult/impossible without extensions/improvements.
The key changes are likely to be:
- Removing descriptor sets as top-level objects
- Rename descriptors/descriptor sets as used in the APIs as bindings/bind groups (WebGPU/Metal terminology)
- Add an
iree_hal_command_buffer_executeAPI for primary/one-shot command buffers that takes a secondary reusable command buffer and a binding table - Add an
iree_hal_command_buffer_push_binding_refspair toiree_hal_command_buffer_push_bindingsthat indirects through binding table slots
One-shot/primary command buffers can still use push bindings, while reusable ones that may have their bindings change dynamically per execution will want to use the indirect binding table references for anything that may change at runtime. Command buffer creation will take a binding table slot capacity indicating how many dynamic bindings there will be and executable layouts will have a bit indicating that a binding may be indirectly specified. Each time a binding is referenced it'll be for a table slot #, offset, and length and the compiler can rebase ranges to make each push relative to the base table entry (so the table may declare range [N,M) and then several dispatches may use subranges [N+offset,N+offset+length)).
The general execution strategy in the compiler for a particular stream.cmd.execute may look like:
- record primary: one-shot, recorded per-invocation
- perform dynamic copy/transfer operations
- update uniform buffer with dynamic operands (instead of push constants)
- execute memoized/pre-recorded secondary with binding table of dynamic bindings
- uses the updated uniform buffer contents for dynamic operands
- may still use push constants/push bindings for known-constant values
- uses push binding references into table for dynamic bindings
- perform additional dynamic copy/transfer operations (readback/etc)
- submit primary to queue
There's quite a few benefits that come along with this like reduced runtime resource tracking costs (binding table retains each unique resource once vs needing to do it each time a resource is bound for use), reduced VM/validation overhead even if the target doesn't natively support reuse, and safer execution control (no way to update descriptor sets at invalid times as binding tables are immutable and in queue-order with their submission).
Prior art here comes from:
- CUDA graphs
- Metal argument buffers
- D3D12 ExecuteIndirect
- VK_EXT_descriptor_indexing (binding table/root descriptor table-like, with a few caveats on submission ordering)
- VK_EXT_buffer_device_address (to approximate argument buffers), as well as the now-deprecated VK_NVX_device_generated_commands extension
All of these follow the rough concept of a relatively fixed command buffer topology with selective per-execution mutable resources (bindings, in some cases push constants, and in other cases the kernels being launched).
For implementations without reusable command buffers as concepts or without the ability to modify command buffers we'd route initial recording through an iree_hal_deferred_command_buffer_t updated to support indirect binding references and apply them when submitted. For APIs with bindless support we could use that (including CPU). For CUDA graphs we'd use cuGraphExecKernelNodeSetParams to update the per-launch kernel node bindings when submitting them to their target stream. To start Vulkan will use the fallback path but could have optional support added based on device feature availability (update-after-bind descriptor sets cached per command buffer, descriptor indexing, or bindless/device pointers). WebGPU doesn't currently have a solution here beyond re-recording, but maybe one day it'll at least get descriptor indexing (investigation issue).
There's still a few open design questions:
- Allow more than just bindings to change? (uniform buffers handle most things besides copy/update/etc ops so likely not needed, and not well supported outside of CUDA graphs)
- Allow binding tables to be used with all command buffers including top-level submissions? (
iree_hal_device_queue_executewould take a list of command buffers as it does today along with a list of binding tables) - How early do we have to decide which bindings will be indirect? (if we use descriptor indexing in vulkan we may have to know this when materializing interfaces before executable translation, and if optional at runtime things get messy)
- Remove push bindings entirely and always use tables? (would simplify quite a bit of the resource tracking implementation and be cheap in the compiler, though harder for users of the C API)
- Remove the concept of primary/one-shot command buffers entirely? (queue_update_buffer/queue_copy_buffer/queue_fill_buffer/etc - adds redundancy to the API and puts pressure on semaphores instead of intra-command buffer synchronization, bad for most APIs)
Work plan:
- Remove unimplemented descriptor set objects
- Clean up remaining descriptor set layout
- Rename descriptors -> bindings
- Simplify command buffer types/usage (one shot == primary, reusable == secondary, no mixing)
- Add push binding refs API and binding tables to command buffers
- Add support in deferred command buffers (apply-time indirection into table)
- Add CUDA graph updates on execute
- Implement stream->hal lowering support for producing binding tables
- Implement stream-dialect reusable command buffer partitioning/memoization (future work)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn Progress
Activity
Removing !hal.descriptor_set/iree_hal_descriptor_set_t.
Removing !hal.descriptor_set/iree_hal_descriptor_set_t.
Removing !hal.descriptor_set/iree_hal_descriptor_set_t.
Removing !hal.descriptor_set/iree_hal_descriptor_set_t. (#10146)
Merging descriptor_set_layout.h into executable_layout.h.
[NFC] Merging descriptor_set_layout.h into executable_layout.h. (#10154)
Starting rename of executable_layout -> pipeline_layout.
Renaming `!hal.executable_layout` to `!hal.pipeline_layout`
Renaming `!hal.executable_layout` to `!hal.pipeline_layout`
Replacing descriptor set layout usage with a flag bitfield.
Replacing descriptor set layout usage with a flag bitfield.
Merge pull request #10170 from iree-org/benvanik-pipeline-layout-3
Plumbing support for nested command buffers and binding tables.
Plumbing support for nested command buffers and binding tables.
Plumbing support for nested command buffers and binding tables.
Plumbing support for nested command buffers and binding tables.
Merge pull request #10181 from iree-org/benvanik-execute-commands
Squashed commit of the following:
benvanik commentedon Jul 11, 2024
Closing in favor of #17875 as a more up-to-date resource.