|
| 1 | +============================================================== |
| 2 | +Specification of DXIL Operations using TableGen Representation |
| 3 | +============================================================== |
| 4 | +.. contents:: |
| 5 | + :local: |
| 6 | + |
| 7 | +.. toctree |
| 8 | + :hidden |
| 9 | +
|
| 10 | +Introduction |
| 11 | +============ |
| 12 | + |
| 13 | +`DirectXShaderCompiler <https://github.com/microsoft/DirectXShaderCompiler>`_ |
| 14 | +encapsulates, among other information, various DXIL Operations in |
| 15 | +`hctdb.py <https://github.com/microsoft/DirectXShaderCompiler/blob/main/utils/hct/hctdb.py>`_. |
| 16 | +DXIL Operations are represented in one of the following `two ways |
| 17 | +<https://github.com/microsoft/DirectXShaderCompiler/blob/130877392c263888ef06bab768856d3dab1f1c9a/docs/DXIL.rst#L1978>`_: |
| 18 | + |
| 19 | +#. Using LLVM instructions. |
| 20 | +#. Using LLVM External functions. These are represented in LLVM IR as follows: |
| 21 | + * "Standard" LLVM intrinsics (e.g., ``llvm.sin.*``) and |
| 22 | + * HLSL intrinsics (defined as LLVM intrinsics in ``llvm/include/llvm/IR/IntrinsicsDirectX.td``, e.g., ``llvm.dx.*``) |
| 23 | + |
| 24 | + These are collectively referred to as `LLVM Intrinsics` in this note. |
| 25 | + |
| 26 | +Following is the complete list of properties of DXIL Ops with the corresponding field name |
| 27 | +as used in ``hctdb.py``. A DXIL Op is represented by a set of associated properties. These |
| 28 | +are categorized into two groups - viz., those that are (1) consumed in DXIL backend passes |
| 29 | +and (2) consumed in other usage scenarios such as validation, DXIL reader, etc. |
| 30 | + |
| 31 | +A. Properties consumed in DXIL backend passes |
| 32 | + |
| 33 | + 1. Name of operation (``dxil_op``) |
| 34 | + 2. The generic or HLSL-specific intrinsic that maps to the operation (``llvm_name``). |
| 35 | + 3. Unique Integer ID (``dxil_opid``) |
| 36 | + 4. Operation Class signifying the name and function signature of the operation (``dxil_class``). |
| 37 | + This string is an integral part of the DXIL Op function name and is constructed in |
| 38 | + the format ``dx.op.<class-name>.<overload-type>``. The DXIL validator checks for any |
| 39 | + deviation from this for each of the DXIL Op call. |
| 40 | + 5. List of valid overload types for the operation (``oload_types``). |
| 41 | + 6. Required DXIL Version with support for the operation. |
| 42 | + 7. A string that documents the operation (``doc``) - This is not strictly necessary but is included |
| 43 | + for readability and documentation of the operation. |
| 44 | + |
| 45 | +B. Properties consumed in other usage scenarios |
| 46 | + |
| 47 | + 1. Required minimum Shader Model (``shader_model``). |
| 48 | + 2. Minimum shader model required with translation by linker (``shader_model_translated``) |
| 49 | + 3. List of shader stages applicable to (``shader_stages``), empty for all. |
| 50 | + 4. Memory access attributes of the operation (``fn_attr``). |
| 51 | + 5. Boolean attributes of operation to indicate if it |
| 52 | + |
| 53 | + * is some kind of a derivative (``is_derivative``) |
| 54 | + * requires gradient calculation (``is_gradient``) |
| 55 | + * is a sampler feedback (``is_feedback``) |
| 56 | + * requires in-wave, cross-lane functionality (``is_wave``) |
| 57 | + * requires that all of its inputs are uniform across the wave (``requires_uniform_inputs``). |
| 58 | + * is a barrier operation (``is_barrier``). |
| 59 | + |
| 60 | +Motivation |
| 61 | +========== |
| 62 | + |
| 63 | +DXIL backend passes depend on various properties of DXIL Operations. For example, ``DXILLowering`` |
| 64 | +pass will need information such as the DXIL operation an LLVM intrinsic is to be lowered to, |
| 65 | +along with valid overload and parameter types etc. The TableGen file - |
| 66 | +``llvm/lib/Target/DirectX/DXIL.td`` - is used to represent DXIL Operations |
| 67 | +by specifying their properties listed above. ``DXIL.td`` is designed to be the single source |
| 68 | +of reference of DXIL Operations for DXIL backend implementation in ``llvm-project`` repo - |
| 69 | +analogous to ``hctdb.py`` for ``DirectXShadeCompiler`` repo. It needs to have a rich |
| 70 | +representation capabilities that TableGen backends (such as ``DXILEmitter``) can rely on. |
| 71 | +Additionally, the DXIL Op specification should be easy to read and comprehend. |
| 72 | + |
| 73 | +This note focuses on specification of the set of properties consumed by DXIL backend |
| 74 | +passes identified above in category A. Any of the properties from category B are expected to be |
| 75 | +included as deemed necessary during implementation. |
| 76 | + |
| 77 | +Design |
| 78 | +====== |
| 79 | + |
| 80 | +1. Each DXIL Operation is represented as a TableGen record. The name of each of the records |
| 81 | + signifies operation name. |
| 82 | +2. The LLVM Intrinsic that maps to the operation is represented using ``Intrinsic::*``. |
| 83 | +3. The unique operation id is represented by an integer. |
| 84 | +4. DXIL Operation Class is represented as follows |
| 85 | + |
| 86 | + .. code-block:: |
| 87 | +
|
| 88 | + // Abstraction of DXIL Operation class. |
| 89 | + // It encapsulates an associated function signature viz., |
| 90 | + // returnTy(param1Ty, param2Ty, ...) represented as a list of LLVMTypes. |
| 91 | + // DXIL Ops that belong to a DXILOpClass record the signature of that DXILOpClass |
| 92 | +
|
| 93 | + class DXILOpClass<list<LLVMType> OpSig> { |
| 94 | + list<LLVMType> OpSignature = OpSig; |
| 95 | + } |
| 96 | +
|
| 97 | + Concrete operation classes, such as ``unary`` are defined inheriting from ``DXILOpClass``. |
| 98 | +5. Valid overload types are represented as a list of ``LLVMType``. |
| 99 | +6. Concrete records of DXIL versions and are defined by inheriting from the class |
| 100 | + |
| 101 | + .. code-block:: |
| 102 | +
|
| 103 | + // Abstract class to represent major and minor version values |
| 104 | + class Version<int major, int minor> { |
| 105 | + int Major = major; |
| 106 | + int Minor = minor; |
| 107 | + } |
| 108 | +
|
| 109 | +7. A documentation string for the operation. |
| 110 | + |
| 111 | + |
| 112 | +A DXIL Operation is represented by the following TableGen class by encapsulating the various |
| 113 | +TableGen representations of its properties described above. |
| 114 | + |
| 115 | +.. code-block:: |
| 116 | +
|
| 117 | + // Abstraction DXIL Operation |
| 118 | + class DXILOpPropertiesBase { |
| 119 | + int OpCode = 0; // Opcode of DXIL Operation |
| 120 | + DXILOpClass OpClass = UnknownOpClass; // Class of DXIL Operation. |
| 121 | + Intrinsic LLVMIntrinsic = ?; // LLVM Intrinsic DXIL Operation maps to |
| 122 | + list<LLVMType> OpOverloadTypes = ?; // Valid overload type |
| 123 | + // of DXIL Operation |
| 124 | + Version DXILVer = ?; // Min DXIL version |
| 125 | + string Doc = ""; // A short description of the operation |
| 126 | + } |
| 127 | +
|
| 128 | +
|
| 129 | +The following convenience class, definitions of ``unary`` and ``DXVer1_0`` are used to |
| 130 | +illustrate the definitions of ``Sin`` and ``Cos`` operations: |
| 131 | + |
| 132 | + .. code-block:: |
| 133 | +
|
| 134 | + class DXILOpProperties<int opCode, |
| 135 | + Intrinsic intrinsic, |
| 136 | + list<LLVMType> overloadTypes, |
| 137 | + string doc> : DXILOpPropertiesBase { |
| 138 | + int OpCode = opCode; |
| 139 | + Intrinsic LLVMIntrinsic = intrinsic; |
| 140 | + list<LLVMType> OpOverloadTypes = overloadTypes; |
| 141 | + string Doc = doc; |
| 142 | + } |
| 143 | +
|
| 144 | + def unary : DXILOpClass<[llvm_any_ty, LLVMMatchType<0>]>; |
| 145 | + def DXVer1_0 : Version<1, 0>; |
| 146 | +
|
| 147 | + let OpClass = unary, DXILVer = DXVer1_0 in { |
| 148 | + def Cos : DXILOpProperties<12, int_cos, [llvm_half_ty, llvm_float_ty], |
| 149 | + "Returns cosine(theta) for theta in radians.">; |
| 150 | + def Sin : DXILOpProperties<13, int_sin, [llvm_half_ty, llvm_float_ty], |
| 151 | + "Returns sine(theta) for theta in radians.">; |
| 152 | + } |
| 153 | +
|
| 154 | +Summary |
| 155 | +======= |
| 156 | + |
| 157 | +This note sketches the design of a readable and maintainable TableGen specification of |
| 158 | +DXIL Ops in ``DXIL.td`` intended to serve as a single source of reference for TableGen |
| 159 | +backends (such as ``DXILEmitter``) that generates C++ representations used in DXIL |
| 160 | +backend passes. |
0 commit comments