DxilTypeSystem.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 LLVM function annotation.
  117. class DxilFunctionAnnotation {
  118. friend class DxilTypeSystem;
  119. public:
  120. unsigned GetNumParameters() const;
  121. DxilParameterAnnotation &GetParameterAnnotation(unsigned ParamIdx);
  122. const DxilParameterAnnotation &GetParameterAnnotation(unsigned ParamIdx) const;
  123. const llvm::Function *GetFunction() const;
  124. DxilParameterAnnotation &GetRetTypeAnnotation();
  125. const DxilParameterAnnotation &GetRetTypeAnnotation() const;
  126. private:
  127. const llvm::Function *m_pFunction;
  128. std::vector<DxilParameterAnnotation> m_parameterAnnotations;
  129. DxilParameterAnnotation m_retTypeAnnotation;
  130. };
  131. /// Use this class to represent structure type annotations in HL and DXIL.
  132. class DxilTypeSystem {
  133. public:
  134. using StructAnnotationMap = llvm::MapVector<const llvm::StructType *, std::unique_ptr<DxilStructAnnotation> >;
  135. using FunctionAnnotationMap = llvm::MapVector<const llvm::Function *, std::unique_ptr<DxilFunctionAnnotation> >;
  136. DxilTypeSystem(llvm::Module *pModule);
  137. DxilStructAnnotation *AddStructAnnotation(const llvm::StructType *pStructType);
  138. DxilStructAnnotation *GetStructAnnotation(const llvm::StructType *pStructType);
  139. const DxilStructAnnotation *GetStructAnnotation(const llvm::StructType *pStructType) const;
  140. void EraseStructAnnotation(const llvm::StructType *pStructType);
  141. StructAnnotationMap &GetStructAnnotationMap();
  142. DxilFunctionAnnotation *AddFunctionAnnotation(const llvm::Function *pFunction);
  143. DxilFunctionAnnotation *GetFunctionAnnotation(const llvm::Function *pFunction);
  144. const DxilFunctionAnnotation *GetFunctionAnnotation(const llvm::Function *pFunction) const;
  145. void EraseFunctionAnnotation(const llvm::Function *pFunction);
  146. FunctionAnnotationMap &GetFunctionAnnotationMap();
  147. // Utility methods to create stand-alone SNORM and UNORM.
  148. // We may want to move them to a more centralized place for most utilities.
  149. llvm::StructType *GetSNormF32Type(unsigned NumComps);
  150. llvm::StructType *GetUNormF32Type(unsigned NumComps);
  151. // Methods to copy annotation from another DxilTypeSystem.
  152. void CopyTypeAnnotation(const llvm::Type *Ty, const DxilTypeSystem &src);
  153. void CopyFunctionAnnotation(const llvm::Function *pDstFunction,
  154. const llvm::Function *pSrcFunction,
  155. const DxilTypeSystem &src);
  156. bool UseMinPrecision();
  157. private:
  158. llvm::Module *m_pModule;
  159. StructAnnotationMap m_StructAnnotations;
  160. FunctionAnnotationMap m_FunctionAnnotations;
  161. DXIL::LowPrecisionMode m_LowPrecisionMode;
  162. llvm::StructType *GetNormFloatType(CompType CT, unsigned NumComps);
  163. };
  164. DXIL::SigPointKind SigPointFromInputQual(DxilParamInputQual Q, DXIL::ShaderKind SK, bool isPC);
  165. class DxilStructTypeIterator
  166. : public std::iterator<std::input_iterator_tag,
  167. std::pair<llvm::Type *, DxilFieldAnnotation *>> {
  168. private:
  169. llvm::StructType *STy;
  170. DxilStructAnnotation *SAnnotation;
  171. unsigned index;
  172. public:
  173. DxilStructTypeIterator(llvm::StructType *sTy,
  174. DxilStructAnnotation *sAnnotation, unsigned idx = 0);
  175. // prefix
  176. DxilStructTypeIterator &operator++();
  177. // postfix
  178. DxilStructTypeIterator operator++(int);
  179. bool operator==(DxilStructTypeIterator iter);
  180. bool operator!=(DxilStructTypeIterator iter);
  181. std::pair<llvm::Type *, DxilFieldAnnotation *> operator*();
  182. };
  183. DxilStructTypeIterator begin(llvm::StructType *STy,
  184. DxilStructAnnotation *SAnno);
  185. DxilStructTypeIterator end(llvm::StructType *STy, DxilStructAnnotation *SAnno);
  186. } // namespace hlsl