|
| 1 | +<!-- {% raw %} --> |
| 2 | + |
| 3 | +# Target Extension Types for Inline SPIR-V and Decorated Types |
| 4 | + |
| 5 | +* Proposal: [0017](0017-inline-spirv-and-decorated-types.md) |
| 6 | +* Author(s): [Cassandra Beckley](https://github.com/cassiebeckley) |
| 7 | +* Status: **Design In Progress** |
| 8 | +* PRs: [#125316](https://github.com/llvm/llvm-project/pull/125316) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +In this proposal, we define the `spirv.Type` and `spirv.DecoratedType` |
| 13 | +target extension types for the SPIR-V backend. |
| 14 | + |
| 15 | +## Motivation |
| 16 | + |
| 17 | +We would like to implement `SpirvType` and `SpirvOpaqueType` from [Inline |
| 18 | +SPIR-V (HLSL proposal 0011)](https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md#types) |
| 19 | +in the SPIR-V backend. These allow users to create arbitrary `OpType` |
| 20 | +instructions, and use them like any other HLSL type. Using these types, a |
| 21 | +vendor creating a new extension can expose it to users by creating a header |
| 22 | +file without needing to modify the compiler. |
| 23 | + |
| 24 | +Additionally, we need a way to represent types with SPIR-V decorations in LLVM |
| 25 | +IR. |
| 26 | + |
| 27 | +## Proposed solution |
| 28 | + |
| 29 | +### SpirvType |
| 30 | + |
| 31 | +To represent `vk::SpirvType` and `vk::SpirvOpaqueType` in LLVM IR, we will add |
| 32 | +three new target extension types: |
| 33 | + |
| 34 | +| Type | HasZeroInit | CanBeGlobal | CanBeLocal | |
| 35 | +| ------------------------ | ----------- | ----------- | ---------- | |
| 36 | +| `spirv.Type` | false | true | true | |
| 37 | +| `spirv.IntegralConstant` | false | false | false | |
| 38 | +| `spirv.Literal` | false | false | false | |
| 39 | + |
| 40 | +`IntegralConstant` and `Literal` are used to encode arguments to `Type`, and |
| 41 | +may not be used outside that context. They are necessary because target |
| 42 | +extension types must have all type arguments precede all integer arguments, |
| 43 | +whereas SPIR-V type instructions may have an arbitrary number of type, |
| 44 | +immediate literal, and constant id operands in any order. |
| 45 | + |
| 46 | +#### `spirv.Type` |
| 47 | + |
| 48 | +``` |
| 49 | +target("spirv.Type", operands..., opcode, size, alignment) |
| 50 | +``` |
| 51 | + |
| 52 | +`opcode` is an integer literal representing the opcode of the `OpType` |
| 53 | +instruction to be generated. `size` and `alignment` are integer literals |
| 54 | +representing the number of bytes a single value of the type occupies and the |
| 55 | +power of two that the value will be aligned to in memory. An opaque type can be |
| 56 | +represented by setting `size` and `alignment` to zero. `operands` represents a |
| 57 | +list of type arguments encoding the operands of the `OpType` instruction. Each |
| 58 | +operand must be one of: |
| 59 | + |
| 60 | +* A type argument, which will be lowered to the id of the lowered SPIR-V type |
| 61 | +* A `spirv.IntegralConstant`, which will be lowered to the id of an |
| 62 | + `OpConstant` instruction |
| 63 | +* A `spirv.Literal`, which will be lowered to an immediate literal value |
| 64 | + |
| 65 | +#### `spirv.IntegralConstant` |
| 66 | + |
| 67 | +``` |
| 68 | +target("spirv.IntegralConstant", integral_type, value...) |
| 69 | +``` |
| 70 | + |
| 71 | +`integral_type` is the type argument for the `OpConstant` instruction to be |
| 72 | +generated, and `value` is its literal integer value. To represent a value larger |
| 73 | +than a single 32-bit word, multiple 32-bit words may be passed as arguments. As |
| 74 | +stated in the |
| 75 | +[SPIR-V Specification](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpConstant), |
| 76 | +they should be in little-endian order. |
| 77 | + |
| 78 | +#### `spirv.Literal` |
| 79 | + |
| 80 | +``` |
| 81 | +target("spirv.Literal", value...) |
| 82 | +``` |
| 83 | + |
| 84 | +`value` is the literal integer value to be generated. To represent a value |
| 85 | +larger than a single 32-bit word, multiple 32-bit words may be passed as |
| 86 | +arguments, in little-endian order. |
| 87 | + |
| 88 | +#### Example |
| 89 | + |
| 90 | +Here's an example of using these types to represent an array of images: |
| 91 | + |
| 92 | +``` |
| 93 | +%type_2d_image = type target("spirv.Image", float, 1, 2, 0, 0, 1, 0) |
| 94 | +%integral_constant_28 = type target("spirv.IntegralConstant", i32, 28) |
| 95 | +%integral_constant_4 = type target("spirv.IntegralConstant", i32, 4) |
| 96 | +%ArrayTex2D = type target("spirv.Type", %type_2d_image, %integral_constant_4, 28) |
| 97 | +``` |
| 98 | + |
| 99 | +### Type decorations |
| 100 | + |
| 101 | +In order to represent types with the `vk::ext_decorate`, `vk::ext_decorate_id`, |
| 102 | +and `vk::ext_decorate_string` annotations, we will use the |
| 103 | +[`int_spv_assign_decoration`](https://github.com/llvm/llvm-project/blob/main/llvm/docs/SPIRVUsage.rst#target-intrinsics) |
| 104 | +intrinsic. |
| 105 | + |
| 106 | +<!-- |
| 107 | +## Detailed design |
| 108 | +
|
| 109 | +_The detailed design is not required until the feature is under review._ |
| 110 | +
|
| 111 | +This section should grow into a full specification that will provide enough |
| 112 | +information for someone who isn't the proposal author to implement the feature. |
| 113 | +It should also serve as the basis for documentation for the feature. Each |
| 114 | +feature will need different levels of detail here, but some common things to |
| 115 | +think through are: |
| 116 | +
|
| 117 | +* Is there any potential for changed behavior? |
| 118 | +* Will this expose new interfaces that will have support burden? |
| 119 | +* How will this proposal be tested? |
| 120 | +* Does this require additional hardware/software/human resources? |
| 121 | +* What documentation should be updated or authored? |
| 122 | +
|
| 123 | +## Alternatives considered (Optional) |
| 124 | +
|
| 125 | +If alternative solutions were considered, please provide a brief overview. This |
| 126 | +section can also be populated based on conversations that occur during |
| 127 | +reviewing. |
| 128 | +
|
| 129 | +## Acknowledgments (Optional) |
| 130 | +
|
| 131 | +Take a moment to acknowledge the contributions of people other than the author |
| 132 | +and sponsor. |
| 133 | +--> |
| 134 | + |
| 135 | +<!-- {% endraw %} --> |
0 commit comments