DxilModule.cpp 64 KB

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