DxilModule.cpp 61 KB

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