DxilModule.cpp 63 KB

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