HLModule.h 11 KB

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