Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 1 | # HAL Driver Features |
| 2 | |
| 3 | Heterogeneity is one of IREE's core design principles. IREE aims to support |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 4 | various accelerators for compute, ranging from general purpose CPUs, GPUs, to |
| 5 | other special purpose accelerators. IREE provides a |
| 6 | [Hardware Abstraction Layer (HAL)][iree-hal] as a common interface to these |
| 7 | accelerators. IREE exposes it via an [C API][iree-hal-c-api] for programmers and |
| 8 | an MLIR [dialect][iree-hal-dialect] for compilers. |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 9 | |
| 10 | Heterogeneity inevitably means IREE needs to provide a solution for managing |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 11 | different features on different accelerators and their availability. This doc |
| 12 | describes the designs and mechanisms. |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 13 | |
| 14 | ## General HAL Driver Features |
| 15 | |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 16 | IREE uses compilers to generate native code for each accelerator, serialize the |
| 17 | native code, and embed the code in one flat byte code following FlatBuffer |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 18 | encoding format. The native code embedded in the final FlatBuffer file will |
| 19 | indicate the target architecture and required feature sets. At runtime IREE |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 20 | selects a HAL driver meeting all the requirements to dispatch the workload to. |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 21 | |
| 22 | [TODO: describe the HAL functionality, C API, and dialect abstraction] |
| 23 | |
| 24 | ## Vulkan HAL Driver Features |
| 25 | |
| 26 | Vulkan has many mechanisms for supporting different hardware implementations: |
| 27 | versions, extensions, features, limits. Vulkan uses SPIR-V to express the GPU |
| 28 | program but Vulkan is just one client SPIR-V supports. So SPIR-V has its own |
| 29 | mechanisms for supporting different clients: versions, capabilities, extensions. |
SaintMalik | 3619f1f | 2021-09-03 16:05:37 +0100 | [diff] [blame] | 30 | The mechanism in these two domains bear lots of similarity, but they are not |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 31 | exactly the same. We need to bridge these two worlds inside IREE. |
| 32 | |
| 33 | IREE has its own [Vulkan dialect][iree-vulkan-dialect], which defines the Vulkan |
Lei Zhang | e25cd20 | 2020-09-23 15:25:12 -0400 | [diff] [blame] | 34 | target environment, including [versions][iree-vulkan-base-td], |
| 35 | [extensions][iree-vulkan-base-td], [features][iree-vulkan-cap-td]. These |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 36 | definitions leverage MLIR attribute for storage, parsing/printing, and |
| 37 | validation. For example, we can have the following Vulkan target environment: |
| 38 | |
| 39 | ``` |
| 40 | target_env = #vk.target_env< |
| 41 | v1.1, r(120), |
| 42 | [VK_KHR_spirv_1_4, VK_KHR_storage_buffer_storage_class], |
| 43 | { |
Lei Zhang | e25cd20 | 2020-09-23 15:25:12 -0400 | [diff] [blame] | 44 | maxComputeSharedMemorySize = 16384 : i32, |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 45 | maxComputeWorkGroupInvocations = 1024: i32, |
Lei Zhang | e25cd20 | 2020-09-23 15:25:12 -0400 | [diff] [blame] | 46 | maxComputeWorkGroupSize = dense<[128, 8, 4]>: vector<3xi32>, |
| 47 | subgroupFeatures = 7: i32, |
| 48 | subgroupSize = 64 : i32 |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 49 | } |
| 50 | > |
| 51 | ``` |
| 52 | |
| 53 | The above describes a Vulkan implementation that supports specification version |
| 54 | 1.1.120, supports `VK_KHR_spirv_1_4` and `VK_KHR_storage_buffer_storage_classs` |
| 55 | extensions, has a max compute workgroup invocation of 1024, and so on. |
| 56 | |
| 57 | The above bears lots of similarity with the output of the |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 58 | [`vulkaninfo`][vulkaninfo] utility. That's intended: `vulkaninfo` gives a |
| 59 | detailed dump of a Vulkan implementation by following the structures of all the |
| 60 | registered extensions to the specification. We pick relevant fields from it to |
| 61 | compose the list in the above to drive code generation. These are just different |
| 62 | formats for expressing the Vulkan implementation; one can image having a tool to |
| 63 | directly dump the MLIR attribute form used by IREE from the `vulkaninfo`'s JSON |
| 64 | dump. |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 65 | |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 66 | When compiling ML models towards Vulkan, one specifies the target environment as |
| 67 | a `#vk.target_env` attribute assembly via the |
Lei Zhang | e25cd20 | 2020-09-23 15:25:12 -0400 | [diff] [blame] | 68 | [`iree-vulkan-target-env`][iree-vulkan-target-env] command-line option. At the |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 69 | moment only one target environment is supported; in the future this is expected |
| 70 | to support multiple ones so that one can compile towards different Vulkan |
| 71 | implementations at once and embed all of them in the final FlatBuffer and select |
Lei Zhang | e25cd20 | 2020-09-23 15:25:12 -0400 | [diff] [blame] | 72 | at runtime. Another command-line option, `iree-vulkan-target-triple` is also |
| 73 | available to allow specifying common triples and avoiding the lengthy target |
| 74 | environment assembly string. `iree-vulkan-target-triple` will be overridden by |
| 75 | `iree-vulkan-target-env` if both are given. |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 76 | |
| 77 | Under the hood, this Vulkan target environment is then converted to the SPIR-V |
| 78 | target environment counterpart to drive code generation. The conversion happens |
| 79 | in one of Vulkan dialect's [utility function][iree-vulkan-target-conv]. The |
| 80 | converted SPIR-V target environment is [attached][iree-spirv-target-attach] to |
| 81 | the dispatch region's module for SPIR-V passes to use. |
| 82 | |
Copybara-Service | 4bbf566 | 2020-08-17 09:43:28 -0700 | [diff] [blame] | 83 | SPIR-V's target environment is very similar to the Vulkan target environment in |
| 84 | the above; it lives in upstream MLIR repo and is documented |
| 85 | [here][mlir-spirv-target] and implemented in SPIR-V dialect's |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 86 | [`SPIRVAttribues.h`][mlir-spirv-attr] and |
| 87 | [`TargetAndABI.td`][mlir-spirv-target-td]. |
| 88 | |
MaheshRavishankar | 10d9a40 | 2020-10-15 21:51:46 -0700 | [diff] [blame] | 89 | [PR #3469][pr-3469], along with patch [D89364][d89364], shows an example of the |
| 90 | changes needed to add support for the |
| 91 | [VK_NV_cooperative_matrix][vk-coop-mat-ext] extension for Vulkan/SPIR-V. The |
| 92 | overall steps are as follows: |
| 93 | 1. Add the enum corresponding to the extension to `VK_ExtensionAttr` in |
| 94 | [VulkanBase.td][iree-vulkan-base-td]. |
| 95 | 1. Add necessary capability bits to [`VK_CapabilitiesAttr`][iree-vulkan-cap-td]. |
| 96 | 1. Add a corresponding attribute to the `SPV_ResourceLimitsAttr` in |
| 97 | [TargetAndABI.td][mlir-spirv-target-td]. (Note: The corresponding SPIR-V |
| 98 | extension is likely already defined in |
| 99 | [`SPV_ExtensionAttr`][mlir-spirv-extensions-attr]) |
| 100 | 1. Convert the capability bits specified in the attribute added to |
| 101 | `VK_CapabilitiesAttr` to the attribute added to `SPV_ResourceLimitsAttr`. |
| 102 | |
| 103 | [d89364]: https://reviews.llvm.org/D89364 |
Scott Todd | 7df3973 | 2022-06-28 09:21:35 -0700 | [diff] [blame^] | 104 | [iree-hal]: https://github.com/iree-org/iree/tree/main/iree/hal |
| 105 | [iree-hal-c-api]: https://github.com/iree-org/iree/blob/main/iree/hal/api.h |
| 106 | [iree-hal-dialect]: https://github.com/iree-org/iree/tree/main/iree/compiler/Dialect/HAL |
| 107 | [iree-vulkan-dialect]: https://github.com/iree-org/iree/tree/main/iree/compiler/Dialect/Vulkan |
| 108 | [iree-vulkan-base-td]: https://github.com/iree-org/iree/blob/main/iree/compiler/Dialect/Vulkan/IR/VulkanBase.td |
| 109 | [iree-vulkan-cap-td]: https://github.com/iree-org/iree/blob/main/iree/compiler/Dialect/Vulkan/IR/VulkanAttributes.td |
| 110 | [iree-vulkan-target-env]: https://github.com/iree-org/iree/blob/b4739d704de15029cd671e53e7d7e743f4ca2e35/iree/compiler/Dialect/HAL/Target/VulkanSPIRV/VulkanSPIRVTarget.cpp#L66-L70 |
| 111 | [iree-vulkan-target-triple]: https://github.com/iree-org/iree/blob/main/iree/compiler/Dialect/Vulkan/Utils/TargetEnvUtils.cpp |
| 112 | [iree-vulkan-target-conv]: https://github.com/iree-org/iree/blob/b4739d704de15029cd671e53e7d7e743f4ca2e35/iree/compiler/Dialect/Vulkan/Utils/TargetEnvUtils.h#L29-L42 |
| 113 | [iree-spirv-target-attach]: https://github.com/iree-org/iree/blob/b4739d704de15029cd671e53e7d7e743f4ca2e35/iree/compiler/Dialect/HAL/Target/VulkanSPIRV/VulkanSPIRVTarget.cpp#L228-L240 |
MaheshRavishankar | 10d9a40 | 2020-10-15 21:51:46 -0700 | [diff] [blame] | 114 | [mlir-spirv-extensions-attr]: https://github.com/llvm/llvm-project/blob/076305568cd6c7c02ceb9cfc35e1543153406d19/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td#L314 |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 115 | [mlir-spirv-target]: https://mlir.llvm.org/docs/Dialects/SPIR-V/#target-environment |
| 116 | [mlir-spirv-attr]: https://github.com/llvm/llvm-project/blob/076305568cd6c7c02ceb9cfc35e1543153406d19/mlir/include/mlir/Dialect/SPIRV/SPIRVAttributes.h |
| 117 | [mlir-spirv-target-td]: https://github.com/llvm/llvm-project/blob/076305568cd6c7c02ceb9cfc35e1543153406d19/mlir/include/mlir/Dialect/SPIRV/TargetAndABI.td |
Scott Todd | 7df3973 | 2022-06-28 09:21:35 -0700 | [diff] [blame^] | 118 | [pr-3469]: https://github.com/iree-org/iree/pull/3469 |
MaheshRavishankar | 10d9a40 | 2020-10-15 21:51:46 -0700 | [diff] [blame] | 119 | [vk-coop-mat-ext]: khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_NV_cooperative_matrix.html |
Lei Zhang | 3b513ff | 2020-08-14 14:08:27 -0400 | [diff] [blame] | 120 | [vulkaninfo]: https://vulkan.lunarg.com/doc/view/latest/linux/vulkaninfo.html |