DxilModule.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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 "llvm/IR/Constants.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/IR/Metadata.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/DebugInfo.h"
  23. #include "llvm/IR/DiagnosticInfo.h"
  24. #include "llvm/IR/DiagnosticPrinter.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <unordered_set>
  27. using namespace llvm;
  28. using std::string;
  29. using std::vector;
  30. using std::unique_ptr;
  31. namespace {
  32. class DxilErrorDiagnosticInfo : public DiagnosticInfo {
  33. private:
  34. const char *m_message;
  35. public:
  36. DxilErrorDiagnosticInfo(const char *str)
  37. : DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
  38. m_message(str) { }
  39. __override void print(DiagnosticPrinter &DP) const {
  40. DP << m_message;
  41. }
  42. };
  43. } // anon namespace
  44. namespace hlsl {
  45. //------------------------------------------------------------------------------
  46. //
  47. // DxilModule methods.
  48. //
  49. DxilModule::DxilModule(Module *pModule)
  50. : m_Ctx(pModule->getContext())
  51. , m_pModule(pModule)
  52. , m_pOP(std::make_unique<OP>(pModule->getContext(), pModule))
  53. , m_pTypeSystem(std::make_unique<DxilTypeSystem>(pModule))
  54. , m_pViewIdState(std::make_unique<DxilViewIdState>(this))
  55. , m_pMDHelper(std::make_unique<DxilMDHelper>(pModule, std::make_unique<DxilExtraPropertyHelper>(pModule)))
  56. , m_pDebugInfoFinder(nullptr)
  57. , m_pEntryFunc(nullptr)
  58. , m_EntryName("")
  59. , m_pPatchConstantFunc(nullptr)
  60. , m_pSM(nullptr)
  61. , m_DxilMajor(DXIL::kDxilMajor)
  62. , m_DxilMinor(DXIL::kDxilMinor)
  63. , m_ValMajor(1)
  64. , m_ValMinor(0)
  65. , m_InputPrimitive(DXIL::InputPrimitive::Undefined)
  66. , m_MaxVertexCount(0)
  67. , m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
  68. , m_ActiveStreamMask(0)
  69. , m_NumGSInstances(1)
  70. , m_InputControlPointCount(0)
  71. , m_TessellatorDomain(DXIL::TessellatorDomain::Undefined)
  72. , m_OutputControlPointCount(0)
  73. , m_TessellatorPartitioning(DXIL::TessellatorPartitioning::Undefined)
  74. , m_TessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive::Undefined)
  75. , m_MaxTessellationFactor(0.f)
  76. , m_RootSignature(nullptr) {
  77. DXASSERT_NOMSG(m_pModule != nullptr);
  78. m_NumThreads[0] = m_NumThreads[1] = m_NumThreads[2] = 0;
  79. #if defined(_DEBUG) || defined(DBG)
  80. // Pin LLVM dump methods.
  81. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  82. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  83. void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
  84. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  85. m_pUnused -= (size_t)&pfnViewCFGOnly;
  86. #endif
  87. }
  88. DxilModule::~DxilModule() {
  89. }
  90. DxilModule::ShaderFlags::ShaderFlags():
  91. m_bDisableOptimizations(false)
  92. , m_bDisableMathRefactoring(false)
  93. , m_bEnableDoublePrecision(false)
  94. , m_bForceEarlyDepthStencil(false)
  95. , m_bEnableRawAndStructuredBuffers(false)
  96. , m_bEnableMinPrecision(false)
  97. , m_bEnableDoubleExtensions(false)
  98. , m_bEnableMSAD(false)
  99. , m_bAllResourcesBound(false)
  100. , m_bViewportAndRTArrayIndex(false)
  101. , m_bInnerCoverage(false)
  102. , m_bStencilRef(false)
  103. , m_bTiledResources(false)
  104. , m_bUAVLoadAdditionalFormats(false)
  105. , m_bLevel9ComparisonFiltering(false)
  106. , m_bCSRawAndStructuredViaShader4X(false)
  107. , m_b64UAVs(false)
  108. , m_UAVsAtEveryStage(false)
  109. , m_bROVS(false)
  110. , m_bWaveOps(false)
  111. , m_bInt64Ops(false)
  112. , m_bViewID(false)
  113. , m_bBarycentrics(false)
  114. , m_align0(0)
  115. , m_align1(0)
  116. {}
  117. LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
  118. Module *DxilModule::GetModule() const { return m_pModule; }
  119. OP *DxilModule::GetOP() const { return m_pOP.get(); }
  120. void DxilModule::SetShaderModel(const ShaderModel *pSM) {
  121. DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM), "shader model must not change for the module");
  122. DXASSERT(pSM != nullptr && pSM->IsValidForDxil(), "shader model must be valid");
  123. m_pSM = pSM;
  124. m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
  125. m_pMDHelper->SetShaderModel(m_pSM);
  126. DXIL::ShaderKind shaderKind = pSM->GetKind();
  127. m_InputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Input));
  128. m_OutputSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::Output));
  129. m_PatchConstantSignature.reset(new DxilSignature(shaderKind, DXIL::SignatureKind::PatchConstant));
  130. m_RootSignature.reset(new RootSignatureHandle());
  131. }
  132. const ShaderModel *DxilModule::GetShaderModel() const {
  133. return m_pSM;
  134. }
  135. void DxilModule::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  136. DxilMajor = m_DxilMajor;
  137. DxilMinor = m_DxilMinor;
  138. }
  139. void DxilModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  140. m_ValMajor = ValMajor;
  141. m_ValMinor = ValMinor;
  142. }
  143. bool DxilModule::UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  144. if (ValMajor > m_ValMajor || (ValMajor == m_ValMajor && ValMinor > m_ValMinor)) {
  145. // Module requires higher validator version than previously set
  146. SetValidatorVersion(ValMajor, ValMinor);
  147. return true;
  148. }
  149. return false;
  150. }
  151. void DxilModule::GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  152. ValMajor = m_ValMajor;
  153. ValMinor = m_ValMinor;
  154. }
  155. bool DxilModule::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  156. if (!m_pSM)
  157. return false;
  158. m_pSM->GetMinValidatorVersion(ValMajor, ValMinor);
  159. if (ValMajor == 1 && ValMinor == 0 && (m_ShaderFlags.GetFeatureInfo() & hlsl::ShaderFeatureInfo_ViewID))
  160. ValMinor = 1;
  161. return true;
  162. }
  163. bool DxilModule::UpgradeToMinValidatorVersion() {
  164. unsigned ValMajor = 1, ValMinor = 0;
  165. if (GetMinValidatorVersion(ValMajor, ValMinor)) {
  166. return UpgradeValidatorVersion(ValMajor, ValMinor);
  167. }
  168. return false;
  169. }
  170. Function *DxilModule::GetEntryFunction() {
  171. return m_pEntryFunc;
  172. }
  173. const Function *DxilModule::GetEntryFunction() const {
  174. return m_pEntryFunc;
  175. }
  176. void DxilModule::SetEntryFunction(Function *pEntryFunc) {
  177. m_pEntryFunc = pEntryFunc;
  178. }
  179. const string &DxilModule::GetEntryFunctionName() const {
  180. return m_EntryName;
  181. }
  182. void DxilModule::SetEntryFunctionName(const string &name) {
  183. m_EntryName = name;
  184. }
  185. llvm::Function *DxilModule::GetPatchConstantFunction() {
  186. return m_pPatchConstantFunc;
  187. }
  188. const llvm::Function *DxilModule::GetPatchConstantFunction() const {
  189. return m_pPatchConstantFunc;
  190. }
  191. void DxilModule::SetPatchConstantFunction(llvm::Function *pFunc) {
  192. m_pPatchConstantFunc = pFunc;
  193. }
  194. unsigned DxilModule::ShaderFlags::GetGlobalFlags() const {
  195. unsigned Flags = 0;
  196. Flags |= m_bDisableOptimizations ? DXIL::kDisableOptimizations : 0;
  197. Flags |= m_bDisableMathRefactoring ? DXIL::kDisableMathRefactoring : 0;
  198. Flags |= m_bEnableDoublePrecision ? DXIL::kEnableDoublePrecision : 0;
  199. Flags |= m_bForceEarlyDepthStencil ? DXIL::kForceEarlyDepthStencil : 0;
  200. Flags |= m_bEnableRawAndStructuredBuffers ? DXIL::kEnableRawAndStructuredBuffers : 0;
  201. Flags |= m_bEnableMinPrecision ? DXIL::kEnableMinPrecision : 0;
  202. Flags |= m_bEnableDoubleExtensions ? DXIL::kEnableDoubleExtensions : 0;
  203. Flags |= m_bEnableMSAD ? DXIL::kEnableMSAD : 0;
  204. Flags |= m_bAllResourcesBound ? DXIL::kAllResourcesBound : 0;
  205. return Flags;
  206. }
  207. uint64_t DxilModule::ShaderFlags::GetFeatureInfo() const {
  208. uint64_t Flags = 0;
  209. Flags |= m_bEnableDoublePrecision ? hlsl::ShaderFeatureInfo_Doubles : 0;
  210. Flags |= m_bEnableMinPrecision ? hlsl::ShaderFeatureInfo_MininumPrecision : 0;
  211. Flags |= m_bEnableDoubleExtensions ? hlsl::ShaderFeatureInfo_11_1_DoubleExtensions : 0;
  212. Flags |= m_bWaveOps ? hlsl::ShaderFeatureInfo_WaveOps : 0;
  213. Flags |= m_bInt64Ops ? hlsl::ShaderFeatureInfo_Int64Ops : 0;
  214. Flags |= m_bROVS ? hlsl::ShaderFeatureInfo_ROVs : 0;
  215. Flags |= m_bViewportAndRTArrayIndex ? hlsl::ShaderFeatureInfo_ViewportAndRTArrayIndexFromAnyShaderFeedingRasterizer : 0;
  216. Flags |= m_bInnerCoverage ? hlsl::ShaderFeatureInfo_InnerCoverage : 0;
  217. Flags |= m_bStencilRef ? hlsl::ShaderFeatureInfo_StencilRef : 0;
  218. Flags |= m_bTiledResources ? hlsl::ShaderFeatureInfo_TiledResources : 0;
  219. Flags |= m_bEnableMSAD ? hlsl::ShaderFeatureInfo_11_1_ShaderExtensions : 0;
  220. Flags |= m_bCSRawAndStructuredViaShader4X ? hlsl::ShaderFeatureInfo_ComputeShadersPlusRawAndStructuredBuffersViaShader4X : 0;
  221. Flags |= m_UAVsAtEveryStage ? hlsl::ShaderFeatureInfo_UAVsAtEveryStage : 0;
  222. Flags |= m_b64UAVs ? hlsl::ShaderFeatureInfo_64UAVs : 0;
  223. Flags |= m_bLevel9ComparisonFiltering ? hlsl::ShaderFeatureInfo_LEVEL9ComparisonFiltering : 0;
  224. Flags |= m_bUAVLoadAdditionalFormats ? hlsl::ShaderFeatureInfo_TypedUAVLoadAdditionalFormats : 0;
  225. Flags |= m_bViewID ? hlsl::ShaderFeatureInfo_ViewID : 0;
  226. Flags |= m_bBarycentrics ? hlsl::ShaderFeatureInfo_Barycentrics : 0;
  227. return Flags;
  228. }
  229. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRaw() const {
  230. union Cast {
  231. Cast(const DxilModule::ShaderFlags &flags) {
  232. shaderFlags = flags;
  233. }
  234. DxilModule::ShaderFlags shaderFlags;
  235. uint64_t rawData;
  236. };
  237. static_assert(sizeof(uint64_t) == sizeof(DxilModule::ShaderFlags),
  238. "size must match to make sure no undefined bits when cast");
  239. Cast rawCast(*this);
  240. return rawCast.rawData;
  241. }
  242. void DxilModule::ShaderFlags::SetShaderFlagsRaw(uint64_t data) {
  243. union Cast {
  244. Cast(uint64_t data) {
  245. rawData = data;
  246. }
  247. DxilModule::ShaderFlags shaderFlags;
  248. uint64_t rawData;
  249. };
  250. Cast rawCast(data);
  251. *this = rawCast.shaderFlags;
  252. }
  253. unsigned DxilModule::GetGlobalFlags() const {
  254. unsigned Flags = m_ShaderFlags.GetGlobalFlags();
  255. return Flags;
  256. }
  257. static bool IsResourceSingleComponent(llvm::Type *Ty) {
  258. if (llvm::ArrayType *arrType = llvm::dyn_cast<llvm::ArrayType>(Ty)) {
  259. if (arrType->getArrayNumElements() > 1) {
  260. return false;
  261. }
  262. return IsResourceSingleComponent(arrType->getArrayElementType());
  263. } else if (llvm::StructType *structType =
  264. llvm::dyn_cast<llvm::StructType>(Ty)) {
  265. if (structType->getStructNumElements() > 1) {
  266. return false;
  267. }
  268. return IsResourceSingleComponent(structType->getStructElementType(0));
  269. } else if (llvm::VectorType *vectorType =
  270. llvm::dyn_cast<llvm::VectorType>(Ty)) {
  271. if (vectorType->getNumElements() > 1) {
  272. return false;
  273. }
  274. return IsResourceSingleComponent(vectorType->getVectorElementType());
  275. }
  276. return true;
  277. }
  278. // Given a CreateHandle call, returns arbitrary ConstantInt rangeID
  279. // Note: HLSL is currently assuming that rangeID is a constant value, but this code is assuming
  280. // that it can be either constant, phi node, or select instruction
  281. static ConstantInt *GetArbitraryConstantRangeID(CallInst *handleCall) {
  282. Value *rangeID =
  283. handleCall->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  284. ConstantInt *ConstantRangeID = dyn_cast<ConstantInt>(rangeID);
  285. while (ConstantRangeID == nullptr) {
  286. if (ConstantInt *CI = dyn_cast<ConstantInt>(rangeID)) {
  287. ConstantRangeID = CI;
  288. } else if (PHINode *PN = dyn_cast<PHINode>(rangeID)) {
  289. rangeID = PN->getIncomingValue(0);
  290. } else if (SelectInst *SI = dyn_cast<SelectInst>(rangeID)) {
  291. rangeID = SI->getTrueValue();
  292. } else {
  293. return nullptr;
  294. }
  295. }
  296. return ConstantRangeID;
  297. }
  298. void DxilModule::CollectShaderFlags(ShaderFlags &Flags) {
  299. bool hasDouble = false;
  300. // ddiv dfma drcp d2i d2u i2d u2d.
  301. // fma has dxil op. Others should check IR instruction div/cast.
  302. bool hasDoubleExtension = false;
  303. bool has64Int = false;
  304. bool has16FloatInt = false;
  305. bool hasWaveOps = false;
  306. bool hasCheckAccessFully = false;
  307. bool hasMSAD = false;
  308. bool hasInnerCoverage = false;
  309. bool hasViewID = false;
  310. bool hasMulticomponentUAVLoads = false;
  311. bool hasMulticomponentUAVLoadsBackCompat = false;
  312. // Try to maintain compatibility with a v1.0 validator if that's what we have.
  313. {
  314. unsigned valMajor, valMinor;
  315. GetValidatorVersion(valMajor, valMinor);
  316. hasMulticomponentUAVLoadsBackCompat = valMajor <= 1 && valMinor == 0;
  317. }
  318. Type *int16Ty = Type::getInt16Ty(GetCtx());
  319. Type *int64Ty = Type::getInt64Ty(GetCtx());
  320. for (Function &F : GetModule()->functions()) {
  321. for (BasicBlock &BB : F.getBasicBlockList()) {
  322. for (Instruction &I : BB.getInstList()) {
  323. // Skip none dxil function call.
  324. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  325. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  326. continue;
  327. }
  328. Type *Ty = I.getType();
  329. bool isDouble = Ty->isDoubleTy();
  330. bool isHalf = Ty->isHalfTy();
  331. bool isInt16 = Ty == int16Ty;
  332. bool isInt64 = Ty == int64Ty;
  333. if (isa<ExtractElementInst>(&I) ||
  334. isa<InsertElementInst>(&I))
  335. continue;
  336. for (Value *operand : I.operands()) {
  337. Type *Ty = operand->getType();
  338. isDouble |= Ty->isDoubleTy();
  339. isHalf |= Ty->isHalfTy();
  340. isInt16 |= Ty == int16Ty;
  341. isInt64 |= Ty == int64Ty;
  342. }
  343. if (isDouble) {
  344. hasDouble = true;
  345. switch (I.getOpcode()) {
  346. case Instruction::FDiv:
  347. case Instruction::UIToFP:
  348. case Instruction::SIToFP:
  349. case Instruction::FPToUI:
  350. case Instruction::FPToSI:
  351. hasDoubleExtension = true;
  352. break;
  353. }
  354. }
  355. has16FloatInt |= isHalf;
  356. has16FloatInt |= isInt16;
  357. has64Int |= isInt64;
  358. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  359. if (!OP::IsDxilOpFunc(CI->getCalledFunction()))
  360. continue;
  361. Value *opcodeArg = CI->getArgOperand(DXIL::OperandIndex::kOpcodeIdx);
  362. ConstantInt *opcodeConst = dyn_cast<ConstantInt>(opcodeArg);
  363. DXASSERT(opcodeConst, "DXIL opcode arg must be immediate");
  364. unsigned opcode = opcodeConst->getLimitedValue();
  365. DXASSERT(opcode < static_cast<unsigned>(DXIL::OpCode::NumOpCodes),
  366. "invalid DXIL opcode");
  367. DXIL::OpCode dxilOp = static_cast<DXIL::OpCode>(opcode);
  368. if (hlsl::OP::IsDxilOpWave(dxilOp))
  369. hasWaveOps = true;
  370. switch (dxilOp) {
  371. case DXIL::OpCode::CheckAccessFullyMapped:
  372. hasCheckAccessFully = true;
  373. break;
  374. case DXIL::OpCode::Msad:
  375. hasMSAD = true;
  376. break;
  377. case DXIL::OpCode::BufferLoad:
  378. case DXIL::OpCode::TextureLoad: {
  379. if (hasMulticomponentUAVLoads) continue;
  380. // This is the old-style computation (overestimating requirements).
  381. Value *resHandle = CI->getArgOperand(DXIL::OperandIndex::kBufferStoreHandleOpIdx);
  382. CallInst *handleCall = cast<CallInst>(resHandle);
  383. if (ConstantInt *resClassArg =
  384. dyn_cast<ConstantInt>(handleCall->getArgOperand(
  385. DXIL::OperandIndex::kCreateHandleResClassOpIdx))) {
  386. DXIL::ResourceClass resClass = static_cast<DXIL::ResourceClass>(
  387. resClassArg->getLimitedValue());
  388. if (resClass == DXIL::ResourceClass::UAV) {
  389. // Validator 1.0 assumes that all uav load is multi component load.
  390. if (hasMulticomponentUAVLoadsBackCompat) {
  391. hasMulticomponentUAVLoads = true;
  392. continue;
  393. }
  394. else {
  395. ConstantInt *rangeID = GetArbitraryConstantRangeID(handleCall);
  396. if (rangeID) {
  397. DxilResource resource = GetUAV(rangeID->getLimitedValue());
  398. if ((resource.IsTypedBuffer() ||
  399. resource.IsAnyTexture()) &&
  400. !IsResourceSingleComponent(resource.GetRetType())) {
  401. hasMulticomponentUAVLoads = true;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. else {
  408. DXASSERT(false, "Resource class must be constant.");
  409. }
  410. } break;
  411. case DXIL::OpCode::Fma:
  412. hasDoubleExtension |= isDouble;
  413. break;
  414. case DXIL::OpCode::InnerCoverage:
  415. hasInnerCoverage = true;
  416. break;
  417. case DXIL::OpCode::ViewID:
  418. hasViewID = true;
  419. break;
  420. default:
  421. // Normal opcodes.
  422. break;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. Flags.SetEnableDoublePrecision(hasDouble);
  429. Flags.SetInt64Ops(has64Int);
  430. Flags.SetEnableMinPrecision(has16FloatInt);
  431. Flags.SetEnableDoubleExtensions(hasDoubleExtension);
  432. Flags.SetWaveOps(hasWaveOps);
  433. Flags.SetTiledResources(hasCheckAccessFully);
  434. Flags.SetEnableMSAD(hasMSAD);
  435. Flags.SetUAVLoadAdditionalFormats(hasMulticomponentUAVLoads);
  436. Flags.SetViewID(hasViewID);
  437. const ShaderModel *SM = GetShaderModel();
  438. if (SM->IsPS()) {
  439. bool hasStencilRef = false;
  440. DxilSignature &outS = GetOutputSignature();
  441. for (auto &&E : outS.GetElements()) {
  442. if (E->GetKind() == Semantic::Kind::StencilRef) {
  443. hasStencilRef = true;
  444. } else if (E->GetKind() == Semantic::Kind::InnerCoverage) {
  445. hasInnerCoverage = true;
  446. }
  447. }
  448. Flags.SetStencilRef(hasStencilRef);
  449. Flags.SetInnerCoverage(hasInnerCoverage);
  450. }
  451. bool checkInputRTArrayIndex =
  452. SM->IsGS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  453. if (checkInputRTArrayIndex) {
  454. bool hasViewportArrayIndex = false;
  455. bool hasRenderTargetArrayIndex = false;
  456. DxilSignature &inS = GetInputSignature();
  457. for (auto &E : inS.GetElements()) {
  458. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  459. hasViewportArrayIndex = true;
  460. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  461. hasRenderTargetArrayIndex = true;
  462. }
  463. }
  464. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  465. hasRenderTargetArrayIndex);
  466. }
  467. bool checkOutputRTArrayIndex =
  468. SM->IsVS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  469. if (checkOutputRTArrayIndex) {
  470. bool hasViewportArrayIndex = false;
  471. bool hasRenderTargetArrayIndex = false;
  472. DxilSignature &outS = GetOutputSignature();
  473. for (auto &E : outS.GetElements()) {
  474. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  475. hasViewportArrayIndex = true;
  476. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  477. hasRenderTargetArrayIndex = true;
  478. }
  479. }
  480. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  481. hasRenderTargetArrayIndex);
  482. }
  483. unsigned NumUAVs = m_UAVs.size();
  484. const unsigned kSmallUAVCount = 8;
  485. if (NumUAVs > kSmallUAVCount)
  486. Flags.Set64UAVs(true);
  487. if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
  488. Flags.SetUAVsAtEveryStage(true);
  489. bool hasRawAndStructuredBuffer = false;
  490. for (auto &UAV : m_UAVs) {
  491. if (UAV->IsROV())
  492. Flags.SetROVs(true);
  493. switch (UAV->GetKind()) {
  494. case DXIL::ResourceKind::RawBuffer:
  495. case DXIL::ResourceKind::StructuredBuffer:
  496. hasRawAndStructuredBuffer = true;
  497. break;
  498. default:
  499. // Not raw/structured.
  500. break;
  501. }
  502. }
  503. for (auto &SRV : m_SRVs) {
  504. switch (SRV->GetKind()) {
  505. case DXIL::ResourceKind::RawBuffer:
  506. case DXIL::ResourceKind::StructuredBuffer:
  507. hasRawAndStructuredBuffer = true;
  508. break;
  509. default:
  510. // Not raw/structured.
  511. break;
  512. }
  513. }
  514. Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
  515. bool hasCSRawAndStructuredViaShader4X =
  516. hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
  517. Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
  518. }
  519. void DxilModule::CollectShaderFlags() {
  520. CollectShaderFlags(m_ShaderFlags);
  521. }
  522. uint64_t DxilModule::ShaderFlags::GetShaderFlagsRawForCollection() {
  523. // This should be all the flags that can be set by DxilModule::CollectShaderFlags.
  524. ShaderFlags Flags;
  525. Flags.SetEnableDoublePrecision(true);
  526. Flags.SetInt64Ops(true);
  527. Flags.SetEnableMinPrecision(true);
  528. Flags.SetEnableDoubleExtensions(true);
  529. Flags.SetWaveOps(true);
  530. Flags.SetTiledResources(true);
  531. Flags.SetEnableMSAD(true);
  532. Flags.SetUAVLoadAdditionalFormats(true);
  533. Flags.SetStencilRef(true);
  534. Flags.SetInnerCoverage(true);
  535. Flags.SetViewportAndRTArrayIndex(true);
  536. Flags.Set64UAVs(true);
  537. Flags.SetUAVsAtEveryStage(true);
  538. Flags.SetEnableRawAndStructuredBuffers(true);
  539. Flags.SetCSRawAndStructuredViaShader4X(true);
  540. Flags.SetViewID(true);
  541. Flags.SetBarycentrics(true);
  542. return Flags.GetShaderFlagsRaw();
  543. }
  544. DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
  545. return m_InputPrimitive;
  546. }
  547. void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
  548. DXASSERT_NOMSG(m_InputPrimitive == DXIL::InputPrimitive::Undefined);
  549. DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
  550. m_InputPrimitive = IP;
  551. }
  552. unsigned DxilModule::GetMaxVertexCount() const {
  553. DXASSERT_NOMSG(m_MaxVertexCount != 0);
  554. return m_MaxVertexCount;
  555. }
  556. void DxilModule::SetMaxVertexCount(unsigned Count) {
  557. DXASSERT_NOMSG(m_MaxVertexCount == 0);
  558. m_MaxVertexCount = Count;
  559. }
  560. DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
  561. return m_StreamPrimitiveTopology;
  562. }
  563. void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
  564. m_StreamPrimitiveTopology = Topology;
  565. }
  566. bool DxilModule::HasMultipleOutputStreams() const {
  567. if (!m_pSM->IsGS()) {
  568. return false;
  569. } else {
  570. unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
  571. ((m_ActiveStreamMask & 0x2) >> 1) +
  572. ((m_ActiveStreamMask & 0x4) >> 2) +
  573. ((m_ActiveStreamMask & 0x8) >> 3);
  574. DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
  575. return NumStreams > 1;
  576. }
  577. }
  578. unsigned DxilModule::GetOutputStream() const {
  579. if (!m_pSM->IsGS()) {
  580. return 0;
  581. } else {
  582. DXASSERT_NOMSG(!HasMultipleOutputStreams());
  583. switch (m_ActiveStreamMask) {
  584. case 0x1: return 0;
  585. case 0x2: return 1;
  586. case 0x4: return 2;
  587. case 0x8: return 3;
  588. default: DXASSERT_NOMSG(false);
  589. }
  590. return (unsigned)(-1);
  591. }
  592. }
  593. unsigned DxilModule::GetGSInstanceCount() const {
  594. return m_NumGSInstances;
  595. }
  596. void DxilModule::SetGSInstanceCount(unsigned Count) {
  597. m_NumGSInstances = Count;
  598. }
  599. bool DxilModule::IsStreamActive(unsigned Stream) const {
  600. return (m_ActiveStreamMask & (1<<Stream)) != 0;
  601. }
  602. void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
  603. if (bActive) {
  604. m_ActiveStreamMask |= (1<<Stream);
  605. } else {
  606. m_ActiveStreamMask &= ~(1<<Stream);
  607. }
  608. }
  609. void DxilModule::SetActiveStreamMask(unsigned Mask) {
  610. m_ActiveStreamMask = Mask;
  611. }
  612. unsigned DxilModule::GetActiveStreamMask() const {
  613. return m_ActiveStreamMask;
  614. }
  615. unsigned DxilModule::GetInputControlPointCount() const {
  616. return m_InputControlPointCount;
  617. }
  618. void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
  619. m_InputControlPointCount = NumICPs;
  620. }
  621. DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
  622. return m_TessellatorDomain;
  623. }
  624. void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
  625. m_TessellatorDomain = TessDomain;
  626. }
  627. unsigned DxilModule::GetOutputControlPointCount() const {
  628. return m_OutputControlPointCount;
  629. }
  630. void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
  631. m_OutputControlPointCount = NumOCPs;
  632. }
  633. DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
  634. return m_TessellatorPartitioning;
  635. }
  636. void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
  637. m_TessellatorPartitioning = TessPartitioning;
  638. }
  639. DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
  640. return m_TessellatorOutputPrimitive;
  641. }
  642. void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
  643. m_TessellatorOutputPrimitive = TessOutputPrimitive;
  644. }
  645. float DxilModule::GetMaxTessellationFactor() const {
  646. return m_MaxTessellationFactor;
  647. }
  648. void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
  649. m_MaxTessellationFactor = MaxTessellationFactor;
  650. }
  651. template<typename T> unsigned
  652. DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  653. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  654. unsigned Id = (unsigned)Vec.size();
  655. Vec.emplace_back(std::move(pRes));
  656. return Id;
  657. }
  658. unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
  659. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
  660. }
  661. DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
  662. return *m_CBuffers[idx];
  663. }
  664. const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
  665. return *m_CBuffers[idx];
  666. }
  667. const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
  668. return m_CBuffers;
  669. }
  670. unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  671. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  672. }
  673. DxilSampler &DxilModule::GetSampler(unsigned idx) {
  674. return *m_Samplers[idx];
  675. }
  676. const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
  677. return *m_Samplers[idx];
  678. }
  679. const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
  680. return m_Samplers;
  681. }
  682. unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
  683. return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
  684. }
  685. DxilResource &DxilModule::GetSRV(unsigned idx) {
  686. return *m_SRVs[idx];
  687. }
  688. const DxilResource &DxilModule::GetSRV(unsigned idx) const {
  689. return *m_SRVs[idx];
  690. }
  691. const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
  692. return m_SRVs;
  693. }
  694. unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
  695. return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
  696. }
  697. DxilResource &DxilModule::GetUAV(unsigned idx) {
  698. return *m_UAVs[idx];
  699. }
  700. const DxilResource &DxilModule::GetUAV(unsigned idx) const {
  701. return *m_UAVs[idx];
  702. }
  703. const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
  704. return m_UAVs;
  705. }
  706. void DxilModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
  707. return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
  708. }
  709. void DxilModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
  710. return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
  711. }
  712. void DxilModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
  713. return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
  714. }
  715. template <typename TResource>
  716. static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
  717. std::unordered_set<unsigned> &immResID) {
  718. for (std::vector<std::unique_ptr<TResource>>::iterator p = vec.begin(); p != vec.end();) {
  719. std::vector<std::unique_ptr<TResource>>::iterator c = p++;
  720. if (immResID.count((*c)->GetID()) == 0) {
  721. p = vec.erase(c);
  722. }
  723. }
  724. }
  725. static void CollectUsedResource(Value *resID,
  726. std::unordered_set<Value *> &usedResID) {
  727. if (usedResID.count(resID) > 0)
  728. return;
  729. usedResID.insert(resID);
  730. if (ConstantInt *cResID = dyn_cast<ConstantInt>(resID)) {
  731. // Do nothing
  732. } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
  733. if (ZEI->getSrcTy()->isIntegerTy()) {
  734. IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
  735. if (ITy->getBitWidth() == 1) {
  736. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
  737. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
  738. }
  739. }
  740. } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
  741. CollectUsedResource(SI->getTrueValue(), usedResID);
  742. CollectUsedResource(SI->getFalseValue(), usedResID);
  743. } else if (PHINode *Phi = dyn_cast<PHINode>(resID)) {
  744. for (Use &U : Phi->incoming_values()) {
  745. CollectUsedResource(U.get(), usedResID);
  746. }
  747. }
  748. // TODO: resID could be other types of instructions depending on the compiler optimization.
  749. }
  750. static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
  751. std::unordered_set<Value *> &usedResID) {
  752. for (Value *V : usedResID) {
  753. if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
  754. immResID.insert(cResID->getLimitedValue());
  755. }
  756. }
  757. }
  758. void DxilModule::RemoveFunction(llvm::Function *F) {
  759. DXASSERT_NOMSG(F != nullptr);
  760. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  761. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  762. m_pOP->RemoveFunction(F);
  763. }
  764. void DxilModule::RemoveUnusedResources() {
  765. hlsl::OP *hlslOP = GetOP();
  766. Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
  767. if (createHandleFunc->user_empty()) {
  768. m_CBuffers.clear();
  769. m_UAVs.clear();
  770. m_SRVs.clear();
  771. m_Samplers.clear();
  772. createHandleFunc->eraseFromParent();
  773. return;
  774. }
  775. std::unordered_set<Value *> usedUAVID;
  776. std::unordered_set<Value *> usedSRVID;
  777. std::unordered_set<Value *> usedSamplerID;
  778. std::unordered_set<Value *> usedCBufID;
  779. // Collect used ID.
  780. for (User *U : createHandleFunc->users()) {
  781. CallInst *CI = cast<CallInst>(U);
  782. Value *vResClass =
  783. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  784. ConstantInt *cResClass = cast<ConstantInt>(vResClass);
  785. DXIL::ResourceClass resClass =
  786. static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
  787. // Skip unused resource handle.
  788. if (CI->user_empty())
  789. continue;
  790. Value *resID =
  791. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  792. switch (resClass) {
  793. case DXIL::ResourceClass::CBuffer:
  794. CollectUsedResource(resID, usedCBufID);
  795. break;
  796. case DXIL::ResourceClass::Sampler:
  797. CollectUsedResource(resID, usedSamplerID);
  798. break;
  799. case DXIL::ResourceClass::SRV:
  800. CollectUsedResource(resID, usedSRVID);
  801. break;
  802. case DXIL::ResourceClass::UAV:
  803. CollectUsedResource(resID, usedUAVID);
  804. break;
  805. default:
  806. DXASSERT(0, "invalid res class");
  807. break;
  808. }
  809. }
  810. std::unordered_set<unsigned> immUAVID;
  811. std::unordered_set<unsigned> immSRVID;
  812. std::unordered_set<unsigned> immSamplerID;
  813. std::unordered_set<unsigned> immCBufID;
  814. ConvertUsedResource(immUAVID, usedUAVID);
  815. RemoveResources(m_UAVs, immUAVID);
  816. ConvertUsedResource(immSRVID, usedSRVID);
  817. ConvertUsedResource(immSamplerID, usedSamplerID);
  818. ConvertUsedResource(immCBufID, usedCBufID);
  819. RemoveResources(m_SRVs, immSRVID);
  820. RemoveResources(m_Samplers, immSamplerID);
  821. RemoveResources(m_CBuffers, immCBufID);
  822. }
  823. DxilSignature &DxilModule::GetInputSignature() {
  824. return *m_InputSignature;
  825. }
  826. const DxilSignature &DxilModule::GetInputSignature() const {
  827. return *m_InputSignature;
  828. }
  829. DxilSignature &DxilModule::GetOutputSignature() {
  830. return *m_OutputSignature;
  831. }
  832. const DxilSignature &DxilModule::GetOutputSignature() const {
  833. return *m_OutputSignature;
  834. }
  835. DxilSignature &DxilModule::GetPatchConstantSignature() {
  836. return *m_PatchConstantSignature;
  837. }
  838. const DxilSignature &DxilModule::GetPatchConstantSignature() const {
  839. return *m_PatchConstantSignature;
  840. }
  841. const RootSignatureHandle &DxilModule::GetRootSignature() const {
  842. return *m_RootSignature;
  843. }
  844. void DxilModule::StripRootSignatureFromMetadata() {
  845. NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
  846. if (pRootSignatureNamedMD) {
  847. GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
  848. }
  849. }
  850. void DxilModule::UpdateValidatorVersionMetadata() {
  851. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  852. }
  853. void DxilModule::ResetInputSignature(DxilSignature *pValue) {
  854. m_InputSignature.reset(pValue);
  855. }
  856. void DxilModule::ResetOutputSignature(DxilSignature *pValue) {
  857. m_OutputSignature.reset(pValue);
  858. }
  859. void DxilModule::ResetPatchConstantSignature(DxilSignature *pValue) {
  860. m_PatchConstantSignature.reset(pValue);
  861. }
  862. void DxilModule::ResetRootSignature(RootSignatureHandle *pValue) {
  863. m_RootSignature.reset(pValue);
  864. }
  865. DxilTypeSystem &DxilModule::GetTypeSystem() {
  866. return *m_pTypeSystem;
  867. }
  868. DxilViewIdState &DxilModule::GetViewIdState() {
  869. return *m_pViewIdState;
  870. }
  871. const DxilViewIdState &DxilModule::GetViewIdState() const {
  872. return *m_pViewIdState;
  873. }
  874. void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
  875. m_pTypeSystem.reset(pValue);
  876. }
  877. void DxilModule::ResetOP(hlsl::OP *hlslOP) {
  878. m_pOP.reset(hlslOP);
  879. }
  880. void DxilModule::EmitLLVMUsed() {
  881. if (m_LLVMUsed.empty())
  882. return;
  883. vector<llvm::Constant*> GVs;
  884. Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
  885. GVs.resize(m_LLVMUsed.size());
  886. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  887. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  888. PointerType * pPtrType = dyn_cast<PointerType>(pConst->getType());
  889. if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
  890. // Cast pointer to addrspace 0, as LLVMUsed elements must have the same type.
  891. GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
  892. } else {
  893. GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
  894. }
  895. }
  896. ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
  897. StringRef llvmUsedName = "llvm.used";
  898. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  899. oldGV->eraseFromParent();
  900. }
  901. GlobalVariable *pGV = new GlobalVariable(*m_pModule, pATy, false,
  902. GlobalValue::AppendingLinkage,
  903. ConstantArray::get(pATy, GVs),
  904. llvmUsedName);
  905. pGV->setSection("llvm.metadata");
  906. }
  907. vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
  908. return m_LLVMUsed;
  909. }
  910. // DXIL metadata serialization/deserialization.
  911. void DxilModule::EmitDxilMetadata() {
  912. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  913. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  914. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  915. MDTuple *pMDProperties = EmitDxilShaderProperties();
  916. MDTuple *pMDSignatures = m_pMDHelper->EmitDxilSignatures(*m_InputSignature,
  917. *m_OutputSignature,
  918. *m_PatchConstantSignature);
  919. MDTuple *pMDResources = EmitDxilResources();
  920. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  921. if (!m_pSM->IsCS() &&
  922. (m_ValMajor > 1 || (m_ValMajor == 1 && m_ValMinor >= 1))) {
  923. m_pMDHelper->EmitDxilViewIdState(GetViewIdState());
  924. }
  925. EmitLLVMUsed();
  926. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  927. vector<MDNode *> Entries;
  928. Entries.emplace_back(pEntry);
  929. m_pMDHelper->EmitDxilEntryPoints(Entries);
  930. if (!m_RootSignature->IsEmpty()) {
  931. m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
  932. }
  933. }
  934. bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
  935. return DxilMDHelper::IsKnownNamedMetaData(Node);
  936. }
  937. void DxilModule::LoadDxilMetadata() {
  938. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  939. m_pMDHelper->LoadValidatorVersion(m_ValMajor, m_ValMinor);
  940. const ShaderModel *loadedModule;
  941. m_pMDHelper->LoadDxilShaderModel(loadedModule);
  942. SetShaderModel(loadedModule);
  943. DXASSERT(m_InputSignature != nullptr, "else SetShaderModel didn't create input signature");
  944. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  945. IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
  946. Function *pEntryFunc;
  947. string EntryName;
  948. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  949. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
  950. SetEntryFunction(pEntryFunc);
  951. SetEntryFunctionName(EntryName);
  952. LoadDxilShaderProperties(*pProperties);
  953. m_pMDHelper->LoadDxilSignatures(*pSignatures, *m_InputSignature,
  954. *m_OutputSignature, *m_PatchConstantSignature);
  955. LoadDxilResources(*pResources);
  956. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  957. m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
  958. m_pMDHelper->LoadDxilViewIdState(*m_pViewIdState.get());
  959. }
  960. MDTuple *DxilModule::EmitDxilResources() {
  961. // Emit SRV records.
  962. MDTuple *pTupleSRVs = nullptr;
  963. if (!m_SRVs.empty()) {
  964. vector<Metadata *> MDVals;
  965. for (size_t i = 0; i < m_SRVs.size(); i++) {
  966. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  967. }
  968. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  969. }
  970. // Emit UAV records.
  971. MDTuple *pTupleUAVs = nullptr;
  972. if (!m_UAVs.empty()) {
  973. vector<Metadata *> MDVals;
  974. for (size_t i = 0; i < m_UAVs.size(); i++) {
  975. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  976. }
  977. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  978. }
  979. // Emit CBuffer records.
  980. MDTuple *pTupleCBuffers = nullptr;
  981. if (!m_CBuffers.empty()) {
  982. vector<Metadata *> MDVals;
  983. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  984. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  985. }
  986. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  987. }
  988. // Emit Sampler records.
  989. MDTuple *pTupleSamplers = nullptr;
  990. if (!m_Samplers.empty()) {
  991. vector<Metadata *> MDVals;
  992. for (size_t i = 0; i < m_Samplers.size(); i++) {
  993. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  994. }
  995. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  996. }
  997. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  998. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  999. } else {
  1000. return nullptr;
  1001. }
  1002. }
  1003. void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
  1004. if (MDO.get() == nullptr)
  1005. return;
  1006. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  1007. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  1008. // Load SRV records.
  1009. if (pSRVs != nullptr) {
  1010. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  1011. unique_ptr<DxilResource> pSRV(new DxilResource);
  1012. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  1013. AddSRV(std::move(pSRV));
  1014. }
  1015. }
  1016. // Load UAV records.
  1017. if (pUAVs != nullptr) {
  1018. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  1019. unique_ptr<DxilResource> pUAV(new DxilResource);
  1020. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  1021. AddUAV(std::move(pUAV));
  1022. }
  1023. }
  1024. // Load CBuffer records.
  1025. if (pCBuffers != nullptr) {
  1026. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  1027. unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
  1028. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  1029. AddCBuffer(std::move(pCB));
  1030. }
  1031. }
  1032. // Load Sampler records.
  1033. if (pSamplers != nullptr) {
  1034. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  1035. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  1036. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  1037. AddSampler(std::move(pSampler));
  1038. }
  1039. }
  1040. }
  1041. MDTuple *DxilModule::EmitDxilShaderProperties() {
  1042. vector<Metadata *> MDVals;
  1043. // DXIL shader flags.
  1044. uint64_t Flags = m_ShaderFlags.GetShaderFlagsRaw();
  1045. if (Flags != 0) {
  1046. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilShaderFlagsTag));
  1047. MDVals.emplace_back(m_pMDHelper->Uint64ToConstMD(Flags));
  1048. }
  1049. // Compute shader.
  1050. if (m_pSM->IsCS()) {
  1051. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilNumThreadsTag));
  1052. vector<Metadata *> NumThreadVals;
  1053. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[0]));
  1054. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[1]));
  1055. NumThreadVals.emplace_back(m_pMDHelper->Uint32ToConstMD(m_NumThreads[2]));
  1056. MDVals.emplace_back(MDNode::get(m_Ctx, NumThreadVals));
  1057. }
  1058. // Geometry shader.
  1059. if (m_pSM->IsGS()) {
  1060. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilGSStateTag));
  1061. MDTuple *pMDTuple = m_pMDHelper->EmitDxilGSState(m_InputPrimitive,
  1062. m_MaxVertexCount,
  1063. GetActiveStreamMask(),
  1064. m_StreamPrimitiveTopology,
  1065. m_NumGSInstances);
  1066. MDVals.emplace_back(pMDTuple);
  1067. }
  1068. // Domain shader.
  1069. if (m_pSM->IsDS()) {
  1070. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilDSStateTag));
  1071. MDTuple *pMDTuple = m_pMDHelper->EmitDxilDSState(m_TessellatorDomain,
  1072. m_InputControlPointCount);
  1073. MDVals.emplace_back(pMDTuple);
  1074. }
  1075. // Hull shader.
  1076. if (m_pSM->IsHS()) {
  1077. MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilHSStateTag));
  1078. MDTuple *pMDTuple = m_pMDHelper->EmitDxilHSState(m_pPatchConstantFunc,
  1079. m_InputControlPointCount,
  1080. m_OutputControlPointCount,
  1081. m_TessellatorDomain,
  1082. m_TessellatorPartitioning,
  1083. m_TessellatorOutputPrimitive,
  1084. m_MaxTessellationFactor);
  1085. MDVals.emplace_back(pMDTuple);
  1086. }
  1087. if (!MDVals.empty())
  1088. return MDNode::get(m_Ctx, MDVals);
  1089. else
  1090. return nullptr;
  1091. }
  1092. void DxilModule::LoadDxilShaderProperties(const MDOperand &MDO) {
  1093. if (MDO.get() == nullptr)
  1094. return;
  1095. const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
  1096. IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  1097. IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0, DXC_E_INCORRECT_DXIL_METADATA);
  1098. for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
  1099. unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
  1100. const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
  1101. IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
  1102. switch (Tag) {
  1103. case DxilMDHelper::kDxilShaderFlagsTag:
  1104. m_ShaderFlags.SetShaderFlagsRaw(DxilMDHelper::ConstMDToUint64(MDO));
  1105. break;
  1106. case DxilMDHelper::kDxilNumThreadsTag: {
  1107. MDNode *pNode = cast<MDNode>(MDO.get());
  1108. m_NumThreads[0] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(0));
  1109. m_NumThreads[1] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(1));
  1110. m_NumThreads[2] = DxilMDHelper::ConstMDToUint32(pNode->getOperand(2));
  1111. break;
  1112. }
  1113. case DxilMDHelper::kDxilGSStateTag: {
  1114. m_pMDHelper->LoadDxilGSState(MDO, m_InputPrimitive, m_MaxVertexCount, m_ActiveStreamMask,
  1115. m_StreamPrimitiveTopology, m_NumGSInstances);
  1116. break;
  1117. }
  1118. case DxilMDHelper::kDxilDSStateTag:
  1119. m_pMDHelper->LoadDxilDSState(MDO, m_TessellatorDomain, m_InputControlPointCount);
  1120. break;
  1121. case DxilMDHelper::kDxilHSStateTag:
  1122. m_pMDHelper->LoadDxilHSState(MDO,
  1123. m_pPatchConstantFunc,
  1124. m_InputControlPointCount,
  1125. m_OutputControlPointCount,
  1126. m_TessellatorDomain,
  1127. m_TessellatorPartitioning,
  1128. m_TessellatorOutputPrimitive,
  1129. m_MaxTessellationFactor);
  1130. break;
  1131. default:
  1132. DXASSERT(false, "Unknown extended shader properties tag");
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. void DxilModule::StripDebugRelatedCode() {
  1138. // Remove all users of global resources.
  1139. for (GlobalVariable &GV : m_pModule->globals()) {
  1140. if (GV.hasInternalLinkage())
  1141. continue;
  1142. if (GV.getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
  1143. continue;
  1144. for (auto git = GV.user_begin(); git != GV.user_end();) {
  1145. User *U = *(git++);
  1146. // Try to remove load of GV.
  1147. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  1148. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1149. Instruction *LIUser = cast<Instruction>(*(it++));
  1150. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1151. Value *Ptr = SI->getPointerOperand();
  1152. SI->eraseFromParent();
  1153. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1154. if (Ptr->user_empty())
  1155. PtrInst->eraseFromParent();
  1156. }
  1157. }
  1158. }
  1159. if (LI->user_empty())
  1160. LI->eraseFromParent();
  1161. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
  1162. for (auto GEPIt = GEP->user_begin(); GEPIt != GEP->user_end();) {
  1163. User *GEPU = *(GEPIt++);
  1164. // Try to remove load of GEP.
  1165. if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
  1166. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1167. Instruction *LIUser = cast<Instruction>(*(it++));
  1168. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1169. Value *Ptr = SI->getPointerOperand();
  1170. SI->eraseFromParent();
  1171. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1172. if (Ptr->user_empty())
  1173. PtrInst->eraseFromParent();
  1174. }
  1175. }
  1176. if (LI->user_empty())
  1177. LI->eraseFromParent();
  1178. }
  1179. }
  1180. }
  1181. if (GEP->user_empty())
  1182. GEP->eraseFromParent();
  1183. }
  1184. }
  1185. }
  1186. }
  1187. DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
  1188. if (m_pDebugInfoFinder == nullptr) {
  1189. m_pDebugInfoFinder = std::make_unique<llvm::DebugInfoFinder>();
  1190. m_pDebugInfoFinder->processModule(*m_pModule);
  1191. }
  1192. return *m_pDebugInfoFinder;
  1193. }
  1194. hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule(llvm::Module *pModule) {
  1195. LLVMContext &Ctx = pModule->getContext();
  1196. std::string diagStr;
  1197. raw_string_ostream diagStream(diagStr);
  1198. hlsl::DxilModule *pDxilModule = nullptr;
  1199. // TODO: add detail error in DxilMDHelper.
  1200. try {
  1201. pDxilModule = &pModule->GetOrCreateDxilModule();
  1202. } catch (const ::hlsl::Exception &hlslException) {
  1203. diagStream << "load dxil metadata failed -";
  1204. try {
  1205. const char *msg = hlslException.what();
  1206. if (msg == nullptr || *msg == '\0')
  1207. diagStream << " error code " << hlslException.hr << "\n";
  1208. else
  1209. diagStream << msg;
  1210. } catch (...) {
  1211. diagStream << " unable to retrieve error message.\n";
  1212. }
  1213. Ctx.diagnose(DxilErrorDiagnosticInfo(diagStream.str().c_str()));
  1214. } catch (...) {
  1215. Ctx.diagnose(DxilErrorDiagnosticInfo("load dxil metadata failed - unknown error.\n"));
  1216. }
  1217. return pDxilModule;
  1218. }
  1219. } // namespace hlsl
  1220. namespace llvm {
  1221. hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
  1222. std::unique_ptr<hlsl::DxilModule> M;
  1223. if (!HasDxilModule()) {
  1224. M = std::make_unique<hlsl::DxilModule>(this);
  1225. if (!skipInit) {
  1226. M->LoadDxilMetadata();
  1227. }
  1228. SetDxilModule(M.release());
  1229. }
  1230. return GetDxilModule();
  1231. }
  1232. void Module::ResetDxilModule() {
  1233. if (HasDxilModule()) {
  1234. delete TheDxilModule;
  1235. TheDxilModule = nullptr;
  1236. }
  1237. }
  1238. }