DxilMetadataHelper.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilMetadataHelper.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. // Helper to serialize/desialize metadata for DxilModule. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "dxc/DXIL/DxilConstants.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. namespace llvm {
  18. class LLVMContext;
  19. class Module;
  20. class Function;
  21. class Instruction;
  22. class DbgDeclareInst;
  23. class Value;
  24. class MDOperand;
  25. class Metadata;
  26. class ConstantAsMetadata;
  27. class MDTuple;
  28. class MDNode;
  29. class NamedMDNode;
  30. class GlobalVariable;
  31. class StringRef;
  32. }
  33. namespace hlsl {
  34. class ShaderModel;
  35. class DxilSignature;
  36. struct DxilEntrySignature;
  37. class DxilSignatureElement;
  38. class DxilModule;
  39. class DxilResourceBase;
  40. class DxilCBuffer;
  41. class DxilResource;
  42. class DxilSampler;
  43. class DxilTypeSystem;
  44. class DxilStructAnnotation;
  45. class DxilFieldAnnotation;
  46. class DxilTemplateArgAnnotation;
  47. class DxilFunctionAnnotation;
  48. class DxilParameterAnnotation;
  49. class RootSignatureHandle;
  50. struct DxilFunctionProps;
  51. class DxilSubobjects;
  52. class DxilSubobject;
  53. // Additional debug information for SROA'ed array variables,
  54. // where adjacent elements in DXIL might not have been adjacent
  55. // in the original user variable.
  56. struct DxilDIArrayDim {
  57. unsigned StrideInBits;
  58. unsigned NumElements;
  59. };
  60. /// Use this class to manipulate DXIL-spcific metadata.
  61. // In our code, only DxilModule and HLModule should use this class.
  62. class DxilMDHelper {
  63. public:
  64. //
  65. // Constants for metadata names and field positions.
  66. //
  67. // Dxil version.
  68. static const char kDxilVersionMDName[];
  69. static const unsigned kDxilVersionNumFields = 2;
  70. static const unsigned kDxilVersionMajorIdx = 0; // DXIL version major.
  71. static const unsigned kDxilVersionMinorIdx = 1; // DXIL version minor.
  72. // Shader model.
  73. static const char kDxilShaderModelMDName[];
  74. static const unsigned kDxilShaderModelNumFields = 3;
  75. static const unsigned kDxilShaderModelTypeIdx = 0; // Shader type (vs,ps,cs,gs,ds,hs).
  76. static const unsigned kDxilShaderModelMajorIdx = 1; // Shader model major.
  77. static const unsigned kDxilShaderModelMinorIdx = 2; // Shader model minor.
  78. // Intermediate codegen/optimizer options, not valid in final DXIL module.
  79. static const char kDxilIntermediateOptionsMDName[];
  80. static const unsigned kDxilIntermediateOptionsFlags = 0; // Unique element ID.
  81. // Entry points.
  82. static const char kDxilEntryPointsMDName[];
  83. // Root Signature, for intermediate use, not valid in final DXIL module.
  84. static const char kDxilRootSignatureMDName[];
  85. // ViewId state.
  86. static const char kDxilViewIdStateMDName[];
  87. // Subobjects
  88. static const char kDxilSubobjectsMDName[];
  89. // Source info.
  90. static const char kDxilSourceContentsMDName[];
  91. static const char kDxilSourceDefinesMDName[];
  92. static const char kDxilSourceMainFileNameMDName[];
  93. static const char kDxilSourceArgsMDName[];
  94. static const unsigned kDxilEntryPointNumFields = 5;
  95. static const unsigned kDxilEntryPointFunction = 0; // Entry point function symbol.
  96. static const unsigned kDxilEntryPointName = 1; // Entry point unmangled name.
  97. static const unsigned kDxilEntryPointSignatures = 2; // Entry point signature tuple.
  98. static const unsigned kDxilEntryPointResources = 3; // Entry point resource tuple.
  99. static const unsigned kDxilEntryPointProperties = 4; // Entry point properties tuple.
  100. // Signatures.
  101. static const unsigned kDxilNumSignatureFields = 3;
  102. static const unsigned kDxilInputSignature = 0; // Shader input signature.
  103. static const unsigned kDxilOutputSignature = 1; // Shader output signature.
  104. static const unsigned kDxilPatchConstantSignature = 2; // Shader patch constant (PC) signature.
  105. // Signature Element.
  106. static const unsigned kDxilSignatureElementNumFields = 11;
  107. static const unsigned kDxilSignatureElementID = 0; // Unique element ID.
  108. static const unsigned kDxilSignatureElementName = 1; // Element name.
  109. static const unsigned kDxilSignatureElementType = 2; // Element type.
  110. static const unsigned kDxilSignatureElementSystemValue = 3; // Effective system value.
  111. static const unsigned kDxilSignatureElementIndexVector = 4; // Semantic index vector.
  112. static const unsigned kDxilSignatureElementInterpMode = 5; // Interpolation mode.
  113. static const unsigned kDxilSignatureElementRows = 6; // Number of rows.
  114. static const unsigned kDxilSignatureElementCols = 7; // Number of columns.
  115. static const unsigned kDxilSignatureElementStartRow = 8; // Element packing start row.
  116. static const unsigned kDxilSignatureElementStartCol = 9; // Element packing start column.
  117. static const unsigned kDxilSignatureElementNameValueList = 10; // Name-value list for extended properties.
  118. // Signature Element Extended Properties.
  119. static const unsigned kDxilSignatureElementOutputStreamTag = 0;
  120. static const unsigned kHLSignatureElementGlobalSymbolTag = 1;
  121. static const unsigned kDxilSignatureElementDynIdxCompMaskTag = 2;
  122. static const unsigned kDxilSignatureElementUsageCompMaskTag = 3;
  123. // Resources.
  124. static const char kDxilResourcesMDName[];
  125. static const unsigned kDxilNumResourceFields = 4;
  126. static const unsigned kDxilResourceSRVs = 0;
  127. static const unsigned kDxilResourceUAVs = 1;
  128. static const unsigned kDxilResourceCBuffers = 2;
  129. static const unsigned kDxilResourceSamplers = 3;
  130. // ResourceBase.
  131. static const unsigned kDxilResourceBaseNumFields = 6;
  132. static const unsigned kDxilResourceBaseID = 0; // Unique (per type) resource ID.
  133. static const unsigned kDxilResourceBaseVariable = 1; // Resource global variable.
  134. static const unsigned kDxilResourceBaseName = 2; // Original (HLSL) name of the resource.
  135. static const unsigned kDxilResourceBaseSpaceID = 3; // Resource range space ID.
  136. static const unsigned kDxilResourceBaseLowerBound = 4; // Resource range lower bound.
  137. static const unsigned kDxilResourceBaseRangeSize = 5; // Resource range size.
  138. // SRV-specific.
  139. static const unsigned kDxilSRVNumFields = 9;
  140. static const unsigned kDxilSRVShape = 6; // SRV shape.
  141. static const unsigned kDxilSRVSampleCount = 7; // SRV sample count.
  142. static const unsigned kDxilSRVNameValueList = 8; // Name-value list for extended properties.
  143. // UAV-specific.
  144. static const unsigned kDxilUAVNumFields = 11;
  145. static const unsigned kDxilUAVShape = 6; // UAV shape.
  146. static const unsigned kDxilUAVGloballyCoherent = 7; // Globally-coherent UAV.
  147. static const unsigned kDxilUAVCounter = 8; // UAV with a counter.
  148. static const unsigned kDxilUAVRasterizerOrderedView = 9; // UAV that is a ROV.
  149. static const unsigned kDxilUAVNameValueList = 10; // Name-value list for extended properties.
  150. // CBuffer-specific.
  151. static const unsigned kDxilCBufferNumFields = 8;
  152. static const unsigned kDxilCBufferSizeInBytes = 6; // CBuffer size in bytes.
  153. static const unsigned kDxilCBufferNameValueList = 7; // Name-value list for extended properties.
  154. // CBuffer extended properties
  155. static const unsigned kHLCBufferIsTBufferTag = 0; // CBuffer is actually TBuffer, not yet converted to SRV.
  156. // Sampler-specific.
  157. static const unsigned kDxilSamplerNumFields = 8;
  158. static const unsigned kDxilSamplerType = 6; // Sampler type.
  159. static const unsigned kDxilSamplerNameValueList = 7; // Name-value list for extended properties.
  160. // Resource extended property tags.
  161. static const unsigned kDxilTypedBufferElementTypeTag = 0;
  162. static const unsigned kDxilStructuredBufferElementStrideTag = 1;
  163. static const unsigned kDxilSamplerFeedbackKindTag = 2;
  164. // Type system.
  165. static const char kDxilTypeSystemMDName[];
  166. static const char kDxilTypeSystemHelperVariablePrefix[];
  167. static const unsigned kDxilTypeSystemStructTag = 0;
  168. static const unsigned kDxilTypeSystemFunctionTag = 1;
  169. static const unsigned kDxilFieldAnnotationSNormTag = 0;
  170. static const unsigned kDxilFieldAnnotationUNormTag = 1;
  171. static const unsigned kDxilFieldAnnotationMatrixTag = 2;
  172. static const unsigned kDxilFieldAnnotationCBufferOffsetTag = 3;
  173. static const unsigned kDxilFieldAnnotationSemanticStringTag = 4;
  174. static const unsigned kDxilFieldAnnotationInterpolationModeTag = 5;
  175. static const unsigned kDxilFieldAnnotationFieldNameTag = 6;
  176. static const unsigned kDxilFieldAnnotationCompTypeTag = 7;
  177. static const unsigned kDxilFieldAnnotationPreciseTag = 8;
  178. static const unsigned kDxilFieldAnnotationCBUsedTag = 9;
  179. // StructAnnotation extended property tags (DXIL 1.5+ only, appended)
  180. static const unsigned kDxilTemplateArgumentsTag = 0; // Name for name-value list of extended struct properties
  181. // TemplateArgument tags
  182. static const unsigned kDxilTemplateArgTypeTag = 0; // Type template argument, followed by undef of type
  183. static const unsigned kDxilTemplateArgIntegralTag = 1; // Integral template argument, followed by i64 value
  184. static const unsigned kDxilTemplateArgValue = 1; // Position of template arg value (type or int)
  185. // Control flow hint.
  186. static const char kDxilControlFlowHintMDName[];
  187. // Resource attribute.
  188. static const char kHLDxilResourceAttributeMDName[];
  189. static const unsigned kHLDxilResourceAttributeNumFields = 2;
  190. static const unsigned kHLDxilResourceAttributeClass = 0;
  191. static const unsigned kHLDxilResourceAttributeMeta = 1;
  192. // Precise attribute.
  193. static const char kDxilPreciseAttributeMDName[];
  194. // NonUniform attribute.
  195. static const char kDxilNonUniformAttributeMDName[];
  196. // Variable debug layout metadata.
  197. static const char kDxilVariableDebugLayoutMDName[];
  198. // Indication of temporary storage metadata.
  199. static const char kDxilTempAllocaMDName[];
  200. // Validator version.
  201. static const char kDxilValidatorVersionMDName[];
  202. // Validator version uses the same constants for fields as kDxilVersion*
  203. // Extended shader property tags.
  204. static const unsigned kDxilShaderFlagsTag = 0;
  205. static const unsigned kDxilGSStateTag = 1;
  206. static const unsigned kDxilDSStateTag = 2;
  207. static const unsigned kDxilHSStateTag = 3;
  208. static const unsigned kDxilNumThreadsTag = 4;
  209. static const unsigned kDxilAutoBindingSpaceTag = 5;
  210. static const unsigned kDxilRayPayloadSizeTag = 6;
  211. static const unsigned kDxilRayAttribSizeTag = 7;
  212. static const unsigned kDxilShaderKindTag = 8;
  213. static const unsigned kDxilMSStateTag = 9;
  214. static const unsigned kDxilASStateTag = 10;
  215. // GSState.
  216. static const unsigned kDxilGSStateNumFields = 5;
  217. static const unsigned kDxilGSStateInputPrimitive = 0;
  218. static const unsigned kDxilGSStateMaxVertexCount = 1;
  219. static const unsigned kDxilGSStateActiveStreamMask = 2;
  220. static const unsigned kDxilGSStateOutputStreamTopology = 3;
  221. static const unsigned kDxilGSStateGSInstanceCount = 4;
  222. // DSState.
  223. static const unsigned kDxilDSStateNumFields = 2;
  224. static const unsigned kDxilDSStateTessellatorDomain = 0;
  225. static const unsigned kDxilDSStateInputControlPointCount = 1;
  226. // HSState.
  227. static const unsigned kDxilHSStateNumFields = 7;
  228. static const unsigned kDxilHSStatePatchConstantFunction = 0;
  229. static const unsigned kDxilHSStateInputControlPointCount = 1;
  230. static const unsigned kDxilHSStateOutputControlPointCount = 2;
  231. static const unsigned kDxilHSStateTessellatorDomain = 3;
  232. static const unsigned kDxilHSStateTessellatorPartitioning = 4;
  233. static const unsigned kDxilHSStateTessellatorOutputPrimitive= 5;
  234. static const unsigned kDxilHSStateMaxTessellationFactor = 6;
  235. // MSState.
  236. static const unsigned kDxilMSStateNumFields = 5;
  237. static const unsigned kDxilMSStateNumThreads = 0;
  238. static const unsigned kDxilMSStateMaxVertexCount = 1;
  239. static const unsigned kDxilMSStateMaxPrimitiveCount = 2;
  240. static const unsigned kDxilMSStateOutputTopology = 3;
  241. static const unsigned kDxilMSStatePayloadSizeInBytes = 4;
  242. // ASState.
  243. static const unsigned kDxilASStateNumFields = 2;
  244. static const unsigned kDxilASStateNumThreads = 0;
  245. static const unsigned kDxilASStatePayloadSizeInBytes = 1;
  246. public:
  247. /// Use this class to manipulate metadata of DXIL or high-level DX IR specific fields in the record.
  248. class ExtraPropertyHelper {
  249. public:
  250. ExtraPropertyHelper(llvm::Module *pModule);
  251. virtual ~ExtraPropertyHelper() {}
  252. virtual void EmitSRVProperties(const DxilResource &SRV, std::vector<llvm::Metadata *> &MDVals) = 0;
  253. virtual void LoadSRVProperties(const llvm::MDOperand &MDO, DxilResource &SRV) = 0;
  254. virtual void EmitUAVProperties(const DxilResource &UAV, std::vector<llvm::Metadata *> &MDVals) = 0;
  255. virtual void LoadUAVProperties(const llvm::MDOperand &MDO, DxilResource &UAV) = 0;
  256. virtual void EmitCBufferProperties(const DxilCBuffer &CB, std::vector<llvm::Metadata *> &MDVals) = 0;
  257. virtual void LoadCBufferProperties(const llvm::MDOperand &MDO, DxilCBuffer &CB) = 0;
  258. virtual void EmitSamplerProperties(const DxilSampler &S, std::vector<llvm::Metadata *> &MDVals) = 0;
  259. virtual void LoadSamplerProperties(const llvm::MDOperand &MDO, DxilSampler &S) = 0;
  260. virtual void EmitSignatureElementProperties(const DxilSignatureElement &SE, std::vector<llvm::Metadata *> &MDVals) = 0;
  261. virtual void LoadSignatureElementProperties(const llvm::MDOperand &MDO, DxilSignatureElement &SE) = 0;
  262. protected:
  263. llvm::LLVMContext &m_Ctx;
  264. llvm::Module *m_pModule;
  265. public:
  266. unsigned m_ValMajor, m_ValMinor; // Reported validation version in DXIL
  267. unsigned m_MinValMajor, m_MinValMinor; // Minimum validation version dictated by shader model
  268. bool m_bExtraMetadata;
  269. };
  270. public:
  271. DxilMDHelper(llvm::Module *pModule, std::unique_ptr<ExtraPropertyHelper> EPH);
  272. ~DxilMDHelper();
  273. void SetShaderModel(const ShaderModel *pSM);
  274. const ShaderModel *GetShaderModel() const;
  275. // Dxil version.
  276. void EmitDxilVersion(unsigned Major, unsigned Minor);
  277. void LoadDxilVersion(unsigned &Major, unsigned &Minor);
  278. // Validator version.
  279. void EmitValidatorVersion(unsigned Major, unsigned Minor);
  280. void LoadValidatorVersion(unsigned &Major, unsigned &Minor);
  281. // Shader model.
  282. void EmitDxilShaderModel(const ShaderModel *pSM);
  283. void LoadDxilShaderModel(const ShaderModel *&pSM);
  284. // Intermediate flags
  285. void EmitDxilIntermediateOptions(uint32_t flags);
  286. void LoadDxilIntermediateOptions(uint32_t &flags);
  287. // Entry points.
  288. void EmitDxilEntryPoints(std::vector<llvm::MDNode *> &MDEntries);
  289. void UpdateDxilEntryPoints(std::vector<llvm::MDNode *> &MDEntries);
  290. const llvm::NamedMDNode *GetDxilEntryPoints();
  291. llvm::MDTuple *EmitDxilEntryPointTuple(llvm::Function *pFunc, const std::string &Name, llvm::MDTuple *pSignatures,
  292. llvm::MDTuple *pResources, llvm::MDTuple *pProperties);
  293. void GetDxilEntryPoint(const llvm::MDNode *MDO, llvm::Function *&pFunc, std::string &Name,
  294. const llvm::MDOperand *&pSignatures, const llvm::MDOperand *&pResources,
  295. const llvm::MDOperand *&pProperties);
  296. // Signatures.
  297. llvm::MDTuple *EmitDxilSignatures(const DxilEntrySignature &EntrySig);
  298. void LoadDxilSignatures(const llvm::MDOperand &MDO,
  299. DxilEntrySignature &EntrySig);
  300. llvm::MDTuple *EmitSignatureMetadata(const DxilSignature &Sig);
  301. void EmitRootSignature(std::vector<uint8_t> &SerializedRootSignature);
  302. void LoadSignatureMetadata(const llvm::MDOperand &MDO, DxilSignature &Sig);
  303. llvm::MDTuple *EmitSignatureElement(const DxilSignatureElement &SE);
  304. void LoadSignatureElement(const llvm::MDOperand &MDO, DxilSignatureElement &SE);
  305. void LoadRootSignature(std::vector<uint8_t> &SerializedRootSignature);
  306. // Resources.
  307. llvm::MDTuple *EmitDxilResourceTuple(llvm::MDTuple *pSRVs, llvm::MDTuple *pUAVs,
  308. llvm::MDTuple *pCBuffers, llvm::MDTuple *pSamplers);
  309. void EmitDxilResources(llvm::MDTuple *pDxilResourceTuple);
  310. void UpdateDxilResources(llvm::MDTuple *pDxilResourceTuple);
  311. void GetDxilResources(const llvm::MDOperand &MDO, const llvm::MDTuple *&pSRVs, const llvm::MDTuple *&pUAVs,
  312. const llvm::MDTuple *&pCBuffers, const llvm::MDTuple *&pSamplers);
  313. void EmitDxilResourceBase(const DxilResourceBase &R, llvm::Metadata *ppMDVals[]);
  314. void LoadDxilResourceBase(const llvm::MDOperand &MDO, DxilResourceBase &R);
  315. llvm::MDTuple *EmitDxilSRV(const DxilResource &SRV);
  316. void LoadDxilSRV(const llvm::MDOperand &MDO, DxilResource &SRV);
  317. llvm::MDTuple *EmitDxilUAV(const DxilResource &UAV);
  318. void LoadDxilUAV(const llvm::MDOperand &MDO, DxilResource &UAV);
  319. llvm::MDTuple *EmitDxilCBuffer(const DxilCBuffer &CB);
  320. void LoadDxilCBuffer(const llvm::MDOperand &MDO, DxilCBuffer &CB);
  321. llvm::MDTuple *EmitDxilSampler(const DxilSampler &S);
  322. void LoadDxilSampler(const llvm::MDOperand &MDO, DxilSampler &S);
  323. const llvm::MDOperand &GetResourceClass(llvm::MDNode *MD, DXIL::ResourceClass &RC);
  324. void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
  325. void LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R);
  326. void LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S);
  327. // Type system.
  328. void EmitDxilTypeSystem(DxilTypeSystem &TypeSystem, std::vector<llvm::GlobalVariable *> &LLVMUsed);
  329. void LoadDxilTypeSystemNode(const llvm::MDTuple &MDT, DxilTypeSystem &TypeSystem);
  330. void LoadDxilTypeSystem(DxilTypeSystem &TypeSystem);
  331. llvm::Metadata *EmitDxilStructAnnotation(const DxilStructAnnotation &SA);
  332. void LoadDxilStructAnnotation(const llvm::MDOperand &MDO, DxilStructAnnotation &SA);
  333. llvm::Metadata *EmitDxilFieldAnnotation(const DxilFieldAnnotation &FA);
  334. void LoadDxilFieldAnnotation(const llvm::MDOperand &MDO, DxilFieldAnnotation &FA);
  335. llvm::Metadata *EmitDxilFunctionAnnotation(const DxilFunctionAnnotation &FA);
  336. void LoadDxilFunctionAnnotation(const llvm::MDOperand &MDO, DxilFunctionAnnotation &FA);
  337. llvm::Metadata *EmitDxilParamAnnotation(const DxilParameterAnnotation &PA);
  338. void LoadDxilParamAnnotation(const llvm::MDOperand &MDO, DxilParameterAnnotation &PA);
  339. llvm::Metadata *EmitDxilParamAnnotations(const DxilFunctionAnnotation &FA);
  340. void LoadDxilParamAnnotations(const llvm::MDOperand &MDO, DxilFunctionAnnotation &FA);
  341. llvm::Metadata *EmitDxilTemplateArgAnnotation(const DxilTemplateArgAnnotation &annotation);
  342. void LoadDxilTemplateArgAnnotation(const llvm::MDOperand &MDO, DxilTemplateArgAnnotation &annotation);
  343. // Function props.
  344. llvm::MDTuple *EmitDxilFunctionProps(const hlsl::DxilFunctionProps *props,
  345. const llvm::Function *F);
  346. const llvm::Function *LoadDxilFunctionProps(const llvm::MDTuple *pProps,
  347. hlsl::DxilFunctionProps *props);
  348. llvm::MDTuple *EmitDxilEntryProperties(uint64_t rawShaderFlag,
  349. const hlsl::DxilFunctionProps &props,
  350. uint32_t autoBindingSpace);
  351. void LoadDxilEntryProperties(const llvm::MDOperand &MDO,
  352. uint64_t &rawShaderFlag,
  353. hlsl::DxilFunctionProps &props,
  354. uint32_t &autoBindingSpace);
  355. // ViewId state.
  356. void EmitDxilViewIdState(std::vector<unsigned> &SerializedState);
  357. void LoadDxilViewIdState(std::vector<unsigned> &SerializedState);
  358. // Control flow hints.
  359. static llvm::MDNode *EmitControlFlowHints(llvm::LLVMContext &Ctx, std::vector<DXIL::ControlFlowHint> &hints);
  360. static unsigned GetControlFlowHintMask(const llvm::Instruction *I);
  361. static bool HasControlFlowHintToPreventFlatten(const llvm::Instruction *I);
  362. // Subobjects
  363. void EmitSubobjects(const DxilSubobjects &Subobjects);
  364. void LoadSubobjects(DxilSubobjects &Subobjects);
  365. llvm::Metadata *EmitSubobject(const DxilSubobject &obj);
  366. void LoadSubobject(const llvm::MDNode &MDO, DxilSubobjects &Subobjects);
  367. // Extra metadata present
  368. bool HasExtraMetadata() { return m_bExtraMetadata; }
  369. // Shader specific.
  370. private:
  371. llvm::MDTuple *EmitDxilGSState(DXIL::InputPrimitive Primitive, unsigned MaxVertexCount,
  372. unsigned ActiveStreamMask, DXIL::PrimitiveTopology StreamPrimitiveTopology,
  373. unsigned GSInstanceCount);
  374. void LoadDxilGSState(const llvm::MDOperand &MDO, DXIL::InputPrimitive &Primitive, unsigned &MaxVertexCount,
  375. unsigned &ActiveStreamMask, DXIL::PrimitiveTopology &StreamPrimitiveTopology,
  376. unsigned &GSInstanceCount);
  377. llvm::MDTuple *EmitDxilDSState(DXIL::TessellatorDomain Domain, unsigned InputControlPointCount);
  378. void LoadDxilDSState(const llvm::MDOperand &MDO, DXIL::TessellatorDomain &Domain, unsigned &InputControlPointCount);
  379. llvm::MDTuple *EmitDxilHSState(llvm::Function *pPatchConstantFunction,
  380. unsigned InputControlPointCount,
  381. unsigned OutputControlPointCount,
  382. DXIL::TessellatorDomain TessDomain,
  383. DXIL::TessellatorPartitioning TessPartitioning,
  384. DXIL::TessellatorOutputPrimitive TessOutputPrimitive,
  385. float MaxTessFactor);
  386. void LoadDxilHSState(const llvm::MDOperand &MDO,
  387. llvm::Function *&pPatchConstantFunction,
  388. unsigned &InputControlPointCount,
  389. unsigned &OutputControlPointCount,
  390. DXIL::TessellatorDomain &TessDomain,
  391. DXIL::TessellatorPartitioning &TessPartitioning,
  392. DXIL::TessellatorOutputPrimitive &TessOutputPrimitive,
  393. float &MaxTessFactor);
  394. llvm::MDTuple *EmitDxilMSState(const unsigned *NumThreads,
  395. unsigned MaxVertexCount,
  396. unsigned MaxPrimitiveCount,
  397. DXIL::MeshOutputTopology OutputTopology,
  398. unsigned payloadSizeInBytes);
  399. void LoadDxilMSState(const llvm::MDOperand &MDO,
  400. unsigned *NumThreads,
  401. unsigned &MaxVertexCount,
  402. unsigned &MaxPrimitiveCount,
  403. DXIL::MeshOutputTopology &OutputTopology,
  404. unsigned &payloadSizeInBytes);
  405. llvm::MDTuple *EmitDxilASState(const unsigned *NumThreads, unsigned payloadSizeInBytes);
  406. void LoadDxilASState(const llvm::MDOperand &MDO, unsigned *NumThreads, unsigned &payloadSizeInBytes);
  407. public:
  408. // Utility functions.
  409. static bool IsKnownNamedMetaData(const llvm::NamedMDNode &Node);
  410. static void combineDxilMetadata(llvm::Instruction *K, const llvm::Instruction *J);
  411. static llvm::ConstantAsMetadata *Int32ToConstMD(int32_t v, llvm::LLVMContext &Ctx);
  412. llvm::ConstantAsMetadata *Int32ToConstMD(int32_t v);
  413. static llvm::ConstantAsMetadata *Uint32ToConstMD(unsigned v, llvm::LLVMContext &Ctx);
  414. llvm::ConstantAsMetadata *Uint32ToConstMD(unsigned v);
  415. static llvm::ConstantAsMetadata *Uint64ToConstMD(uint64_t v, llvm::LLVMContext &Ctx);
  416. llvm::ConstantAsMetadata *Uint64ToConstMD(uint64_t v);
  417. llvm::ConstantAsMetadata *Int8ToConstMD(int8_t v);
  418. llvm::ConstantAsMetadata *Uint8ToConstMD(uint8_t v);
  419. static llvm::ConstantAsMetadata *BoolToConstMD(bool v, llvm::LLVMContext &Ctx);
  420. llvm::ConstantAsMetadata *BoolToConstMD(bool v);
  421. llvm::ConstantAsMetadata *FloatToConstMD(float v);
  422. static int32_t ConstMDToInt32(const llvm::MDOperand &MDO);
  423. static unsigned ConstMDToUint32(const llvm::MDOperand &MDO);
  424. static uint64_t ConstMDToUint64(const llvm::MDOperand &MDO);
  425. static int8_t ConstMDToInt8(const llvm::MDOperand &MDO);
  426. static uint8_t ConstMDToUint8(const llvm::MDOperand &MDO);
  427. static bool ConstMDToBool(const llvm::MDOperand &MDO);
  428. static float ConstMDToFloat(const llvm::MDOperand &MDO);
  429. static std::string StringMDToString(const llvm::MDOperand &MDO);
  430. static llvm::StringRef StringMDToStringRef(const llvm::MDOperand &MDO);
  431. static llvm::Value *ValueMDToValue(const llvm::MDOperand &MDO);
  432. llvm::MDTuple *Uint32VectorToConstMDTuple(const std::vector<unsigned> &Vec);
  433. void ConstMDTupleToUint32Vector(llvm::MDTuple *pTupleMD, std::vector<unsigned> &Vec);
  434. static bool IsMarkedPrecise(const llvm::Instruction *inst);
  435. static void MarkPrecise(llvm::Instruction *inst);
  436. static bool IsMarkedNonUniform(const llvm::Instruction *inst);
  437. static void MarkNonUniform(llvm::Instruction *inst);
  438. static bool GetVariableDebugLayout(llvm::DbgDeclareInst *inst,
  439. unsigned &StartOffsetInBits, std::vector<DxilDIArrayDim> &ArrayDims);
  440. static void SetVariableDebugLayout(llvm::DbgDeclareInst *inst,
  441. unsigned StartOffsetInBits, const std::vector<DxilDIArrayDim> &ArrayDims);
  442. static void CopyMetadata(llvm::Instruction &I, llvm::Instruction &SrcInst, llvm::ArrayRef<unsigned>WL = llvm::ArrayRef<unsigned>());
  443. private:
  444. llvm::LLVMContext &m_Ctx;
  445. llvm::Module *m_pModule;
  446. const ShaderModel *m_pSM;
  447. std::unique_ptr<ExtraPropertyHelper> m_ExtraPropertyHelper;
  448. unsigned m_ValMajor, m_ValMinor; // Reported validation version in DXIL
  449. unsigned m_MinValMajor, m_MinValMinor; // Minimum validation version dictated by shader model
  450. // Non-fatal if extra metadata is found, but will fail validation.
  451. // This is how metadata can be exteneded.
  452. bool m_bExtraMetadata;
  453. };
  454. /// Use this class to manipulate metadata of extra metadata record properties that are specific to DXIL.
  455. class DxilExtraPropertyHelper : public DxilMDHelper::ExtraPropertyHelper {
  456. public:
  457. DxilExtraPropertyHelper(llvm::Module *pModule);
  458. virtual ~DxilExtraPropertyHelper() {}
  459. virtual void EmitSRVProperties(const DxilResource &SRV, std::vector<llvm::Metadata *> &MDVals);
  460. virtual void LoadSRVProperties(const llvm::MDOperand &MDO, DxilResource &SRV);
  461. virtual void EmitUAVProperties(const DxilResource &UAV, std::vector<llvm::Metadata *> &MDVals);
  462. virtual void LoadUAVProperties(const llvm::MDOperand &MDO, DxilResource &UAV);
  463. virtual void EmitCBufferProperties(const DxilCBuffer &CB, std::vector<llvm::Metadata *> &MDVals);
  464. virtual void LoadCBufferProperties(const llvm::MDOperand &MDO, DxilCBuffer &CB);
  465. virtual void EmitSamplerProperties(const DxilSampler &S, std::vector<llvm::Metadata *> &MDVals);
  466. virtual void LoadSamplerProperties(const llvm::MDOperand &MDO, DxilSampler &S);
  467. virtual void EmitSignatureElementProperties(const DxilSignatureElement &SE, std::vector<llvm::Metadata *> &MDVals);
  468. virtual void LoadSignatureElementProperties(const llvm::MDOperand &MDO, DxilSignatureElement &SE);
  469. };
  470. } // namespace hlsl