2
0

DxilModule.cpp 59 KB

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