DxilModule.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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 "DxilEntryProps.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/IR/Metadata.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/Operator.h"
  25. #include "llvm/IR/DebugInfo.h"
  26. #include "llvm/IR/DiagnosticInfo.h"
  27. #include "llvm/IR/DiagnosticPrinter.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <unordered_set>
  30. using namespace llvm;
  31. using std::string;
  32. using std::vector;
  33. using std::unique_ptr;
  34. namespace {
  35. class DxilErrorDiagnosticInfo : public DiagnosticInfo {
  36. private:
  37. const char *m_message;
  38. public:
  39. DxilErrorDiagnosticInfo(const char *str)
  40. : DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
  41. m_message(str) { }
  42. void print(DiagnosticPrinter &DP) const override {
  43. DP << m_message;
  44. }
  45. };
  46. } // anon namespace
  47. namespace hlsl {
  48. namespace DXIL {
  49. // Define constant variables exposed in DxilConstants.h
  50. // TODO: revisit data layout descriptions for the following:
  51. // - x64 pointers?
  52. // - Keep elf manging(m:e)?
  53. // For legacy data layout, everything less than 32 align to 32.
  54. const char* kLegacyLayoutString = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f:64:64-n8:16:32:64";
  55. // New data layout with native low precision types
  56. 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";
  57. // Function Attributes
  58. // TODO: consider generating attributes from hctdb
  59. const char* kFP32DenormKindString = "fp32-denorm-mode";
  60. const char* kFP32DenormValueAnyString = "any";
  61. const char* kFP32DenormValuePreserveString = "preserve";
  62. const char* kFP32DenormValueFtzString = "ftz";
  63. }
  64. //------------------------------------------------------------------------------
  65. //
  66. // DxilModule methods.
  67. //
  68. DxilModule::DxilModule(Module *pModule)
  69. : m_RootSignature(nullptr)
  70. , m_StreamPrimitiveTopology(DXIL::PrimitiveTopology::Undefined)
  71. , m_ActiveStreamMask(0)
  72. , m_Ctx(pModule->getContext())
  73. , m_pModule(pModule)
  74. , m_pEntryFunc(nullptr)
  75. , m_EntryName("")
  76. , m_pMDHelper(llvm::make_unique<DxilMDHelper>(pModule, llvm::make_unique<DxilExtraPropertyHelper>(pModule)))
  77. , m_pDebugInfoFinder(nullptr)
  78. , m_pSM(nullptr)
  79. , m_DxilMajor(DXIL::kDxilMajor)
  80. , m_DxilMinor(DXIL::kDxilMinor)
  81. , m_ValMajor(1)
  82. , m_ValMinor(0)
  83. , m_pOP(llvm::make_unique<OP>(pModule->getContext(), pModule))
  84. , m_pTypeSystem(llvm::make_unique<DxilTypeSystem>(pModule))
  85. , m_pViewIdState(llvm::make_unique<DxilViewIdState>(this))
  86. , m_bDisableOptimizations(false)
  87. , m_bUseMinPrecision(true) // use min precision by default
  88. , m_bAllResourcesBound(false)
  89. , m_AutoBindingSpace(UINT_MAX) {
  90. DXASSERT_NOMSG(m_pModule != nullptr);
  91. #if defined(_DEBUG) || defined(DBG)
  92. // Pin LLVM dump methods.
  93. void (__thiscall Module::*pfnModuleDump)() const = &Module::dump;
  94. void (__thiscall Type::*pfnTypeDump)() const = &Type::dump;
  95. void (__thiscall Function::*pfnViewCFGOnly)() const = &Function::viewCFGOnly;
  96. m_pUnused = (char *)&pfnModuleDump - (char *)&pfnTypeDump;
  97. m_pUnused -= (size_t)&pfnViewCFGOnly;
  98. #endif
  99. }
  100. DxilModule::~DxilModule() {
  101. }
  102. LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
  103. Module *DxilModule::GetModule() const { return m_pModule; }
  104. OP *DxilModule::GetOP() const { return m_pOP.get(); }
  105. void DxilModule::SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision) {
  106. DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM), "shader model must not change for the module");
  107. DXASSERT(pSM != nullptr && pSM->IsValidForDxil(), "shader model must be valid");
  108. DXASSERT(pSM->IsValidForModule(), "shader model must be valid for top-level module use");
  109. m_pSM = pSM;
  110. m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
  111. m_pMDHelper->SetShaderModel(m_pSM);
  112. m_bUseMinPrecision = bUseMinPrecision;
  113. if (!m_pSM->IsLib()) {
  114. // Always have valid entry props for non-lib case from this point on.
  115. DxilFunctionProps props;
  116. props.shaderKind = m_pSM->GetKind();
  117. m_DxilEntryPropsMap[nullptr] =
  118. llvm::make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
  119. }
  120. m_RootSignature.reset(new RootSignatureHandle());
  121. }
  122. const ShaderModel *DxilModule::GetShaderModel() const {
  123. return m_pSM;
  124. }
  125. void DxilModule::GetDxilVersion(unsigned &DxilMajor, unsigned &DxilMinor) const {
  126. DxilMajor = m_DxilMajor;
  127. DxilMinor = m_DxilMinor;
  128. }
  129. void DxilModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  130. m_ValMajor = ValMajor;
  131. m_ValMinor = ValMinor;
  132. }
  133. bool DxilModule::UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
  134. // Don't upgrade if validation was disabled.
  135. if (m_ValMajor == 0 && m_ValMinor == 0) {
  136. return false;
  137. }
  138. if (ValMajor > m_ValMajor || (ValMajor == m_ValMajor && ValMinor > m_ValMinor)) {
  139. // Module requires higher validator version than previously set
  140. SetValidatorVersion(ValMajor, ValMinor);
  141. return true;
  142. }
  143. return false;
  144. }
  145. void DxilModule::GetValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  146. ValMajor = m_ValMajor;
  147. ValMinor = m_ValMinor;
  148. }
  149. bool DxilModule::GetMinValidatorVersion(unsigned &ValMajor, unsigned &ValMinor) const {
  150. if (!m_pSM)
  151. return false;
  152. m_pSM->GetMinValidatorVersion(ValMajor, ValMinor);
  153. if (ValMajor == 1 && ValMinor == 0 && (m_ShaderFlags.GetFeatureInfo() & hlsl::ShaderFeatureInfo_ViewID))
  154. ValMinor = 1;
  155. return true;
  156. }
  157. bool DxilModule::UpgradeToMinValidatorVersion() {
  158. unsigned ValMajor = 1, ValMinor = 0;
  159. if (GetMinValidatorVersion(ValMajor, ValMinor)) {
  160. return UpgradeValidatorVersion(ValMajor, ValMinor);
  161. }
  162. return false;
  163. }
  164. Function *DxilModule::GetEntryFunction() {
  165. return m_pEntryFunc;
  166. }
  167. const Function *DxilModule::GetEntryFunction() const {
  168. return m_pEntryFunc;
  169. }
  170. void DxilModule::SetEntryFunction(Function *pEntryFunc) {
  171. if (m_pSM->IsLib()) {
  172. DXASSERT(pEntryFunc == nullptr,
  173. "Otherwise, trying to set an entry function on library");
  174. m_pEntryFunc = nullptr;
  175. return;
  176. }
  177. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  178. m_pEntryFunc = pEntryFunc;
  179. // Move entry props to new function in order to preserve them.
  180. std::unique_ptr<DxilEntryProps> Props = std::move(m_DxilEntryPropsMap.begin()->second);
  181. m_DxilEntryPropsMap.clear();
  182. m_DxilEntryPropsMap[m_pEntryFunc] = std::move(Props);
  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. if (!m_pSM->IsHS())
  192. return nullptr;
  193. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  194. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  195. DXASSERT(props.IsHS(), "Must be HS profile");
  196. return props.ShaderProps.HS.patchConstantFunc;
  197. }
  198. const llvm::Function *DxilModule::GetPatchConstantFunction() const {
  199. if (!m_pSM->IsHS())
  200. return nullptr;
  201. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  202. const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  203. DXASSERT(props.IsHS(), "Must be HS profile");
  204. return props.ShaderProps.HS.patchConstantFunc;
  205. }
  206. void DxilModule::SetPatchConstantFunction(llvm::Function *patchConstantFunc) {
  207. if (!m_pSM->IsHS())
  208. return;
  209. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  210. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  211. DXASSERT(props.IsHS(), "Must be HS profile");
  212. auto &HS = props.ShaderProps.HS;
  213. if (HS.patchConstantFunc != patchConstantFunc) {
  214. if (HS.patchConstantFunc)
  215. m_PatchConstantFunctions.erase(HS.patchConstantFunc);
  216. HS.patchConstantFunc = patchConstantFunc;
  217. if (patchConstantFunc)
  218. m_PatchConstantFunctions.insert(patchConstantFunc);
  219. }
  220. }
  221. unsigned DxilModule::GetGlobalFlags() const {
  222. unsigned Flags = m_ShaderFlags.GetGlobalFlags();
  223. return Flags;
  224. }
  225. void DxilModule::CollectShaderFlagsForModule(ShaderFlags &Flags) {
  226. for (Function &F : GetModule()->functions()) {
  227. ShaderFlags funcFlags = ShaderFlags::CollectShaderFlags(&F, this);
  228. Flags.CombineShaderFlags(funcFlags);
  229. };
  230. const ShaderModel *SM = GetShaderModel();
  231. if (SM->IsPS()) {
  232. bool hasStencilRef = false;
  233. DxilSignature &outS = GetOutputSignature();
  234. for (auto &&E : outS.GetElements()) {
  235. if (E->GetKind() == Semantic::Kind::StencilRef) {
  236. hasStencilRef = true;
  237. } else if (E->GetKind() == Semantic::Kind::InnerCoverage) {
  238. Flags.SetInnerCoverage(true);
  239. }
  240. }
  241. Flags.SetStencilRef(hasStencilRef);
  242. }
  243. bool checkInputRTArrayIndex =
  244. SM->IsGS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  245. if (checkInputRTArrayIndex) {
  246. bool hasViewportArrayIndex = false;
  247. bool hasRenderTargetArrayIndex = false;
  248. DxilSignature &inS = GetInputSignature();
  249. for (auto &E : inS.GetElements()) {
  250. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  251. hasViewportArrayIndex = true;
  252. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  253. hasRenderTargetArrayIndex = true;
  254. }
  255. }
  256. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  257. hasRenderTargetArrayIndex);
  258. }
  259. bool checkOutputRTArrayIndex =
  260. SM->IsVS() || SM->IsDS() || SM->IsHS() || SM->IsPS();
  261. if (checkOutputRTArrayIndex) {
  262. bool hasViewportArrayIndex = false;
  263. bool hasRenderTargetArrayIndex = false;
  264. DxilSignature &outS = GetOutputSignature();
  265. for (auto &E : outS.GetElements()) {
  266. if (E->GetKind() == Semantic::Kind::ViewPortArrayIndex) {
  267. hasViewportArrayIndex = true;
  268. } else if (E->GetKind() == Semantic::Kind::RenderTargetArrayIndex) {
  269. hasRenderTargetArrayIndex = true;
  270. }
  271. }
  272. Flags.SetViewportAndRTArrayIndex(hasViewportArrayIndex |
  273. hasRenderTargetArrayIndex);
  274. }
  275. unsigned NumUAVs = m_UAVs.size();
  276. const unsigned kSmallUAVCount = 8;
  277. if (NumUAVs > kSmallUAVCount)
  278. Flags.Set64UAVs(true);
  279. if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
  280. Flags.SetUAVsAtEveryStage(true);
  281. bool hasRawAndStructuredBuffer = false;
  282. for (auto &UAV : m_UAVs) {
  283. if (UAV->IsROV())
  284. Flags.SetROVs(true);
  285. switch (UAV->GetKind()) {
  286. case DXIL::ResourceKind::RawBuffer:
  287. case DXIL::ResourceKind::StructuredBuffer:
  288. hasRawAndStructuredBuffer = true;
  289. break;
  290. default:
  291. // Not raw/structured.
  292. break;
  293. }
  294. }
  295. for (auto &SRV : m_SRVs) {
  296. switch (SRV->GetKind()) {
  297. case DXIL::ResourceKind::RawBuffer:
  298. case DXIL::ResourceKind::StructuredBuffer:
  299. hasRawAndStructuredBuffer = true;
  300. break;
  301. default:
  302. // Not raw/structured.
  303. break;
  304. }
  305. }
  306. Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
  307. bool hasCSRawAndStructuredViaShader4X =
  308. hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
  309. Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
  310. }
  311. void DxilModule::CollectShaderFlagsForModule() {
  312. CollectShaderFlagsForModule(m_ShaderFlags);
  313. }
  314. void DxilModule::SetNumThreads(unsigned x, unsigned y, unsigned z) {
  315. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsCS(),
  316. "only works for CS profile");
  317. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  318. DXASSERT(props.IsCS(), "Must be CS profile");
  319. unsigned *numThreads = props.ShaderProps.CS.numThreads;
  320. numThreads[0] = x;
  321. numThreads[1] = y;
  322. numThreads[2] = z;
  323. }
  324. unsigned DxilModule::GetNumThreads(unsigned idx) const {
  325. DXASSERT(idx < 3, "Thread dimension index must be 0-2");
  326. if (!m_pSM->IsCS())
  327. return 0;
  328. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  329. __analysis_assume(idx < 3);
  330. const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  331. DXASSERT(props.IsCS(), "Must be CS profile");
  332. return props.ShaderProps.CS.numThreads[idx];
  333. }
  334. DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
  335. if (!m_pSM->IsGS())
  336. return DXIL::InputPrimitive::Undefined;
  337. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  338. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  339. DXASSERT(props.IsGS(), "Must be GS profile");
  340. return props.ShaderProps.GS.inputPrimitive;
  341. }
  342. void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
  343. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  344. "only works for GS profile");
  345. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  346. DXASSERT(props.IsGS(), "Must be GS profile");
  347. auto &GS = props.ShaderProps.GS;
  348. DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP && IP < DXIL::InputPrimitive::LastEntry);
  349. GS.inputPrimitive = IP;
  350. }
  351. unsigned DxilModule::GetMaxVertexCount() const {
  352. if (!m_pSM->IsGS())
  353. return 0;
  354. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  355. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  356. DXASSERT(props.IsGS(), "Must be GS profile");
  357. auto &GS = props.ShaderProps.GS;
  358. DXASSERT_NOMSG(GS.maxVertexCount != 0);
  359. return GS.maxVertexCount;
  360. }
  361. void DxilModule::SetMaxVertexCount(unsigned Count) {
  362. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  363. "only works for GS profile");
  364. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  365. DXASSERT(props.IsGS(), "Must be GS profile");
  366. auto &GS = props.ShaderProps.GS;
  367. GS.maxVertexCount = Count;
  368. }
  369. DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
  370. return m_StreamPrimitiveTopology;
  371. }
  372. void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
  373. m_StreamPrimitiveTopology = Topology;
  374. SetActiveStreamMask(m_ActiveStreamMask); // Update props
  375. }
  376. bool DxilModule::HasMultipleOutputStreams() const {
  377. if (!m_pSM->IsGS()) {
  378. return false;
  379. } else {
  380. unsigned NumStreams = (m_ActiveStreamMask & 0x1) +
  381. ((m_ActiveStreamMask & 0x2) >> 1) +
  382. ((m_ActiveStreamMask & 0x4) >> 2) +
  383. ((m_ActiveStreamMask & 0x8) >> 3);
  384. DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
  385. return NumStreams > 1;
  386. }
  387. }
  388. unsigned DxilModule::GetOutputStream() const {
  389. if (!m_pSM->IsGS()) {
  390. return 0;
  391. } else {
  392. DXASSERT_NOMSG(!HasMultipleOutputStreams());
  393. switch (m_ActiveStreamMask) {
  394. case 0x1: return 0;
  395. case 0x2: return 1;
  396. case 0x4: return 2;
  397. case 0x8: return 3;
  398. default: DXASSERT_NOMSG(false);
  399. }
  400. return (unsigned)(-1);
  401. }
  402. }
  403. unsigned DxilModule::GetGSInstanceCount() const {
  404. if (!m_pSM->IsGS())
  405. return 0;
  406. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  407. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  408. DXASSERT(props.IsGS(), "Must be GS profile");
  409. return props.ShaderProps.GS.instanceCount;
  410. }
  411. void DxilModule::SetGSInstanceCount(unsigned Count) {
  412. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  413. "only works for GS profile");
  414. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  415. DXASSERT(props.IsGS(), "Must be GS profile");
  416. props.ShaderProps.GS.instanceCount = Count;
  417. }
  418. bool DxilModule::IsStreamActive(unsigned Stream) const {
  419. return (m_ActiveStreamMask & (1<<Stream)) != 0;
  420. }
  421. void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
  422. if (bActive) {
  423. m_ActiveStreamMask |= (1<<Stream);
  424. } else {
  425. m_ActiveStreamMask &= ~(1<<Stream);
  426. }
  427. SetActiveStreamMask(m_ActiveStreamMask);
  428. }
  429. void DxilModule::SetActiveStreamMask(unsigned Mask) {
  430. m_ActiveStreamMask = Mask;
  431. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
  432. "only works for GS profile");
  433. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  434. DXASSERT(props.IsGS(), "Must be GS profile");
  435. for (unsigned i = 0; i < 4; i++) {
  436. if (IsStreamActive(i))
  437. props.ShaderProps.GS.streamPrimitiveTopologies[i] = m_StreamPrimitiveTopology;
  438. else
  439. props.ShaderProps.GS.streamPrimitiveTopologies[i] = DXIL::PrimitiveTopology::Undefined;
  440. }
  441. }
  442. unsigned DxilModule::GetActiveStreamMask() const {
  443. return m_ActiveStreamMask;
  444. }
  445. bool DxilModule::GetUseMinPrecision() const {
  446. return m_bUseMinPrecision;
  447. }
  448. void DxilModule::SetDisableOptimization(bool DisableOptimization) {
  449. m_bDisableOptimizations = DisableOptimization;
  450. }
  451. bool DxilModule::GetDisableOptimization() const {
  452. return m_bDisableOptimizations;
  453. }
  454. void DxilModule::SetAllResourcesBound(bool ResourcesBound) {
  455. m_bAllResourcesBound = ResourcesBound;
  456. }
  457. bool DxilModule::GetAllResourcesBound() const {
  458. return m_bAllResourcesBound;
  459. }
  460. unsigned DxilModule::GetInputControlPointCount() const {
  461. if (!(m_pSM->IsHS() || m_pSM->IsDS()))
  462. return 0;
  463. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  464. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  465. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  466. if (props.IsHS())
  467. return props.ShaderProps.HS.inputControlPoints;
  468. else
  469. return props.ShaderProps.DS.inputControlPoints;
  470. }
  471. void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
  472. DXASSERT(m_DxilEntryPropsMap.size() == 1
  473. && (m_pSM->IsHS() || m_pSM->IsDS()),
  474. "only works for non-lib profile");
  475. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  476. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  477. if (props.IsHS())
  478. props.ShaderProps.HS.inputControlPoints = NumICPs;
  479. else
  480. props.ShaderProps.DS.inputControlPoints = NumICPs;
  481. }
  482. DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
  483. if (!(m_pSM->IsHS() || m_pSM->IsDS()))
  484. return DXIL::TessellatorDomain::Undefined;
  485. DXASSERT_NOMSG(m_DxilEntryPropsMap.size() == 1);
  486. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  487. if (props.IsHS())
  488. return props.ShaderProps.HS.domain;
  489. else
  490. return props.ShaderProps.DS.domain;
  491. }
  492. void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
  493. DXASSERT(m_DxilEntryPropsMap.size() == 1
  494. && (m_pSM->IsHS() || m_pSM->IsDS()),
  495. "only works for HS or DS profile");
  496. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  497. DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
  498. if (props.IsHS())
  499. props.ShaderProps.HS.domain = TessDomain;
  500. else
  501. props.ShaderProps.DS.domain = TessDomain;
  502. }
  503. unsigned DxilModule::GetOutputControlPointCount() const {
  504. if (!m_pSM->IsHS())
  505. return 0;
  506. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  507. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  508. DXASSERT(props.IsHS(), "Must be HS profile");
  509. return props.ShaderProps.HS.outputControlPoints;
  510. }
  511. void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
  512. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  513. "only works for HS profile");
  514. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  515. DXASSERT(props.IsHS(), "Must be HS profile");
  516. props.ShaderProps.HS.outputControlPoints = NumOCPs;
  517. }
  518. DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
  519. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  520. "only works for HS profile");
  521. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  522. DXASSERT(props.IsHS(), "Must be HS profile");
  523. return props.ShaderProps.HS.partition;
  524. }
  525. void DxilModule::SetTessellatorPartitioning(DXIL::TessellatorPartitioning TessPartitioning) {
  526. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  527. "only works for HS profile");
  528. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  529. DXASSERT(props.IsHS(), "Must be HS profile");
  530. props.ShaderProps.HS.partition = TessPartitioning;
  531. }
  532. DXIL::TessellatorOutputPrimitive DxilModule::GetTessellatorOutputPrimitive() const {
  533. if (!m_pSM->IsHS())
  534. return DXIL::TessellatorOutputPrimitive::Undefined;
  535. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  536. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  537. DXASSERT(props.IsHS(), "Must be HS profile");
  538. return props.ShaderProps.HS.outputPrimitive;
  539. }
  540. void DxilModule::SetTessellatorOutputPrimitive(DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
  541. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  542. "only works for HS profile");
  543. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  544. DXASSERT(props.IsHS(), "Must be HS profile");
  545. props.ShaderProps.HS.outputPrimitive = TessOutputPrimitive;
  546. }
  547. float DxilModule::GetMaxTessellationFactor() const {
  548. if (!m_pSM->IsHS())
  549. return 0.0F;
  550. DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
  551. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  552. DXASSERT(props.IsHS(), "Must be HS profile");
  553. return props.ShaderProps.HS.maxTessFactor;
  554. }
  555. void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
  556. DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
  557. "only works for HS profile");
  558. DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
  559. DXASSERT(props.IsHS(), "Must be HS profile");
  560. props.ShaderProps.HS.maxTessFactor = MaxTessellationFactor;
  561. }
  562. void DxilModule::SetAutoBindingSpace(uint32_t Space) {
  563. m_AutoBindingSpace = Space;
  564. }
  565. uint32_t DxilModule::GetAutoBindingSpace() const {
  566. return m_AutoBindingSpace;
  567. }
  568. void DxilModule::SetShaderProperties(DxilFunctionProps *props) {
  569. if (!props)
  570. return;
  571. DxilFunctionProps &ourProps = GetDxilFunctionProps(GetEntryFunction());
  572. if (props != &ourProps) {
  573. ourProps.shaderKind = props->shaderKind;
  574. ourProps.ShaderProps = props->ShaderProps;
  575. }
  576. switch (props->shaderKind) {
  577. case DXIL::ShaderKind::Pixel: {
  578. auto &PS = props->ShaderProps.PS;
  579. m_ShaderFlags.SetForceEarlyDepthStencil(PS.EarlyDepthStencil);
  580. } break;
  581. case DXIL::ShaderKind::Compute:
  582. case DXIL::ShaderKind::Domain:
  583. case DXIL::ShaderKind::Hull:
  584. case DXIL::ShaderKind::Vertex:
  585. break;
  586. default: {
  587. DXASSERT(props->shaderKind == DXIL::ShaderKind::Geometry,
  588. "else invalid shader kind");
  589. auto &GS = props->ShaderProps.GS;
  590. m_ActiveStreamMask = 0;
  591. for (size_t i = 0; i < _countof(GS.streamPrimitiveTopologies); ++i) {
  592. if (GS.streamPrimitiveTopologies[i] !=
  593. DXIL::PrimitiveTopology::Undefined) {
  594. m_ActiveStreamMask |= (1 << i);
  595. DXASSERT_NOMSG(m_StreamPrimitiveTopology ==
  596. DXIL::PrimitiveTopology::Undefined ||
  597. m_StreamPrimitiveTopology ==
  598. GS.streamPrimitiveTopologies[i]);
  599. m_StreamPrimitiveTopology = GS.streamPrimitiveTopologies[i];
  600. }
  601. }
  602. // Refresh props:
  603. SetActiveStreamMask(m_ActiveStreamMask);
  604. } break;
  605. }
  606. }
  607. template<typename T> unsigned
  608. DxilModule::AddResource(vector<unique_ptr<T> > &Vec, unique_ptr<T> pRes) {
  609. DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
  610. unsigned Id = (unsigned)Vec.size();
  611. Vec.emplace_back(std::move(pRes));
  612. return Id;
  613. }
  614. unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
  615. return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
  616. }
  617. DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) {
  618. return *m_CBuffers[idx];
  619. }
  620. const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
  621. return *m_CBuffers[idx];
  622. }
  623. const vector<unique_ptr<DxilCBuffer> > &DxilModule::GetCBuffers() const {
  624. return m_CBuffers;
  625. }
  626. unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
  627. return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
  628. }
  629. DxilSampler &DxilModule::GetSampler(unsigned idx) {
  630. return *m_Samplers[idx];
  631. }
  632. const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
  633. return *m_Samplers[idx];
  634. }
  635. const vector<unique_ptr<DxilSampler> > &DxilModule::GetSamplers() const {
  636. return m_Samplers;
  637. }
  638. unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
  639. return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
  640. }
  641. DxilResource &DxilModule::GetSRV(unsigned idx) {
  642. return *m_SRVs[idx];
  643. }
  644. const DxilResource &DxilModule::GetSRV(unsigned idx) const {
  645. return *m_SRVs[idx];
  646. }
  647. const vector<unique_ptr<DxilResource> > &DxilModule::GetSRVs() const {
  648. return m_SRVs;
  649. }
  650. unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
  651. return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
  652. }
  653. DxilResource &DxilModule::GetUAV(unsigned idx) {
  654. return *m_UAVs[idx];
  655. }
  656. const DxilResource &DxilModule::GetUAV(unsigned idx) const {
  657. return *m_UAVs[idx];
  658. }
  659. const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
  660. return m_UAVs;
  661. }
  662. void DxilModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
  663. return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
  664. }
  665. void DxilModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
  666. return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
  667. }
  668. void DxilModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
  669. return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
  670. }
  671. template <typename TResource>
  672. static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
  673. std::unordered_set<unsigned> &immResID) {
  674. for (auto p = vec.begin(); p != vec.end();) {
  675. auto c = p++;
  676. if (immResID.count((*c)->GetID()) == 0) {
  677. p = vec.erase(c);
  678. }
  679. }
  680. }
  681. static void CollectUsedResource(Value *resID,
  682. std::unordered_set<Value *> &usedResID) {
  683. if (usedResID.count(resID) > 0)
  684. return;
  685. usedResID.insert(resID);
  686. if (dyn_cast<ConstantInt>(resID)) {
  687. // Do nothing
  688. } else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
  689. if (ZEI->getSrcTy()->isIntegerTy()) {
  690. IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
  691. if (ITy->getBitWidth() == 1) {
  692. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
  693. usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
  694. }
  695. }
  696. } else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
  697. CollectUsedResource(SI->getTrueValue(), usedResID);
  698. CollectUsedResource(SI->getFalseValue(), usedResID);
  699. } else if (PHINode *Phi = dyn_cast<PHINode>(resID)) {
  700. for (Use &U : Phi->incoming_values()) {
  701. CollectUsedResource(U.get(), usedResID);
  702. }
  703. }
  704. // TODO: resID could be other types of instructions depending on the compiler optimization.
  705. }
  706. static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
  707. std::unordered_set<Value *> &usedResID) {
  708. for (Value *V : usedResID) {
  709. if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
  710. immResID.insert(cResID->getLimitedValue());
  711. }
  712. }
  713. }
  714. void DxilModule::RemoveFunction(llvm::Function *F) {
  715. DXASSERT_NOMSG(F != nullptr);
  716. m_DxilEntryPropsMap.erase(F);
  717. if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
  718. m_pTypeSystem.get()->EraseFunctionAnnotation(F);
  719. m_pOP->RemoveFunction(F);
  720. }
  721. void DxilModule::RemoveUnusedResources() {
  722. DXASSERT(!m_pSM->IsLib(), "this function not work on library");
  723. hlsl::OP *hlslOP = GetOP();
  724. Function *createHandleFunc = hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
  725. if (createHandleFunc->user_empty()) {
  726. m_CBuffers.clear();
  727. m_UAVs.clear();
  728. m_SRVs.clear();
  729. m_Samplers.clear();
  730. createHandleFunc->eraseFromParent();
  731. return;
  732. }
  733. std::unordered_set<Value *> usedUAVID;
  734. std::unordered_set<Value *> usedSRVID;
  735. std::unordered_set<Value *> usedSamplerID;
  736. std::unordered_set<Value *> usedCBufID;
  737. // Collect used ID.
  738. for (User *U : createHandleFunc->users()) {
  739. CallInst *CI = cast<CallInst>(U);
  740. Value *vResClass =
  741. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
  742. ConstantInt *cResClass = cast<ConstantInt>(vResClass);
  743. DXIL::ResourceClass resClass =
  744. static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
  745. // Skip unused resource handle.
  746. if (CI->user_empty())
  747. continue;
  748. Value *resID =
  749. CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
  750. switch (resClass) {
  751. case DXIL::ResourceClass::CBuffer:
  752. CollectUsedResource(resID, usedCBufID);
  753. break;
  754. case DXIL::ResourceClass::Sampler:
  755. CollectUsedResource(resID, usedSamplerID);
  756. break;
  757. case DXIL::ResourceClass::SRV:
  758. CollectUsedResource(resID, usedSRVID);
  759. break;
  760. case DXIL::ResourceClass::UAV:
  761. CollectUsedResource(resID, usedUAVID);
  762. break;
  763. default:
  764. DXASSERT(0, "invalid res class");
  765. break;
  766. }
  767. }
  768. std::unordered_set<unsigned> immUAVID;
  769. std::unordered_set<unsigned> immSRVID;
  770. std::unordered_set<unsigned> immSamplerID;
  771. std::unordered_set<unsigned> immCBufID;
  772. ConvertUsedResource(immUAVID, usedUAVID);
  773. RemoveResources(m_UAVs, immUAVID);
  774. ConvertUsedResource(immSRVID, usedSRVID);
  775. ConvertUsedResource(immSamplerID, usedSamplerID);
  776. ConvertUsedResource(immCBufID, usedCBufID);
  777. RemoveResources(m_SRVs, immSRVID);
  778. RemoveResources(m_Samplers, immSamplerID);
  779. RemoveResources(m_CBuffers, immCBufID);
  780. }
  781. namespace {
  782. template <typename TResource>
  783. static void RemoveResourceSymbols(std::vector<std::unique_ptr<TResource>> &vec) {
  784. unsigned resID = 0;
  785. for (std::vector<std::unique_ptr<TResource>>::iterator p = vec.begin(); p != vec.end();) {
  786. std::vector<std::unique_ptr<TResource>>::iterator c = p++;
  787. GlobalVariable *GV = cast<GlobalVariable>((*c)->GetGlobalSymbol());
  788. GV->removeDeadConstantUsers();
  789. if (GV->user_empty()) {
  790. p = vec.erase(c);
  791. GV->eraseFromParent();
  792. continue;
  793. }
  794. if ((*c)->GetID() != resID) {
  795. (*c)->SetID(resID);
  796. }
  797. resID++;
  798. }
  799. }
  800. }
  801. void DxilModule::RemoveUnusedResourceSymbols() {
  802. RemoveResourceSymbols(m_SRVs);
  803. RemoveResourceSymbols(m_UAVs);
  804. RemoveResourceSymbols(m_CBuffers);
  805. RemoveResourceSymbols(m_Samplers);
  806. }
  807. DxilSignature &DxilModule::GetInputSignature() {
  808. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  809. "only works for non-lib profile");
  810. return m_DxilEntryPropsMap.begin()->second->sig.InputSignature;
  811. }
  812. const DxilSignature &DxilModule::GetInputSignature() const {
  813. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  814. "only works for non-lib profile");
  815. return m_DxilEntryPropsMap.begin()->second->sig.InputSignature;
  816. }
  817. DxilSignature &DxilModule::GetOutputSignature() {
  818. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  819. "only works for non-lib profile");
  820. return m_DxilEntryPropsMap.begin()->second->sig.OutputSignature;
  821. }
  822. const DxilSignature &DxilModule::GetOutputSignature() const {
  823. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  824. "only works for non-lib profile");
  825. return m_DxilEntryPropsMap.begin()->second->sig.OutputSignature;
  826. }
  827. DxilSignature &DxilModule::GetPatchConstantSignature() {
  828. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  829. "only works for non-lib profile");
  830. return m_DxilEntryPropsMap.begin()->second->sig.PatchConstantSignature;
  831. }
  832. const DxilSignature &DxilModule::GetPatchConstantSignature() const {
  833. DXASSERT(m_DxilEntryPropsMap.size() == 1 && !m_pSM->IsLib(),
  834. "only works for non-lib profile");
  835. return m_DxilEntryPropsMap.begin()->second->sig.PatchConstantSignature;
  836. }
  837. const RootSignatureHandle &DxilModule::GetRootSignature() const {
  838. return *m_RootSignature;
  839. }
  840. // Entry props.
  841. bool DxilModule::HasDxilEntrySignature(const llvm::Function *F) const {
  842. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  843. }
  844. DxilEntrySignature &DxilModule::GetDxilEntrySignature(const llvm::Function *F) {
  845. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  846. return m_DxilEntryPropsMap[F].get()->sig;
  847. }
  848. void DxilModule::ReplaceDxilEntryProps(llvm::Function *F,
  849. llvm::Function *NewF) {
  850. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  851. std::unique_ptr<DxilEntryProps> Props = std::move(m_DxilEntryPropsMap[F]);
  852. m_DxilEntryPropsMap.erase(F);
  853. m_DxilEntryPropsMap[NewF] = std::move(Props);
  854. }
  855. void DxilModule::CloneDxilEntryProps(llvm::Function *F, llvm::Function *NewF) {
  856. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  857. std::unique_ptr<DxilEntryProps> Props =
  858. llvm::make_unique<DxilEntryProps>(*m_DxilEntryPropsMap[F]);
  859. m_DxilEntryPropsMap[NewF] = std::move(Props);
  860. }
  861. bool DxilModule::HasDxilEntryProps(const llvm::Function *F) const {
  862. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  863. }
  864. DxilEntryProps &DxilModule::GetDxilEntryProps(const llvm::Function *F) {
  865. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  866. return *m_DxilEntryPropsMap.find(F)->second.get();
  867. }
  868. bool DxilModule::HasDxilFunctionProps(const llvm::Function *F) const {
  869. return m_DxilEntryPropsMap.find(F) != m_DxilEntryPropsMap.end();
  870. }
  871. DxilFunctionProps &DxilModule::GetDxilFunctionProps(const llvm::Function *F) {
  872. return const_cast<DxilFunctionProps &>(
  873. static_cast<const DxilModule *>(this)->GetDxilFunctionProps(F));
  874. }
  875. const DxilFunctionProps &
  876. DxilModule::GetDxilFunctionProps(const llvm::Function *F) const {
  877. DXASSERT(m_DxilEntryPropsMap.count(F) != 0, "cannot find F in map");
  878. return m_DxilEntryPropsMap.find(F)->second.get()->props;
  879. }
  880. void DxilModule::SetPatchConstantFunctionForHS(llvm::Function *hullShaderFunc, llvm::Function *patchConstantFunc) {
  881. auto propIter = m_DxilEntryPropsMap.find(hullShaderFunc);
  882. DXASSERT(propIter != m_DxilEntryPropsMap.end(),
  883. "Hull shader must already have function props!");
  884. DxilFunctionProps &props = propIter->second->props;
  885. DXASSERT(props.IsHS(), "else hullShaderFunc is not a Hull Shader");
  886. auto &HS = props.ShaderProps.HS;
  887. if (HS.patchConstantFunc != patchConstantFunc) {
  888. if (HS.patchConstantFunc)
  889. m_PatchConstantFunctions.erase(HS.patchConstantFunc);
  890. HS.patchConstantFunc = patchConstantFunc;
  891. if (patchConstantFunc)
  892. m_PatchConstantFunctions.insert(patchConstantFunc);
  893. }
  894. }
  895. bool DxilModule::IsGraphicsShader(const llvm::Function *F) const {
  896. return HasDxilFunctionProps(F) && GetDxilFunctionProps(F).IsGraphics();
  897. }
  898. bool DxilModule::IsPatchConstantShader(const llvm::Function *F) const {
  899. return m_PatchConstantFunctions.count(F) != 0;
  900. }
  901. bool DxilModule::IsComputeShader(const llvm::Function *F) const {
  902. return HasDxilFunctionProps(F) && GetDxilFunctionProps(F).IsCS();
  903. }
  904. bool DxilModule::IsEntryThatUsesSignatures(const llvm::Function *F) const {
  905. auto propIter = m_DxilEntryPropsMap.find(F);
  906. if (propIter != m_DxilEntryPropsMap.end()) {
  907. DxilFunctionProps &props = propIter->second->props;
  908. return props.IsGraphics() || props.IsCS();
  909. }
  910. // Otherwise, return true if patch constant function
  911. return IsPatchConstantShader(F);
  912. }
  913. void DxilModule::StripRootSignatureFromMetadata() {
  914. NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
  915. if (pRootSignatureNamedMD) {
  916. GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
  917. }
  918. }
  919. void DxilModule::UpdateValidatorVersionMetadata() {
  920. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  921. }
  922. void DxilModule::ResetRootSignature(RootSignatureHandle *pValue) {
  923. m_RootSignature.reset(pValue);
  924. }
  925. DxilTypeSystem &DxilModule::GetTypeSystem() {
  926. return *m_pTypeSystem;
  927. }
  928. DxilViewIdState &DxilModule::GetViewIdState() {
  929. return *m_pViewIdState;
  930. }
  931. const DxilViewIdState &DxilModule::GetViewIdState() const {
  932. return *m_pViewIdState;
  933. }
  934. void DxilModule::ResetTypeSystem(DxilTypeSystem *pValue) {
  935. m_pTypeSystem.reset(pValue);
  936. }
  937. void DxilModule::ResetOP(hlsl::OP *hlslOP) { m_pOP.reset(hlslOP); }
  938. void DxilModule::ResetEntryPropsMap(DxilEntryPropsMap &&PropMap) {
  939. m_DxilEntryPropsMap.clear();
  940. std::move(PropMap.begin(), PropMap.end(),
  941. inserter(m_DxilEntryPropsMap, m_DxilEntryPropsMap.begin()));
  942. }
  943. static const StringRef llvmUsedName = "llvm.used";
  944. void DxilModule::EmitLLVMUsed() {
  945. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  946. oldGV->eraseFromParent();
  947. }
  948. if (m_LLVMUsed.empty())
  949. return;
  950. vector<llvm::Constant *> GVs;
  951. Type *pI8PtrType = Type::getInt8PtrTy(m_Ctx, DXIL::kDefaultAddrSpace);
  952. GVs.resize(m_LLVMUsed.size());
  953. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  954. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  955. PointerType *pPtrType = dyn_cast<PointerType>(pConst->getType());
  956. if (pPtrType->getPointerAddressSpace() != DXIL::kDefaultAddrSpace) {
  957. // Cast pointer to addrspace 0, as LLVMUsed elements must have the same
  958. // type.
  959. GVs[i] = ConstantExpr::getAddrSpaceCast(pConst, pI8PtrType);
  960. } else {
  961. GVs[i] = ConstantExpr::getPointerCast(pConst, pI8PtrType);
  962. }
  963. }
  964. ArrayType *pATy = ArrayType::get(pI8PtrType, GVs.size());
  965. GlobalVariable *pGV =
  966. new GlobalVariable(*m_pModule, pATy, false, GlobalValue::AppendingLinkage,
  967. ConstantArray::get(pATy, GVs), llvmUsedName);
  968. pGV->setSection("llvm.metadata");
  969. }
  970. void DxilModule::ClearLLVMUsed() {
  971. if (GlobalVariable *oldGV = m_pModule->getGlobalVariable(llvmUsedName)) {
  972. oldGV->eraseFromParent();
  973. }
  974. if (m_LLVMUsed.empty())
  975. return;
  976. for (size_t i = 0, e = m_LLVMUsed.size(); i != e; i++) {
  977. Constant *pConst = cast<Constant>(&*m_LLVMUsed[i]);
  978. pConst->removeDeadConstantUsers();
  979. }
  980. m_LLVMUsed.clear();
  981. }
  982. vector<GlobalVariable* > &DxilModule::GetLLVMUsed() {
  983. return m_LLVMUsed;
  984. }
  985. // DXIL metadata serialization/deserialization.
  986. void DxilModule::ClearDxilMetadata(Module &M) {
  987. // Delete: DXIL version, validator version, DXIL shader model,
  988. // entry point tuples (shader properties, signatures, resources)
  989. // type system, view ID state, LLVM used, entry point tuples,
  990. // root signature, function properties.
  991. // Other cases for libs pending.
  992. // LLVM used is a global variable - handle separately.
  993. Module::named_metadata_iterator
  994. b = M.named_metadata_begin(),
  995. e = M.named_metadata_end();
  996. SmallVector<NamedMDNode*, 8> nodes;
  997. for (; b != e; ++b) {
  998. StringRef name = b->getName();
  999. if (name == DxilMDHelper::kDxilVersionMDName ||
  1000. name == DxilMDHelper::kDxilValidatorVersionMDName ||
  1001. name == DxilMDHelper::kDxilShaderModelMDName ||
  1002. name == DxilMDHelper::kDxilEntryPointsMDName ||
  1003. name == DxilMDHelper::kDxilRootSignatureMDName ||
  1004. name == DxilMDHelper::kDxilResourcesMDName ||
  1005. name == DxilMDHelper::kDxilTypeSystemMDName ||
  1006. name == DxilMDHelper::kDxilViewIdStateMDName ||
  1007. name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
  1008. nodes.push_back(b);
  1009. }
  1010. }
  1011. for (size_t i = 0; i < nodes.size(); ++i) {
  1012. M.eraseNamedMetadata(nodes[i]);
  1013. }
  1014. }
  1015. void DxilModule::EmitDxilMetadata() {
  1016. m_pMDHelper->EmitDxilVersion(m_DxilMajor, m_DxilMinor);
  1017. m_pMDHelper->EmitValidatorVersion(m_ValMajor, m_ValMinor);
  1018. m_pMDHelper->EmitDxilShaderModel(m_pSM);
  1019. MDTuple *pMDProperties = nullptr;
  1020. uint64_t flag = m_ShaderFlags.GetShaderFlagsRaw();
  1021. if (m_pSM->IsLib()) {
  1022. DxilFunctionProps props;
  1023. props.shaderKind = DXIL::ShaderKind::Library;
  1024. pMDProperties = m_pMDHelper->EmitDxilEntryProperties(flag, props,
  1025. GetAutoBindingSpace());
  1026. } else {
  1027. pMDProperties = m_pMDHelper->EmitDxilEntryProperties(
  1028. flag, m_DxilEntryPropsMap.begin()->second->props,
  1029. GetAutoBindingSpace());
  1030. }
  1031. MDTuple *pMDSignatures = nullptr;
  1032. if (!m_pSM->IsLib()) {
  1033. pMDSignatures = m_pMDHelper->EmitDxilSignatures(
  1034. m_DxilEntryPropsMap.begin()->second->sig);
  1035. }
  1036. MDTuple *pMDResources = EmitDxilResources();
  1037. if (pMDResources)
  1038. m_pMDHelper->EmitDxilResources(pMDResources);
  1039. m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
  1040. if (!m_pSM->IsLib() && !m_pSM->IsCS() &&
  1041. ((m_ValMajor == 0 && m_ValMinor == 0) ||
  1042. (m_ValMajor > 1 || (m_ValMajor == 1 && m_ValMinor >= 1)))) {
  1043. m_pMDHelper->EmitDxilViewIdState(GetViewIdState());
  1044. }
  1045. EmitLLVMUsed();
  1046. MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
  1047. vector<MDNode *> Entries;
  1048. Entries.emplace_back(pEntry);
  1049. if (m_pSM->IsLib()) {
  1050. // Sort functions by name to keep metadata deterministic
  1051. vector<const Function *> funcOrder;
  1052. funcOrder.reserve(m_DxilEntryPropsMap.size());
  1053. std::transform( m_DxilEntryPropsMap.begin(),
  1054. m_DxilEntryPropsMap.end(),
  1055. std::back_inserter(funcOrder),
  1056. [](auto &p) -> const Function* { return p.first; } );
  1057. std::sort(funcOrder.begin(), funcOrder.end(), [](const Function *F1, const Function *F2) {
  1058. return F1->getName() < F2->getName();
  1059. });
  1060. for (auto F : funcOrder) {
  1061. auto &entryProps = m_DxilEntryPropsMap[F];
  1062. MDTuple *pProps = m_pMDHelper->EmitDxilEntryProperties(0, entryProps->props, 0);
  1063. MDTuple *pSig = m_pMDHelper->EmitDxilSignatures(entryProps->sig);
  1064. MDTuple *pSubEntry = m_pMDHelper->EmitDxilEntryPointTuple(
  1065. const_cast<Function *>(F), F->getName(), pSig, nullptr, pProps);
  1066. Entries.emplace_back(pSubEntry);
  1067. }
  1068. funcOrder.clear();
  1069. }
  1070. m_pMDHelper->EmitDxilEntryPoints(Entries);
  1071. if (!m_RootSignature->IsEmpty()) {
  1072. m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
  1073. }
  1074. }
  1075. bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
  1076. return DxilMDHelper::IsKnownNamedMetaData(Node);
  1077. }
  1078. void DxilModule::LoadDxilMetadata() {
  1079. m_pMDHelper->LoadDxilVersion(m_DxilMajor, m_DxilMinor);
  1080. m_pMDHelper->LoadValidatorVersion(m_ValMajor, m_ValMinor);
  1081. const ShaderModel *loadedSM;
  1082. m_pMDHelper->LoadDxilShaderModel(loadedSM);
  1083. // This must be set before LoadDxilEntryProperties
  1084. m_pMDHelper->SetShaderModel(loadedSM);
  1085. // Setting module shader model requires UseMinPrecision flag,
  1086. // which requires loading m_ShaderFlags,
  1087. // which requires global entry properties,
  1088. // so load entry properties first, then set the shader model
  1089. const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
  1090. if (!loadedSM->IsLib()) {
  1091. IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
  1092. }
  1093. Function *pEntryFunc;
  1094. string EntryName;
  1095. const llvm::MDOperand *pEntrySignatures, *pEntryResources, *pEntryProperties;
  1096. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0),
  1097. pEntryFunc, EntryName,
  1098. pEntrySignatures, pEntryResources,
  1099. pEntryProperties);
  1100. uint64_t rawShaderFlags = 0;
  1101. DxilFunctionProps entryFuncProps;
  1102. entryFuncProps.shaderKind = loadedSM->GetKind();
  1103. if (loadedSM->IsLib()) {
  1104. // Get rawShaderFlags and m_AutoBindingSpace; entryFuncProps unused.
  1105. m_pMDHelper->LoadDxilEntryProperties(*pEntryProperties, rawShaderFlags,
  1106. entryFuncProps, m_AutoBindingSpace);
  1107. }
  1108. else {
  1109. m_pMDHelper->LoadDxilEntryProperties(*pEntryProperties, rawShaderFlags,
  1110. entryFuncProps, m_AutoBindingSpace);
  1111. }
  1112. m_bUseMinPrecision = true;
  1113. if (rawShaderFlags) {
  1114. m_ShaderFlags.SetShaderFlagsRaw(rawShaderFlags);
  1115. m_bUseMinPrecision = !m_ShaderFlags.GetUseNativeLowPrecision();
  1116. m_bDisableOptimizations = m_ShaderFlags.GetDisableOptimizations();
  1117. m_bAllResourcesBound = m_ShaderFlags.GetAllResourcesBound();
  1118. }
  1119. // Now that we have the UseMinPrecision flag, set shader model:
  1120. SetShaderModel(loadedSM, m_bUseMinPrecision);
  1121. if (loadedSM->IsLib()) {
  1122. for (unsigned i = 1; i < pEntries->getNumOperands(); i++) {
  1123. Function *pFunc;
  1124. string Name;
  1125. const llvm::MDOperand *pSignatures, *pResources, *pProperties;
  1126. m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(i), pFunc, Name,
  1127. pSignatures, pResources, pProperties);
  1128. DxilFunctionProps props;
  1129. uint64_t rawShaderFlags = 0;
  1130. unsigned autoBindingSpace = 0;
  1131. m_pMDHelper->LoadDxilEntryProperties(
  1132. *pProperties, rawShaderFlags, props, autoBindingSpace);
  1133. if (props.IsHS() && props.ShaderProps.HS.patchConstantFunc) {
  1134. // Add patch constant function to m_PatchConstantFunctions
  1135. m_PatchConstantFunctions.insert(props.ShaderProps.HS.patchConstantFunc);
  1136. }
  1137. std::unique_ptr<DxilEntryProps> pEntryProps =
  1138. llvm::make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
  1139. DXASSERT(pSignatures->get() == nullptr || !props.IsRay(),
  1140. "Raytracing has no signature");
  1141. m_pMDHelper->LoadDxilSignatures(*pSignatures, pEntryProps->sig);
  1142. m_DxilEntryPropsMap[pFunc] = std::move(pEntryProps);
  1143. }
  1144. } else {
  1145. std::unique_ptr<DxilEntryProps> pEntryProps =
  1146. llvm::make_unique<DxilEntryProps>(entryFuncProps, m_bUseMinPrecision);
  1147. DxilFunctionProps *pFuncProps = &pEntryProps->props;
  1148. m_pMDHelper->LoadDxilSignatures(*pEntrySignatures, pEntryProps->sig);
  1149. m_DxilEntryPropsMap.clear();
  1150. m_DxilEntryPropsMap[pEntryFunc] = std::move(pEntryProps);
  1151. SetEntryFunction(pEntryFunc);
  1152. SetEntryFunctionName(EntryName);
  1153. SetShaderProperties(pFuncProps);
  1154. }
  1155. LoadDxilResources(*pEntryResources);
  1156. m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
  1157. m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
  1158. m_pMDHelper->LoadDxilViewIdState(*m_pViewIdState.get());
  1159. }
  1160. MDTuple *DxilModule::EmitDxilResources() {
  1161. // Emit SRV records.
  1162. MDTuple *pTupleSRVs = nullptr;
  1163. if (!m_SRVs.empty()) {
  1164. vector<Metadata *> MDVals;
  1165. for (size_t i = 0; i < m_SRVs.size(); i++) {
  1166. MDVals.emplace_back(m_pMDHelper->EmitDxilSRV(*m_SRVs[i]));
  1167. }
  1168. pTupleSRVs = MDNode::get(m_Ctx, MDVals);
  1169. }
  1170. // Emit UAV records.
  1171. MDTuple *pTupleUAVs = nullptr;
  1172. if (!m_UAVs.empty()) {
  1173. vector<Metadata *> MDVals;
  1174. for (size_t i = 0; i < m_UAVs.size(); i++) {
  1175. MDVals.emplace_back(m_pMDHelper->EmitDxilUAV(*m_UAVs[i]));
  1176. }
  1177. pTupleUAVs = MDNode::get(m_Ctx, MDVals);
  1178. }
  1179. // Emit CBuffer records.
  1180. MDTuple *pTupleCBuffers = nullptr;
  1181. if (!m_CBuffers.empty()) {
  1182. vector<Metadata *> MDVals;
  1183. for (size_t i = 0; i < m_CBuffers.size(); i++) {
  1184. MDVals.emplace_back(m_pMDHelper->EmitDxilCBuffer(*m_CBuffers[i]));
  1185. }
  1186. pTupleCBuffers = MDNode::get(m_Ctx, MDVals);
  1187. }
  1188. // Emit Sampler records.
  1189. MDTuple *pTupleSamplers = nullptr;
  1190. if (!m_Samplers.empty()) {
  1191. vector<Metadata *> MDVals;
  1192. for (size_t i = 0; i < m_Samplers.size(); i++) {
  1193. MDVals.emplace_back(m_pMDHelper->EmitDxilSampler(*m_Samplers[i]));
  1194. }
  1195. pTupleSamplers = MDNode::get(m_Ctx, MDVals);
  1196. }
  1197. if (pTupleSRVs != nullptr || pTupleUAVs != nullptr || pTupleCBuffers != nullptr || pTupleSamplers != nullptr) {
  1198. return m_pMDHelper->EmitDxilResourceTuple(pTupleSRVs, pTupleUAVs, pTupleCBuffers, pTupleSamplers);
  1199. } else {
  1200. return nullptr;
  1201. }
  1202. }
  1203. void DxilModule::ReEmitDxilResources() {
  1204. ClearDxilMetadata(*m_pModule);
  1205. if (!m_pSM->IsCS() && !m_pSM->IsLib())
  1206. m_pViewIdState->Compute();
  1207. EmitDxilMetadata();
  1208. }
  1209. void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
  1210. if (MDO.get() == nullptr)
  1211. return;
  1212. const llvm::MDTuple *pSRVs, *pUAVs, *pCBuffers, *pSamplers;
  1213. m_pMDHelper->GetDxilResources(MDO, pSRVs, pUAVs, pCBuffers, pSamplers);
  1214. // Load SRV records.
  1215. if (pSRVs != nullptr) {
  1216. for (unsigned i = 0; i < pSRVs->getNumOperands(); i++) {
  1217. unique_ptr<DxilResource> pSRV(new DxilResource);
  1218. m_pMDHelper->LoadDxilSRV(pSRVs->getOperand(i), *pSRV);
  1219. AddSRV(std::move(pSRV));
  1220. }
  1221. }
  1222. // Load UAV records.
  1223. if (pUAVs != nullptr) {
  1224. for (unsigned i = 0; i < pUAVs->getNumOperands(); i++) {
  1225. unique_ptr<DxilResource> pUAV(new DxilResource);
  1226. m_pMDHelper->LoadDxilUAV(pUAVs->getOperand(i), *pUAV);
  1227. AddUAV(std::move(pUAV));
  1228. }
  1229. }
  1230. // Load CBuffer records.
  1231. if (pCBuffers != nullptr) {
  1232. for (unsigned i = 0; i < pCBuffers->getNumOperands(); i++) {
  1233. unique_ptr<DxilCBuffer> pCB(new DxilCBuffer);
  1234. m_pMDHelper->LoadDxilCBuffer(pCBuffers->getOperand(i), *pCB);
  1235. AddCBuffer(std::move(pCB));
  1236. }
  1237. }
  1238. // Load Sampler records.
  1239. if (pSamplers != nullptr) {
  1240. for (unsigned i = 0; i < pSamplers->getNumOperands(); i++) {
  1241. unique_ptr<DxilSampler> pSampler(new DxilSampler);
  1242. m_pMDHelper->LoadDxilSampler(pSamplers->getOperand(i), *pSampler);
  1243. AddSampler(std::move(pSampler));
  1244. }
  1245. }
  1246. }
  1247. void DxilModule::StripDebugRelatedCode() {
  1248. // Remove all users of global resources.
  1249. for (GlobalVariable &GV : m_pModule->globals()) {
  1250. if (GV.hasInternalLinkage())
  1251. continue;
  1252. if (GV.getType()->getPointerAddressSpace() == DXIL::kTGSMAddrSpace)
  1253. continue;
  1254. for (auto git = GV.user_begin(); git != GV.user_end();) {
  1255. User *U = *(git++);
  1256. // Try to remove load of GV.
  1257. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  1258. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1259. Instruction *LIUser = cast<Instruction>(*(it++));
  1260. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1261. Value *Ptr = SI->getPointerOperand();
  1262. SI->eraseFromParent();
  1263. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1264. if (Ptr->user_empty())
  1265. PtrInst->eraseFromParent();
  1266. }
  1267. }
  1268. }
  1269. if (LI->user_empty())
  1270. LI->eraseFromParent();
  1271. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
  1272. for (auto GEPIt = GEP->user_begin(); GEPIt != GEP->user_end();) {
  1273. User *GEPU = *(GEPIt++);
  1274. // Try to remove load of GEP.
  1275. if (LoadInst *LI = dyn_cast<LoadInst>(GEPU)) {
  1276. for (auto it = LI->user_begin(); it != LI->user_end();) {
  1277. Instruction *LIUser = cast<Instruction>(*(it++));
  1278. if (StoreInst *SI = dyn_cast<StoreInst>(LIUser)) {
  1279. Value *Ptr = SI->getPointerOperand();
  1280. SI->eraseFromParent();
  1281. if (Instruction *PtrInst = dyn_cast<Instruction>(Ptr)) {
  1282. if (Ptr->user_empty())
  1283. PtrInst->eraseFromParent();
  1284. }
  1285. }
  1286. if (LI->user_empty())
  1287. LI->eraseFromParent();
  1288. }
  1289. }
  1290. }
  1291. if (GEP->user_empty())
  1292. GEP->eraseFromParent();
  1293. }
  1294. }
  1295. }
  1296. // Remove dx.source metadata.
  1297. if (NamedMDNode *contents = m_pModule->getNamedMetadata(
  1298. DxilMDHelper::kDxilSourceContentsMDName)) {
  1299. contents->eraseFromParent();
  1300. }
  1301. if (NamedMDNode *defines =
  1302. m_pModule->getNamedMetadata(DxilMDHelper::kDxilSourceDefinesMDName)) {
  1303. defines->eraseFromParent();
  1304. }
  1305. if (NamedMDNode *mainFileName = m_pModule->getNamedMetadata(
  1306. DxilMDHelper::kDxilSourceMainFileNameMDName)) {
  1307. mainFileName->eraseFromParent();
  1308. }
  1309. if (NamedMDNode *arguments =
  1310. m_pModule->getNamedMetadata(DxilMDHelper::kDxilSourceArgsMDName)) {
  1311. arguments->eraseFromParent();
  1312. }
  1313. }
  1314. DebugInfoFinder &DxilModule::GetOrCreateDebugInfoFinder() {
  1315. if (m_pDebugInfoFinder == nullptr) {
  1316. m_pDebugInfoFinder = llvm::make_unique<llvm::DebugInfoFinder>();
  1317. m_pDebugInfoFinder->processModule(*m_pModule);
  1318. }
  1319. return *m_pDebugInfoFinder;
  1320. }
  1321. hlsl::DxilModule *hlsl::DxilModule::TryGetDxilModule(llvm::Module *pModule) {
  1322. LLVMContext &Ctx = pModule->getContext();
  1323. std::string diagStr;
  1324. raw_string_ostream diagStream(diagStr);
  1325. hlsl::DxilModule *pDxilModule = nullptr;
  1326. // TODO: add detail error in DxilMDHelper.
  1327. try {
  1328. pDxilModule = &pModule->GetOrCreateDxilModule();
  1329. } catch (const ::hlsl::Exception &hlslException) {
  1330. diagStream << "load dxil metadata failed -";
  1331. try {
  1332. const char *msg = hlslException.what();
  1333. if (msg == nullptr || *msg == '\0')
  1334. diagStream << " error code " << hlslException.hr << "\n";
  1335. else
  1336. diagStream << msg;
  1337. } catch (...) {
  1338. diagStream << " unable to retrieve error message.\n";
  1339. }
  1340. Ctx.diagnose(DxilErrorDiagnosticInfo(diagStream.str().c_str()));
  1341. } catch (...) {
  1342. Ctx.diagnose(DxilErrorDiagnosticInfo("load dxil metadata failed - unknown error.\n"));
  1343. }
  1344. return pDxilModule;
  1345. }
  1346. // Check if the instruction has fast math flags configured to indicate
  1347. // the instruction is precise.
  1348. // Precise fast math flags means none of the fast math flags are set.
  1349. bool DxilModule::HasPreciseFastMathFlags(const Instruction *inst) {
  1350. return isa<FPMathOperator>(inst) && !inst->getFastMathFlags().any();
  1351. }
  1352. // Set fast math flags configured to indicate the instruction is precise.
  1353. void DxilModule::SetPreciseFastMathFlags(llvm::Instruction *inst) {
  1354. assert(isa<FPMathOperator>(inst));
  1355. inst->copyFastMathFlags(FastMathFlags());
  1356. }
  1357. // True if fast math flags are preserved across serialization/deserialization
  1358. // of the dxil module.
  1359. //
  1360. // We need to check for this when querying fast math flags for preciseness
  1361. // otherwise we will be overly conservative by reporting instructions precise
  1362. // because their fast math flags were not preserved.
  1363. //
  1364. // Currently we restrict it to the instruction types that have fast math
  1365. // preserved in the bitcode. We can expand this by converting fast math
  1366. // flags to dx.precise metadata during serialization and back to fast
  1367. // math flags during deserialization.
  1368. bool DxilModule::PreservesFastMathFlags(const llvm::Instruction *inst) {
  1369. return
  1370. isa<FPMathOperator>(inst) && (isa<BinaryOperator>(inst) || isa<FCmpInst>(inst));
  1371. }
  1372. bool DxilModule::IsPrecise(const Instruction *inst) const {
  1373. if (m_ShaderFlags.GetDisableMathRefactoring())
  1374. return true;
  1375. else if (DxilMDHelper::IsMarkedPrecise(inst))
  1376. return true;
  1377. else if (PreservesFastMathFlags(inst))
  1378. return HasPreciseFastMathFlags(inst);
  1379. else
  1380. return false;
  1381. }
  1382. } // namespace hlsl
  1383. namespace llvm {
  1384. hlsl::DxilModule &Module::GetOrCreateDxilModule(bool skipInit) {
  1385. std::unique_ptr<hlsl::DxilModule> M;
  1386. if (!HasDxilModule()) {
  1387. M = llvm::make_unique<hlsl::DxilModule>(this);
  1388. if (!skipInit) {
  1389. M->LoadDxilMetadata();
  1390. }
  1391. SetDxilModule(M.release());
  1392. }
  1393. return GetDxilModule();
  1394. }
  1395. void Module::ResetDxilModule() {
  1396. if (HasDxilModule()) {
  1397. delete TheDxilModule;
  1398. TheDxilModule = nullptr;
  1399. }
  1400. }
  1401. }