DxilModule.cpp 59 KB

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