2
0

DxilModule.cpp 66 KB

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