DxilMetadataHelper.h 28 KB

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