HLModule.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // HLModule.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. // HighLevel DX IR module. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "dxc/Support/Global.h"
  13. #include "dxc/HLSL/DxilMetadataHelper.h"
  14. #include "dxc/HLSL/DxilConstants.h"
  15. #include "dxc/HLSL/HLResource.h"
  16. #include "dxc/HLSL/HLOperations.h"
  17. #include "dxc/HLSL/DxilSampler.h"
  18. #include "dxc/HLSL/DxilShaderModel.h"
  19. #include "dxc/HLSL/DxilSignature.h"
  20. #include "dxc/HLSL/DxilFunctionProps.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 CallInst;
  32. class MDTuple;
  33. class MDNode;
  34. class GlobalVariable;
  35. class DIGlobalVariable;
  36. class DebugInfoFinder;
  37. }
  38. namespace hlsl {
  39. class ShaderModel;
  40. class OP;
  41. class RootSignatureHandle;
  42. struct HLOptions {
  43. HLOptions()
  44. : bDefaultRowMajor(false), bIEEEStrict(false), bDisableOptimizations(false),
  45. bLegacyCBufferLoad(false), PackingStrategy(0), unused(0) {
  46. }
  47. uint32_t GetHLOptionsRaw() const;
  48. void SetHLOptionsRaw(uint32_t data);
  49. unsigned bDefaultRowMajor : 1;
  50. unsigned bIEEEStrict : 1;
  51. unsigned bAllResourcesBound : 1;
  52. unsigned bDisableOptimizations : 1;
  53. unsigned bLegacyCBufferLoad : 1;
  54. unsigned PackingStrategy : 2;
  55. static_assert((unsigned)DXIL::PackingStrategy::Invalid < 4, "otherwise 2 bits is not enough to store PackingStrategy");
  56. unsigned bUseMinPrecision : 1;
  57. unsigned unused : 24;
  58. };
  59. typedef std::unordered_map<const llvm::Function *, std::unique_ptr<DxilFunctionProps>> DxilFunctionPropsMap;
  60. /// Use this class to manipulate HLDXIR of a shader.
  61. class HLModule {
  62. public:
  63. HLModule(llvm::Module *pModule);
  64. ~HLModule();
  65. using Domain = DXIL::TessellatorDomain;
  66. // Subsystems.
  67. llvm::LLVMContext &GetCtx() const;
  68. llvm::Module *GetModule() const;
  69. OP *GetOP() const;
  70. void SetShaderModel(const ShaderModel *pSM);
  71. const ShaderModel *GetShaderModel() const;
  72. void SetValidatorVersion(unsigned ValMajor, unsigned ValMinor);
  73. void GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const;
  74. // HLOptions
  75. void SetHLOptions(HLOptions &opts);
  76. const HLOptions &GetHLOptions() const;
  77. // AutoBindingSpace also enables automatic binding for libraries if set.
  78. // UINT_MAX == unset
  79. void SetAutoBindingSpace(uint32_t Space);
  80. uint32_t GetAutoBindingSpace() const;
  81. // Entry function.
  82. llvm::Function *GetEntryFunction() const;
  83. void SetEntryFunction(llvm::Function *pEntryFunc);
  84. const std::string &GetEntryFunctionName() const;
  85. void SetEntryFunctionName(const std::string &name);
  86. llvm::Function *GetPatchConstantFunction();
  87. // Resources.
  88. unsigned AddCBuffer(std::unique_ptr<DxilCBuffer> pCB);
  89. DxilCBuffer &GetCBuffer(unsigned idx);
  90. const DxilCBuffer &GetCBuffer(unsigned idx) const;
  91. const std::vector<std::unique_ptr<DxilCBuffer> > &GetCBuffers() const;
  92. unsigned AddSampler(std::unique_ptr<DxilSampler> pSampler);
  93. DxilSampler &GetSampler(unsigned idx);
  94. const DxilSampler &GetSampler(unsigned idx) const;
  95. const std::vector<std::unique_ptr<DxilSampler> > &GetSamplers() const;
  96. unsigned AddSRV(std::unique_ptr<HLResource> pSRV);
  97. HLResource &GetSRV(unsigned idx);
  98. const HLResource &GetSRV(unsigned idx) const;
  99. const std::vector<std::unique_ptr<HLResource> > &GetSRVs() const;
  100. unsigned AddUAV(std::unique_ptr<HLResource> pUAV);
  101. HLResource &GetUAV(unsigned idx);
  102. const HLResource &GetUAV(unsigned idx) const;
  103. const std::vector<std::unique_ptr<HLResource> > &GetUAVs() const;
  104. void RemoveGlobal(llvm::GlobalVariable *GV);
  105. void RemoveFunction(llvm::Function *F);
  106. void RemoveResources(llvm::GlobalVariable **ppVariables, unsigned count);
  107. // ThreadGroupSharedMemory.
  108. typedef std::vector<llvm::GlobalVariable*>::iterator tgsm_iterator;
  109. tgsm_iterator tgsm_begin();
  110. tgsm_iterator tgsm_end();
  111. void AddGroupSharedVariable(llvm::GlobalVariable *GV);
  112. // Signatures.
  113. RootSignatureHandle &GetRootSignature();
  114. // DxilFunctionProps.
  115. bool HasDxilFunctionProps(llvm::Function *F);
  116. DxilFunctionProps &GetDxilFunctionProps(llvm::Function *F);
  117. void AddDxilFunctionProps(llvm::Function *F, std::unique_ptr<DxilFunctionProps> &info);
  118. void SetPatchConstantFunctionForHS(llvm::Function *hullShaderFunc, llvm::Function *patchConstantFunc);
  119. bool IsGraphicsShader(llvm::Function *F); // vs,hs,ds,gs,ps
  120. bool IsPatchConstantShader(llvm::Function *F);
  121. bool IsComputeShader(llvm::Function *F);
  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(llvm::Function *F);
  125. DxilFunctionAnnotation *GetFunctionAnnotation(llvm::Function *F);
  126. DxilFunctionAnnotation *AddFunctionAnnotation(llvm::Function *F);
  127. void AddResourceTypeAnnotation(llvm::Type *Ty, DXIL::ResourceClass resClass,
  128. DXIL::ResourceKind kind);
  129. DXIL::ResourceClass GetResourceClass(llvm::Type *Ty);
  130. DXIL::ResourceKind GetResourceKind(llvm::Type *Ty);
  131. // Float Denorm mode.
  132. void SetFloat32DenormMode(const DXIL::Float32DenormMode mode);
  133. DXIL::Float32DenormMode GetFloat32DenormMode() const;
  134. // Default function linkage for libraries
  135. DXIL::DefaultLinkage GetDefaultLinkage() const;
  136. void SetDefaultLinkage(const DXIL::DefaultLinkage linkage);
  137. // HLDXIR metadata manipulation.
  138. /// Serialize HLDXIR in-memory form to metadata form.
  139. void EmitHLMetadata();
  140. /// Deserialize HLDXIR metadata form into in-memory form.
  141. void LoadHLMetadata();
  142. /// Delete any HLDXIR from the specified module.
  143. static void ClearHLMetadata(llvm::Module &M);
  144. /// Create Metadata from a resource.
  145. llvm::MDNode *DxilSamplerToMDNode(const DxilSampler &S);
  146. llvm::MDNode *DxilSRVToMDNode(const DxilResource &SRV);
  147. llvm::MDNode *DxilUAVToMDNode(const DxilResource &UAV);
  148. llvm::MDNode *DxilCBufferToMDNode(const DxilCBuffer &CB);
  149. void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
  150. void AddResourceWithGlobalVariableAndMDNode(llvm::Constant *GV,
  151. llvm::MDNode *MD);
  152. // Type related methods.
  153. static bool IsStreamOutputPtrType(llvm::Type *Ty);
  154. static bool IsStreamOutputType(llvm::Type *Ty);
  155. static bool IsHLSLObjectType(llvm::Type *Ty);
  156. static void GetParameterRowsAndCols(llvm::Type *Ty, unsigned &rows, unsigned &cols,
  157. DxilParameterAnnotation &paramAnnotation);
  158. static void MergeGepUse(llvm::Value *V);
  159. // HL code gen.
  160. template<class BuilderTy>
  161. static llvm::CallInst *EmitHLOperationCall(BuilderTy &Builder,
  162. HLOpcodeGroup group, unsigned opcode,
  163. llvm::Type *RetType,
  164. llvm::ArrayRef<llvm::Value *> paramList,
  165. llvm::Module &M);
  166. static unsigned FindCastOp(bool fromUnsigned, bool toUnsigned,
  167. llvm::Type *SrcTy, llvm::Type *DstTy);
  168. // Precise attribute.
  169. // Note: Precise will be marked on alloca inst with metadata in code gen.
  170. // But mem2reg will remove alloca inst, so need mark precise with
  171. // function call before mem2reg.
  172. static bool HasPreciseAttributeWithMetadata(llvm::Instruction *I);
  173. static void MarkPreciseAttributeWithMetadata(llvm::Instruction *I);
  174. static void ClearPreciseAttributeWithMetadata(llvm::Instruction *I);
  175. static void MarkPreciseAttributeOnPtrWithFunctionCall(llvm::Value *Ptr,
  176. llvm::Module &M);
  177. static bool HasPreciseAttribute(llvm::Function *F);
  178. // Resource attribute.
  179. static void MarkDxilResourceAttrib(llvm::Function *F, llvm::MDNode *MD);
  180. static llvm::MDNode *GetDxilResourceAttrib(llvm::Function *F);
  181. void MarkDxilResourceAttrib(llvm::Argument *Arg, llvm::MDNode *MD);
  182. llvm::MDNode *GetDxilResourceAttrib(llvm::Argument *Arg);
  183. static llvm::MDNode *GetDxilResourceAttrib(llvm::Type *Ty, llvm::Module &M);
  184. // DXIL type system.
  185. DxilTypeSystem &GetTypeSystem();
  186. /// Emit llvm.used array to make sure that optimizations do not remove unreferenced globals.
  187. void EmitLLVMUsed();
  188. std::vector<llvm::GlobalVariable* > &GetLLVMUsed();
  189. // Release functions used to transfer ownership.
  190. DxilTypeSystem *ReleaseTypeSystem();
  191. OP *ReleaseOP();
  192. RootSignatureHandle *ReleaseRootSignature();
  193. DxilFunctionPropsMap &&ReleaseFunctionPropsMap();
  194. llvm::DebugInfoFinder &GetOrCreateDebugInfoFinder();
  195. static llvm::DIGlobalVariable *
  196. FindGlobalVariableDebugInfo(llvm::GlobalVariable *GV,
  197. llvm::DebugInfoFinder &DbgInfoFinder);
  198. // Create global variable debug info for element global variable based on the
  199. // whole global variable.
  200. static void CreateElementGlobalVariableDebugInfo(
  201. llvm::GlobalVariable *GV, llvm::DebugInfoFinder &DbgInfoFinder,
  202. llvm::GlobalVariable *EltGV, unsigned sizeInBits, unsigned alignInBits,
  203. unsigned offsetInBits, llvm::StringRef eltName);
  204. // Replace GV with NewGV in GlobalVariable debug info.
  205. static void
  206. UpdateGlobalVariableDebugInfo(llvm::GlobalVariable *GV,
  207. llvm::DebugInfoFinder &DbgInfoFinder,
  208. llvm::GlobalVariable *NewGV);
  209. private:
  210. // Signatures.
  211. std::unique_ptr<RootSignatureHandle> m_RootSignature;
  212. // Shader resources.
  213. std::vector<std::unique_ptr<HLResource> > m_SRVs;
  214. std::vector<std::unique_ptr<HLResource> > m_UAVs;
  215. std::vector<std::unique_ptr<DxilCBuffer> > m_CBuffers;
  216. std::vector<std::unique_ptr<DxilSampler> > m_Samplers;
  217. // ThreadGroupSharedMemory.
  218. std::vector<llvm::GlobalVariable*> m_TGSMVariables;
  219. // High level function info.
  220. std::unordered_map<const llvm::Function *, std::unique_ptr<DxilFunctionProps>> m_DxilFunctionPropsMap;
  221. std::unordered_set<llvm::Function *> m_PatchConstantFunctions;
  222. // Resource type annotation.
  223. std::unordered_map<llvm::Type *, std::pair<DXIL::ResourceClass, DXIL::ResourceKind>> m_ResTypeAnnotation;
  224. private:
  225. llvm::LLVMContext &m_Ctx;
  226. llvm::Module *m_pModule;
  227. llvm::Function *m_pEntryFunc;
  228. std::string m_EntryName;
  229. std::unique_ptr<DxilMDHelper> m_pMDHelper;
  230. std::unique_ptr<llvm::DebugInfoFinder> m_pDebugInfoFinder;
  231. const ShaderModel *m_pSM;
  232. unsigned m_DxilMajor;
  233. unsigned m_DxilMinor;
  234. unsigned m_ValMajor;
  235. unsigned m_ValMinor;
  236. DXIL::Float32DenormMode m_Float32DenormMode;
  237. HLOptions m_Options;
  238. std::unique_ptr<OP> m_pOP;
  239. size_t m_pUnused;
  240. uint32_t m_AutoBindingSpace;
  241. DXIL::DefaultLinkage m_DefaultLinkage;
  242. // DXIL metadata serialization/deserialization.
  243. llvm::MDTuple *EmitHLResources();
  244. void LoadHLResources(const llvm::MDOperand &MDO);
  245. llvm::MDTuple *EmitHLShaderProperties();
  246. void LoadHLShaderProperties(const llvm::MDOperand &MDO);
  247. llvm::MDTuple *EmitDxilShaderProperties();
  248. llvm::MDTuple *EmitResTyAnnotations();
  249. void LoadResTyAnnotations(const llvm::MDOperand &MDO);
  250. // LLVM used.
  251. std::vector<llvm::GlobalVariable*> m_LLVMUsed;
  252. // Type annotations.
  253. std::unique_ptr<DxilTypeSystem> m_pTypeSystem;
  254. // Helpers.
  255. template<typename T> unsigned AddResource(std::vector<std::unique_ptr<T> > &Vec, std::unique_ptr<T> pRes);
  256. };
  257. /// Use this class to manipulate metadata of extra metadata record properties that are specific to high-level DX IR.
  258. class HLExtraPropertyHelper : public DxilExtraPropertyHelper {
  259. public:
  260. HLExtraPropertyHelper(llvm::Module *pModule);
  261. virtual ~HLExtraPropertyHelper() {}
  262. virtual void EmitSignatureElementProperties(const DxilSignatureElement &SE, std::vector<llvm::Metadata *> &MDVals);
  263. virtual void LoadSignatureElementProperties(const llvm::MDOperand &MDO, DxilSignatureElement &SE);
  264. };
  265. } // namespace hlsl