2
0

DxilModule.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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/HLSL/DxilOperations.h"
  11. #include "dxc/HLSL/DxilModule.h"
  12. #include "dxc/HLSL/DxilShaderModel.h"
  13. #include "dxc/HLSL/DxilSignatureElement.h"
  14. #include "dxc/HLSL/DxilContainer.h"
  15. #include "dxc/HLSL/DxilRootSignature.h"
  16. #include "dxc/HLSL/DxilFunctionProps.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Metadata.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/IR/Operator.h"
  24. #include "llvm/IR/DebugInfo.h"
  25. #include "llvm/IR/DiagnosticInfo.h"
  26. #include "llvm/IR/DiagnosticPrinter.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <unordered_set>
  29. using namespace llvm;
  30. using std::string;
  31. using std::vector;
  32. using std::unique_ptr;
  33. namespace {
  34. class DxilErrorDiagnosticInfo : public DiagnosticInfo {
  35. private:
  36. const char *m_message;
  37. public:
  38. DxilErrorDiagnosticInfo(const char *str)
  39. : DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
  40. m_message(str) { }
  41. __override void print(DiagnosticPrinter &DP) const {
  42. DP << m_message;
  43. }
  44. };
  45. } // anon namespace
  46. namespace hlsl {
  47. //------------------------------------------------------------------------------
  48. //
  49. // DxilModule methods.
  50. //
  51. DxilModule::DxilModule(Module *pModule)
  52. : m_Ctx(pModule->getContext())
  53. , m_pModule(pModule)
  54. , m_pOP(std::make_unique<OP>(pModule->getContext(), pModule))
  55. , m_pTypeSystem(std::make_unique<DxilTypeSystem>(pModule))
  56. , m_pViewIdState(std::make_unique<DxilViewIdState>(this))
  57. , m_pMDHelper(std::make_unique<DxilMDHelper>(pModule, std::make_unique<DxilExtraPropertyHelper>(pModule)))
  58. , m_pDebugInfoFinder(nullptr)
  59. , m_pEntryFunc(nullptr)
  60. , m_EntryName("")
  61. , m_pPatchConstantFunc(nullptr)
  62. , m_pSM(nullptr)
  63. , m_DxilMajor(DXIL::kDxilMajor)
  64. , m_DxilMinor(DXIL::kDxilMinor)
  65. , m_ValMajor(1)
  66. , m_ValMinor(0)
  67. , m_InputPrimitive(DXIL::InputPrimitive::Undefined)
  68. , m_MaxVertexCount(0)
  69. , m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
  70. , m_ActiveStreamMask(0)
  71. , m_NumGSInstances(1)
  72. , m_InputControlPointCount(0)
  73. , m_TessellatorDomain(DXIL::TessellatorDomain::Undefined)
  74. , m_OutputControlPointCount(0)
  75. , m_TessellatorPartitioning(DXIL::TessellatorPartitioning::Undefined)
  76. , m_TessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive::Undefined)
  77. , m_MaxTessellationFactor(0.f)
  78. , m_RootSignature(nullptr) {
  79. DXASSERT_NOMSG(m_pModule != nullptr);
  80. m_NumThreads[0] = m_NumThreads[1] = m_NumThreads[2] = 0;
  81. #if defined(_DEBUG) || defined(DBG)
  82. // Pin LLVM dump methods.
  83. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  84. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  85. void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
  86. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  87. m_pUnused -= (size_t)&pfnViewCFGOnly;
  88. #endif
  89. }
  90. DxilModule::~DxilModule() {
  91. }
  92. DxilModule::ShaderFlags::ShaderFlags():
  93. m_bDisableOptimizations(false)
  94. , m_bDisableMathRefactoring(false)
  95. , m_bEnableDoublePrecision(false)
  96. , m_bForceEarlyDepthStencil(false)
  97. , m_bEnableRawAndStructuredBuffers(false)
  98. , m_bLowPrecisionPresent(false)
  99. , m_bEnableDoubleExtensions(false)
  100. , m_bEnableMSAD(false)
  101. , m_bAllResourcesBound(false)
  102. , m_bViewportAndRTArrayIndex(false)
  103. , m_bInnerCoverage(false)
  104. , m_bStencilRef(false)
  105. , m_bTiledResources(false)
  106. , m_bUAVLoadAdditionalFormats(false)
  107. , m_bLevel9ComparisonFiltering(false)
  108. , m_bCSRawAndStructuredViaShader4X(false)
  109. , m_b64UAVs(false)
  110. , m_UAVsAtEveryStage(false)
  111. , m_bROVS(false)
  112. , m_bWaveOps(false)
  113. , m_bInt64Ops(false)
  114. , m_bViewID(false)
  115. , m_bBarycentrics(false)
  116. , m_bUseNativeLowPrecision(false)
  117. , m_align0(0)
  118. , m_align1(0)
  119. {}
  120. LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
  121. Module *DxilModule::GetModule() const { return m_pModule; }
  122. OP *DxilModule::GetOP() const { return m_pOP.get(); }
  123. void DxilModule::SetShaderModel(const ShaderModel *pSM) {
  124. DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM), "shader model must not change for the module");
  125. DXASSERT(pSM != nullptr && pSM->IsValidForDxil(), "shader model must be valid");
  126. DXASSERT(pSM->IsValidForModule(), "shader model must be valid for top-level module use");
  127. m_pSM = pSM;
  128. m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
  129. m_pMDHelper->SetShaderModel(m_pSM);
  130. DXIL::ShaderKind shaderKind = pSM->GetKind();
  131. m_EntrySignature = llvm::make_unique<DxilEntrySignature>(shaderKind, !m_ShaderFlags.GetUseNativeLowPrecision());
  132. m_RootSignature.reset(new RootSignatureHandle());
  133. }
  134. const ShaderModel *DxilModule::GetShaderModel() const {
  135. return m_pSM;
  136. }
  137. void DxilModule::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  138. DxilMajor = m_DxilMajor;
  139. DxilMinor = m_DxilMinor;
  140. }
  141. void DxilModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  142. m_ValMajor = ValMajor;
  143. m_ValMinor = ValMinor;
  144. }
  145. bool DxilModule::UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  146. // Don't upgrade if validation was disabled.
  147. if (m_ValMajor == 0 && m_ValMinor == 0) {
  148. return false;
  149. }
  150. if (ValMajor > m_ValMajor || (ValMajor == m_ValMajor && ValMinor > m_ValMinor)) {
  151. // Module requires higher validator version than previously set
  152. SetValidatorVersion(ValMajor, ValMinor);
  153. return true;
  154. }
  155. return false;
  156. }
  157. void DxilModule::GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  158. ValMajor = m_ValMajor;
  159. ValMinor = m_ValMinor;
  160. }
  161. bool DxilModule::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  162. if (!m_pSM)
  163. return false;
  164. m_pSM->GetMinValidatorVersion(ValMajor, ValMinor);
  165. if (ValMajor == 1 && ValMinor == 0 && (m_ShaderFlags.GetFeatureInfo() & hlsl::ShaderFeatureInfo_ViewID))
  166. ValMinor = 1;
  167. return true;
  168. }
  169. bool DxilModule::UpgradeToMinValidatorVersion() {
  170. unsigned ValMajor = 1, ValMinor = 0;
  171. if (GetMinValidatorVersion(ValMajor, ValMinor)) {
  172. return UpgradeValidatorVersion(ValMajor, ValMinor);
  173. }
  174. return false;
  175. }
  176. Function *DxilModule::GetEntryFunction() {
  177. return m_pEntryFunc;
  178. }
  179. const Function *DxilModule::GetEntryFunction() const {
  180. return m_pEntryFunc;
  181. }
  182. void DxilModule::SetEntryFunction(Function *pEntryFunc) {
  183. m_pEntryFunc = pEntryFunc;
  184. }
  185. const string &DxilModule::GetEntryFunctionName() const {
  186. return m_EntryName;
  187. }
  188. void DxilModule::SetEntryFunctionName(const string &name) {
  189. m_EntryName = name;
  190. }
  191. llvm::Function *DxilModule::GetPatchConstantFunction() {
  192. return m_pPatchConstantFunc;
  193. }
  194. const llvm::Function *DxilModule::GetPatchConstantFunction() const {
  195. return m_pPatchConstantFunc;
  196. }
  197. void DxilModule::SetPatchConstantFunction(llvm::Function *pFunc) {
  198. m_pPatchConstantFunc = pFunc;
  199. }
  200. unsigned DxilModule::ShaderFlags::GetGlobalFlags() const {
  201. unsigned Flags = 0;
  202. Flags |= m_bDisableOptimizations ? DXIL::kDisableOptimizations : 0;
  203. Flags |= m_bDisableMathRefactoring ? DXIL::kDisableMathRefactoring : 0;
  204. Flags |= m_bEnableDoublePrecision ? DXIL::kEnableDoublePrecision : 0;
  205. Flags |= m_bForceEarlyDepthStencil ? DXIL::kForceEarlyDepthStencil : 0;
  206. Flags |= m_bEnableRawAndStructuredBuffers ? DXIL::kEnableRawAndStructuredBuffers : 0;
  207. Flags |= m_bLowPrecisionPresent && !m_bUseNativeLowPrecision? DXIL::kEnableMinPrecision : 0;
  208. Flags |= m_bEnableDoubleExtensions ? DXIL::kEnableDoubleExtensions : 0;
  209. Flags |= m_bEnableMSAD ? DXIL::kEnableMSAD : 0;
  210. Flags |= m_bAllResourcesBound ? DXIL::kAllResourcesBound : 0;
  211. return Flags;
  212. }
  213. uint64_t DxilModule::ShaderFlags::GetFeatureInfo() const {
  214. uint64_t Flags = 0;
  215. Flags |= m_bEnableDoublePrecision ? hlsl::ShaderFeatureInfo_Doubles : 0;
  216. Flags |= m_bLowPrecisionPresent && !m_bUseNativeLowPrecision ? hlsl::ShaderFeatureInfo_MinimumPrecision: 0;
  217. Flags |= m_bLowPrecisionPresent && m_bUseNativeLowPrecision ? hlsl::ShaderFeatureInfo_NativeLowPrecision : 0;
  218. Flags |= m_bEnableDoubleExtensions ? hlsl::ShaderFeatureInfo_11_1_DoubleExtensions : 0;
  219. Flags |= m_bWaveOps ? hlsl::ShaderFeatureInfo_WaveOps : 0;
  220. Flags |= m_bInt64Ops ? hlsl::ShaderFeatureInfo_Int64Ops : 0;
  221. Flags |= m_bROVS ? hlsl::ShaderFeatureInfo_ROVs : 0;
  222. Flags |= m_bViewportAndRTArrayIndex ? hlsl::ShaderFeatureInfo_ViewportAndRTArrayIndexFromAnyShaderFeedingRasterizer : 0;
  223. Flags |= m_bInnerCoverage ? hlsl::ShaderFeatureInfo_InnerCoverage : 0;
  224. Flags |= m_bStencilRef ? hlsl::ShaderFeatureInfo_StencilRef : 0;
  225. Flags |= m_bTiledResources ? hlsl::ShaderFeatureInfo_TiledResources : 0;
  226. Flags |= m_bEnableMSAD ? hlsl::ShaderFeatureInfo_11_1_ShaderExtensions : 0;
  227. Flags |= m_bCSRawAndStructuredViaShader4X ? hlsl::ShaderFeatureInfo_ComputeShadersPlusRawAndStructuredBuffersViaShader4X : 0;
  228. Flags |= m_UAVsAtEveryStage ? hlsl::ShaderFeatureInfo_UAVsAtEveryStage : 0;
  229. Flags |= m_b64UAVs ? hlsl::ShaderFeatureInfo_64UAVs : 0;
  230. Flags |= m_bLevel9ComparisonFiltering ? hlsl::ShaderFeatureInfo_LEVEL9ComparisonFiltering : 0;
  231. Flags |= m_bUAVLoadAdditionalFormats ? hlsl::ShaderFeatureInfo_TypedUAVLoadAdditionalFormats : 0;
  232. Flags |= m_bViewID ? hlsl::ShaderFeatureInfo_ViewID : 0;
  233. Flags |= m_bBarycentrics ? hlsl::ShaderFeatureInfo_Barycentrics : 0;
  234. return Flags;
  235. }
  236. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRaw() const {
  237. union Cast {
  238. Cast(const DxilModule::ShaderFlags &flags) {
  239. shaderFlags = flags;
  240. }
  241. DxilModule::ShaderFlags shaderFlags;
  242. uint64_t rawData;
  243. };
  244. static_assert(sizeof(uint64_t) == sizeof(DxilModule::ShaderFlags),
  245. "size must match to make sure no undefined bits when cast");
  246. Cast rawCast(*this);
  247. return rawCast.rawData;
  248. }
  249. void DxilModule::ShaderFlags::SetShaderFlagsRaw(uint64_t data) {
  250. union Cast {
  251. Cast(uint64_t data) {
  252. rawData = data;
  253. }
  254. DxilModule::ShaderFlags shaderFlags;
  255. uint64_t rawData;
  256. };
  257. Cast rawCast(data);
  258. *this = rawCast.shaderFlags;
  259. }
  260. unsigned DxilModule::GetGlobalFlags() const {
  261. unsigned Flags = m_ShaderFlags.GetGlobalFlags();
  262. return Flags;
  263. }
  264. static bool IsResourceSingleComponent(llvm::Type *Ty) {
  265. if (llvm::ArrayType *arrType = llvm::dyn_cast<llvm::ArrayType>(Ty)) {
  266. if (arrType->getArrayNumElements() > 1) {
  267. return false;
  268. }
  269. return IsResourceSingleComponent(arrType->getArrayElementType());
  270. } else if (llvm::StructType *structType =
  271. llvm::dyn_cast<llvm::StructType>(Ty)) {
  272. if (structType->getStructNumElements() > 1) {
  273. return false;
  274. }
  275. return IsResourceSingleComponent(structType->getStructElementType(0));
  276. } else if (llvm::VectorType *vectorType =
  277. llvm::dyn_cast<llvm::VectorType>(Ty)) {
  278. if (vectorType->getNumElements() > 1) {
  279. return false;
  280. }
  281. return IsResourceSingleComponent(vectorType->getVectorElementType());
  282. }
  283. return true;
  284. }
  285. // Given a CreateHandle call, returns arbitrary ConstantInt rangeID
  286. // Note: HLSL is currently assuming that rangeID is a constant value, but this code is assuming
  287. // that it can be either constant, phi node, or select instruction
  288. static ConstantInt *GetArbitraryConstantRangeID(CallInst *handleCall) {
  289. Value *rangeID =
  290. handleCall->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  291. ConstantInt *ConstantRangeID = dyn_cast<ConstantInt>(rangeID);
  292. while (ConstantRangeID == nullptr) {
  293. if (ConstantInt *CI = dyn_cast<ConstantInt>(rangeID)) {
  294. ConstantRangeID = CI;
  295. } else if (PHINode *PN = dyn_cast<PHINode>(rangeID)) {
  296. rangeID = PN->getIncomingValue(0);
  297. } else if (SelectInst *SI = dyn_cast<SelectInst>(rangeID)) {
  298. rangeID = SI->getTrueValue();
  299. } else {
  300. return nullptr;
  301. }
  302. }
  303. return ConstantRangeID;
  304. }
  305. void DxilModule::CollectShaderFlags(ShaderFlags &Flags) {
  306. bool hasDouble = false;
  307. // ddiv dfma drcp d2i d2u i2d u2d.
  308. // fma has dxil op. Others should check IR instruction div/cast.
  309. bool hasDoubleExtension = false;
  310. bool has64Int = false;
  311. bool has16 = false;
  312. bool hasWaveOps = false;
  313. bool hasCheckAccessFully = false;
  314. bool hasMSAD = false;
  315. bool hasInnerCoverage = false;
  316. bool hasViewID = false;
  317. bool hasMulticomponentUAVLoads = false;
  318. bool hasMulticomponentUAVLoadsBackCompat = false;
  319. // Try to maintain compatibility with a v1.0 validator if that's what we have.
  320. {
  321. unsigned valMajor, valMinor;
  322. GetValidatorVersion(valMajor, valMinor);
  323. hasMulticomponentUAVLoadsBackCompat = valMajor <= 1 && valMinor == 0;
  324. }
  325. Type *int16Ty = Type::getInt16Ty(GetCtx());
  326. Type *int64Ty = Type::getInt64Ty(GetCtx());
  327. for (Function &F : GetModule()->functions()) {
  328. for (BasicBlock &BB : F.getBasicBlockList()) {
  329. for (Instruction &I : BB.getInstList()) {
  330. // Skip none dxil function call.
  331. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  332. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  333. continue;
  334. }
  335. Type *Ty = I.getType();
  336. bool isDouble = Ty->isDoubleTy();
  337. bool isHalf = Ty->isHalfTy();
  338. bool isInt16 = Ty == int16Ty;
  339. bool isInt64 = Ty == int64Ty;
  340. if (isa<ExtractElementInst>(&I) ||
  341. isa<InsertElementInst>(&I))
  342. continue;
  343. for (Value *operand : I.operands()) {
  344. Type *Ty = operand->getType();
  345. isDouble |= Ty->isDoubleTy();
  346. isHalf |= Ty->isHalfTy();
  347. isInt16 |= Ty == int16Ty;
  348. isInt64 |= Ty == int64Ty;
  349. }
  350. if (isDouble) {
  351. hasDouble = true;
  352. switch (I.getOpcode()) {
  353. case Instruction::FDiv:
  354. case Instruction::UIToFP:
  355. case Instruction::SIToFP:
  356. case Instruction::FPToUI:
  357. case Instruction::FPToSI:
  358. hasDoubleExtension = true;
  359. break;
  360. }
  361. }
  362. has16 |= isHalf;
  363. has16 |= isInt16;
  364. has64Int |= isInt64;
  365. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  366. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  367. continue;
  368. Value *opcodeArg = CI->getArgOperand(DXIL::OperandIndex::kOpcodeIdx);
  369. ConstantInt *opcodeConst = dyn_cast<ConstantInt>(opcodeArg);
  370. DXASSERT(opcodeConst, "DXIL opcode arg must be immediate");
  371. unsigned opcode = opcodeConst->getLimitedValue();
  372. DXASSERT(opcode < static_cast<unsigned>(DXIL::OpCode::NumOpCodes),
  373. "invalid DXIL opcode");
  374. DXIL::OpCode dxilOp = static_cast<DXIL::OpCode>(opcode);
  375. if (hlsl::OP::IsDxilOpWave(dxilOp))
  376. hasWaveOps = true;
  377. switch (dxilOp) {
  378. case DXIL::OpCode::CheckAccessFullyMapped:
  379. hasCheckAccessFully = true;
  380. break;
  381. case DXIL::OpCode::Msad:
  382. hasMSAD = true;
  383. break;
  384. case DXIL::OpCode::BufferLoad:
  385. case DXIL::OpCode::TextureLoad: {
  386. if (hasMulticomponentUAVLoads) continue;
  387. // This is the old-style computation (overestimating requirements).
  388. Value *resHandle = CI->getArgOperand(DXIL::OperandIndex::kBufferStoreHandleOpIdx);
  389. CallInst *handleCall = cast<CallInst>(resHandle);
  390. if (ConstantInt *resClassArg =
  391. dyn_cast<ConstantInt>(handleCall->getArgOperand(
  392. DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
  393. DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
  394. resClassArg->getLimitedValue());
  395. if (resClass == DXIL::ResourceClass::UAV) {
  396. // Validator 1.0 assumes that all uav load is multi component load.
  397. if (hasMulticomponentUAVLoadsBackCompat) {
  398. hasMulticomponentUAVLoads = true;
  399. continue;
  400. }
  401. else {
  402. ConstantInt *rangeID = GetArbitraryConstantRangeID(handleCall);
  403. if (rangeID) {
  404. DxilResource resource = GetUAV(rangeID->getLimitedValue());
  405. if ((resource.IsTypedBuffer() ||
  406. resource.IsAnyTexture()) &&
  407. !IsResourceSingleComponent(resource.GetRetType())) {
  408. hasMulticomponentUAVLoads = true;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. else {
  415. DXASSERT(false, "Resource class must be constant.");
  416. }
  417. } break;
  418. case DXIL::OpCode::Fma:
  419. hasDoubleExtension |= isDouble;
  420. break;
  421. case DXIL::OpCode::InnerCoverage:
  422. hasInnerCoverage = true;
  423. break;
  424. case DXIL::OpCode::ViewID:
  425. hasViewID = true;
  426. break;
  427. default:
  428. // Normal opcodes.
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. Flags.SetEnableDoublePrecision(hasDouble);
  436. Flags.SetInt64Ops(has64Int);
  437. Flags.SetLowPrecisionPresent(has16);
  438. Flags.SetEnableDoubleExtensions(hasDoubleExtension);
  439. Flags.SetWaveOps(hasWaveOps);
  440. Flags.SetTiledResources(hasCheckAccessFully);
  441. Flags.SetEnableMSAD(hasMSAD);
  442. Flags.SetUAVLoadAdditionalFormats(hasMulticomponentUAVLoads);
  443. Flags.SetViewID(hasViewID);
  444. const ShaderModel *SM = GetShaderModel();
  445. if (SM->IsPS()) {
  446. bool hasStencilRef = false;
  447. DxilSignature &outS = GetOutputSignature();
  448. for (auto &&E : outS.GetElements()) {
  449. if (E->GetKind() == Semantic::Kind::StencilRef) {
  450. hasStencilRef = true;
  451. } else if (E->GetKind() == Semantic::Kind::InnerCoverage) {
  452. hasInnerCoverage = true;
  453. }
  454. }
  455. Flags.SetStencilRef(hasStencilRef);
  456. Flags.SetInnerCoverage(hasInnerCoverage);
  457. }
  458. bool checkInputRTArrayIndex =
  459. SM->IsGS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  460. if (checkInputRTArrayIndex) {
  461. bool hasViewportArrayIndex = false;
  462. bool hasRenderTargetArrayIndex = false;
  463. DxilSignature &inS = GetInputSignature();
  464. for (auto &E : inS.GetElements()) {
  465. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  466. hasViewportArrayIndex = true;
  467. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  468. hasRenderTargetArrayIndex = true;
  469. }
  470. }
  471. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  472. hasRenderTargetArrayIndex);
  473. }
  474. bool checkOutputRTArrayIndex =
  475. SM->IsVS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  476. if (checkOutputRTArrayIndex) {
  477. bool hasViewportArrayIndex = false;
  478. bool hasRenderTargetArrayIndex = false;
  479. DxilSignature &outS = GetOutputSignature();
  480. for (auto &E : outS.GetElements()) {
  481. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  482. hasViewportArrayIndex = true;
  483. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  484. hasRenderTargetArrayIndex = true;
  485. }
  486. }
  487. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  488. hasRenderTargetArrayIndex);
  489. }
  490. unsigned NumUAVs = m_UAVs.size();
  491. const unsigned kSmallUAVCount = 8;
  492. if (NumUAVs > kSmallUAVCount)
  493. Flags.Set64UAVs(true);
  494. if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
  495. Flags.SetUAVsAtEveryStage(true);
  496. bool hasRawAndStructuredBuffer = false;
  497. for (auto &UAV : m_UAVs) {
  498. if (UAV->IsROV())
  499. Flags.SetROVs(true);
  500. switch (UAV->GetKind()) {
  501. case DXIL::ResourceKind::RawBuffer:
  502. case DXIL::ResourceKind::StructuredBuffer:
  503. hasRawAndStructuredBuffer = true;
  504. break;
  505. default:
  506. // Not raw/structured.
  507. break;
  508. }
  509. }
  510. for (auto &SRV : m_SRVs) {
  511. switch (SRV->GetKind()) {
  512. case DXIL::ResourceKind::RawBuffer:
  513. case DXIL::ResourceKind::StructuredBuffer:
  514. hasRawAndStructuredBuffer = true;
  515. break;
  516. default:
  517. // Not raw/structured.
  518. break;
  519. }
  520. }
  521. Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
  522. bool hasCSRawAndStructuredViaShader4X =
  523. hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
  524. Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
  525. }
  526. void DxilModule::CollectShaderFlags() {
  527. CollectShaderFlags(m_ShaderFlags);
  528. }
  529. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRawForCollection() {
  530. // This should be all the flags that can be set by DxilModule::CollectShaderFlags.
  531. ShaderFlags Flags;
  532. Flags.SetEnableDoublePrecision(true);
  533. Flags.SetInt64Ops(true);
  534. Flags.SetLowPrecisionPresent(true);
  535. Flags.SetEnableDoubleExtensions(true);
  536. Flags.SetWaveOps(true);
  537. Flags.SetTiledResources(true);
  538. Flags.SetEnableMSAD(true);
  539. Flags.SetUAVLoadAdditionalFormats(true);
  540. Flags.SetStencilRef(true);
  541. Flags.SetInnerCoverage(true);
  542. Flags.SetViewportAndRTArrayIndex(true);
  543. Flags.Set64UAVs(true);
  544. Flags.SetUAVsAtEveryStage(true);
  545. Flags.SetEnableRawAndStructuredBuffers(true);
  546. Flags.SetCSRawAndStructuredViaShader4X(true);
  547. Flags.SetViewID(true);
  548. Flags.SetBarycentrics(true);
  549. return Flags.GetShaderFlagsRaw();
  550. }
  551. DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
  552. return m_InputPrimitive;
  553. }
  554. void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
  555. DXASSERT_NOMSG(m_InputPrimitive == DXIL::InputPrimitive::Undefined);
  556. DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
  557. m_InputPrimitive = IP;
  558. }
  559. unsigned DxilModule::GetMaxVertexCount() const {
  560. DXASSERT_NOMSG(m_MaxVertexCount != 0);
  561. return m_MaxVertexCount;
  562. }
  563. void DxilModule::SetMaxVertexCount(unsigned Count) {
  564. DXASSERT_NOMSG(m_MaxVertexCount == 0);
  565. m_MaxVertexCount = Count;
  566. }
  567. DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
  568. return m_StreamPrimitiveTopology;
  569. }
  570. void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
  571. m_StreamPrimitiveTopology = Topology;
  572. }
  573. bool DxilModule::HasMultipleOutputStreams() const {
  574. if (!m_pSM->IsGS()) {
  575. return false;
  576. } else {
  577. unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
  578. ((m_ActiveStreamMask & 0x2) >> 1) +
  579. ((m_ActiveStreamMask & 0x4) >> 2) +
  580. ((m_ActiveStreamMask & 0x8) >> 3);
  581. DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
  582. return NumStreams > 1;
  583. }
  584. }
  585. unsigned DxilModule::GetOutputStream() const {
  586. if (!m_pSM->IsGS()) {
  587. return 0;
  588. } else {
  589. DXASSERT_NOMSG(!HasMultipleOutputStreams());
  590. switch (m_ActiveStreamMask) {
  591. case 0x1: return 0;
  592. case 0x2: return 1;
  593. case 0x4: return 2;
  594. case 0x8: return 3;
  595. default: DXASSERT_NOMSG(false);
  596. }
  597. return (unsigned)(-1);
  598. }
  599. }
  600. unsigned DxilModule::GetGSInstanceCount() const {
  601. return m_NumGSInstances;
  602. }
  603. void DxilModule::SetGSInstanceCount(unsigned Count) {
  604. m_NumGSInstances = Count;
  605. }
  606. bool DxilModule::IsStreamActive(unsigned Stream) const {
  607. return (m_ActiveStreamMask & (1<<Stream)) != 0;
  608. }
  609. void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
  610. if (bActive) {
  611. m_ActiveStreamMask |= (1<<Stream);
  612. } else {
  613. m_ActiveStreamMask &= ~(1<<Stream);
  614. }
  615. }
  616. void DxilModule::SetActiveStreamMask(unsigned Mask) {
  617. m_ActiveStreamMask = Mask;
  618. }
  619. unsigned DxilModule::GetActiveStreamMask() const {
  620. return m_ActiveStreamMask;
  621. }
  622. unsigned DxilModule::GetInputControlPointCount() const {
  623. return m_InputControlPointCount;
  624. }
  625. void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
  626. m_InputControlPointCount = NumICPs;
  627. }
  628. DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
  629. return m_TessellatorDomain;
  630. }
  631. void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
  632. m_TessellatorDomain = TessDomain;
  633. }
  634. unsigned DxilModule::GetOutputControlPointCount() const {
  635. return m_OutputControlPointCount;
  636. }
  637. void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
  638. m_OutputControlPointCount = NumOCPs;
  639. }
  640. DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
  641. return m_TessellatorPartitioning;
  642. }
  643. void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
  644. m_TessellatorPartitioning = TessPartitioning;
  645. }
  646. DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
  647. return m_TessellatorOutputPrimitive;
  648. }
  649. void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
  650. m_TessellatorOutputPrimitive = TessOutputPrimitive;
  651. }
  652. float DxilModule::GetMaxTessellationFactor() const {
  653. return m_MaxTessellationFactor;
  654. }
  655. void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
  656. m_MaxTessellationFactor = MaxTessellationFactor;
  657. }
  658. void DxilModule::SetShaderProperties(DxilFunctionProps *props) {
  659. if (!props)
  660. return;
  661. switch (props->shaderKind) {
  662. case DXIL::ShaderKind::Pixel: {
  663. auto &PS = props->ShaderProps.PS;
  664. m_ShaderFlags.SetForceEarlyDepthStencil(PS.EarlyDepthStencil);
  665. } break;
  666. case DXIL::ShaderKind::Compute: {
  667. auto &CS = props->ShaderProps.CS;
  668. for (size_t i = 0; i < _countof(m_NumThreads); ++i)
  669. m_NumThreads[i] = CS.numThreads[i];
  670. } break;
  671. case DXIL::ShaderKind::Domain: {
  672. auto &DS = props->ShaderProps.DS;
  673. SetTessellatorDomain(DS.domain);
  674. SetInputControlPointCount(DS.inputControlPoints);
  675. } break;
  676. case DXIL::ShaderKind::Hull: {
  677. auto &HS = props->ShaderProps.HS;
  678. SetPatchConstantFunction(HS.patchConstantFunc);
  679. SetTessellatorDomain(HS.domain);
  680. SetTessellatorPartitioning(HS.partition);
  681. SetTessellatorOutputPrimitive(HS.outputPrimitive);
  682. SetInputControlPointCount(HS.inputControlPoints);
  683. SetOutputControlPointCount(HS.outputControlPoints);
  684. SetMaxTessellationFactor(HS.maxTessFactor);
  685. } break;
  686. case DXIL::ShaderKind::Vertex:
  687. break;
  688. default: {
  689. DXASSERT(props->shaderKind == DXIL::ShaderKind::Geometry,
  690. "else invalid shader kind");
  691. auto &GS = props->ShaderProps.GS;
  692. SetInputPrimitive(GS.inputPrimitive);
  693. SetMaxVertexCount(GS.maxVertexCount);
  694. for (size_t i = 0; i < _countof(GS.streamPrimitiveTopologies); ++i) {
  695. if (GS.streamPrimitiveTopologies[i] !=
  696. DXIL::PrimitiveTopology::Undefined) {
  697. SetStreamActive(i, true);
  698. DXASSERT_NOMSG(GetStreamPrimitiveTopology() ==
  699. DXIL::PrimitiveTopology::Undefined ||
  700. GetStreamPrimitiveTopology() ==
  701. GS.streamPrimitiveTopologies[i]);
  702. SetStreamPrimitiveTopology(GS.streamPrimitiveTopologies[i]);
  703. }
  704. }
  705. SetGSInstanceCount(GS.instanceCount);
  706. } break;
  707. }
  708. }
  709. template<typename T> unsigned
  710. DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  711. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  712. unsigned Id = (unsigned)Vec.size();
  713. Vec.emplace_back(std::move(pRes));
  714. return Id;
  715. }
  716. unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
  717. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
  718. }
  719. DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
  720. return *m_CBuffers[idx];
  721. }
  722. const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
  723. return *m_CBuffers[idx];
  724. }
  725. const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
  726. return m_CBuffers;
  727. }
  728. unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  729. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  730. }
  731. DxilSampler &DxilModule::GetSampler(unsigned idx) {
  732. return *m_Samplers[idx];
  733. }
  734. const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
  735. return *m_Samplers[idx];
  736. }
  737. const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
  738. return m_Samplers;
  739. }
  740. unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
  741. return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
  742. }
  743. DxilResource &DxilModule::GetSRV(unsigned idx) {
  744. return *m_SRVs[idx];
  745. }
  746. const DxilResource &DxilModule::GetSRV(unsigned idx) const {
  747. return *m_SRVs[idx];
  748. }
  749. const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
  750. return m_SRVs;
  751. }
  752. unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
  753. return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
  754. }
  755. DxilResource &DxilModule::GetUAV(unsigned idx) {
  756. return *m_UAVs[idx];
  757. }
  758. const DxilResource &DxilModule::GetUAV(unsigned idx) const {
  759. return *m_UAVs[idx];
  760. }
  761. const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
  762. return m_UAVs;
  763. }
  764. static void CreateResourceLinkConstant(Module &M, DxilResourceBase *pRes,
  765. std::vector<DxilModule::ResourceLinkInfo> &resLinkInfo) {
  766. Type *i32Ty = Type::getInt32Ty(M.getContext());
  767. const bool IsConstantTrue = true;
  768. Constant *NullInitVal = nullptr;
  769. GlobalVariable *rangeID = new GlobalVariable(
  770. M, i32Ty, IsConstantTrue, llvm::GlobalValue::ExternalLinkage, NullInitVal,
  771. pRes->GetGlobalName() + "_rangeID");
  772. resLinkInfo.emplace_back(DxilModule::ResourceLinkInfo{rangeID});
  773. }
  774. void DxilModule::CreateResourceLinkInfo() {
  775. DXASSERT(GetShaderModel()->IsLib(), "only for library profile");
  776. DXASSERT(m_SRVsLinkInfo.empty() && m_UAVsLinkInfo.empty() &&
  777. m_CBuffersLinkInfo.empty() && m_SamplersLinkInfo.empty(),
  778. "else resource link info was already created");
  779. Module &M = *m_pModule;
  780. for (auto &SRV : m_SRVs) {
  781. CreateResourceLinkConstant(M, SRV.get(), m_SRVsLinkInfo);
  782. }
  783. for (auto &UAV : m_UAVs) {
  784. CreateResourceLinkConstant(M, UAV.get(), m_UAVsLinkInfo);
  785. }
  786. for (auto &CBuffer : m_CBuffers) {
  787. CreateResourceLinkConstant(M, CBuffer.get(), m_CBuffersLinkInfo);
  788. }
  789. for (auto &Sampler : m_Samplers) {
  790. CreateResourceLinkConstant(M, Sampler.get(), m_SamplersLinkInfo);
  791. }
  792. }
  793. const DxilModule::ResourceLinkInfo &
  794. DxilModule::GetResourceLinkInfo(DXIL::ResourceClass resClass,
  795. unsigned rangeID) const {
  796. switch (resClass) {
  797. case DXIL::ResourceClass::UAV:
  798. return m_UAVsLinkInfo[rangeID];
  799. case DXIL::ResourceClass::CBuffer:
  800. return m_CBuffersLinkInfo[rangeID];
  801. case DXIL::ResourceClass::Sampler:
  802. return m_SamplersLinkInfo[rangeID];
  803. default:
  804. DXASSERT(DXIL::ResourceClass::SRV == resClass,
  805. "else invalid resource class");
  806. return m_SRVsLinkInfo[rangeID];
  807. }
  808. }
  809. void DxilModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
  810. return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
  811. }
  812. void DxilModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
  813. return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
  814. }
  815. void DxilModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
  816. return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
  817. }
  818. template <typename TResource>
  819. static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
  820. std::unordered_set<unsigned> &immResID) {
  821. for (std::vector<std::unique_ptr<TResource>>::iterator p = vec.begin(); p != vec.end();) {
  822. std::vector<std::unique_ptr<TResource>>::iterator c = p++;
  823. if (immResID.count((*c)->GetID()) == 0) {
  824. p = vec.erase(c);
  825. }
  826. }
  827. }
  828. static void CollectUsedResource(Value *resID,
  829. std::unordered_set<Value *> &usedResID) {
  830. if (usedResID.count(resID) > 0)
  831. return;
  832. usedResID.insert(resID);
  833. if (ConstantInt *cResID = dyn_cast<ConstantInt>(resID)) {
  834. // Do nothing
  835. } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
  836. if (ZEI->getSrcTy()->isIntegerTy()) {
  837. IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
  838. if (ITy->getBitWidth() == 1) {
  839. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
  840. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
  841. }
  842. }
  843. } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
  844. CollectUsedResource(SI->getTrueValue(), usedResID);
  845. CollectUsedResource(SI->getFalseValue(), usedResID);
  846. } else if (PHINode *Phi = dyn_cast<PHINode>(resID)) {
  847. for (Use &U : Phi->incoming_values()) {
  848. CollectUsedResource(U.get(), usedResID);
  849. }
  850. }
  851. // TODO: resID could be other types of instructions depending on the compiler optimization.
  852. }
  853. static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
  854. std::unordered_set<Value *> &usedResID) {
  855. for (Value *V : usedResID) {
  856. if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
  857. immResID.insert(cResID->getLimitedValue());
  858. }
  859. }
  860. }
  861. void DxilModule::RemoveFunction(llvm::Function *F) {
  862. DXASSERT_NOMSG(F != nullptr);
  863. m_DxilFunctionPropsMap.erase(F);
  864. m_DxilEntrySignatureMap.erase(F);
  865. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  866. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  867. m_pOP->RemoveFunction(F);
  868. }
  869. void DxilModule::RemoveUnusedResources() {
  870. hlsl::OP *hlslOP = GetOP();
  871. Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
  872. if (createHandleFunc->user_empty()) {
  873. m_CBuffers.clear();
  874. m_UAVs.clear();
  875. m_SRVs.clear();
  876. m_Samplers.clear();
  877. createHandleFunc->eraseFromParent();
  878. return;
  879. }
  880. std::unordered_set<Value *> usedUAVID;
  881. std::unordered_set<Value *> usedSRVID;
  882. std::unordered_set<Value *> usedSamplerID;
  883. std::unordered_set<Value *> usedCBufID;
  884. // Collect used ID.
  885. for (User *U : createHandleFunc->users()) {
  886. CallInst *CI = cast<CallInst>(U);
  887. Value *vResClass =
  888. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  889. ConstantInt *cResClass = cast<ConstantInt>(vResClass);
  890. DXIL::ResourceClass resClass =
  891. static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
  892. // Skip unused resource handle.
  893. if (CI->user_empty())
  894. continue;
  895. Value *resID =
  896. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  897. switch (resClass) {
  898. case DXIL::ResourceClass::CBuffer:
  899. CollectUsedResource(resID, usedCBufID);
  900. break;
  901. case DXIL::ResourceClass::Sampler:
  902. CollectUsedResource(resID, usedSamplerID);
  903. break;
  904. case DXIL::ResourceClass::SRV:
  905. CollectUsedResource(resID, usedSRVID);
  906. break;
  907. case DXIL::ResourceClass::UAV:
  908. CollectUsedResource(resID, usedUAVID);
  909. break;
  910. default:
  911. DXASSERT(0, "invalid res class");
  912. break;
  913. }
  914. }
  915. std::unordered_set<unsigned> immUAVID;
  916. std::unordered_set<unsigned> immSRVID;
  917. std::unordered_set<unsigned> immSamplerID;
  918. std::unordered_set<unsigned> immCBufID;
  919. ConvertUsedResource(immUAVID, usedUAVID);
  920. RemoveResources(m_UAVs, immUAVID);
  921. ConvertUsedResource(immSRVID, usedSRVID);
  922. ConvertUsedResource(immSamplerID, usedSamplerID);
  923. ConvertUsedResource(immCBufID, usedCBufID);
  924. RemoveResources(m_SRVs, immSRVID);
  925. RemoveResources(m_Samplers, immSamplerID);
  926. RemoveResources(m_CBuffers, immCBufID);
  927. }
  928. DxilSignature &DxilModule::GetInputSignature() {
  929. return m_EntrySignature->InputSignature;
  930. }
  931. const DxilSignature &DxilModule::GetInputSignature() const {
  932. return m_EntrySignature->InputSignature;
  933. }
  934. DxilSignature &DxilModule::GetOutputSignature() {
  935. return m_EntrySignature->OutputSignature;
  936. }
  937. const DxilSignature &DxilModule::GetOutputSignature() const {
  938. return m_EntrySignature->OutputSignature;
  939. }
  940. DxilSignature &DxilModule::GetPatchConstantSignature() {
  941. return m_EntrySignature->PatchConstantSignature;
  942. }
  943. const DxilSignature &DxilModule::GetPatchConstantSignature() const {
  944. return m_EntrySignature->PatchConstantSignature;
  945. }
  946. const RootSignatureHandle &DxilModule::GetRootSignature() const {
  947. return *m_RootSignature;
  948. }
  949. bool DxilModule::HasDxilEntrySignature(llvm::Function *F) const {
  950. return m_DxilEntrySignatureMap.find(F) != m_DxilEntrySignatureMap.end();
  951. }
  952. DxilEntrySignature &DxilModule::GetDxilEntrySignature(llvm::Function *F) {
  953. DXASSERT(m_DxilEntrySignatureMap.count(F) != 0, "cannot find F in map");
  954. return *m_DxilEntrySignatureMap[F];
  955. }
  956. void DxilModule::ReplaceDxilEntrySignature(llvm::Function *F,
  957. llvm::Function *NewF) {
  958. DXASSERT(m_DxilEntrySignatureMap.count(F) != 0, "cannot find F in map");
  959. std::unique_ptr<DxilEntrySignature> Sig =
  960. std::move(m_DxilEntrySignatureMap[F]);
  961. m_DxilEntrySignatureMap.erase(F);
  962. m_DxilEntrySignatureMap[NewF] = std::move(Sig);
  963. }
  964. bool DxilModule::HasDxilFunctionProps(llvm::Function *F) const {
  965. return m_DxilFunctionPropsMap.find(F) != m_DxilFunctionPropsMap.end();
  966. }
  967. DxilFunctionProps &DxilModule::GetDxilFunctionProps(llvm::Function *F) {
  968. DXASSERT(m_DxilFunctionPropsMap.count(F) != 0, "cannot find F in map");
  969. return *m_DxilFunctionPropsMap[F];
  970. }
  971. void DxilModule::ReplaceDxilFunctionProps(llvm::Function *F,
  972. llvm::Function *NewF) {
  973. DXASSERT(m_DxilFunctionPropsMap.count(F) != 0, "cannot find F in map");
  974. std::unique_ptr<DxilFunctionProps> props =
  975. std::move(m_DxilFunctionPropsMap[F]);
  976. m_DxilFunctionPropsMap.erase(F);
  977. m_DxilFunctionPropsMap[NewF] = std::move(props);
  978. }
  979. void DxilModule::StripRootSignatureFromMetadata() {
  980. NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
  981. if (pRootSignatureNamedMD) {
  982. GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
  983. }
  984. }
  985. void DxilModule::UpdateValidatorVersionMetadata() {
  986. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  987. }
  988. void DxilModule::ResetEntrySignature(DxilEntrySignature *pValue) {
  989. m_EntrySignature.reset(pValue);
  990. }
  991. void DxilModule::ResetRootSignature(RootSignatureHandle *pValue) {
  992. m_RootSignature.reset(pValue);
  993. }
  994. DxilTypeSystem &DxilModule::GetTypeSystem() {
  995. return *m_pTypeSystem;
  996. }
  997. DxilViewIdState &DxilModule::GetViewIdState() {
  998. return *m_pViewIdState;
  999. }
  1000. const DxilViewIdState &DxilModule::GetViewIdState() const {
  1001. return *m_pViewIdState;
  1002. }
  1003. void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
  1004. m_pTypeSystem.reset(pValue);
  1005. }
  1006. void DxilModule::ResetOP(hlsl::OP *hlslOP) { m_pOP.reset(hlslOP); }
  1007. void DxilModule::ResetFunctionPropsMap(
  1008. std::unordered_map<llvm::Function *, std::unique_ptr<DxilFunctionProps>>
  1009. &&propsMap) {
  1010. m_DxilFunctionPropsMap = std::move(propsMap);
  1011. }
  1012. void DxilModule::ResetEntrySignatureMap(
  1013. std::unordered_map<llvm::Function *, std::unique_ptr<DxilEntrySignature>>
  1014. &&SigMap) {
  1015. m_DxilEntrySignatureMap = std::move(SigMap);
  1016. }
  1017. void DxilModule::EmitLLVMUsed() {
  1018. if (m_LLVMUsed.empty())
  1019. return;
  1020. vector<llvm::Constant*> GVs;
  1021. Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
  1022. GVs.resize(m_LLVMUsed.size());
  1023. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  1024. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  1025. PointerType * pPtrType = dyn_cast<PointerType>(pConst->getType());
  1026. if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
  1027. // Cast pointer to addrspace 0, as LLVMUsed elements must have the same type.
  1028. GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
  1029. } else {
  1030. GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
  1031. }
  1032. }
  1033. ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
  1034. StringRef llvmUsedName = "llvm.used";
  1035. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  1036. oldGV->eraseFromParent();
  1037. }
  1038. GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
  1039. GlobalValue::AppendingLinkage,
  1040. ConstantArray::get(pATy, GVs),
  1041. llvmUsedName);
  1042. pGV->setSection("llvm.metadata");
  1043. }
  1044. vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
  1045. return m_LLVMUsed;
  1046. }
  1047. // DXIL metadata serialization/deserialization.
  1048. void DxilModule::ClearDxilMetadata(Module &M) {
  1049. // Delete: DXIL version, validator version, DXIL shader model,
  1050. // entry point tuples (shader properties, signatures, resources)
  1051. // type system, view ID state, LLVM used, entry point tuples,
  1052. // root signature, function properties.
  1053. // Other cases for libs pending.
  1054. // LLVM used is a global variable - handle separately.
  1055. Module::named_metadata_iterator
  1056. b = M.named_metadata_begin(),
  1057. e = M.named_metadata_end();
  1058. SmallVector<NamedMDNode*, 8> nodes;
  1059. for (; b != e; ++b) {
  1060. StringRef name = b->getName();
  1061. if (name == DxilMDHelper::kDxilVersionMDName ||
  1062. name == DxilMDHelper::kDxilValidatorVersionMDName ||
  1063. name == DxilMDHelper::kDxilShaderModelMDName ||
  1064. name == DxilMDHelper::kDxilEntryPointsMDName ||
  1065. name == DxilMDHelper::kDxilRootSignatureMDName ||
  1066. name == DxilMDHelper::kDxilResourcesMDName ||
  1067. name == DxilMDHelper::kDxilTypeSystemMDName ||
  1068. name == DxilMDHelper::kDxilViewIdStateMDName ||
  1069. name == DxilMDHelper::kDxilFunctionPropertiesMDName || // used in libraries
  1070. name == DxilMDHelper::kDxilEntrySignaturesMDName || // used in libraries
  1071. name == DxilMDHelper::kDxilResourcesLinkInfoMDName || // used in libraries
  1072. name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
  1073. nodes.push_back(b);
  1074. }
  1075. }
  1076. for (size_t i = 0; i < nodes.size(); ++i) {
  1077. M.eraseNamedMetadata(nodes[i]);
  1078. }
  1079. }
  1080. void DxilModule::EmitDxilMetadata() {
  1081. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  1082. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  1083. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  1084. MDTuple *pMDProperties = EmitDxilShaderProperties();
  1085. MDTuple *pMDSignatures = m_pMDHelper->EmitDxilSignatures(*m_EntrySignature);
  1086. MDTuple *pMDResources = EmitDxilResources();
  1087. if (pMDResources)
  1088. m_pMDHelper->EmitDxilResources(pMDResources);
  1089. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  1090. if (!m_pSM->IsLib() && !m_pSM->IsCS() &&
  1091. ((m_ValMajor == 0 && m_ValMinor == 0) ||
  1092. (m_ValMajor > 1 || (m_ValMajor == 1 && m_ValMinor >= 1)))) {
  1093. m_pMDHelper->EmitDxilViewIdState(GetViewIdState());
  1094. }
  1095. EmitLLVMUsed();
  1096. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  1097. vector<MDNode *> Entries;
  1098. Entries.emplace_back(pEntry);
  1099. m_pMDHelper->EmitDxilEntryPoints(Entries);
  1100. if (!m_RootSignature->IsEmpty()) {
  1101. m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
  1102. }
  1103. if (m_pSM->IsLib()) {
  1104. EmitDxilResourcesLinkInfo();
  1105. NamedMDNode *fnProps = m_pModule->getOrInsertNamedMetadata(
  1106. DxilMDHelper::kDxilFunctionPropertiesMDName);
  1107. for (auto &&pair : m_DxilFunctionPropsMap) {
  1108. const hlsl::DxilFunctionProps *props = pair.second.get();
  1109. MDTuple *pProps = m_pMDHelper->EmitDxilFunctionProps(props, pair.first);
  1110. fnProps->addOperand(pProps);
  1111. }
  1112. NamedMDNode *entrySigs = m_pModule->getOrInsertNamedMetadata(
  1113. DxilMDHelper::kDxilEntrySignaturesMDName);
  1114. for (auto &&pair : m_DxilEntrySignatureMap) {
  1115. Function *F = pair.first;
  1116. DxilEntrySignature *Sig = pair.second.get();
  1117. MDTuple *pSig = m_pMDHelper->EmitDxilSignatures(*Sig);
  1118. entrySigs->addOperand(
  1119. MDTuple::get(m_Ctx, {ValueAsMetadata::get(F), pSig}));
  1120. }
  1121. }
  1122. }
  1123. bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
  1124. return DxilMDHelper::IsKnownNamedMetaData(Node);
  1125. }
  1126. void DxilModule::LoadDxilMetadata() {
  1127. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  1128. m_pMDHelper->LoadValidatorVersion(m_ValMajor, m_ValMinor);
  1129. const ShaderModel *loadedModule;
  1130. m_pMDHelper->LoadDxilShaderModel(loadedModule);
  1131. SetShaderModel(loadedModule);
  1132. DXASSERT(m_EntrySignature != nullptr, "else SetShaderModel didn't create entry signature");
  1133. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  1134. IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
  1135. Function *pEntryFunc;
  1136. string EntryName;
  1137. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  1138. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
  1139. SetEntryFunction(pEntryFunc);
  1140. SetEntryFunctionName(EntryName);
  1141. LoadDxilShaderProperties(*pProperties);
  1142. m_pMDHelper->LoadDxilSignatures(*pSignatures, *m_EntrySignature);
  1143. LoadDxilResources(*pResources);
  1144. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  1145. m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
  1146. m_pMDHelper->LoadDxilViewIdState(*m_pViewIdState.get());
  1147. if (loadedModule->IsLib()) {
  1148. LoadDxilResourcesLinkInfo();
  1149. NamedMDNode *fnProps = m_pModule->getNamedMetadata(
  1150. DxilMDHelper::kDxilFunctionPropertiesMDName);
  1151. size_t propIdx = 0;
  1152. while (propIdx < fnProps->getNumOperands()) {
  1153. MDTuple *pProps = dyn_cast<MDTuple>(fnProps->getOperand(propIdx++));
  1154. std::unique_ptr<hlsl::DxilFunctionProps> props =
  1155. llvm::make_unique<hlsl::DxilFunctionProps>();
  1156. Function *F = m_pMDHelper->LoadDxilFunctionProps(pProps, props.get());
  1157. m_DxilFunctionPropsMap[F] = std::move(props);
  1158. }
  1159. NamedMDNode *entrySigs = m_pModule->getOrInsertNamedMetadata(
  1160. DxilMDHelper::kDxilEntrySignaturesMDName);
  1161. size_t sigIdx = 0;
  1162. while (sigIdx < entrySigs->getNumOperands()) {
  1163. MDTuple *pSig = dyn_cast<MDTuple>(entrySigs->getOperand(sigIdx++));
  1164. unsigned idx = 0;
  1165. Function *F = dyn_cast<Function>(
  1166. dyn_cast<ValueAsMetadata>(pSig->getOperand(idx++))->getValue());
  1167. // Entry must have props.
  1168. IFTBOOL(m_DxilFunctionPropsMap.count(F), DXC_E_INCORRECT_DXIL_METADATA);
  1169. DXIL::ShaderKind shaderKind = m_DxilFunctionPropsMap[F]->shaderKind;
  1170. std::unique_ptr<hlsl::DxilEntrySignature> Sig =
  1171. llvm::make_unique<hlsl::DxilEntrySignature>(shaderKind, !m_ShaderFlags.GetUseNativeLowPrecision());
  1172. m_pMDHelper->LoadDxilSignatures(pSig->getOperand(idx), *Sig);
  1173. m_DxilEntrySignatureMap[F] = std::move(Sig);
  1174. }
  1175. }
  1176. }
  1177. MDTuple *DxilModule::EmitDxilResources() {
  1178. // Emit SRV records.
  1179. MDTuple *pTupleSRVs = nullptr;
  1180. if (!m_SRVs.empty()) {
  1181. vector<Metadata *> MDVals;
  1182. for (size_t i = 0; i < m_SRVs.size(); i++) {
  1183. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  1184. }
  1185. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  1186. }
  1187. // Emit UAV records.
  1188. MDTuple *pTupleUAVs = nullptr;
  1189. if (!m_UAVs.empty()) {
  1190. vector<Metadata *> MDVals;
  1191. for (size_t i = 0; i < m_UAVs.size(); i++) {
  1192. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  1193. }
  1194. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  1195. }
  1196. // Emit CBuffer records.
  1197. MDTuple *pTupleCBuffers = nullptr;
  1198. if (!m_CBuffers.empty()) {
  1199. vector<Metadata *> MDVals;
  1200. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  1201. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  1202. }
  1203. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  1204. }
  1205. // Emit Sampler records.
  1206. MDTuple *pTupleSamplers = nullptr;
  1207. if (!m_Samplers.empty()) {
  1208. vector<Metadata *> MDVals;
  1209. for (size_t i = 0; i < m_Samplers.size(); i++) {
  1210. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  1211. }
  1212. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  1213. }
  1214. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  1215. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  1216. } else {
  1217. return nullptr;
  1218. }
  1219. }
  1220. void DxilModule::ReEmitDxilResources() {
  1221. ClearDxilMetadata(*m_pModule);
  1222. if (!m_pSM->IsCS() && !m_pSM->IsLib())
  1223. m_pViewIdState->Compute();
  1224. EmitDxilMetadata();
  1225. }
  1226. void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
  1227. if (MDO.get() == nullptr)
  1228. return;
  1229. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  1230. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  1231. // Load SRV records.
  1232. if (pSRVs != nullptr) {
  1233. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  1234. unique_ptr<DxilResource> pSRV(new DxilResource);
  1235. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  1236. AddSRV(std::move(pSRV));
  1237. }
  1238. }
  1239. // Load UAV records.
  1240. if (pUAVs != nullptr) {
  1241. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  1242. unique_ptr<DxilResource> pUAV(new DxilResource);
  1243. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  1244. AddUAV(std::move(pUAV));
  1245. }
  1246. }
  1247. // Load CBuffer records.
  1248. if (pCBuffers != nullptr) {
  1249. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  1250. unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
  1251. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  1252. AddCBuffer(std::move(pCB));
  1253. }
  1254. }
  1255. // Load Sampler records.
  1256. if (pSamplers != nullptr) {
  1257. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  1258. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  1259. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  1260. AddSampler(std::move(pSampler));
  1261. }
  1262. }
  1263. }
  1264. static MDTuple *CreateResourcesLinkInfo(std::vector<DxilModule::ResourceLinkInfo> &LinkInfoList,
  1265. unsigned size, LLVMContext &Ctx) {
  1266. DXASSERT(size == LinkInfoList.size(), "link info size must match resource size");
  1267. if (LinkInfoList.empty())
  1268. return nullptr;
  1269. vector<Metadata *> MDVals;
  1270. for (size_t i = 0; i < size; i++) {
  1271. MDVals.emplace_back(ValueAsMetadata::get(LinkInfoList[i].ResRangeID));
  1272. }
  1273. return MDNode::get(Ctx, MDVals);
  1274. }
  1275. void DxilModule::EmitDxilResourcesLinkInfo() {
  1276. // Emit SRV base records.
  1277. MDTuple *pTupleSRVs =
  1278. CreateResourcesLinkInfo(m_SRVsLinkInfo, m_SRVs.size(), m_Ctx);
  1279. // Emit UAV base records.
  1280. MDTuple *pTupleUAVs =
  1281. CreateResourcesLinkInfo(m_UAVsLinkInfo, m_UAVs.size(), m_Ctx);
  1282. // Emit CBuffer base records.
  1283. MDTuple *pTupleCBuffers =
  1284. CreateResourcesLinkInfo(m_CBuffersLinkInfo, m_CBuffers.size(), m_Ctx);
  1285. // Emit Sampler records.
  1286. MDTuple *pTupleSamplers =
  1287. CreateResourcesLinkInfo(m_SamplersLinkInfo, m_Samplers.size(), m_Ctx);
  1288. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr ||
  1289. pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  1290. m_pMDHelper->EmitDxilResourceLinkInfoTuple(pTupleSRVs, pTupleUAVs,
  1291. pTupleCBuffers, pTupleSamplers);
  1292. }
  1293. }
  1294. static void
  1295. LoadResourcesLinkInfo(const llvm::MDTuple *pMD,
  1296. std::vector<DxilModule::ResourceLinkInfo> &LinkInfoList,
  1297. unsigned size, DxilMDHelper *pMDHelper) {
  1298. if (!pMD) {
  1299. IFTBOOL(size == 0, DXC_E_INCORRECT_DXIL_METADATA);
  1300. return;
  1301. }
  1302. unsigned operandSize = pMD->getNumOperands();
  1303. IFTBOOL(operandSize == size, DXC_E_INCORRECT_DXIL_METADATA);
  1304. for (unsigned i = 0; i < operandSize; i++) {
  1305. Constant *rangeID =
  1306. dyn_cast<Constant>(pMDHelper->ValueMDToValue(pMD->getOperand(i)));
  1307. LinkInfoList.emplace_back(DxilModule::ResourceLinkInfo{rangeID});
  1308. }
  1309. }
  1310. void DxilModule::LoadDxilResourcesLinkInfo() {
  1311. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  1312. m_pMDHelper->LoadDxilResourceLinkInfoTuple(pSRVs, pUAVs, pCBuffers,
  1313. pSamplers);
  1314. // Load SRV base records.
  1315. LoadResourcesLinkInfo(pSRVs, m_SRVsLinkInfo, m_SRVs.size(),
  1316. m_pMDHelper.get());
  1317. // Load UAV base records.
  1318. LoadResourcesLinkInfo(pUAVs, m_UAVsLinkInfo, m_UAVs.size(),
  1319. m_pMDHelper.get());
  1320. // Load CBuffer records.
  1321. LoadResourcesLinkInfo(pCBuffers, m_CBuffersLinkInfo, m_CBuffers.size(),
  1322. m_pMDHelper.get());
  1323. // Load Sampler records.
  1324. LoadResourcesLinkInfo(pSamplers, m_SamplersLinkInfo, m_Samplers.size(),
  1325. m_pMDHelper.get());
  1326. }
  1327. MDTuple *DxilModule::EmitDxilShaderProperties() {
  1328. vector<Metadata *> MDVals;
  1329. // DXIL shader flags.
  1330. uint64_t flag = m_ShaderFlags.GetShaderFlagsRaw();
  1331. if (flag != 0) {
  1332. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilShaderFlagsTag));
  1333. MDVals.emplace_back(m_pMDHelper->Uint64ToConstMD(flag));
  1334. }
  1335. // Compute shader.
  1336. if (m_pSM->IsCS()) {
  1337. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilNumThreadsTag));
  1338. vector<Metadata *> NumThreadVals;
  1339. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[0]));
  1340. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[1]));
  1341. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[2]));
  1342. MDVals.emplace_back(MDNode::get(m_Ctx, NumThreadVals));
  1343. }
  1344. // Geometry shader.
  1345. if (m_pSM->IsGS()) {
  1346. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilGSStateTag));
  1347. MDTuple *pMDTuple = m_pMDHelper->EmitDxilGSState(m_InputPrimitive,
  1348. m_MaxVertexCount,
  1349. GetActiveStreamMask(),
  1350. m_StreamPrimitiveTopology,
  1351. m_NumGSInstances);
  1352. MDVals.emplace_back(pMDTuple);
  1353. }
  1354. // Domain shader.
  1355. if (m_pSM->IsDS()) {
  1356. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilDSStateTag));
  1357. MDTuple *pMDTuple = m_pMDHelper->EmitDxilDSState(m_TessellatorDomain,
  1358. m_InputControlPointCount);
  1359. MDVals.emplace_back(pMDTuple);
  1360. }
  1361. // Hull shader.
  1362. if (m_pSM->IsHS()) {
  1363. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilHSStateTag));
  1364. MDTuple *pMDTuple = m_pMDHelper->EmitDxilHSState(m_pPatchConstantFunc,
  1365. m_InputControlPointCount,
  1366. m_OutputControlPointCount,
  1367. m_TessellatorDomain,
  1368. m_TessellatorPartitioning,
  1369. m_TessellatorOutputPrimitive,
  1370. m_MaxTessellationFactor);
  1371. MDVals.emplace_back(pMDTuple);
  1372. }
  1373. if (!MDVals.empty())
  1374. return MDNode::get(m_Ctx, MDVals);
  1375. else
  1376. return nullptr;
  1377. }
  1378. void DxilModule::LoadDxilShaderProperties(const MDOperand &MDO) {
  1379. if (MDO.get() == nullptr)
  1380. return;
  1381. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  1382. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  1383. IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0, DXC_E_INCORRECT_DXIL_METADATA);
  1384. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
  1385. unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
  1386. const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
  1387. IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  1388. switch (Tag) {
  1389. case DxilMDHelper::kDxilShaderFlagsTag:
  1390. m_ShaderFlags.SetShaderFlagsRaw(DxilMDHelper::ConstMDToUint64(MDO));
  1391. break;
  1392. case DxilMDHelper::kDxilNumThreadsTag: {
  1393. MDNode *pNode = cast<MDNode>(MDO.get());
  1394. m_NumThreads[0] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(0));
  1395. m_NumThreads[1] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(1));
  1396. m_NumThreads[2] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(2));
  1397. break;
  1398. }
  1399. case DxilMDHelper::kDxilGSStateTag: {
  1400. m_pMDHelper->LoadDxilGSState(MDO, m_InputPrimitive, m_MaxVertexCount, m_ActiveStreamMask,
  1401. m_StreamPrimitiveTopology, m_NumGSInstances);
  1402. break;
  1403. }
  1404. case DxilMDHelper::kDxilDSStateTag:
  1405. m_pMDHelper->LoadDxilDSState(MDO, m_TessellatorDomain, m_InputControlPointCount);
  1406. break;
  1407. case DxilMDHelper::kDxilHSStateTag:
  1408. m_pMDHelper->LoadDxilHSState(MDO,
  1409. m_pPatchConstantFunc,
  1410. m_InputControlPointCount,
  1411. m_OutputControlPointCount,
  1412. m_TessellatorDomain,
  1413. m_TessellatorPartitioning,
  1414. m_TessellatorOutputPrimitive,
  1415. m_MaxTessellationFactor);
  1416. break;
  1417. default:
  1418. DXASSERT(false, "Unknown extended shader properties tag");
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. void DxilModule::StripDebugRelatedCode() {
  1424. // Remove all users of global resources.
  1425. for (GlobalVariable &GV : m_pModule->globals()) {
  1426. if (GV.hasInternalLinkage())
  1427. continue;
  1428. if (GV.getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
  1429. continue;
  1430. for (auto git = GV.user_begin(); git != GV.user_end();) {
  1431. User *U = *(git++);
  1432. // Try to remove load of GV.
  1433. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  1434. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1435. Instruction *LIUser = cast<Instruction>(*(it++));
  1436. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1437. Value *Ptr = SI->getPointerOperand();
  1438. SI->eraseFromParent();
  1439. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1440. if (Ptr->user_empty())
  1441. PtrInst->eraseFromParent();
  1442. }
  1443. }
  1444. }
  1445. if (LI->user_empty())
  1446. LI->eraseFromParent();
  1447. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
  1448. for (auto GEPIt = GEP->user_begin(); GEPIt != GEP->user_end();) {
  1449. User *GEPU = *(GEPIt++);
  1450. // Try to remove load of GEP.
  1451. if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
  1452. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1453. Instruction *LIUser = cast<Instruction>(*(it++));
  1454. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1455. Value *Ptr = SI->getPointerOperand();
  1456. SI->eraseFromParent();
  1457. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1458. if (Ptr->user_empty())
  1459. PtrInst->eraseFromParent();
  1460. }
  1461. }
  1462. if (LI->user_empty())
  1463. LI->eraseFromParent();
  1464. }
  1465. }
  1466. }
  1467. if (GEP->user_empty())
  1468. GEP->eraseFromParent();
  1469. }
  1470. }
  1471. }
  1472. }
  1473. DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
  1474. if (m_pDebugInfoFinder == nullptr) {
  1475. m_pDebugInfoFinder = std::make_unique<llvm::DebugInfoFinder>();
  1476. m_pDebugInfoFinder->processModule(*m_pModule);
  1477. }
  1478. return *m_pDebugInfoFinder;
  1479. }
  1480. hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule(llvm::Module *pModule) {
  1481. LLVMContext &Ctx = pModule->getContext();
  1482. std::string diagStr;
  1483. raw_string_ostream diagStream(diagStr);
  1484. hlsl::DxilModule *pDxilModule = nullptr;
  1485. // TODO: add detail error in DxilMDHelper.
  1486. try {
  1487. pDxilModule = &pModule->GetOrCreateDxilModule();
  1488. } catch (const ::hlsl::Exception &hlslException) {
  1489. diagStream << "load dxil metadata failed -";
  1490. try {
  1491. const char *msg = hlslException.what();
  1492. if (msg == nullptr || *msg == '\0')
  1493. diagStream << " error code " << hlslException.hr << "\n";
  1494. else
  1495. diagStream << msg;
  1496. } catch (...) {
  1497. diagStream << " unable to retrieve error message.\n";
  1498. }
  1499. Ctx.diagnose(DxilErrorDiagnosticInfo(diagStream.str().c_str()));
  1500. } catch (...) {
  1501. Ctx.diagnose(DxilErrorDiagnosticInfo("load dxil metadata failed - unknown error.\n"));
  1502. }
  1503. return pDxilModule;
  1504. }
  1505. // Check if the instruction has fast math flags configured to indicate
  1506. // the instruction is precise.
  1507. // Precise fast math flags means none of the fast math flags are set.
  1508. bool DxilModule::HasPreciseFastMathFlags(const Instruction *inst) {
  1509. return isa<FPMathOperator>(inst) && !inst->getFastMathFlags().any();
  1510. }
  1511. // Set fast math flags configured to indicate the instruction is precise.
  1512. void DxilModule::SetPreciseFastMathFlags(llvm::Instruction *inst) {
  1513. assert(isa<FPMathOperator>(inst));
  1514. inst->copyFastMathFlags(FastMathFlags());
  1515. }
  1516. // True if fast math flags are preserved across serialization/deserialization
  1517. // of the dxil module.
  1518. //
  1519. // We need to check for this when querying fast math flags for preciseness
  1520. // otherwise we will be overly conservative by reporting instructions precise
  1521. // because their fast math flags were not preserved.
  1522. //
  1523. // Currently we restrict it to the instruction types that have fast math
  1524. // preserved in the bitcode. We can expand this by converting fast math
  1525. // flags to dx.precise metadata during serialization and back to fast
  1526. // math flags during deserialization.
  1527. bool DxilModule::PreservesFastMathFlags(const llvm::Instruction *inst) {
  1528. return
  1529. isa<FPMathOperator>(inst) && (isa<BinaryOperator>(inst) || isa<FCmpInst>(inst));
  1530. }
  1531. bool DxilModule::IsPrecise(const Instruction *inst) const {
  1532. if (m_ShaderFlags.GetDisableMathRefactoring())
  1533. return true;
  1534. else if (DxilMDHelper::IsMarkedPrecise(inst))
  1535. return true;
  1536. else if (PreservesFastMathFlags(inst))
  1537. return HasPreciseFastMathFlags(inst);
  1538. else
  1539. return false;
  1540. }
  1541. } // namespace hlsl
  1542. namespace llvm {
  1543. hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
  1544. std::unique_ptr<hlsl::DxilModule> M;
  1545. if (!HasDxilModule()) {
  1546. M = std::make_unique<hlsl::DxilModule>(this);
  1547. if (!skipInit) {
  1548. M->LoadDxilMetadata();
  1549. }
  1550. SetDxilModule(M.release());
  1551. }
  1552. return GetDxilModule();
  1553. }
  1554. void Module::ResetDxilModule() {
  1555. if (HasDxilModule()) {
  1556. delete TheDxilModule;
  1557. TheDxilModule = nullptr;
  1558. }
  1559. }
  1560. }