DxilTypeSystem.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilTypeSystem.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // DXIL extension to LLVM type system. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/MapVector.h"
  14. #include "dxc/HLSL/DxilCompType.h"
  15. #include "dxc/HLSL/DxilInterpolationMode.h"
  16. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. namespace llvm {
  20. class LLVMContext;
  21. class Module;
  22. class Function;
  23. class MDNode;
  24. class Type;
  25. class StructType;
  26. class StringRef;
  27. };
  28. namespace hlsl {
  29. enum class MatrixOrientation { Undefined = 0, RowMajor, ColumnMajor, LastEntry };
  30. struct DxilMatrixAnnotation {
  31. unsigned Rows;
  32. unsigned Cols;
  33. MatrixOrientation Orientation;
  34. DxilMatrixAnnotation();
  35. };
  36. /// Use this class to represent type annotation for structure field.
  37. class DxilFieldAnnotation {
  38. public:
  39. DxilFieldAnnotation();
  40. bool IsPrecise() const;
  41. void SetPrecise(bool b = true);
  42. bool HasMatrixAnnotation() const;
  43. const DxilMatrixAnnotation &GetMatrixAnnotation() const;
  44. void SetMatrixAnnotation(const DxilMatrixAnnotation &MA);
  45. bool HasResourceAttribute() const;
  46. llvm::MDNode *GetResourceAttribute() const;
  47. void SetResourceAttribute(llvm::MDNode *MD);
  48. bool HasCBufferOffset() const;
  49. unsigned GetCBufferOffset() const;
  50. void SetCBufferOffset(unsigned Offset);
  51. bool HasCompType() const;
  52. const CompType &GetCompType() const;
  53. void SetCompType(CompType::Kind kind);
  54. bool HasSemanticString() const;
  55. const std::string &GetSemanticString() const;
  56. llvm::StringRef GetSemanticStringRef() const;
  57. void SetSemanticString(const std::string &SemString);
  58. bool HasInterpolationMode() const;
  59. const InterpolationMode &GetInterpolationMode() const;
  60. void SetInterpolationMode(const InterpolationMode &IM);
  61. bool HasFieldName() const;
  62. const std::string &GetFieldName() const;
  63. void SetFieldName(const std::string &FieldName);
  64. private:
  65. bool m_bPrecise;
  66. CompType m_CompType;
  67. DxilMatrixAnnotation m_Matrix;
  68. llvm::MDNode *m_ResourceAttribute;
  69. unsigned m_CBufferOffset;
  70. std::string m_Semantic;
  71. InterpolationMode m_InterpMode;
  72. std::string m_FieldName;
  73. };
  74. /// Use this class to represent LLVM structure annotation.
  75. class DxilStructAnnotation {
  76. friend class DxilTypeSystem;
  77. public:
  78. unsigned GetNumFields() const;
  79. DxilFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx);
  80. const DxilFieldAnnotation &GetFieldAnnotation(unsigned FieldIdx) const;
  81. const llvm::StructType *GetStructType() const;
  82. unsigned GetCBufferSize() const;
  83. void SetCBufferSize(unsigned size);
  84. void MarkEmptyStruct();
  85. bool IsEmptyStruct();
  86. private:
  87. const llvm::StructType *m_pStructType;
  88. std::vector<DxilFieldAnnotation> m_FieldAnnotations;
  89. unsigned m_CBufferSize; // The size of struct if inside constant buffer.
  90. };
  91. enum class DxilParamInputQual {
  92. In,
  93. Out,
  94. Inout,
  95. InputPatch,
  96. OutputPatch,
  97. OutStream0,
  98. OutStream1,
  99. OutStream2,
  100. OutStream3,
  101. InputPrimitive,
  102. };
  103. /// Use this class to represent type annotation for function parameter.
  104. class DxilParameterAnnotation : public DxilFieldAnnotation {
  105. public:
  106. DxilParameterAnnotation();
  107. DxilParamInputQual GetParamInputQual() const;
  108. void SetParamInputQual(DxilParamInputQual qual);
  109. const std::vector<unsigned> &GetSemanticIndexVec() const;
  110. void SetSemanticIndexVec(const std::vector<unsigned> &Vec);
  111. void AppendSemanticIndex(unsigned SemIdx);
  112. private:
  113. DxilParamInputQual m_inputQual;
  114. std::vector<unsigned> m_semanticIndex;
  115. };
  116. /// Use this class to represent floating point operations flags for LLVM function
  117. class DxilFunctionFPFlag {
  118. friend class DxilFunctionAnnotation;
  119. public:
  120. static const unsigned kFPDenormMask = 0x00000007;
  121. static const unsigned kFPDenormOffset = 0;
  122. void SetFP32DenormMode(const DXIL::FPDenormMode mode);
  123. DXIL::FPDenormMode GetFP32DenormMode();
  124. uint32_t GetFlagValue();
  125. const uint32_t GetFlagValue() const;
  126. void SetFlagValue(const uint32_t flag);
  127. DxilFunctionFPFlag(uint32_t flag = 0) : m_flag(flag) {}
  128. private:
  129. uint32_t m_flag;
  130. };
  131. /// Use this class to represent LLVM function annotation.
  132. class DxilFunctionAnnotation {
  133. friend class DxilTypeSystem;
  134. public:
  135. unsigned GetNumParameters() const;
  136. DxilParameterAnnotation &GetParameterAnnotation(unsigned ParamIdx);
  137. const DxilParameterAnnotation &GetParameterAnnotation(unsigned ParamIdx) const;
  138. const llvm::Function *GetFunction() const;
  139. DxilParameterAnnotation &GetRetTypeAnnotation();
  140. const DxilParameterAnnotation &GetRetTypeAnnotation() const;
  141. DxilFunctionFPFlag &GetFlag();
  142. const DxilFunctionFPFlag &GetFlag() const;
  143. private:
  144. const llvm::Function *m_pFunction;
  145. std::vector<DxilParameterAnnotation> m_parameterAnnotations;
  146. DxilParameterAnnotation m_retTypeAnnotation;
  147. DxilFunctionFPFlag m_fpFlag;
  148. };
  149. /// Use this class to represent structure type annotations in HL and DXIL.
  150. class DxilTypeSystem {
  151. public:
  152. using StructAnnotationMap = llvm::MapVector<const llvm::StructType *, std::unique_ptr<DxilStructAnnotation> >;
  153. using FunctionAnnotationMap = llvm::MapVector<const llvm::Function *, std::unique_ptr<DxilFunctionAnnotation> >;
  154. DxilTypeSystem(llvm::Module *pModule);
  155. DxilStructAnnotation *AddStructAnnotation(const llvm::StructType *pStructType);
  156. DxilStructAnnotation *GetStructAnnotation(const llvm::StructType *pStructType);
  157. const DxilStructAnnotation *GetStructAnnotation(const llvm::StructType *pStructType) const;
  158. void EraseStructAnnotation(const llvm::StructType *pStructType);
  159. StructAnnotationMap &GetStructAnnotationMap();
  160. DxilFunctionAnnotation *AddFunctionAnnotation(const llvm::Function *pFunction);
  161. DxilFunctionAnnotation *AddFunctionAnnotationWithFPFlag(const llvm::Function *pFunction, const DxilFunctionFPFlag *flag);
  162. DxilFunctionAnnotation *GetFunctionAnnotation(const llvm::Function *pFunction);
  163. const DxilFunctionAnnotation *GetFunctionAnnotation(const llvm::Function *pFunction) const;
  164. void EraseFunctionAnnotation(const llvm::Function *pFunction);
  165. FunctionAnnotationMap &GetFunctionAnnotationMap();
  166. // Utility methods to create stand-alone SNORM and UNORM.
  167. // We may want to move them to a more centralized place for most utilities.
  168. llvm::StructType *GetSNormF32Type(unsigned NumComps);
  169. llvm::StructType *GetUNormF32Type(unsigned NumComps);
  170. // Methods to copy annotation from another DxilTypeSystem.
  171. void CopyTypeAnnotation(const llvm::Type *Ty, const DxilTypeSystem &src);
  172. void CopyFunctionAnnotation(const llvm::Function *pDstFunction,
  173. const llvm::Function *pSrcFunction,
  174. const DxilTypeSystem &src);
  175. bool UseMinPrecision();
  176. private:
  177. llvm::Module *m_pModule;
  178. StructAnnotationMap m_StructAnnotations;
  179. FunctionAnnotationMap m_FunctionAnnotations;
  180. DXIL::LowPrecisionMode m_LowPrecisionMode;
  181. llvm::StructType *GetNormFloatType(CompType CT, unsigned NumComps);
  182. };
  183. DXIL::SigPointKind SigPointFromInputQual(DxilParamInputQual Q, DXIL::ShaderKind SK, bool isPC);
  184. } // namespace hlsl