DxilModule.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilModule.cpp //
  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. ///////////////////////////////////////////////////////////////////////////////
  9. #include "dxc/Support/Global.h"
  10. #include "dxc/DXIL/DxilOperations.h"
  11. #include "dxc/DXIL/DxilModule.h"
  12. #include "dxc/DXIL/DxilConstants.h"
  13. #include "dxc/DXIL/DxilShaderModel.h"
  14. #include "dxc/DXIL/DxilSignatureElement.h"
  15. #include "dxc/DXIL/DxilFunctionProps.h"
  16. #include "dxc/Support/WinAdapter.h"
  17. #include "dxc/DXIL/DxilEntryProps.h"
  18. #include "dxc/DXIL/DxilSubobject.h"
  19. #include "dxc/DXIL/DxilInstructions.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/IR/Instructions.h"
  23. #include "llvm/IR/LLVMContext.h"
  24. #include "llvm/IR/Metadata.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/IR/Operator.h"
  27. #include "llvm/IR/DebugInfo.h"
  28. #include "llvm/IR/DiagnosticInfo.h"
  29. #include "llvm/IR/DiagnosticPrinter.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/ADT/STLExtras.h"
  32. #include <unordered_set>
  33. using namespace llvm;
  34. using std::string;
  35. using std::vector;
  36. using std::unique_ptr;
  37. namespace {
  38. class DxilErrorDiagnosticInfo : public DiagnosticInfo {
  39. private:
  40. const char *m_message;
  41. public:
  42. DxilErrorDiagnosticInfo(const char *str)
  43. : DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
  44. m_message(str) { }
  45. void print(DiagnosticPrinter &DP) const override {
  46. DP << m_message;
  47. }
  48. };
  49. } // anon namespace
  50. namespace hlsl {
  51. namespace DXIL {
  52. // Define constant variables exposed in DxilConstants.h
  53. // TODO: revisit data layout descriptions for the following:
  54. // - x64 pointers?
  55. // - Keep elf manging(m:e)?
  56. // For legacy data layout, everything less than 32 align to 32.
  57. const char* kLegacyLayoutString = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64";
  58. // New data layout with native low precision types
  59. const char* kNewLayoutString = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64";
  60. // Function Attributes
  61. // TODO: consider generating attributes from hctdb
  62. const char* kFP32DenormKindString = "fp32-denorm-mode";
  63. const char* kFP32DenormValueAnyString = "any";
  64. const char* kFP32DenormValuePreserveString = "preserve";
  65. const char* kFP32DenormValueFtzString = "ftz";
  66. }
  67. // Avoid dependency on DxilModule from llvm::Module using this:
  68. void DxilModule_RemoveGlobal(llvm::Module* M, llvm::GlobalObject* G) {
  69. if (M && G && M->HasDxilModule()) {
  70. if (llvm::Function *F = dyn_cast<llvm::Function>(G))
  71. M->GetDxilModule().RemoveFunction(F);
  72. }
  73. }
  74. void DxilModule_ResetModule(llvm::Module* M) {
  75. if (M && M->HasDxilModule())
  76. delete &M->GetDxilModule();
  77. M->SetDxilModule(nullptr);
  78. }
  79. //------------------------------------------------------------------------------
  80. //
  81. // DxilModule methods.
  82. //
  83. DxilModule::DxilModule(Module *pModule)
  84. : m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
  85. , m_ActiveStreamMask(0)
  86. , m_Ctx(pModule->getContext())
  87. , m_pModule(pModule)
  88. , m_pEntryFunc(nullptr)
  89. , m_EntryName("")
  90. , m_pMDHelper(llvm::make_unique<DxilMDHelper>(pModule, llvm::make_unique<DxilExtraPropertyHelper>(pModule)))
  91. , m_pDebugInfoFinder(nullptr)
  92. , m_pSM(nullptr)
  93. , m_DxilMajor(DXIL::kDxilMajor)
  94. , m_DxilMinor(DXIL::kDxilMinor)
  95. , m_ValMajor(1)
  96. , m_ValMinor(0)
  97. , m_pOP(llvm::make_unique<OP>(pModule->getContext(), pModule))
  98. , m_pTypeSystem(llvm::make_unique<DxilTypeSystem>(pModule))
  99. , m_bDisableOptimizations(false)
  100. , m_bUseMinPrecision(true) // use min precision by default
  101. , m_bAllResourcesBound(false)
  102. , m_IntermediateFlags(0)
  103. , m_AutoBindingSpace(UINT_MAX)
  104. , m_pSubobjects(nullptr)
  105. {
  106. DXASSERT_NOMSG(m_pModule != nullptr);
  107. m_pModule->pfnRemoveGlobal = &DxilModule_RemoveGlobal;
  108. m_pModule->pfnResetDxilModule = &DxilModule_ResetModule;
  109. #if defined(_DEBUG) || defined(DBG)
  110. // Pin LLVM dump methods.
  111. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  112. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  113. void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
  114. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  115. m_pUnused -= (size_t)&pfnViewCFGOnly;
  116. #endif
  117. }
  118. DxilModule::~DxilModule() {
  119. if (m_pModule->pfnRemoveGlobal == &DxilModule_RemoveGlobal)
  120. m_pModule->pfnRemoveGlobal = nullptr;
  121. }
  122. LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
  123. Module *DxilModule::GetModule() const { return m_pModule; }
  124. OP *DxilModule::GetOP() const { return m_pOP.get(); }
  125. void DxilModule::SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision) {
  126. DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM), "shader model must not change for the module");
  127. DXASSERT(pSM != nullptr && pSM->IsValidForDxil(), "shader model must be valid");
  128. DXASSERT(pSM->IsValidForModule(), "shader model must be valid for top-level module use");
  129. m_pSM = pSM;
  130. m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
  131. m_pMDHelper->SetShaderModel(m_pSM);
  132. m_bUseMinPrecision = bUseMinPrecision;
  133. m_pOP->SetMinPrecision(m_bUseMinPrecision);
  134. m_pTypeSystem->SetMinPrecision(m_bUseMinPrecision);
  135. if (!m_pSM->IsLib()) {
  136. // Always have valid entry props for non-lib case from this point on.
  137. DxilFunctionProps props;
  138. props.shaderKind = m_pSM->GetKind();
  139. m_DxilEntryPropsMap[nullptr] =
  140. llvm::make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
  141. }
  142. m_SerializedRootSignature.clear();
  143. }
  144. const ShaderModel *DxilModule::GetShaderModel() const {
  145. return m_pSM;
  146. }
  147. void DxilModule::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  148. DxilMajor = m_DxilMajor;
  149. DxilMinor = m_DxilMinor;
  150. }
  151. void DxilModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  152. m_ValMajor = ValMajor;
  153. m_ValMinor = ValMinor;
  154. }
  155. bool DxilModule::UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  156. // Don't upgrade if validation was disabled.
  157. if (m_ValMajor == 0 && m_ValMinor == 0) {
  158. return false;
  159. }
  160. if (ValMajor > m_ValMajor || (ValMajor == m_ValMajor && ValMinor > m_ValMinor)) {
  161. // Module requires higher validator version than previously set
  162. SetValidatorVersion(ValMajor, ValMinor);
  163. return true;
  164. }
  165. return false;
  166. }
  167. void DxilModule::GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  168. ValMajor = m_ValMajor;
  169. ValMinor = m_ValMinor;
  170. }
  171. bool DxilModule::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  172. if (!m_pSM)
  173. return false;
  174. m_pSM->GetMinValidatorVersion(ValMajor, ValMinor);
  175. if (ValMajor == 1 && ValMinor == 0 &&
  176. (m_ShaderFlags.GetFeatureInfo() & hlsl::DXIL::ShaderFeatureInfo_ViewID))
  177. ValMinor = 1;
  178. return true;
  179. }
  180. bool DxilModule::UpgradeToMinValidatorVersion() {
  181. unsigned ValMajor = 1, ValMinor = 0;
  182. if (GetMinValidatorVersion(ValMajor, ValMinor)) {
  183. return UpgradeValidatorVersion(ValMajor, ValMinor);
  184. }
  185. return false;
  186. }
  187. Function *DxilModule::GetEntryFunction() {
  188. return m_pEntryFunc;
  189. }
  190. const Function *DxilModule::GetEntryFunction() const {
  191. return m_pEntryFunc;
  192. }
  193. void DxilModule::SetEntryFunction(Function *pEntryFunc) {
  194. if (m_pSM->IsLib()) {
  195. DXASSERT(pEntryFunc == nullptr,
  196. "Otherwise, trying to set an entry function on library");
  197. m_pEntryFunc = nullptr;
  198. return;
  199. }
  200. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  201. m_pEntryFunc = pEntryFunc;
  202. // Move entry props to new function in order to preserve them.
  203. std::unique_ptr<DxilEntryProps> Props = std::move(m_DxilEntryPropsMap.begin()->second);
  204. m_DxilEntryPropsMap.clear();
  205. m_DxilEntryPropsMap[m_pEntryFunc] = std::move(Props);
  206. }
  207. const string &DxilModule::GetEntryFunctionName() const {
  208. return m_EntryName;
  209. }
  210. void DxilModule::SetEntryFunctionName(const string &name) {
  211. m_EntryName = name;
  212. }
  213. llvm::Function *DxilModule::GetPatchConstantFunction() {
  214. if (!m_pSM->IsHS())
  215. return nullptr;
  216. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  217. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  218. DXASSERT(props.IsHS(), "Must be HS profile");
  219. return props.ShaderProps.HS.patchConstantFunc;
  220. }
  221. const llvm::Function *DxilModule::GetPatchConstantFunction() const {
  222. if (!m_pSM->IsHS())
  223. return nullptr;
  224. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  225. const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  226. DXASSERT(props.IsHS(), "Must be HS profile");
  227. return props.ShaderProps.HS.patchConstantFunc;
  228. }
  229. void DxilModule::SetPatchConstantFunction(llvm::Function *patchConstantFunc) {
  230. if (!m_pSM->IsHS())
  231. return;
  232. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  233. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  234. DXASSERT(props.IsHS(), "Must be HS profile");
  235. auto &HS = props.ShaderProps.HS;
  236. if (HS.patchConstantFunc != patchConstantFunc) {
  237. if (HS.patchConstantFunc)
  238. m_PatchConstantFunctions.erase(HS.patchConstantFunc);
  239. HS.patchConstantFunc = patchConstantFunc;
  240. if (patchConstantFunc)
  241. m_PatchConstantFunctions.insert(patchConstantFunc);
  242. }
  243. }
  244. bool DxilModule::IsEntryOrPatchConstantFunction(const llvm::Function* pFunc) const {
  245. return pFunc == GetEntryFunction() || pFunc == GetPatchConstantFunction();
  246. }
  247. unsigned DxilModule::GetGlobalFlags() const {
  248. unsigned Flags = m_ShaderFlags.GetGlobalFlags();
  249. return Flags;
  250. }
  251. void DxilModule::CollectShaderFlagsForModule(ShaderFlags &Flags) {
  252. for (Function &F : GetModule()->functions()) {
  253. ShaderFlags funcFlags = ShaderFlags::CollectShaderFlags(&F, this);
  254. Flags.CombineShaderFlags(funcFlags);
  255. };
  256. const ShaderModel *SM = GetShaderModel();
  257. unsigned NumUAVs = m_UAVs.size();
  258. const unsigned kSmallUAVCount = 8;
  259. if (NumUAVs > kSmallUAVCount)
  260. Flags.Set64UAVs(true);
  261. if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
  262. Flags.SetUAVsAtEveryStage(true);
  263. bool hasRawAndStructuredBuffer = false;
  264. for (auto &UAV : m_UAVs) {
  265. if (UAV->IsROV())
  266. Flags.SetROVs(true);
  267. switch (UAV->GetKind()) {
  268. case DXIL::ResourceKind::RawBuffer:
  269. case DXIL::ResourceKind::StructuredBuffer:
  270. hasRawAndStructuredBuffer = true;
  271. break;
  272. default:
  273. // Not raw/structured.
  274. break;
  275. }
  276. }
  277. for (auto &SRV : m_SRVs) {
  278. switch (SRV->GetKind()) {
  279. case DXIL::ResourceKind::RawBuffer:
  280. case DXIL::ResourceKind::StructuredBuffer:
  281. hasRawAndStructuredBuffer = true;
  282. break;
  283. default:
  284. // Not raw/structured.
  285. break;
  286. }
  287. }
  288. Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
  289. bool hasCSRawAndStructuredViaShader4X =
  290. hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
  291. Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
  292. }
  293. void DxilModule::CollectShaderFlagsForModule() {
  294. CollectShaderFlagsForModule(m_ShaderFlags);
  295. // This is also where we record the size of the mesh payload for amplification shader output
  296. for (Function &F : GetModule()->functions()) {
  297. if (HasDxilEntryProps(&F)) {
  298. DxilFunctionProps &props = GetDxilFunctionProps(&F);
  299. if (props.shaderKind == DXIL::ShaderKind::Amplification) {
  300. if (props.ShaderProps.AS.payloadSizeInBytes != 0)
  301. continue;
  302. for (const BasicBlock &BB : F.getBasicBlockList()) {
  303. for (const Instruction &I : BB.getInstList()) {
  304. const DxilInst_DispatchMesh dispatch(const_cast<Instruction*>(&I));
  305. if (dispatch) {
  306. Type *payloadTy = dispatch.get_payload()->getType();
  307. const DataLayout &DL = m_pModule->getDataLayout();
  308. props.ShaderProps.AS.payloadSizeInBytes = DL.getTypeAllocSize(payloadTy);
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. void DxilModule::SetNumThreads(unsigned x, unsigned y, unsigned z) {
  317. DXASSERT(m_DxilEntryPropsMap.size() == 1 &&
  318. (m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()),
  319. "only works for CS/MS/AS profiles");
  320. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  321. DXASSERT_NOMSG(m_pSM->GetKind() == props.shaderKind);
  322. unsigned *numThreads = props.IsCS() ? props.ShaderProps.CS.numThreads :
  323. props.IsMS() ? props.ShaderProps.MS.numThreads : props.ShaderProps.AS.numThreads;
  324. numThreads[0] = x;
  325. numThreads[1] = y;
  326. numThreads[2] = z;
  327. }
  328. unsigned DxilModule::GetNumThreads(unsigned idx) const {
  329. DXASSERT(m_DxilEntryPropsMap.size() == 1 &&
  330. (m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()),
  331. "only works for CS/MS/AS profiles");
  332. DXASSERT(idx < 3, "Thread dimension index must be 0-2");
  333. __analysis_assume(idx < 3);
  334. if (!(m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()))
  335. return 0;
  336. const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  337. DXASSERT_NOMSG(m_pSM->GetKind() == props.shaderKind);
  338. const unsigned *numThreads = props.IsCS() ? props.ShaderProps.CS.numThreads :
  339. props.IsMS() ? props.ShaderProps.MS.numThreads : props.ShaderProps.AS.numThreads;
  340. return numThreads[idx];
  341. }
  342. DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
  343. if (!m_pSM->IsGS())
  344. return DXIL::InputPrimitive::Undefined;
  345. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  346. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  347. DXASSERT(props.IsGS(), "Must be GS profile");
  348. return props.ShaderProps.GS.inputPrimitive;
  349. }
  350. void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
  351. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  352. "only works for GS profile");
  353. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  354. DXASSERT(props.IsGS(), "Must be GS profile");
  355. auto &GS = props.ShaderProps.GS;
  356. DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
  357. GS.inputPrimitive = IP;
  358. }
  359. unsigned DxilModule::GetMaxVertexCount() const {
  360. if (!m_pSM->IsGS())
  361. return 0;
  362. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  363. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  364. DXASSERT(props.IsGS(), "Must be GS profile");
  365. auto &GS = props.ShaderProps.GS;
  366. DXASSERT_NOMSG(GS.maxVertexCount != 0);
  367. return GS.maxVertexCount;
  368. }
  369. void DxilModule::SetMaxVertexCount(unsigned Count) {
  370. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  371. "only works for GS profile");
  372. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  373. DXASSERT(props.IsGS(), "Must be GS profile");
  374. auto &GS = props.ShaderProps.GS;
  375. GS.maxVertexCount = Count;
  376. }
  377. DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
  378. return m_StreamPrimitiveTopology;
  379. }
  380. void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
  381. m_StreamPrimitiveTopology = Topology;
  382. SetActiveStreamMask(m_ActiveStreamMask); // Update props
  383. }
  384. bool DxilModule::HasMultipleOutputStreams() const {
  385. if (!m_pSM->IsGS()) {
  386. return false;
  387. } else {
  388. unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
  389. ((m_ActiveStreamMask & 0x2) >> 1) +
  390. ((m_ActiveStreamMask & 0x4) >> 2) +
  391. ((m_ActiveStreamMask & 0x8) >> 3);
  392. DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
  393. return NumStreams > 1;
  394. }
  395. }
  396. unsigned DxilModule::GetOutputStream() const {
  397. if (!m_pSM->IsGS()) {
  398. return 0;
  399. } else {
  400. DXASSERT_NOMSG(!HasMultipleOutputStreams());
  401. switch (m_ActiveStreamMask) {
  402. case 0x1: return 0;
  403. case 0x2: return 1;
  404. case 0x4: return 2;
  405. case 0x8: return 3;
  406. default: DXASSERT_NOMSG(false);
  407. }
  408. return (unsigned)(-1);
  409. }
  410. }
  411. unsigned DxilModule::GetGSInstanceCount() const {
  412. if (!m_pSM->IsGS())
  413. return 0;
  414. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  415. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  416. DXASSERT(props.IsGS(), "Must be GS profile");
  417. return props.ShaderProps.GS.instanceCount;
  418. }
  419. void DxilModule::SetGSInstanceCount(unsigned Count) {
  420. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  421. "only works for GS profile");
  422. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  423. DXASSERT(props.IsGS(), "Must be GS profile");
  424. props.ShaderProps.GS.instanceCount = Count;
  425. }
  426. bool DxilModule::IsStreamActive(unsigned Stream) const {
  427. return (m_ActiveStreamMask & (1<<Stream)) != 0;
  428. }
  429. void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
  430. if (bActive) {
  431. m_ActiveStreamMask |= (1<<Stream);
  432. } else {
  433. m_ActiveStreamMask &= ~(1<<Stream);
  434. }
  435. SetActiveStreamMask(m_ActiveStreamMask);
  436. }
  437. void DxilModule::SetActiveStreamMask(unsigned Mask) {
  438. m_ActiveStreamMask = Mask;
  439. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  440. "only works for GS profile");
  441. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  442. DXASSERT(props.IsGS(), "Must be GS profile");
  443. for (unsigned i = 0; i < 4; i++) {
  444. if (IsStreamActive(i))
  445. props.ShaderProps.GS.streamPrimitiveTopologies[i] = m_StreamPrimitiveTopology;
  446. else
  447. props.ShaderProps.GS.streamPrimitiveTopologies[i] = DXIL::PrimitiveTopology::Undefined;
  448. }
  449. }
  450. unsigned DxilModule::GetActiveStreamMask() const {
  451. return m_ActiveStreamMask;
  452. }
  453. bool DxilModule::GetUseMinPrecision() const {
  454. return m_bUseMinPrecision;
  455. }
  456. void DxilModule::SetDisableOptimization(bool DisableOptimization) {
  457. m_bDisableOptimizations = DisableOptimization;
  458. }
  459. bool DxilModule::GetDisableOptimization() const {
  460. return m_bDisableOptimizations;
  461. }
  462. void DxilModule::SetAllResourcesBound(bool ResourcesBound) {
  463. m_bAllResourcesBound = ResourcesBound;
  464. }
  465. bool DxilModule::GetAllResourcesBound() const {
  466. return m_bAllResourcesBound;
  467. }
  468. void DxilModule::SetLegacyResourceReservation(bool legacyResourceReservation) {
  469. m_IntermediateFlags &= ~LegacyResourceReservation;
  470. if (legacyResourceReservation) m_IntermediateFlags |= LegacyResourceReservation;
  471. }
  472. bool DxilModule::GetLegacyResourceReservation() const {
  473. return (m_IntermediateFlags & LegacyResourceReservation) != 0;
  474. }
  475. void DxilModule::ClearIntermediateOptions() {
  476. m_IntermediateFlags = 0;
  477. }
  478. unsigned DxilModule::GetInputControlPointCount() const {
  479. if (!(m_pSM->IsHS() || m_pSM->IsDS()))
  480. return 0;
  481. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  482. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  483. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  484. if (props.IsHS())
  485. return props.ShaderProps.HS.inputControlPoints;
  486. else
  487. return props.ShaderProps.DS.inputControlPoints;
  488. }
  489. void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
  490. DXASSERT(m_DxilEntryPropsMap.size() == 1
  491. && (m_pSM->IsHS() || m_pSM->IsDS()),
  492. "only works for non-lib profile");
  493. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  494. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  495. if (props.IsHS())
  496. props.ShaderProps.HS.inputControlPoints = NumICPs;
  497. else
  498. props.ShaderProps.DS.inputControlPoints = NumICPs;
  499. }
  500. DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
  501. if (!(m_pSM->IsHS() || m_pSM->IsDS()))
  502. return DXIL::TessellatorDomain::Undefined;
  503. DXASSERT_NOMSG(m_DxilEntryPropsMap.size() == 1);
  504. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  505. if (props.IsHS())
  506. return props.ShaderProps.HS.domain;
  507. else
  508. return props.ShaderProps.DS.domain;
  509. }
  510. void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
  511. DXASSERT(m_DxilEntryPropsMap.size() == 1
  512. && (m_pSM->IsHS() || m_pSM->IsDS()),
  513. "only works for HS or DS profile");
  514. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  515. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  516. if (props.IsHS())
  517. props.ShaderProps.HS.domain = TessDomain;
  518. else
  519. props.ShaderProps.DS.domain = TessDomain;
  520. }
  521. unsigned DxilModule::GetOutputControlPointCount() const {
  522. if (!m_pSM->IsHS())
  523. return 0;
  524. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  525. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  526. DXASSERT(props.IsHS(), "Must be HS profile");
  527. return props.ShaderProps.HS.outputControlPoints;
  528. }
  529. void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
  530. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  531. "only works for HS profile");
  532. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  533. DXASSERT(props.IsHS(), "Must be HS profile");
  534. props.ShaderProps.HS.outputControlPoints = NumOCPs;
  535. }
  536. DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
  537. if (!m_pSM->IsHS())
  538. return DXIL::TessellatorPartitioning::Undefined;
  539. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  540. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  541. DXASSERT(props.IsHS(), "Must be HS profile");
  542. return props.ShaderProps.HS.partition;
  543. }
  544. void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
  545. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  546. "only works for HS profile");
  547. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  548. DXASSERT(props.IsHS(), "Must be HS profile");
  549. props.ShaderProps.HS.partition = TessPartitioning;
  550. }
  551. DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
  552. if (!m_pSM->IsHS())
  553. return DXIL::TessellatorOutputPrimitive::Undefined;
  554. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  555. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  556. DXASSERT(props.IsHS(), "Must be HS profile");
  557. return props.ShaderProps.HS.outputPrimitive;
  558. }
  559. void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
  560. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  561. "only works for HS profile");
  562. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  563. DXASSERT(props.IsHS(), "Must be HS profile");
  564. props.ShaderProps.HS.outputPrimitive = TessOutputPrimitive;
  565. }
  566. float DxilModule::GetMaxTessellationFactor() const {
  567. if (!m_pSM->IsHS())
  568. return 0.0F;
  569. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  570. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  571. DXASSERT(props.IsHS(), "Must be HS profile");
  572. return props.ShaderProps.HS.maxTessFactor;
  573. }
  574. void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
  575. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  576. "only works for HS profile");
  577. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  578. DXASSERT(props.IsHS(), "Must be HS profile");
  579. props.ShaderProps.HS.maxTessFactor = MaxTessellationFactor;
  580. }
  581. unsigned DxilModule::GetMaxOutputVertices() const {
  582. if (!m_pSM->IsMS())
  583. return 0;
  584. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  585. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  586. DXASSERT(props.IsMS(), "Must be MS profile");
  587. return props.ShaderProps.MS.maxVertexCount;
  588. }
  589. void DxilModule::SetMaxOutputVertices(unsigned NumOVs) {
  590. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
  591. "only works for MS profile");
  592. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  593. DXASSERT(props.IsMS(), "Must be MS profile");
  594. props.ShaderProps.MS.maxVertexCount = NumOVs;
  595. }
  596. unsigned DxilModule::GetMaxOutputPrimitives() const {
  597. if (!m_pSM->IsMS())
  598. return 0;
  599. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  600. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  601. DXASSERT(props.IsMS(), "Must be MS profile");
  602. return props.ShaderProps.MS.maxPrimitiveCount;
  603. }
  604. void DxilModule::SetMaxOutputPrimitives(unsigned NumOPs) {
  605. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
  606. "only works for MS profile");
  607. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  608. DXASSERT(props.IsMS(), "Must be MS profile");
  609. props.ShaderProps.MS.maxPrimitiveCount = NumOPs;
  610. }
  611. DXIL::MeshOutputTopology DxilModule::GetMeshOutputTopology() const {
  612. if (!m_pSM->IsMS())
  613. return DXIL::MeshOutputTopology::Undefined;
  614. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  615. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  616. DXASSERT(props.IsMS(), "Must be MS profile");
  617. return props.ShaderProps.MS.outputTopology;
  618. }
  619. void DxilModule::SetMeshOutputTopology(DXIL::MeshOutputTopology MeshOutputTopology) {
  620. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
  621. "only works for MS profile");
  622. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  623. DXASSERT(props.IsMS(), "Must be MS profile");
  624. props.ShaderProps.MS.outputTopology = MeshOutputTopology;
  625. }
  626. unsigned DxilModule::GetPayloadSizeInBytes() const {
  627. if (m_pSM->IsMS())
  628. {
  629. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  630. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  631. DXASSERT(props.IsMS(), "Must be MS profile");
  632. return props.ShaderProps.MS.payloadSizeInBytes;
  633. }
  634. else if(m_pSM->IsAS())
  635. {
  636. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  637. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  638. DXASSERT(props.IsAS(), "Must be AS profile");
  639. return props.ShaderProps.AS.payloadSizeInBytes;
  640. }
  641. else
  642. {
  643. return 0;
  644. }
  645. }
  646. void DxilModule::SetPayloadSizeInBytes(unsigned Size) {
  647. DXASSERT(m_DxilEntryPropsMap.size() == 1 && (m_pSM->IsMS() || m_pSM->IsAS()),
  648. "only works for MS or AS profile");
  649. if (m_pSM->IsMS())
  650. {
  651. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  652. DXASSERT(props.IsMS(), "Must be MS profile");
  653. props.ShaderProps.MS.payloadSizeInBytes = Size;
  654. }
  655. else if (m_pSM->IsAS())
  656. {
  657. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  658. DXASSERT(props.IsAS(), "Must be AS profile");
  659. props.ShaderProps.AS.payloadSizeInBytes = Size;
  660. }
  661. }
  662. void DxilModule::SetAutoBindingSpace(uint32_t Space) {
  663. m_AutoBindingSpace = Space;
  664. }
  665. uint32_t DxilModule::GetAutoBindingSpace() const {
  666. return m_AutoBindingSpace;
  667. }
  668. void DxilModule::SetShaderProperties(DxilFunctionProps *props) {
  669. if (!props)
  670. return;
  671. DxilFunctionProps &ourProps = GetDxilFunctionProps(GetEntryFunction());
  672. if (props != &ourProps) {
  673. ourProps.shaderKind = props->shaderKind;
  674. ourProps.ShaderProps = props->ShaderProps;
  675. }
  676. switch (props->shaderKind) {
  677. case DXIL::ShaderKind::Pixel: {
  678. auto &PS = props->ShaderProps.PS;
  679. m_ShaderFlags.SetForceEarlyDepthStencil(PS.EarlyDepthStencil);
  680. } break;
  681. case DXIL::ShaderKind::Compute:
  682. case DXIL::ShaderKind::Domain:
  683. case DXIL::ShaderKind::Hull:
  684. case DXIL::ShaderKind::Vertex:
  685. case DXIL::ShaderKind::Mesh:
  686. case DXIL::ShaderKind::Amplification:
  687. break;
  688. default: {
  689. DXASSERT(props->shaderKind == DXIL::ShaderKind::Geometry,
  690. "else invalid shader kind");
  691. auto &GS = props->ShaderProps.GS;
  692. m_ActiveStreamMask = 0;
  693. for (size_t i = 0; i < _countof(GS.streamPrimitiveTopologies); ++i) {
  694. if (GS.streamPrimitiveTopologies[i] !=
  695. DXIL::PrimitiveTopology::Undefined) {
  696. m_ActiveStreamMask |= (1 << i);
  697. DXASSERT_NOMSG(m_StreamPrimitiveTopology ==
  698. DXIL::PrimitiveTopology::Undefined ||
  699. m_StreamPrimitiveTopology ==
  700. GS.streamPrimitiveTopologies[i]);
  701. m_StreamPrimitiveTopology = GS.streamPrimitiveTopologies[i];
  702. }
  703. }
  704. // Refresh props:
  705. SetActiveStreamMask(m_ActiveStreamMask);
  706. } break;
  707. }
  708. }
  709. template<typename T> unsigned
  710. DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  711. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  712. unsigned Id = (unsigned)Vec.size();
  713. Vec.emplace_back(std::move(pRes));
  714. return Id;
  715. }
  716. unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
  717. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
  718. }
  719. DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
  720. return *m_CBuffers[idx];
  721. }
  722. const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
  723. return *m_CBuffers[idx];
  724. }
  725. const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
  726. return m_CBuffers;
  727. }
  728. unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  729. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  730. }
  731. DxilSampler &DxilModule::GetSampler(unsigned idx) {
  732. return *m_Samplers[idx];
  733. }
  734. const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
  735. return *m_Samplers[idx];
  736. }
  737. const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
  738. return m_Samplers;
  739. }
  740. unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
  741. return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
  742. }
  743. DxilResource &DxilModule::GetSRV(unsigned idx) {
  744. return *m_SRVs[idx];
  745. }
  746. const DxilResource &DxilModule::GetSRV(unsigned idx) const {
  747. return *m_SRVs[idx];
  748. }
  749. const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
  750. return m_SRVs;
  751. }
  752. unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
  753. return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
  754. }
  755. DxilResource &DxilModule::GetUAV(unsigned idx) {
  756. return *m_UAVs[idx];
  757. }
  758. const DxilResource &DxilModule::GetUAV(unsigned idx) const {
  759. return *m_UAVs[idx];
  760. }
  761. const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
  762. return m_UAVs;
  763. }
  764. void DxilModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
  765. return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
  766. }
  767. void DxilModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
  768. return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
  769. }
  770. void DxilModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
  771. return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
  772. }
  773. template <typename TResource>
  774. static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
  775. std::unordered_set<unsigned> &immResID) {
  776. for (auto p = vec.begin(); p != vec.end();) {
  777. auto c = p++;
  778. if (immResID.count((*c)->GetID()) == 0) {
  779. p = vec.erase(c);
  780. }
  781. }
  782. }
  783. static void CollectUsedResource(Value *resID,
  784. std::unordered_set<Value *> &usedResID) {
  785. if (usedResID.count(resID) > 0)
  786. return;
  787. usedResID.insert(resID);
  788. if (dyn_cast<ConstantInt>(resID)) {
  789. // Do nothing
  790. } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
  791. if (ZEI->getSrcTy()->isIntegerTy()) {
  792. IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
  793. if (ITy->getBitWidth() == 1) {
  794. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
  795. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
  796. }
  797. }
  798. } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
  799. CollectUsedResource(SI->getTrueValue(), usedResID);
  800. CollectUsedResource(SI->getFalseValue(), usedResID);
  801. } else if (PHINode *Phi = dyn_cast<PHINode>(resID)) {
  802. for (Use &U : Phi->incoming_values()) {
  803. CollectUsedResource(U.get(), usedResID);
  804. }
  805. }
  806. // TODO: resID could be other types of instructions depending on the compiler optimization.
  807. }
  808. static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
  809. std::unordered_set<Value *> &usedResID) {
  810. for (Value *V : usedResID) {
  811. if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
  812. immResID.insert(cResID->getLimitedValue());
  813. }
  814. }
  815. }
  816. void DxilModule::RemoveFunction(llvm::Function *F) {
  817. DXASSERT_NOMSG(F != nullptr);
  818. m_DxilEntryPropsMap.erase(F);
  819. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  820. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  821. m_pOP->RemoveFunction(F);
  822. }
  823. void DxilModule::RemoveUnusedResources() {
  824. DXASSERT(!m_pSM->IsLib(), "this function does not work on libraries");
  825. hlsl::OP *hlslOP = GetOP();
  826. Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
  827. if (createHandleFunc->user_empty()) {
  828. m_CBuffers.clear();
  829. m_UAVs.clear();
  830. m_SRVs.clear();
  831. m_Samplers.clear();
  832. createHandleFunc->eraseFromParent();
  833. return;
  834. }
  835. std::unordered_set<Value *> usedUAVID;
  836. std::unordered_set<Value *> usedSRVID;
  837. std::unordered_set<Value *> usedSamplerID;
  838. std::unordered_set<Value *> usedCBufID;
  839. // Collect used ID.
  840. for (User *U : createHandleFunc->users()) {
  841. CallInst *CI = cast<CallInst>(U);
  842. Value *vResClass =
  843. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  844. ConstantInt *cResClass = cast<ConstantInt>(vResClass);
  845. DXIL::ResourceClass resClass =
  846. static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
  847. // Skip unused resource handle.
  848. if (CI->user_empty())
  849. continue;
  850. Value *resID =
  851. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  852. switch (resClass) {
  853. case DXIL::ResourceClass::CBuffer:
  854. CollectUsedResource(resID, usedCBufID);
  855. break;
  856. case DXIL::ResourceClass::Sampler:
  857. CollectUsedResource(resID, usedSamplerID);
  858. break;
  859. case DXIL::ResourceClass::SRV:
  860. CollectUsedResource(resID, usedSRVID);
  861. break;
  862. case DXIL::ResourceClass::UAV:
  863. CollectUsedResource(resID, usedUAVID);
  864. break;
  865. default:
  866. DXASSERT(0, "invalid res class");
  867. break;
  868. }
  869. }
  870. std::unordered_set<unsigned> immUAVID;
  871. std::unordered_set<unsigned> immSRVID;
  872. std::unordered_set<unsigned> immSamplerID;
  873. std::unordered_set<unsigned> immCBufID;
  874. ConvertUsedResource(immUAVID, usedUAVID);
  875. ConvertUsedResource(immSRVID, usedSRVID);
  876. ConvertUsedResource(immSamplerID, usedSamplerID);
  877. ConvertUsedResource(immCBufID, usedCBufID);
  878. RemoveResources(m_UAVs, immUAVID);
  879. RemoveResources(m_SRVs, immSRVID);
  880. RemoveResources(m_Samplers, immSamplerID);
  881. RemoveResources(m_CBuffers, immCBufID);
  882. }
  883. namespace {
  884. template <typename TResource>
  885. static void RemoveResourcesWithUnusedSymbolsHelper(std::vector<std::unique_ptr<TResource>> &vec) {
  886. unsigned resID = 0;
  887. for (auto p = vec.begin(); p != vec.end();) {
  888. auto c = p++;
  889. Constant *symbol = (*c)->GetGlobalSymbol();
  890. symbol->removeDeadConstantUsers();
  891. if (symbol->user_empty()) {
  892. p = vec.erase(c);
  893. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(symbol))
  894. GV->eraseFromParent();
  895. continue;
  896. }
  897. if ((*c)->GetID() != resID) {
  898. (*c)->SetID(resID);
  899. }
  900. resID++;
  901. }
  902. }
  903. }
  904. void DxilModule::RemoveResourcesWithUnusedSymbols() {
  905. RemoveResourcesWithUnusedSymbolsHelper(m_SRVs);
  906. RemoveResourcesWithUnusedSymbolsHelper(m_UAVs);
  907. RemoveResourcesWithUnusedSymbolsHelper(m_CBuffers);
  908. RemoveResourcesWithUnusedSymbolsHelper(m_Samplers);
  909. }
  910. DxilSignature &DxilModule::GetInputSignature() {
  911. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  912. "only works for non-lib profile");
  913. return m_DxilEntryPropsMap.begin()->second->sig.InputSignature;
  914. }
  915. const DxilSignature &DxilModule::GetInputSignature() const {
  916. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  917. "only works for non-lib profile");
  918. return m_DxilEntryPropsMap.begin()->second->sig.InputSignature;
  919. }
  920. DxilSignature &DxilModule::GetOutputSignature() {
  921. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  922. "only works for non-lib profile");
  923. return m_DxilEntryPropsMap.begin()->second->sig.OutputSignature;
  924. }
  925. const DxilSignature &DxilModule::GetOutputSignature() const {
  926. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  927. "only works for non-lib profile");
  928. return m_DxilEntryPropsMap.begin()->second->sig.OutputSignature;
  929. }
  930. DxilSignature &DxilModule::GetPatchConstOrPrimSignature() {
  931. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  932. "only works for non-lib profile");
  933. return m_DxilEntryPropsMap.begin()->second->sig.PatchConstOrPrimSignature;
  934. }
  935. const DxilSignature &DxilModule::GetPatchConstOrPrimSignature() const {
  936. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  937. "only works for non-lib profile");
  938. return m_DxilEntryPropsMap.begin()->second->sig.PatchConstOrPrimSignature;
  939. }
  940. const std::vector<uint8_t> &DxilModule::GetSerializedRootSignature() const {
  941. return m_SerializedRootSignature;
  942. }
  943. std::vector<uint8_t> &DxilModule::GetSerializedRootSignature() {
  944. return m_SerializedRootSignature;
  945. }
  946. // Entry props.
  947. bool DxilModule::HasDxilEntrySignature(const llvm::Function *F) const {
  948. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  949. }
  950. DxilEntrySignature &DxilModule::GetDxilEntrySignature(const llvm::Function *F) {
  951. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  952. return m_DxilEntryPropsMap[F].get()->sig;
  953. }
  954. void DxilModule::ReplaceDxilEntryProps(llvm::Function *F,
  955. llvm::Function *NewF) {
  956. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  957. std::unique_ptr<DxilEntryProps> Props = std::move(m_DxilEntryPropsMap[F]);
  958. m_DxilEntryPropsMap.erase(F);
  959. m_DxilEntryPropsMap[NewF] = std::move(Props);
  960. }
  961. void DxilModule::CloneDxilEntryProps(llvm::Function *F, llvm::Function *NewF) {
  962. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  963. std::unique_ptr<DxilEntryProps> Props =
  964. llvm::make_unique<DxilEntryProps>(*m_DxilEntryPropsMap[F]);
  965. m_DxilEntryPropsMap[NewF] = std::move(Props);
  966. }
  967. bool DxilModule::HasDxilEntryProps(const llvm::Function *F) const {
  968. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  969. }
  970. DxilEntryProps &DxilModule::GetDxilEntryProps(const llvm::Function *F) {
  971. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  972. return *m_DxilEntryPropsMap.find(F)->second.get();
  973. }
  974. const DxilEntryProps &DxilModule::GetDxilEntryProps(const llvm::Function *F) const {
  975. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  976. return *m_DxilEntryPropsMap.find(F)->second.get();
  977. }
  978. bool DxilModule::HasDxilFunctionProps(const llvm::Function *F) const {
  979. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  980. }
  981. DxilFunctionProps &DxilModule::GetDxilFunctionProps(const llvm::Function *F) {
  982. return const_cast<DxilFunctionProps &>(
  983. static_cast<const DxilModule *>(this)->GetDxilFunctionProps(F));
  984. }
  985. const DxilFunctionProps &
  986. DxilModule::GetDxilFunctionProps(const llvm::Function *F) const {
  987. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  988. return m_DxilEntryPropsMap.find(F)->second.get()->props;
  989. }
  990. void DxilModule::SetPatchConstantFunctionForHS(llvm::Function *hullShaderFunc, llvm::Function *patchConstantFunc) {
  991. auto propIter = m_DxilEntryPropsMap.find(hullShaderFunc);
  992. DXASSERT(propIter != m_DxilEntryPropsMap.end(),
  993. "Hull shader must already have function props!");
  994. DxilFunctionProps &props = propIter->second->props;
  995. DXASSERT(props.IsHS(), "else hullShaderFunc is not a Hull Shader");
  996. auto &HS = props.ShaderProps.HS;
  997. if (HS.patchConstantFunc != patchConstantFunc) {
  998. if (HS.patchConstantFunc)
  999. m_PatchConstantFunctions.erase(HS.patchConstantFunc);
  1000. HS.patchConstantFunc = patchConstantFunc;
  1001. if (patchConstantFunc)
  1002. m_PatchConstantFunctions.insert(patchConstantFunc);
  1003. }
  1004. }
  1005. bool DxilModule::IsGraphicsShader(const llvm::Function *F) const {
  1006. return HasDxilFunctionProps(F) && GetDxilFunctionProps(F).IsGraphics();
  1007. }
  1008. bool DxilModule::IsPatchConstantShader(const llvm::Function *F) const {
  1009. return m_PatchConstantFunctions.count(F) != 0;
  1010. }
  1011. bool DxilModule::IsComputeShader(const llvm::Function *F) const {
  1012. return HasDxilFunctionProps(F) && GetDxilFunctionProps(F).IsCS();
  1013. }
  1014. bool DxilModule::IsEntryThatUsesSignatures(const llvm::Function *F) const {
  1015. auto propIter = m_DxilEntryPropsMap.find(F);
  1016. if (propIter != m_DxilEntryPropsMap.end()) {
  1017. DxilFunctionProps &props = propIter->second->props;
  1018. return props.IsGraphics() || props.IsCS();
  1019. }
  1020. // Otherwise, return true if patch constant function
  1021. return IsPatchConstantShader(F);
  1022. }
  1023. bool DxilModule::StripRootSignatureFromMetadata() {
  1024. NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
  1025. if (pRootSignatureNamedMD) {
  1026. GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
  1027. return true;
  1028. }
  1029. return false;
  1030. }
  1031. DxilSubobjects *DxilModule::GetSubobjects() {
  1032. return m_pSubobjects.get();
  1033. }
  1034. const DxilSubobjects *DxilModule::GetSubobjects() const {
  1035. return m_pSubobjects.get();
  1036. }
  1037. DxilSubobjects *DxilModule::ReleaseSubobjects() {
  1038. return m_pSubobjects.release();
  1039. }
  1040. void DxilModule::ResetSubobjects(DxilSubobjects *subobjects) {
  1041. m_pSubobjects.reset(subobjects);
  1042. }
  1043. bool DxilModule::StripSubobjectsFromMetadata() {
  1044. NamedMDNode *pSubobjectsNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilSubobjectsMDName);
  1045. if (pSubobjectsNamedMD) {
  1046. GetModule()->eraseNamedMetadata(pSubobjectsNamedMD);
  1047. return true;
  1048. }
  1049. return false;
  1050. }
  1051. void DxilModule::UpdateValidatorVersionMetadata() {
  1052. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  1053. }
  1054. void DxilModule::ResetSerializedRootSignature(std::vector<uint8_t> &Value) {
  1055. m_SerializedRootSignature.clear();
  1056. m_SerializedRootSignature.reserve(Value.size());
  1057. m_SerializedRootSignature.assign(Value.begin(), Value.end());
  1058. }
  1059. DxilTypeSystem &DxilModule::GetTypeSystem() {
  1060. return *m_pTypeSystem;
  1061. }
  1062. std::vector<unsigned> &DxilModule::GetSerializedViewIdState() {
  1063. return m_SerializedState;
  1064. }
  1065. const std::vector<unsigned> &DxilModule::GetSerializedViewIdState() const {
  1066. return m_SerializedState;
  1067. }
  1068. void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
  1069. m_pTypeSystem.reset(pValue);
  1070. }
  1071. void DxilModule::ResetOP(hlsl::OP *hlslOP) { m_pOP.reset(hlslOP); }
  1072. void DxilModule::ResetEntryPropsMap(DxilEntryPropsMap &&PropMap) {
  1073. m_DxilEntryPropsMap.clear();
  1074. std::move(PropMap.begin(), PropMap.end(),
  1075. inserter(m_DxilEntryPropsMap, m_DxilEntryPropsMap.begin()));
  1076. }
  1077. static const StringRef llvmUsedName = "llvm.used";
  1078. void DxilModule::EmitLLVMUsed() {
  1079. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  1080. oldGV->eraseFromParent();
  1081. }
  1082. if (m_LLVMUsed.empty())
  1083. return;
  1084. vector<llvm::Constant *> GVs;
  1085. Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
  1086. GVs.resize(m_LLVMUsed.size());
  1087. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  1088. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  1089. PointerType *pPtrType = dyn_cast<PointerType>(pConst->getType());
  1090. if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
  1091. // Cast pointer to addrspace 0, as LLVMUsed elements must have the same
  1092. // type.
  1093. GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
  1094. } else {
  1095. GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
  1096. }
  1097. }
  1098. ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
  1099. GlobalVariable *pGV =
  1100. new GlobalVariable(*m_pModule, pATy, false, GlobalValue::AppendingLinkage,
  1101. ConstantArray::get(pATy, GVs), llvmUsedName);
  1102. pGV->setSection("llvm.metadata");
  1103. }
  1104. void DxilModule::ClearLLVMUsed() {
  1105. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  1106. oldGV->eraseFromParent();
  1107. }
  1108. if (m_LLVMUsed.empty())
  1109. return;
  1110. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  1111. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  1112. pConst->removeDeadConstantUsers();
  1113. }
  1114. m_LLVMUsed.clear();
  1115. }
  1116. vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
  1117. return m_LLVMUsed;
  1118. }
  1119. // DXIL metadata serialization/deserialization.
  1120. void DxilModule::ClearDxilMetadata(Module &M) {
  1121. // Delete: DXIL version, validator version, DXIL shader model,
  1122. // entry point tuples (shader properties, signatures, resources)
  1123. // type system, view ID state, LLVM used, entry point tuples,
  1124. // root signature, function properties.
  1125. // Other cases for libs pending.
  1126. // LLVM used is a global variable - handle separately.
  1127. SmallVector<NamedMDNode*, 8> nodes;
  1128. for (NamedMDNode &b : M.named_metadata()) {
  1129. StringRef name = b.getName();
  1130. if (name == DxilMDHelper::kDxilVersionMDName ||
  1131. name == DxilMDHelper::kDxilValidatorVersionMDName ||
  1132. name == DxilMDHelper::kDxilShaderModelMDName ||
  1133. name == DxilMDHelper::kDxilEntryPointsMDName ||
  1134. name == DxilMDHelper::kDxilRootSignatureMDName ||
  1135. name == DxilMDHelper::kDxilIntermediateOptionsMDName ||
  1136. name == DxilMDHelper::kDxilResourcesMDName ||
  1137. name == DxilMDHelper::kDxilTypeSystemMDName ||
  1138. name == DxilMDHelper::kDxilViewIdStateMDName ||
  1139. name == DxilMDHelper::kDxilSubobjectsMDName ||
  1140. name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
  1141. nodes.push_back(&b);
  1142. }
  1143. }
  1144. for (size_t i = 0; i < nodes.size(); ++i) {
  1145. M.eraseNamedMetadata(nodes[i]);
  1146. }
  1147. }
  1148. void DxilModule::EmitDxilMetadata() {
  1149. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  1150. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  1151. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  1152. m_pMDHelper->EmitDxilIntermediateOptions(m_IntermediateFlags);
  1153. MDTuple *pMDProperties = nullptr;
  1154. uint64_t flag = m_ShaderFlags.GetShaderFlagsRaw();
  1155. if (m_pSM->IsLib()) {
  1156. DxilFunctionProps props;
  1157. props.shaderKind = DXIL::ShaderKind::Library;
  1158. pMDProperties = m_pMDHelper->EmitDxilEntryProperties(flag, props,
  1159. GetAutoBindingSpace());
  1160. } else {
  1161. pMDProperties = m_pMDHelper->EmitDxilEntryProperties(
  1162. flag, m_DxilEntryPropsMap.begin()->second->props,
  1163. GetAutoBindingSpace());
  1164. }
  1165. MDTuple *pMDSignatures = nullptr;
  1166. if (!m_pSM->IsLib()) {
  1167. pMDSignatures = m_pMDHelper->EmitDxilSignatures(
  1168. m_DxilEntryPropsMap.begin()->second->sig);
  1169. }
  1170. MDTuple *pMDResources = EmitDxilResources();
  1171. if (pMDResources)
  1172. m_pMDHelper->EmitDxilResources(pMDResources);
  1173. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  1174. if (!m_pSM->IsLib() && !m_pSM->IsCS() &&
  1175. ((m_ValMajor == 0 && m_ValMinor == 0) ||
  1176. (m_ValMajor > 1 || (m_ValMajor == 1 && m_ValMinor >= 1)))) {
  1177. m_pMDHelper->EmitDxilViewIdState(m_SerializedState);
  1178. }
  1179. EmitLLVMUsed();
  1180. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  1181. vector<MDNode *> Entries;
  1182. Entries.emplace_back(pEntry);
  1183. if (m_pSM->IsLib()) {
  1184. // Sort functions by name to keep metadata deterministic
  1185. vector<const Function *> funcOrder;
  1186. funcOrder.reserve(m_DxilEntryPropsMap.size());
  1187. std::transform( m_DxilEntryPropsMap.begin(),
  1188. m_DxilEntryPropsMap.end(),
  1189. std::back_inserter(funcOrder),
  1190. [](const std::pair<const llvm::Function * const, std::unique_ptr<DxilEntryProps>> &p) -> const Function* { return p.first; } );
  1191. std::sort(funcOrder.begin(), funcOrder.end(), [](const Function *F1, const Function *F2) {
  1192. return F1->getName() < F2->getName();
  1193. });
  1194. for (auto F : funcOrder) {
  1195. auto &entryProps = m_DxilEntryPropsMap[F];
  1196. MDTuple *pProps = m_pMDHelper->EmitDxilEntryProperties(0, entryProps->props, 0);
  1197. MDTuple *pSig = m_pMDHelper->EmitDxilSignatures(entryProps->sig);
  1198. MDTuple *pSubEntry = m_pMDHelper->EmitDxilEntryPointTuple(
  1199. const_cast<Function *>(F), F->getName(), pSig, nullptr, pProps);
  1200. Entries.emplace_back(pSubEntry);
  1201. }
  1202. funcOrder.clear();
  1203. // Save Subobjects
  1204. if (GetSubobjects()) {
  1205. m_pMDHelper->EmitSubobjects(*GetSubobjects());
  1206. }
  1207. }
  1208. m_pMDHelper->EmitDxilEntryPoints(Entries);
  1209. if (!m_SerializedRootSignature.empty()) {
  1210. m_pMDHelper->EmitRootSignature(m_SerializedRootSignature);
  1211. }
  1212. }
  1213. bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
  1214. return DxilMDHelper::IsKnownNamedMetaData(Node);
  1215. }
  1216. void DxilModule::LoadDxilMetadata() {
  1217. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  1218. m_pMDHelper->LoadValidatorVersion(m_ValMajor, m_ValMinor);
  1219. const ShaderModel *loadedSM;
  1220. m_pMDHelper->LoadDxilShaderModel(loadedSM);
  1221. m_pMDHelper->LoadDxilIntermediateOptions(m_IntermediateFlags);
  1222. // This must be set before LoadDxilEntryProperties
  1223. m_pMDHelper->SetShaderModel(loadedSM);
  1224. // Setting module shader model requires UseMinPrecision flag,
  1225. // which requires loading m_ShaderFlags,
  1226. // which requires global entry properties,
  1227. // so load entry properties first, then set the shader model
  1228. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  1229. if (!loadedSM->IsLib()) {
  1230. IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
  1231. }
  1232. Function *pEntryFunc;
  1233. string EntryName;
  1234. const llvm::MDOperand *pEntrySignatures, *pEntryResources, *pEntryProperties;
  1235. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0),
  1236. pEntryFunc, EntryName,
  1237. pEntrySignatures, pEntryResources,
  1238. pEntryProperties);
  1239. uint64_t rawShaderFlags = 0;
  1240. DxilFunctionProps entryFuncProps;
  1241. entryFuncProps.shaderKind = loadedSM->GetKind();
  1242. m_pMDHelper->LoadDxilEntryProperties(*pEntryProperties, rawShaderFlags,
  1243. entryFuncProps, m_AutoBindingSpace);
  1244. m_bUseMinPrecision = true;
  1245. if (rawShaderFlags) {
  1246. m_ShaderFlags.SetShaderFlagsRaw(rawShaderFlags);
  1247. m_bUseMinPrecision = !m_ShaderFlags.GetUseNativeLowPrecision();
  1248. m_bDisableOptimizations = m_ShaderFlags.GetDisableOptimizations();
  1249. m_bAllResourcesBound = m_ShaderFlags.GetAllResourcesBound();
  1250. }
  1251. // Now that we have the UseMinPrecision flag, set shader model:
  1252. SetShaderModel(loadedSM, m_bUseMinPrecision);
  1253. if (loadedSM->IsLib()) {
  1254. for (unsigned i = 1; i < pEntries->getNumOperands(); i++) {
  1255. Function *pFunc;
  1256. string Name;
  1257. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  1258. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(i), pFunc, Name,
  1259. pSignatures, pResources, pProperties);
  1260. DxilFunctionProps props;
  1261. uint64_t rawShaderFlags = 0;
  1262. unsigned autoBindingSpace = 0;
  1263. m_pMDHelper->LoadDxilEntryProperties(
  1264. *pProperties, rawShaderFlags, props, autoBindingSpace);
  1265. if (props.IsHS() && props.ShaderProps.HS.patchConstantFunc) {
  1266. // Add patch constant function to m_PatchConstantFunctions
  1267. m_PatchConstantFunctions.insert(props.ShaderProps.HS.patchConstantFunc);
  1268. }
  1269. std::unique_ptr<DxilEntryProps> pEntryProps =
  1270. llvm::make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
  1271. m_pMDHelper->LoadDxilSignatures(*pSignatures, pEntryProps->sig);
  1272. m_DxilEntryPropsMap[pFunc] = std::move(pEntryProps);
  1273. }
  1274. // Load Subobjects
  1275. std::unique_ptr<DxilSubobjects> pSubobjects(new DxilSubobjects());
  1276. m_pMDHelper->LoadSubobjects(*pSubobjects);
  1277. if (pSubobjects->GetSubobjects().size()) {
  1278. ResetSubobjects(pSubobjects.release());
  1279. }
  1280. } else {
  1281. std::unique_ptr<DxilEntryProps> pEntryProps =
  1282. llvm::make_unique<DxilEntryProps>(entryFuncProps, m_bUseMinPrecision);
  1283. DxilFunctionProps *pFuncProps = &pEntryProps->props;
  1284. m_pMDHelper->LoadDxilSignatures(*pEntrySignatures, pEntryProps->sig);
  1285. m_DxilEntryPropsMap.clear();
  1286. m_DxilEntryPropsMap[pEntryFunc] = std::move(pEntryProps);
  1287. SetEntryFunction(pEntryFunc);
  1288. SetEntryFunctionName(EntryName);
  1289. SetShaderProperties(pFuncProps);
  1290. }
  1291. LoadDxilResources(*pEntryResources);
  1292. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  1293. m_pMDHelper->LoadRootSignature(m_SerializedRootSignature);
  1294. m_pMDHelper->LoadDxilViewIdState(m_SerializedState);
  1295. }
  1296. MDTuple *DxilModule::EmitDxilResources() {
  1297. // Emit SRV records.
  1298. MDTuple *pTupleSRVs = nullptr;
  1299. if (!m_SRVs.empty()) {
  1300. vector<Metadata *> MDVals;
  1301. for (size_t i = 0; i < m_SRVs.size(); i++) {
  1302. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  1303. }
  1304. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  1305. }
  1306. // Emit UAV records.
  1307. MDTuple *pTupleUAVs = nullptr;
  1308. if (!m_UAVs.empty()) {
  1309. vector<Metadata *> MDVals;
  1310. for (size_t i = 0; i < m_UAVs.size(); i++) {
  1311. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  1312. }
  1313. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  1314. }
  1315. // Emit CBuffer records.
  1316. MDTuple *pTupleCBuffers = nullptr;
  1317. if (!m_CBuffers.empty()) {
  1318. vector<Metadata *> MDVals;
  1319. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  1320. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  1321. }
  1322. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  1323. }
  1324. // Emit Sampler records.
  1325. MDTuple *pTupleSamplers = nullptr;
  1326. if (!m_Samplers.empty()) {
  1327. vector<Metadata *> MDVals;
  1328. for (size_t i = 0; i < m_Samplers.size(); i++) {
  1329. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  1330. }
  1331. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  1332. }
  1333. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  1334. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  1335. } else {
  1336. return nullptr;
  1337. }
  1338. }
  1339. void DxilModule::ReEmitDxilResources() {
  1340. ClearDxilMetadata(*m_pModule);
  1341. EmitDxilMetadata();
  1342. }
  1343. template <typename TResource>
  1344. static void
  1345. StripResourcesReflection(std::vector<std::unique_ptr<TResource>> &vec) {
  1346. for (auto &p : vec) {
  1347. p->SetGlobalName("");
  1348. // Cannot remove global symbol which used by validation.
  1349. }
  1350. }
  1351. void DxilModule::StripReflection() {
  1352. // Remove names.
  1353. for (Function &F : m_pModule->functions()) {
  1354. for (BasicBlock &BB : F) {
  1355. if (BB.hasName())
  1356. BB.setName("");
  1357. for (Instruction &I : BB) {
  1358. if (I.hasName())
  1359. I.setName("");
  1360. }
  1361. }
  1362. }
  1363. // Remove struct annotation.
  1364. // FunctionAnnotation is used later, so keep it.
  1365. m_pTypeSystem->GetStructAnnotationMap().clear();
  1366. // Resource
  1367. if (!GetShaderModel()->IsLib()) {
  1368. StripResourcesReflection(m_CBuffers);
  1369. StripResourcesReflection(m_UAVs);
  1370. StripResourcesReflection(m_SRVs);
  1371. StripResourcesReflection(m_Samplers);
  1372. }
  1373. // Unused global.
  1374. SmallVector<GlobalVariable *,2> UnusedGlobals;
  1375. for (GlobalVariable &GV : m_pModule->globals()) {
  1376. if (GV.use_empty())
  1377. UnusedGlobals.emplace_back(&GV);
  1378. }
  1379. for (GlobalVariable *GV : UnusedGlobals) {
  1380. GV->eraseFromParent();
  1381. }
  1382. // ReEmit meta.
  1383. ReEmitDxilResources();
  1384. }
  1385. void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
  1386. if (MDO.get() == nullptr)
  1387. return;
  1388. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  1389. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  1390. // Load SRV records.
  1391. if (pSRVs != nullptr) {
  1392. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  1393. unique_ptr<DxilResource> pSRV(new DxilResource);
  1394. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  1395. AddSRV(std::move(pSRV));
  1396. }
  1397. }
  1398. // Load UAV records.
  1399. if (pUAVs != nullptr) {
  1400. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  1401. unique_ptr<DxilResource> pUAV(new DxilResource);
  1402. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  1403. AddUAV(std::move(pUAV));
  1404. }
  1405. }
  1406. // Load CBuffer records.
  1407. if (pCBuffers != nullptr) {
  1408. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  1409. unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
  1410. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  1411. AddCBuffer(std::move(pCB));
  1412. }
  1413. }
  1414. // Load Sampler records.
  1415. if (pSamplers != nullptr) {
  1416. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  1417. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  1418. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  1419. AddSampler(std::move(pSampler));
  1420. }
  1421. }
  1422. }
  1423. void DxilModule::StripDebugRelatedCode() {
  1424. // Remove dx.source metadata.
  1425. if (NamedMDNode *contents = m_pModule->getNamedMetadata(
  1426. DxilMDHelper::kDxilSourceContentsMDName)) {
  1427. contents->eraseFromParent();
  1428. }
  1429. if (NamedMDNode *defines =
  1430. m_pModule->getNamedMetadata(DxilMDHelper::kDxilSourceDefinesMDName)) {
  1431. defines->eraseFromParent();
  1432. }
  1433. if (NamedMDNode *mainFileName = m_pModule->getNamedMetadata(
  1434. DxilMDHelper::kDxilSourceMainFileNameMDName)) {
  1435. mainFileName->eraseFromParent();
  1436. }
  1437. if (NamedMDNode *arguments =
  1438. m_pModule->getNamedMetadata(DxilMDHelper::kDxilSourceArgsMDName)) {
  1439. arguments->eraseFromParent();
  1440. }
  1441. if (NamedMDNode *flags = m_pModule->getModuleFlagsMetadata()) {
  1442. SmallVector<llvm::Module::ModuleFlagEntry, 4> flagEntries;
  1443. m_pModule->getModuleFlagsMetadata(flagEntries);
  1444. flags->eraseFromParent();
  1445. for (unsigned i = 0; i < flagEntries.size(); i++) {
  1446. llvm::Module::ModuleFlagEntry &entry = flagEntries[i];
  1447. if (entry.Key->getString() == "Dwarf Version" || entry.Key->getString() == "Debug Info Version") {
  1448. continue;
  1449. }
  1450. m_pModule->addModuleFlag(
  1451. entry.Behavior, entry.Key->getString(),
  1452. cast<ConstantAsMetadata>(entry.Val)->getValue());
  1453. }
  1454. }
  1455. }
  1456. DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
  1457. if (m_pDebugInfoFinder == nullptr) {
  1458. m_pDebugInfoFinder = llvm::make_unique<llvm::DebugInfoFinder>();
  1459. m_pDebugInfoFinder->processModule(*m_pModule);
  1460. }
  1461. return *m_pDebugInfoFinder;
  1462. }
  1463. hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule(llvm::Module *pModule) {
  1464. LLVMContext &Ctx = pModule->getContext();
  1465. std::string diagStr;
  1466. raw_string_ostream diagStream(diagStr);
  1467. hlsl::DxilModule *pDxilModule = nullptr;
  1468. // TODO: add detail error in DxilMDHelper.
  1469. try {
  1470. pDxilModule = &pModule->GetOrCreateDxilModule();
  1471. } catch (const ::hlsl::Exception &hlslException) {
  1472. diagStream << "load dxil metadata failed -";
  1473. try {
  1474. const char *msg = hlslException.what();
  1475. if (msg == nullptr || *msg == '\0')
  1476. diagStream << " error code " << hlslException.hr << "\n";
  1477. else
  1478. diagStream << msg;
  1479. } catch (...) {
  1480. diagStream << " unable to retrieve error message.\n";
  1481. }
  1482. Ctx.diagnose(DxilErrorDiagnosticInfo(diagStream.str().c_str()));
  1483. } catch (...) {
  1484. Ctx.diagnose(DxilErrorDiagnosticInfo("load dxil metadata failed - unknown error.\n"));
  1485. }
  1486. return pDxilModule;
  1487. }
  1488. // Check if the instruction has fast math flags configured to indicate
  1489. // the instruction is precise.
  1490. // Precise fast math flags means none of the fast math flags are set.
  1491. bool DxilModule::HasPreciseFastMathFlags(const Instruction *inst) {
  1492. return isa<FPMathOperator>(inst) && !inst->getFastMathFlags().any();
  1493. }
  1494. // Set fast math flags configured to indicate the instruction is precise.
  1495. void DxilModule::SetPreciseFastMathFlags(llvm::Instruction *inst) {
  1496. assert(isa<FPMathOperator>(inst));
  1497. inst->copyFastMathFlags(FastMathFlags());
  1498. }
  1499. // True if fast math flags are preserved across serialization/deserialization
  1500. // of the dxil module.
  1501. //
  1502. // We need to check for this when querying fast math flags for preciseness
  1503. // otherwise we will be overly conservative by reporting instructions precise
  1504. // because their fast math flags were not preserved.
  1505. //
  1506. // Currently we restrict it to the instruction types that have fast math
  1507. // preserved in the bitcode. We can expand this by converting fast math
  1508. // flags to dx.precise metadata during serialization and back to fast
  1509. // math flags during deserialization.
  1510. bool DxilModule::PreservesFastMathFlags(const llvm::Instruction *inst) {
  1511. return
  1512. isa<FPMathOperator>(inst) && (isa<BinaryOperator>(inst) || isa<FCmpInst>(inst));
  1513. }
  1514. bool DxilModule::IsPrecise(const Instruction *inst) const {
  1515. if (m_ShaderFlags.GetDisableMathRefactoring())
  1516. return true;
  1517. else if (DxilMDHelper::IsMarkedPrecise(inst))
  1518. return true;
  1519. else if (PreservesFastMathFlags(inst))
  1520. return HasPreciseFastMathFlags(inst);
  1521. else
  1522. return false;
  1523. }
  1524. } // namespace hlsl
  1525. namespace llvm {
  1526. hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
  1527. std::unique_ptr<hlsl::DxilModule> M;
  1528. if (!HasDxilModule()) {
  1529. M = llvm::make_unique<hlsl::DxilModule>(this);
  1530. if (!skipInit) {
  1531. M->LoadDxilMetadata();
  1532. }
  1533. SetDxilModule(M.release());
  1534. }
  1535. return GetDxilModule();
  1536. }
  1537. }