HLModule.h 13 KB

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