DxilModule.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilModule.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. // The main class to work with DXIL, similar to LLVM module. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "dxc/HLSL/DxilMetadataHelper.h"
  13. #include "dxc/HLSL/DxilCBuffer.h"
  14. #include "dxc/HLSL/DxilResource.h"
  15. #include "dxc/HLSL/DxilSampler.h"
  16. #include "dxc/HLSL/DxilShaderFlags.h"
  17. #include "dxc/HLSL/DxilSignature.h"
  18. #include "dxc/HLSL/DxilConstants.h"
  19. #include "dxc/HLSL/DxilTypeSystem.h"
  20. #include "dxc/HLSL/ComputeViewIdState.h"
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26. namespace llvm {
  27. class LLVMContext;
  28. class Module;
  29. class Function;
  30. class Instruction;
  31. class MDTuple;
  32. class MDOperand;
  33. class DebugInfoFinder;
  34. }
  35. namespace hlsl {
  36. class ShaderModel;
  37. class OP;
  38. class RootSignatureHandle;
  39. struct DxilFunctionProps;
  40. class DxilEntryProps;
  41. using DxilEntryPropsMap =
  42. std::unordered_map<const llvm::Function *, std::unique_ptr<DxilEntryProps>>;
  43. /// Use this class to manipulate DXIL of a shader.
  44. class DxilModule {
  45. public:
  46. DxilModule(llvm::Module *pModule);
  47. ~DxilModule();
  48. // Subsystems.
  49. llvm::LLVMContext &GetCtx() const;
  50. llvm::Module *GetModule() const;
  51. OP *GetOP() const;
  52. void SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision = true);
  53. const ShaderModel *GetShaderModel() const;
  54. void GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const;
  55. void SetValidatorVersion(unsigned ValMajor, unsigned ValMinor);
  56. bool UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor);
  57. void GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const;
  58. // Return true on success, requires valid shader model and CollectShaderFlags to have been set
  59. bool GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const;
  60. // Update validator version to minimum if higher than current (ex: after CollectShaderFlags)
  61. bool UpgradeToMinValidatorVersion();
  62. // Entry functions.
  63. llvm::Function *GetEntryFunction();
  64. const llvm::Function *GetEntryFunction() const;
  65. void SetEntryFunction(llvm::Function *pEntryFunc);
  66. const std::string &GetEntryFunctionName() const;
  67. void SetEntryFunctionName(const std::string &name);
  68. llvm::Function *GetPatchConstantFunction();
  69. const llvm::Function *GetPatchConstantFunction() const;
  70. void SetPatchConstantFunction(llvm::Function *pFunc);
  71. // Flags.
  72. unsigned GetGlobalFlags() const;
  73. void CollectShaderFlagsForModule();
  74. // Resources.
  75. unsigned AddCBuffer(std::unique_ptr<DxilCBuffer> pCB);
  76. DxilCBuffer &GetCBuffer(unsigned idx);
  77. const DxilCBuffer &GetCBuffer(unsigned idx) const;
  78. const std::vector<std::unique_ptr<DxilCBuffer> > &GetCBuffers() const;
  79. unsigned AddSampler(std::unique_ptr<DxilSampler> pSampler);
  80. DxilSampler &GetSampler(unsigned idx);
  81. const DxilSampler &GetSampler(unsigned idx) const;
  82. const std::vector<std::unique_ptr<DxilSampler> > &GetSamplers() const;
  83. unsigned AddSRV(std::unique_ptr<DxilResource> pSRV);
  84. DxilResource &GetSRV(unsigned idx);
  85. const DxilResource &GetSRV(unsigned idx) const;
  86. const std::vector<std::unique_ptr<DxilResource> > &GetSRVs() const;
  87. unsigned AddUAV(std::unique_ptr<DxilResource> pUAV);
  88. DxilResource &GetUAV(unsigned idx);
  89. const DxilResource &GetUAV(unsigned idx) const;
  90. const std::vector<std::unique_ptr<DxilResource> > &GetUAVs() const;
  91. void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
  92. void LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R);
  93. void LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S);
  94. void RemoveUnusedResources();
  95. void RemoveUnusedResourceSymbols();
  96. void RemoveFunction(llvm::Function *F);
  97. // Signatures.
  98. DxilSignature &GetInputSignature();
  99. const DxilSignature &GetInputSignature() const;
  100. DxilSignature &GetOutputSignature();
  101. const DxilSignature &GetOutputSignature() const;
  102. DxilSignature &GetPatchConstantSignature();
  103. const DxilSignature &GetPatchConstantSignature() const;
  104. const RootSignatureHandle &GetRootSignature() const;
  105. bool HasDxilEntrySignature(const llvm::Function *F) const;
  106. DxilEntrySignature &GetDxilEntrySignature(const llvm::Function *F);
  107. // Move DxilEntryProps of F to NewF.
  108. void ReplaceDxilEntryProps(llvm::Function *F, llvm::Function *NewF);
  109. // Clone DxilEntryProps of F to NewF.
  110. void CloneDxilEntryProps(llvm::Function *F, llvm::Function *NewF);
  111. bool HasDxilEntryProps(const llvm::Function *F) const;
  112. DxilEntryProps &GetDxilEntryProps(const llvm::Function *F);
  113. // DxilFunctionProps.
  114. bool HasDxilFunctionProps(const llvm::Function *F) const;
  115. DxilFunctionProps &GetDxilFunctionProps(const llvm::Function *F);
  116. const DxilFunctionProps &GetDxilFunctionProps(const llvm::Function *F) const;
  117. // Move DxilFunctionProps of F to NewF.
  118. void SetPatchConstantFunctionForHS(llvm::Function *hullShaderFunc, llvm::Function *patchConstantFunc);
  119. bool IsGraphicsShader(const llvm::Function *F) const; // vs,hs,ds,gs,ps
  120. bool IsPatchConstantShader(const llvm::Function *F) const;
  121. bool IsComputeShader(const llvm::Function *F) const;
  122. // Is an entry function that uses input/output signature conventions?
  123. // Includes: vs/hs/ds/gs/ps/cs as well as the patch constant function.
  124. bool IsEntryThatUsesSignatures(const llvm::Function *F) const ;
  125. // Remove Root Signature from module metadata
  126. void StripRootSignatureFromMetadata();
  127. // Update validator version metadata to current setting
  128. void UpdateValidatorVersionMetadata();
  129. // DXIL type system.
  130. DxilTypeSystem &GetTypeSystem();
  131. /// Emit llvm.used array to make sure that optimizations do not remove unreferenced globals.
  132. void EmitLLVMUsed();
  133. std::vector<llvm::GlobalVariable* > &GetLLVMUsed();
  134. void ClearLLVMUsed();
  135. // ViewId state.
  136. DxilViewIdState &GetViewIdState();
  137. const DxilViewIdState &GetViewIdState() const;
  138. // DXIL metadata manipulation.
  139. /// Clear all DXIL data that exists in in-memory form.
  140. static void ClearDxilMetadata(llvm::Module &M);
  141. /// Serialize DXIL in-memory form to metadata form.
  142. void EmitDxilMetadata();
  143. /// Update resource metadata.
  144. void ReEmitDxilResources();
  145. /// Deserialize DXIL metadata form into in-memory form.
  146. void LoadDxilMetadata();
  147. /// Check if a Named meta data node is known by dxil module.
  148. static bool IsKnownNamedMetaData(llvm::NamedMDNode &Node);
  149. // Reset functions used to transfer ownership.
  150. void ResetEntrySignature(DxilEntrySignature *pValue);
  151. void ResetRootSignature(RootSignatureHandle *pValue);
  152. void ResetTypeSystem(DxilTypeSystem *pValue);
  153. void ResetOP(hlsl::OP *hlslOP);
  154. void ResetEntryPropsMap(DxilEntryPropsMap &&PropMap);
  155. void StripDebugRelatedCode();
  156. llvm::DebugInfoFinder &GetOrCreateDebugInfoFinder();
  157. static DxilModule *TryGetDxilModule(llvm::Module *pModule);
  158. // Helpers for working with precise.
  159. // Return true if the instruction should be considered precise.
  160. //
  161. // An instruction can be marked precise in the following ways:
  162. //
  163. // 1. Global refactoring is disabled.
  164. // 2. The instruction has a precise metadata annotation.
  165. // 3. The instruction has precise fast math flags set.
  166. //
  167. bool IsPrecise(const llvm::Instruction *inst) const;
  168. // Check if the instruction has fast math flags configured to indicate
  169. // the instruction is precise.
  170. static bool HasPreciseFastMathFlags(const llvm::Instruction *inst);
  171. // Set fast math flags configured to indicate the instruction is precise.
  172. static void SetPreciseFastMathFlags(llvm::Instruction *inst);
  173. // True if fast math flags are preserved across serialize/deserialize.
  174. static bool PreservesFastMathFlags(const llvm::Instruction *inst);
  175. public:
  176. ShaderFlags m_ShaderFlags;
  177. void CollectShaderFlagsForModule(ShaderFlags &Flags);
  178. // Check if DxilModule contains multi component UAV Loads.
  179. // This funciton must be called after unused resources are removed from DxilModule
  180. bool ModuleHasMulticomponentUAVLoads();
  181. // Compute shader.
  182. void SetNumThreads(unsigned x, unsigned y, unsigned z);
  183. unsigned GetNumThreads(unsigned idx) const;
  184. // Geometry shader.
  185. DXIL::InputPrimitive GetInputPrimitive() const;
  186. void SetInputPrimitive(DXIL::InputPrimitive IP);
  187. unsigned GetMaxVertexCount() const;
  188. void SetMaxVertexCount(unsigned Count);
  189. DXIL::PrimitiveTopology GetStreamPrimitiveTopology() const;
  190. void SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology);
  191. bool HasMultipleOutputStreams() const;
  192. unsigned GetOutputStream() const;
  193. unsigned GetGSInstanceCount() const;
  194. void SetGSInstanceCount(unsigned Count);
  195. bool IsStreamActive(unsigned Stream) const;
  196. void SetStreamActive(unsigned Stream, bool bActive);
  197. void SetActiveStreamMask(unsigned Mask);
  198. unsigned GetActiveStreamMask() const;
  199. // Language options
  200. // UseMinPrecision must be set at SetShaderModel time.
  201. bool GetUseMinPrecision() const;
  202. void SetDisableOptimization(bool disableOptimization);
  203. bool GetDisableOptimization() const;
  204. void SetAllResourcesBound(bool resourcesBound);
  205. bool GetAllResourcesBound() const;
  206. // Hull and Domain shaders.
  207. unsigned GetInputControlPointCount() const;
  208. void SetInputControlPointCount(unsigned NumICPs);
  209. DXIL::TessellatorDomain GetTessellatorDomain() const;
  210. void SetTessellatorDomain(DXIL::TessellatorDomain TessDomain);
  211. // Hull shader.
  212. unsigned GetOutputControlPointCount() const;
  213. void SetOutputControlPointCount(unsigned NumOCPs);
  214. DXIL::TessellatorPartitioning GetTessellatorPartitioning() const;
  215. void SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning);
  216. DXIL::TessellatorOutputPrimitive GetTessellatorOutputPrimitive() const;
  217. void SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive);
  218. float GetMaxTessellationFactor() const;
  219. void SetMaxTessellationFactor(float MaxTessellationFactor);
  220. // AutoBindingSpace also enables automatic binding for libraries if set.
  221. // UINT_MAX == unset
  222. void SetAutoBindingSpace(uint32_t Space);
  223. uint32_t GetAutoBindingSpace() const;
  224. void SetShaderProperties(DxilFunctionProps *props);
  225. private:
  226. // Signatures.
  227. std::unique_ptr<RootSignatureHandle> m_RootSignature;
  228. // Shader resources.
  229. std::vector<std::unique_ptr<DxilResource> > m_SRVs;
  230. std::vector<std::unique_ptr<DxilResource> > m_UAVs;
  231. std::vector<std::unique_ptr<DxilCBuffer> > m_CBuffers;
  232. std::vector<std::unique_ptr<DxilSampler> > m_Samplers;
  233. // Geometry shader.
  234. DXIL::PrimitiveTopology m_StreamPrimitiveTopology;
  235. unsigned m_ActiveStreamMask;
  236. private:
  237. llvm::LLVMContext &m_Ctx;
  238. llvm::Module *m_pModule;
  239. llvm::Function *m_pEntryFunc;
  240. std::string m_EntryName;
  241. std::unique_ptr<DxilMDHelper> m_pMDHelper;
  242. std::unique_ptr<llvm::DebugInfoFinder> m_pDebugInfoFinder;
  243. const ShaderModel *m_pSM;
  244. unsigned m_DxilMajor;
  245. unsigned m_DxilMinor;
  246. unsigned m_ValMajor;
  247. unsigned m_ValMinor;
  248. std::unique_ptr<OP> m_pOP;
  249. size_t m_pUnused;
  250. // LLVM used.
  251. std::vector<llvm::GlobalVariable*> m_LLVMUsed;
  252. // Type annotations.
  253. std::unique_ptr<DxilTypeSystem> m_pTypeSystem;
  254. // EntryProps for shader functions.
  255. DxilEntryPropsMap m_DxilEntryPropsMap;
  256. // Keeps track of patch constant functions used by hull shaders
  257. std::unordered_set<const llvm::Function *> m_PatchConstantFunctions;
  258. // ViewId state.
  259. std::unique_ptr<DxilViewIdState> m_pViewIdState;
  260. // DXIL metadata serialization/deserialization.
  261. llvm::MDTuple *EmitDxilResources();
  262. void LoadDxilResources(const llvm::MDOperand &MDO);
  263. // Helpers.
  264. template<typename T> unsigned AddResource(std::vector<std::unique_ptr<T> > &Vec, std::unique_ptr<T> pRes);
  265. void LoadDxilSignature(const llvm::MDTuple *pSigTuple, DxilSignature &Sig, bool bInput);
  266. // properties from HLModule
  267. bool m_bDisableOptimizations;
  268. bool m_bUseMinPrecision;
  269. bool m_bAllResourcesBound;
  270. uint32_t m_AutoBindingSpace;
  271. };
  272. } // namespace hlsl