blob: f5dcbdca504b3ec494e0dadddd35e38a18c67d03 [file] [log] [blame] [view]
Lei Zhang3b513ff2020-08-14 14:08:27 -04001# HAL Driver Features
2
3Heterogeneity is one of IREE's core design principles. IREE aims to support
Copybara-Service4bbf5662020-08-17 09:43:28 -07004various accelerators for compute, ranging from general purpose CPUs, GPUs, to
5other special purpose accelerators. IREE provides a
6[Hardware Abstraction Layer (HAL)][iree-hal] as a common interface to these
7accelerators. IREE exposes it via an [C API][iree-hal-c-api] for programmers and
8an MLIR [dialect][iree-hal-dialect] for compilers.
Lei Zhang3b513ff2020-08-14 14:08:27 -04009
10Heterogeneity inevitably means IREE needs to provide a solution for managing
Copybara-Service4bbf5662020-08-17 09:43:28 -070011different features on different accelerators and their availability. This doc
12describes the designs and mechanisms.
Lei Zhang3b513ff2020-08-14 14:08:27 -040013
14## General HAL Driver Features
15
Copybara-Service4bbf5662020-08-17 09:43:28 -070016IREE uses compilers to generate native code for each accelerator, serialize the
17native code, and embed the code in one flat byte code following FlatBuffer
Lei Zhang3b513ff2020-08-14 14:08:27 -040018encoding format. The native code embedded in the final FlatBuffer file will
19indicate the target architecture and required feature sets. At runtime IREE
Copybara-Service4bbf5662020-08-17 09:43:28 -070020selects a HAL driver meeting all the requirements to dispatch the workload to.
Lei Zhang3b513ff2020-08-14 14:08:27 -040021
22[TODO: describe the HAL functionality, C API, and dialect abstraction]
23
24## Vulkan HAL Driver Features
25
26Vulkan has many mechanisms for supporting different hardware implementations:
27versions, extensions, features, limits. Vulkan uses SPIR-V to express the GPU
28program but Vulkan is just one client SPIR-V supports. So SPIR-V has its own
29mechanisms for supporting different clients: versions, capabilities, extensions.
SaintMalik3619f1f2021-09-03 16:05:37 +010030The mechanism in these two domains bear lots of similarity, but they are not
Lei Zhang3b513ff2020-08-14 14:08:27 -040031exactly the same. We need to bridge these two worlds inside IREE.
32
33IREE has its own [Vulkan dialect][iree-vulkan-dialect], which defines the Vulkan
Lei Zhange25cd202020-09-23 15:25:12 -040034target environment, including [versions][iree-vulkan-base-td],
35[extensions][iree-vulkan-base-td], [features][iree-vulkan-cap-td]. These
Lei Zhang3b513ff2020-08-14 14:08:27 -040036definitions leverage MLIR attribute for storage, parsing/printing, and
37validation. For example, we can have the following Vulkan target environment:
38
39```
40target_env = #vk.target_env<
41 v1.1, r(120),
42 [VK_KHR_spirv_1_4, VK_KHR_storage_buffer_storage_class],
43 {
Lei Zhange25cd202020-09-23 15:25:12 -040044 maxComputeSharedMemorySize = 16384 : i32,
Lei Zhang3b513ff2020-08-14 14:08:27 -040045 maxComputeWorkGroupInvocations = 1024: i32,
Lei Zhange25cd202020-09-23 15:25:12 -040046 maxComputeWorkGroupSize = dense<[128, 8, 4]>: vector<3xi32>,
47 subgroupFeatures = 7: i32,
48 subgroupSize = 64 : i32
Lei Zhang3b513ff2020-08-14 14:08:27 -040049 }
50>
51```
52
53The above describes a Vulkan implementation that supports specification version
541.1.120, supports `VK_KHR_spirv_1_4` and `VK_KHR_storage_buffer_storage_classs`
55extensions, has a max compute workgroup invocation of 1024, and so on.
56
57The above bears lots of similarity with the output of the
Copybara-Service4bbf5662020-08-17 09:43:28 -070058[`vulkaninfo`][vulkaninfo] utility. That's intended: `vulkaninfo` gives a
59detailed dump of a Vulkan implementation by following the structures of all the
60registered extensions to the specification. We pick relevant fields from it to
61compose the list in the above to drive code generation. These are just different
62formats for expressing the Vulkan implementation; one can image having a tool to
63directly dump the MLIR attribute form used by IREE from the `vulkaninfo`'s JSON
64dump.
Lei Zhang3b513ff2020-08-14 14:08:27 -040065
Copybara-Service4bbf5662020-08-17 09:43:28 -070066When compiling ML models towards Vulkan, one specifies the target environment as
67a `#vk.target_env` attribute assembly via the
Lei Zhange25cd202020-09-23 15:25:12 -040068[`iree-vulkan-target-env`][iree-vulkan-target-env] command-line option. At the
Copybara-Service4bbf5662020-08-17 09:43:28 -070069moment only one target environment is supported; in the future this is expected
70to support multiple ones so that one can compile towards different Vulkan
71implementations at once and embed all of them in the final FlatBuffer and select
Lei Zhange25cd202020-09-23 15:25:12 -040072at runtime. Another command-line option, `iree-vulkan-target-triple` is also
73available to allow specifying common triples and avoiding the lengthy target
74environment assembly string. `iree-vulkan-target-triple` will be overridden by
75`iree-vulkan-target-env` if both are given.
Lei Zhang3b513ff2020-08-14 14:08:27 -040076
77Under the hood, this Vulkan target environment is then converted to the SPIR-V
78target environment counterpart to drive code generation. The conversion happens
79in one of Vulkan dialect's [utility function][iree-vulkan-target-conv]. The
80converted SPIR-V target environment is [attached][iree-spirv-target-attach] to
81the dispatch region's module for SPIR-V passes to use.
82
Copybara-Service4bbf5662020-08-17 09:43:28 -070083SPIR-V's target environment is very similar to the Vulkan target environment in
84the above; it lives in upstream MLIR repo and is documented
85[here][mlir-spirv-target] and implemented in SPIR-V dialect's
Lei Zhang3b513ff2020-08-14 14:08:27 -040086[`SPIRVAttribues.h`][mlir-spirv-attr] and
87[`TargetAndABI.td`][mlir-spirv-target-td].
88
MaheshRavishankar10d9a402020-10-15 21:51:46 -070089[PR #3469][pr-3469], along with patch [D89364][d89364], shows an example of the
90changes needed to add support for the
91[VK_NV_cooperative_matrix][vk-coop-mat-ext] extension for Vulkan/SPIR-V. The
92overall steps are as follows:
931. Add the enum corresponding to the extension to `VK_ExtensionAttr` in
94 [VulkanBase.td][iree-vulkan-base-td].
951. Add necessary capability bits to [`VK_CapabilitiesAttr`][iree-vulkan-cap-td].
961. 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])
1001. 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 Todd7df39732022-06-28 09:21:35 -0700104[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
MaheshRavishankar10d9a402020-10-15 21:51:46 -0700114[mlir-spirv-extensions-attr]: https://github.com/llvm/llvm-project/blob/076305568cd6c7c02ceb9cfc35e1543153406d19/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td#L314
Lei Zhang3b513ff2020-08-14 14:08:27 -0400115[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 Todd7df39732022-06-28 09:21:35 -0700118[pr-3469]: https://github.com/iree-org/iree/pull/3469
MaheshRavishankar10d9a402020-10-15 21:51:46 -0700119[vk-coop-mat-ext]: khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_NV_cooperative_matrix.html
Lei Zhang3b513ff2020-08-14 14:08:27 -0400120[vulkaninfo]: https://vulkan.lunarg.com/doc/view/latest/linux/vulkaninfo.html