HLModule.h 12 KB

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