ComputeViewIdStateBuilder.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ComputeViewIdStateBuilder.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/HLSL/ComputeViewIdState.h"
  10. #include "dxc/Support/Global.h"
  11. #include "dxc/HLSL/DxilModule.h"
  12. #include "dxc/HLSL/DxilOperations.h"
  13. #include "dxc/HLSL/DxilInstructions.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/Operator.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/IR/LegacyPassManager.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/IR/CFG.h"
  22. #include "llvm/Analysis/CallGraph.h"
  23. #include <algorithm>
  24. using namespace llvm;
  25. using namespace llvm::legacy;
  26. using namespace hlsl;
  27. using llvm::legacy::PassManager;
  28. using llvm::legacy::FunctionPassManager;
  29. using std::vector;
  30. using std::unordered_set;
  31. using std::unordered_map;
  32. #define DXILVIEWID_DBG 0
  33. #define DEBUG_TYPE "viewid_builder"
  34. namespace {
  35. class DxilViewIdStateBuilder {
  36. static const unsigned kNumComps = 4;
  37. static const unsigned kMaxSigScalars = 32 * 4;
  38. public:
  39. using OutputsDependentOnViewIdType = DxilViewIdStateData::OutputsDependentOnViewIdType;
  40. using InputsContributingToOutputType = DxilViewIdStateData::InputsContributingToOutputType;
  41. DxilViewIdStateBuilder(DxilViewIdStateData &state, DxilModule *pDxilModule)
  42. : m_pModule(pDxilModule),
  43. m_NumInputSigScalars(state.m_NumInputSigScalars),
  44. m_NumOutputSigScalars(state.m_NumOutputSigScalars,
  45. DxilViewIdStateData::kNumStreams),
  46. m_NumPCSigScalars(state.m_NumPCSigScalars),
  47. m_OutputsDependentOnViewId(state.m_OutputsDependentOnViewId,
  48. DxilViewIdStateData::kNumStreams),
  49. m_PCOutputsDependentOnViewId(state.m_PCOutputsDependentOnViewId),
  50. m_InputsContributingToOutputs(state.m_InputsContributingToOutputs,
  51. DxilViewIdStateData::kNumStreams),
  52. m_InputsContributingToPCOutputs(state.m_InputsContributingToPCOutputs),
  53. m_PCInputsContributingToOutputs(state.m_PCInputsContributingToOutputs),
  54. m_SerializedState(state.m_SerializedState),
  55. m_bUsesViewId(state.m_bUsesViewId) {}
  56. void Compute();
  57. private:
  58. static const unsigned kNumStreams = 4;
  59. DxilModule *m_pModule;
  60. unsigned &m_NumInputSigScalars;
  61. MutableArrayRef<unsigned> m_NumOutputSigScalars;
  62. unsigned &m_NumPCSigScalars;
  63. // Set of scalar outputs dependent on ViewID.
  64. MutableArrayRef<OutputsDependentOnViewIdType> m_OutputsDependentOnViewId;
  65. OutputsDependentOnViewIdType &m_PCOutputsDependentOnViewId;
  66. // Set of scalar inputs contributing to computation of scalar outputs.
  67. MutableArrayRef<InputsContributingToOutputType> m_InputsContributingToOutputs;
  68. InputsContributingToOutputType &m_InputsContributingToPCOutputs; // HS PC only.
  69. InputsContributingToOutputType &m_PCInputsContributingToOutputs; // DS only.
  70. // Serialized form.
  71. std::vector<unsigned> &m_SerializedState;
  72. bool &m_bUsesViewId;
  73. // Members for build ViewIdState.
  74. // Dynamically indexed components of signature elements.
  75. using DynamicallyIndexedElemsType = std::unordered_map<unsigned, unsigned>;
  76. DynamicallyIndexedElemsType m_InpSigDynIdxElems;
  77. DynamicallyIndexedElemsType m_OutSigDynIdxElems;
  78. DynamicallyIndexedElemsType m_PCSigDynIdxElems;
  79. // Information per entry point.
  80. using FunctionSetType = std::unordered_set<llvm::Function *>;
  81. using InstructionSetType = std::unordered_set<llvm::Instruction *>;
  82. struct EntryInfo {
  83. llvm::Function *pEntryFunc = nullptr;
  84. // Sets of functions that may be reachable from an entry.
  85. FunctionSetType Functions;
  86. // Outputs to analyze.
  87. InstructionSetType Outputs;
  88. // Contributing instructions per output.
  89. std::unordered_map<unsigned, InstructionSetType>
  90. ContributingInstructions[kNumStreams];
  91. void Clear();
  92. };
  93. EntryInfo m_Entry;
  94. EntryInfo m_PCEntry;
  95. // Information per function.
  96. using FunctionReturnSet = std::unordered_set<llvm::ReturnInst *>;
  97. struct FuncInfo {
  98. FunctionReturnSet Returns;
  99. ControlDependence CtrlDep;
  100. std::unique_ptr<llvm::DominatorTreeBase<llvm::BasicBlock>> pDomTree;
  101. void Clear();
  102. };
  103. std::unordered_map<llvm::Function *, std::unique_ptr<FuncInfo>> m_FuncInfo;
  104. // Cache of decls (global/alloca) reaching a pointer value.
  105. using ValueSetType = std::unordered_set<llvm::Value *>;
  106. std::unordered_map<llvm::Value *, ValueSetType> m_ReachingDeclsCache;
  107. // Cache of stores for each decl.
  108. std::unordered_map<llvm::Value *, ValueSetType> m_StoresPerDeclCache;
  109. void Clear();
  110. void DetermineMaxPackedLocation(DxilSignature &DxilSig, unsigned *pMaxSigLoc,
  111. unsigned NumStreams);
  112. void ComputeReachableFunctionsRec(llvm::CallGraph &CG,
  113. llvm::CallGraphNode *pNode,
  114. FunctionSetType &FuncSet);
  115. void AnalyzeFunctions(EntryInfo &Entry);
  116. void CollectValuesContributingToOutputs(EntryInfo &Entry);
  117. void CollectValuesContributingToOutputRec(
  118. EntryInfo &Entry, llvm::Value *pContributingValue,
  119. InstructionSetType &ContributingInstructions);
  120. void CollectPhiCFValuesContributingToOutputRec(
  121. llvm::PHINode *pPhi, EntryInfo &Entry,
  122. InstructionSetType &ContributingInstructions);
  123. const ValueSetType &CollectReachingDecls(llvm::Value *pValue);
  124. void CollectReachingDeclsRec(llvm::Value *pValue, ValueSetType &ReachingDecls,
  125. ValueSetType &Visited);
  126. const ValueSetType &CollectStores(llvm::Value *pValue);
  127. void CollectStoresRec(llvm::Value *pValue, ValueSetType &Stores,
  128. ValueSetType &Visited);
  129. void UpdateDynamicIndexUsageState() const;
  130. void
  131. CreateViewIdSets(const std::unordered_map<unsigned, InstructionSetType>
  132. &ContributingInstructions,
  133. OutputsDependentOnViewIdType &OutputsDependentOnViewId,
  134. InputsContributingToOutputType &InputsContributingToOutputs,
  135. bool bPC);
  136. void UpdateDynamicIndexUsageStateForSig(
  137. DxilSignature &Sig, const DynamicallyIndexedElemsType &DynIdxElems) const;
  138. unsigned GetLinearIndex(DxilSignatureElement &SigElem, int row,
  139. unsigned col) const;
  140. };
  141. } // namespace
  142. void DxilViewIdStateBuilder::Compute() {
  143. Clear();
  144. const ShaderModel *pSM = m_pModule->GetShaderModel();
  145. m_bUsesViewId = m_pModule->m_ShaderFlags.GetViewID();
  146. // 1. Traverse signature MD to determine max packed location.
  147. DetermineMaxPackedLocation(m_pModule->GetInputSignature(), &m_NumInputSigScalars, 1);
  148. DetermineMaxPackedLocation(m_pModule->GetOutputSignature(), &m_NumOutputSigScalars[0], pSM->IsGS() ? kNumStreams : 1);
  149. DetermineMaxPackedLocation(m_pModule->GetPatchConstantSignature(), &m_NumPCSigScalars, 1);
  150. // 2. Collect sets of functions reachable from main and pc entries.
  151. CallGraphAnalysis CGA;
  152. CallGraph CG = CGA.run(m_pModule->GetModule());
  153. m_Entry.pEntryFunc = m_pModule->GetEntryFunction();
  154. m_PCEntry.pEntryFunc = m_pModule->GetPatchConstantFunction();
  155. ComputeReachableFunctionsRec(CG, CG[m_Entry.pEntryFunc], m_Entry.Functions);
  156. if (m_PCEntry.pEntryFunc) {
  157. DXASSERT_NOMSG(pSM->IsHS());
  158. ComputeReachableFunctionsRec(CG, CG[m_PCEntry.pEntryFunc], m_PCEntry.Functions);
  159. }
  160. // 3. Determine shape components that are dynamically accesses and collect all sig outputs.
  161. AnalyzeFunctions(m_Entry);
  162. if (m_PCEntry.pEntryFunc) {
  163. AnalyzeFunctions(m_PCEntry);
  164. }
  165. // 4. Collect sets of values contributing to outputs.
  166. CollectValuesContributingToOutputs(m_Entry);
  167. if (m_PCEntry.pEntryFunc) {
  168. CollectValuesContributingToOutputs(m_PCEntry);
  169. }
  170. // 5. Construct dependency sets.
  171. for (unsigned StreamId = 0; StreamId < (pSM->IsGS() ? kNumStreams : 1u); StreamId++) {
  172. CreateViewIdSets(m_Entry.ContributingInstructions[StreamId],
  173. m_OutputsDependentOnViewId[StreamId],
  174. m_InputsContributingToOutputs[StreamId], false);
  175. }
  176. if (pSM->IsHS()) {
  177. CreateViewIdSets(m_PCEntry.ContributingInstructions[0],
  178. m_PCOutputsDependentOnViewId,
  179. m_InputsContributingToPCOutputs, true);
  180. } else if (pSM->IsDS()) {
  181. OutputsDependentOnViewIdType OutputsDependentOnViewId;
  182. CreateViewIdSets(m_Entry.ContributingInstructions[0],
  183. OutputsDependentOnViewId,
  184. m_PCInputsContributingToOutputs, true);
  185. DXASSERT_NOMSG(OutputsDependentOnViewId == m_OutputsDependentOnViewId[0]);
  186. }
  187. // 6. Update dynamically indexed input/output component masks.
  188. UpdateDynamicIndexUsageState();
  189. #if DXILVIEWID_DBG
  190. PrintSets(dbgs());
  191. #endif
  192. }
  193. void DxilViewIdStateBuilder::Clear() {
  194. m_bUsesViewId = false;
  195. m_NumInputSigScalars = 0;
  196. for (unsigned i = 0; i < kNumStreams; i++) {
  197. m_NumOutputSigScalars[i] = 0;
  198. m_OutputsDependentOnViewId[i].reset();
  199. m_InputsContributingToOutputs[i].clear();
  200. }
  201. m_NumPCSigScalars = 0;
  202. m_InpSigDynIdxElems.clear();
  203. m_OutSigDynIdxElems.clear();
  204. m_PCSigDynIdxElems.clear();
  205. m_PCOutputsDependentOnViewId.reset();
  206. m_InputsContributingToPCOutputs.clear();
  207. m_PCInputsContributingToOutputs.clear();
  208. m_Entry.Clear();
  209. m_PCEntry.Clear();
  210. m_FuncInfo.clear();
  211. m_ReachingDeclsCache.clear();
  212. m_SerializedState.clear();
  213. }
  214. void DxilViewIdStateBuilder::EntryInfo::Clear() {
  215. pEntryFunc = nullptr;
  216. Functions.clear();
  217. Outputs.clear();
  218. for (unsigned i = 0; i < kNumStreams; i++)
  219. ContributingInstructions[i].clear();
  220. }
  221. void DxilViewIdStateBuilder::FuncInfo::Clear() {
  222. Returns.clear();
  223. CtrlDep.Clear();
  224. pDomTree.reset();
  225. }
  226. void DxilViewIdStateBuilder::DetermineMaxPackedLocation(DxilSignature &DxilSig,
  227. unsigned *pMaxSigLoc,
  228. unsigned NumStreams) {
  229. DXASSERT_NOMSG(NumStreams == 1 || NumStreams == kNumStreams);
  230. for (unsigned i = 0; i < NumStreams; i++) {
  231. pMaxSigLoc[i] = 0;
  232. }
  233. for (auto &E : DxilSig.GetElements()) {
  234. if (E->GetStartRow() == Semantic::kUndefinedRow) continue;
  235. unsigned StreamId = E->GetOutputStream();
  236. unsigned endLoc = GetLinearIndex(*E, E->GetRows() - 1, E->GetCols() - 1);
  237. pMaxSigLoc[StreamId] = std::max(pMaxSigLoc[StreamId], endLoc + 1);
  238. E->GetCols();
  239. }
  240. }
  241. void DxilViewIdStateBuilder::ComputeReachableFunctionsRec(CallGraph &CG, CallGraphNode *pNode, FunctionSetType &FuncSet) {
  242. Function *F = pNode->getFunction();
  243. // Accumulate only functions with bodies.
  244. if (F->empty()) return;
  245. auto itIns = FuncSet.emplace(F);
  246. DXASSERT_NOMSG(itIns.second);
  247. (void)itIns;
  248. for (auto it = pNode->begin(), itEnd = pNode->end(); it != itEnd; ++it) {
  249. CallGraphNode *pSuccNode = it->second;
  250. ComputeReachableFunctionsRec(CG, pSuccNode, FuncSet);
  251. }
  252. }
  253. static bool GetUnsignedVal(Value *V, uint32_t *pValue) {
  254. ConstantInt *CI = dyn_cast<ConstantInt>(V);
  255. if (!CI) return false;
  256. uint64_t u = CI->getZExtValue();
  257. if (u > UINT32_MAX) return false;
  258. *pValue = (uint32_t)u;
  259. return true;
  260. }
  261. void DxilViewIdStateBuilder::AnalyzeFunctions(EntryInfo &Entry) {
  262. for (auto *F : Entry.Functions) {
  263. DXASSERT_NOMSG(!F->empty());
  264. auto itFI = m_FuncInfo.find(F);
  265. FuncInfo *pFuncInfo = nullptr;
  266. if (itFI != m_FuncInfo.end()) {
  267. pFuncInfo = itFI->second.get();
  268. } else {
  269. m_FuncInfo[F] = make_unique<FuncInfo>();
  270. pFuncInfo = m_FuncInfo[F].get();
  271. }
  272. for (auto itBB = F->begin(), endBB = F->end(); itBB != endBB; ++itBB) {
  273. BasicBlock *BB = itBB;
  274. for (auto itInst = BB->begin(), endInst = BB->end(); itInst != endInst; ++itInst) {
  275. if (ReturnInst *RI = dyn_cast<ReturnInst>(itInst)) {
  276. pFuncInfo->Returns.emplace(RI);
  277. continue;
  278. }
  279. CallInst *CI = dyn_cast<CallInst>(itInst);
  280. if (!CI) continue;
  281. DynamicallyIndexedElemsType *pDynIdxElems = nullptr;
  282. int row = Semantic::kUndefinedRow;
  283. unsigned id, col;
  284. if (DxilInst_LoadInput LI = DxilInst_LoadInput(CI)) {
  285. pDynIdxElems = &m_InpSigDynIdxElems;
  286. IFTBOOL(GetUnsignedVal(LI.get_inputSigId(), &id), DXC_E_GENERAL_INTERNAL_ERROR);
  287. GetUnsignedVal(LI.get_rowIndex(), (uint32_t*)&row);
  288. IFTBOOL(GetUnsignedVal(LI.get_colIndex(), &col), DXC_E_GENERAL_INTERNAL_ERROR);
  289. } else if (DxilInst_StoreOutput SO = DxilInst_StoreOutput(CI)) {
  290. pDynIdxElems = &m_OutSigDynIdxElems;
  291. IFTBOOL(GetUnsignedVal(SO.get_outputSigId(), &id), DXC_E_GENERAL_INTERNAL_ERROR);
  292. GetUnsignedVal(SO.get_rowIndex(), (uint32_t*)&row);
  293. IFTBOOL(GetUnsignedVal(SO.get_colIndex(), &col), DXC_E_GENERAL_INTERNAL_ERROR);
  294. Entry.Outputs.emplace(CI);
  295. } else if (DxilInst_LoadPatchConstant LPC = DxilInst_LoadPatchConstant(CI)) {
  296. if (m_pModule->GetShaderModel()->IsDS()) {
  297. pDynIdxElems = &m_PCSigDynIdxElems;
  298. IFTBOOL(GetUnsignedVal(LPC.get_inputSigId(), &id), DXC_E_GENERAL_INTERNAL_ERROR);
  299. GetUnsignedVal(LPC.get_row(), (uint32_t*)&row);
  300. IFTBOOL(GetUnsignedVal(LPC.get_col(), &col), DXC_E_GENERAL_INTERNAL_ERROR);
  301. } else {
  302. // Do nothing. This is an internal helper function for DXBC-2-DXIL converter.
  303. DXASSERT_NOMSG(m_pModule->GetShaderModel()->IsHS());
  304. }
  305. } else if (DxilInst_StorePatchConstant SPC = DxilInst_StorePatchConstant(CI)) {
  306. pDynIdxElems = &m_PCSigDynIdxElems;
  307. IFTBOOL(GetUnsignedVal(SPC.get_outputSigID(), &id), DXC_E_GENERAL_INTERNAL_ERROR);
  308. GetUnsignedVal(SPC.get_row(), (uint32_t*)&row);
  309. IFTBOOL(GetUnsignedVal(SPC.get_col(), &col), DXC_E_GENERAL_INTERNAL_ERROR);
  310. Entry.Outputs.emplace(CI);
  311. } else if (DxilInst_LoadOutputControlPoint LOCP = DxilInst_LoadOutputControlPoint(CI)) {
  312. if (m_pModule->GetShaderModel()->IsDS()) {
  313. pDynIdxElems = &m_InpSigDynIdxElems;
  314. IFTBOOL(GetUnsignedVal(LOCP.get_inputSigId(), &id), DXC_E_GENERAL_INTERNAL_ERROR);
  315. GetUnsignedVal(LOCP.get_row(), (uint32_t*)&row);
  316. IFTBOOL(GetUnsignedVal(LOCP.get_col(), &col), DXC_E_GENERAL_INTERNAL_ERROR);
  317. } else if (m_pModule->GetShaderModel()->IsHS()) {
  318. // Do nothings, as the information has been captured by the output signature of CP entry.
  319. } else {
  320. DXASSERT_NOMSG(false);
  321. }
  322. } else {
  323. continue;
  324. }
  325. // Record dynamic index usage.
  326. if (pDynIdxElems && row == Semantic::kUndefinedRow) {
  327. (*pDynIdxElems)[id] |= (1 << col);
  328. }
  329. }
  330. }
  331. // Compute dominator relation.
  332. pFuncInfo->pDomTree = make_unique<DominatorTreeBase<BasicBlock> >(false);
  333. pFuncInfo->pDomTree->recalculate(*F);
  334. #if DXILVIEWID_DBG
  335. pFuncInfo->pDomTree->print(dbgs());
  336. #endif
  337. // Compute postdominator relation.
  338. DominatorTreeBase<BasicBlock> PDR(true);
  339. PDR.recalculate(*F);
  340. #if DXILVIEWID_DBG
  341. PDR.print(dbgs());
  342. #endif
  343. // Compute control dependence.
  344. pFuncInfo->CtrlDep.Compute(F, PDR);
  345. #if DXILVIEWID_DBG
  346. pFuncInfo->CtrlDep.print(dbgs());
  347. #endif
  348. }
  349. }
  350. void DxilViewIdStateBuilder::CollectValuesContributingToOutputs(EntryInfo &Entry) {
  351. for (auto *CI : Entry.Outputs) { // CI = call instruction
  352. DxilSignature *pDxilSig = nullptr;
  353. Value *pContributingValue = nullptr;
  354. unsigned id = (unsigned)-1;
  355. int startRow = Semantic::kUndefinedRow, endRow = Semantic::kUndefinedRow;
  356. unsigned col = (unsigned)-1;
  357. if (DxilInst_StoreOutput SO = DxilInst_StoreOutput(CI)) {
  358. pDxilSig = &m_pModule->GetOutputSignature();
  359. pContributingValue = SO.get_value();
  360. GetUnsignedVal(SO.get_outputSigId(), &id);
  361. GetUnsignedVal(SO.get_colIndex(), &col);
  362. GetUnsignedVal(SO.get_rowIndex(), (uint32_t*)&startRow);
  363. } else if (DxilInst_StorePatchConstant SPC = DxilInst_StorePatchConstant(CI)) {
  364. pDxilSig = &m_pModule->GetPatchConstantSignature();
  365. pContributingValue = SPC.get_value();
  366. GetUnsignedVal(SPC.get_outputSigID(), &id);
  367. GetUnsignedVal(SPC.get_row(), (uint32_t*)&startRow);
  368. GetUnsignedVal(SPC.get_col(), &col);
  369. } else {
  370. IFT(DXC_E_GENERAL_INTERNAL_ERROR);
  371. }
  372. DxilSignatureElement &SigElem = pDxilSig->GetElement(id);
  373. if (!SigElem.IsAllocated())
  374. continue;
  375. unsigned StreamId = SigElem.GetOutputStream();
  376. if (startRow != Semantic::kUndefinedRow) {
  377. endRow = startRow;
  378. } else {
  379. // The entire column is affected by value.
  380. DXASSERT_NOMSG(SigElem.GetID() == id && SigElem.GetStartRow() != Semantic::kUndefinedRow);
  381. startRow = 0;
  382. endRow = SigElem.GetRows() - 1;
  383. }
  384. InstructionSetType ContributingInstructionsAllRows;
  385. InstructionSetType *pContributingInstructions = &ContributingInstructionsAllRows;
  386. if (startRow == endRow) {
  387. // Scalar or indexable with known index.
  388. unsigned index = GetLinearIndex(SigElem, startRow, col);
  389. pContributingInstructions = &Entry.ContributingInstructions[StreamId][index];
  390. }
  391. CollectValuesContributingToOutputRec(Entry, pContributingValue, *pContributingInstructions);
  392. // Handle control dependence of this instruction BB.
  393. BasicBlock *pBB = CI->getParent();
  394. Function *F = pBB->getParent();
  395. FuncInfo *pFuncInfo = m_FuncInfo[F].get();
  396. const BasicBlockSet &CtrlDepSet = pFuncInfo->CtrlDep.GetCDBlocks(pBB);
  397. for (BasicBlock *B : CtrlDepSet) {
  398. CollectValuesContributingToOutputRec(Entry, B->getTerminator(), *pContributingInstructions);
  399. }
  400. if (pContributingInstructions == &ContributingInstructionsAllRows) {
  401. // Write dynamically indexed output contributions to all rows.
  402. for (int row = startRow; row <= endRow; row++) {
  403. unsigned index = GetLinearIndex(SigElem, row, col);
  404. Entry.ContributingInstructions[StreamId][index].insert(ContributingInstructionsAllRows.begin(), ContributingInstructionsAllRows.end());
  405. }
  406. }
  407. }
  408. }
  409. void DxilViewIdStateBuilder::CollectValuesContributingToOutputRec(EntryInfo &Entry,
  410. Value *pContributingValue,
  411. InstructionSetType &ContributingInstructions) {
  412. if (dyn_cast<Argument>(pContributingValue)) {
  413. // This must be a leftover signature argument of an entry function.
  414. DXASSERT_NOMSG(Entry.pEntryFunc == m_pModule->GetEntryFunction() ||
  415. Entry.pEntryFunc == m_pModule->GetPatchConstantFunction());
  416. return;
  417. }
  418. Instruction *pContributingInst = dyn_cast<Instruction>(pContributingValue);
  419. if (pContributingInst == nullptr) {
  420. // Can be literal constant, global decl, branch target.
  421. DXASSERT_NOMSG(isa<Constant>(pContributingValue) || isa<BasicBlock>(pContributingValue));
  422. return;
  423. }
  424. auto itInst = ContributingInstructions.emplace(pContributingInst);
  425. // Already visited instruction.
  426. if (!itInst.second) return;
  427. // Handle special cases.
  428. if (PHINode *phi = dyn_cast<PHINode>(pContributingInst)) {
  429. CollectPhiCFValuesContributingToOutputRec(phi, Entry, ContributingInstructions);
  430. } else if (isa<LoadInst>(pContributingInst) ||
  431. isa<AtomicCmpXchgInst>(pContributingInst) ||
  432. isa<AtomicRMWInst>(pContributingInst)) {
  433. Value *pPtrValue = pContributingInst->getOperand(0);
  434. DXASSERT_NOMSG(pPtrValue->getType()->isPointerTy());
  435. const ValueSetType &ReachingDecls = CollectReachingDecls(pPtrValue);
  436. DXASSERT_NOMSG(ReachingDecls.size() > 0);
  437. for (Value *pDeclValue : ReachingDecls) {
  438. const ValueSetType &Stores = CollectStores(pDeclValue);
  439. for (Value *V : Stores) {
  440. CollectValuesContributingToOutputRec(Entry, V, ContributingInstructions);
  441. }
  442. }
  443. } else if (CallInst *CI = dyn_cast<CallInst>(pContributingInst)) {
  444. if (!hlsl::OP::IsDxilOpFuncCallInst(CI)) {
  445. Function *F = CI->getCalledFunction();
  446. if (!F->empty()) {
  447. // Return value of a user function.
  448. if (Entry.Functions.find(F) != Entry.Functions.end()) {
  449. const FuncInfo &FI = *m_FuncInfo[F];
  450. for (ReturnInst *pRetInst : FI.Returns) {
  451. CollectValuesContributingToOutputRec(Entry, pRetInst, ContributingInstructions);
  452. }
  453. }
  454. }
  455. }
  456. }
  457. // Handle instruction inputs.
  458. unsigned NumOps = pContributingInst->getNumOperands();
  459. for (unsigned i = 0; i < NumOps; i++) {
  460. Value *O = pContributingInst->getOperand(i);
  461. CollectValuesContributingToOutputRec(Entry, O, ContributingInstructions);
  462. }
  463. // Handle control dependence of this instruction BB.
  464. BasicBlock *pBB = pContributingInst->getParent();
  465. Function *F = pBB->getParent();
  466. FuncInfo *pFuncInfo = m_FuncInfo[F].get();
  467. const BasicBlockSet &CtrlDepSet = pFuncInfo->CtrlDep.GetCDBlocks(pBB);
  468. for (BasicBlock *B : CtrlDepSet) {
  469. CollectValuesContributingToOutputRec(Entry, B->getTerminator(), ContributingInstructions);
  470. }
  471. }
  472. // Only process control-dependent basic blocks for constant operands of the phi-function.
  473. // An obvious "definition" point for a constant operand is the predecessor along corresponding edge.
  474. // However, this may be too conservative and, as such, pick up extra control dependent BBs.
  475. // A better "definition" point is the highest dominator where it is still legal to "insert" constant assignment.
  476. // In this context, "legal" means that only one value "leaves" the dominator and reaches Phi.
  477. void DxilViewIdStateBuilder::CollectPhiCFValuesContributingToOutputRec(PHINode *pPhi,
  478. EntryInfo &Entry,
  479. InstructionSetType &ContributingInstructions) {
  480. Function *F = pPhi->getParent()->getParent();
  481. FuncInfo *pFuncInfo = m_FuncInfo[F].get();
  482. unordered_map<DomTreeNodeBase<BasicBlock> *, Value *> DomTreeMarkers;
  483. // Mark predecessors of each value, so that there is a legal "definition" point.
  484. for (unsigned i = 0; i < pPhi->getNumOperands(); i++) {
  485. Value *pValue = pPhi->getIncomingValue(i);
  486. BasicBlock *pBB = pPhi->getIncomingBlock(i);
  487. DomTreeNodeBase<BasicBlock> *pDomNode = pFuncInfo->pDomTree->getNode(pBB);
  488. auto it = DomTreeMarkers.emplace(pDomNode, pValue);
  489. DXASSERT_NOMSG(it.second || it.first->second == pValue); (void)it;
  490. }
  491. // Mark the dominator tree with "definition" values, walking up to the parent.
  492. for (unsigned i = 0; i < pPhi->getNumOperands(); i++) {
  493. Value *pValue = pPhi->getIncomingValue(i);
  494. BasicBlock *pDefBB = &F->getEntryBlock();
  495. if (Instruction *pDefInst = dyn_cast<Instruction>(pValue)) {
  496. pDefBB = pDefInst->getParent();
  497. }
  498. BasicBlock *pBB = pPhi->getIncomingBlock(i);
  499. if (pBB == pDefBB) {
  500. continue; // we already handled the predecessor.
  501. }
  502. DomTreeNodeBase<BasicBlock> *pDomNode = pFuncInfo->pDomTree->getNode(pBB);
  503. pDomNode = pDomNode->getIDom();
  504. while (pDomNode) {
  505. auto it = DomTreeMarkers.emplace(pDomNode, pValue);
  506. if (!it.second) {
  507. if (it.first->second != pValue && it.first->second != nullptr) {
  508. if (!isa<Constant>(it.first->second) || !isa<Constant>(pValue)) {
  509. // Unless both are different constants, mark the "definition" point as illegal.
  510. it.first->second = nullptr;
  511. // If both are constants, leave the marker of the first one.
  512. }
  513. }
  514. break;
  515. }
  516. // Do not go higher than a legal definition point.
  517. pBB = pDomNode->getBlock();
  518. if (pBB == pDefBB)
  519. break;
  520. pDomNode = pDomNode->getIDom();
  521. }
  522. }
  523. // Handle control dependence for Constant arguments of Phi.
  524. for (unsigned i = 0; i < pPhi->getNumOperands(); i++) {
  525. Value *pValue = pPhi->getIncomingValue(i);
  526. if (!isa<Constant>(pValue))
  527. continue;
  528. // Determine the higher legal "definition" point.
  529. BasicBlock *pBB = pPhi->getIncomingBlock(i);
  530. DomTreeNodeBase<BasicBlock> *pDomNode = pFuncInfo->pDomTree->getNode(pBB);
  531. DomTreeNodeBase<BasicBlock> *pDefDomNode = pDomNode;
  532. while (pDomNode) {
  533. auto it = DomTreeMarkers.find(pDomNode);
  534. DXASSERT_NOMSG(it != DomTreeMarkers.end());
  535. if (it->second != pValue) {
  536. DXASSERT_NOMSG(it->second == nullptr || isa<Constant>(it->second));
  537. break;
  538. }
  539. pDefDomNode = pDomNode;
  540. pDomNode = pDomNode->getIDom();
  541. }
  542. // Handle control dependence of this constant argument highest legal "definition" point.
  543. pBB = pDefDomNode->getBlock();
  544. const BasicBlockSet &CtrlDepSet = pFuncInfo->CtrlDep.GetCDBlocks(pBB);
  545. for (BasicBlock *B : CtrlDepSet) {
  546. CollectValuesContributingToOutputRec(Entry, B->getTerminator(), ContributingInstructions);
  547. }
  548. }
  549. }
  550. const DxilViewIdStateBuilder::ValueSetType &DxilViewIdStateBuilder::CollectReachingDecls(Value *pValue) {
  551. auto it = m_ReachingDeclsCache.emplace(pValue, ValueSetType());
  552. if (it.second) {
  553. // We have not seen this value before.
  554. ValueSetType Visited;
  555. CollectReachingDeclsRec(pValue, it.first->second, Visited);
  556. }
  557. return it.first->second;
  558. }
  559. void DxilViewIdStateBuilder::CollectReachingDeclsRec(Value *pValue, ValueSetType &ReachingDecls, ValueSetType &Visited) {
  560. if (Visited.find(pValue) != Visited.end())
  561. return;
  562. bool bInitialValue = Visited.size() == 0;
  563. Visited.emplace(pValue);
  564. if (!bInitialValue) {
  565. auto it = m_ReachingDeclsCache.find(pValue);
  566. if (it != m_ReachingDeclsCache.end()) {
  567. ReachingDecls.insert(it->second.begin(), it->second.end());
  568. return;
  569. }
  570. }
  571. if (dyn_cast<GlobalVariable>(pValue)) {
  572. ReachingDecls.emplace(pValue);
  573. return;
  574. }
  575. if (GetElementPtrInst *pGepInst = dyn_cast<GetElementPtrInst>(pValue)) {
  576. Value *pPtrValue = pGepInst->getPointerOperand();
  577. CollectReachingDeclsRec(pPtrValue, ReachingDecls, Visited);
  578. } else if (GEPOperator *pGepOp = dyn_cast<GEPOperator>(pValue)) {
  579. Value *pPtrValue = pGepOp->getPointerOperand();
  580. CollectReachingDeclsRec(pPtrValue, ReachingDecls, Visited);
  581. } else if (dyn_cast<AllocaInst>(pValue)) {
  582. ReachingDecls.emplace(pValue);
  583. } else if (PHINode *phi = dyn_cast<PHINode>(pValue)) {
  584. for (Value *pPtrValue : phi->operands()) {
  585. CollectReachingDeclsRec(pPtrValue, ReachingDecls, Visited);
  586. }
  587. } else if (SelectInst *SelI = dyn_cast<SelectInst>(pValue)) {
  588. CollectReachingDeclsRec(SelI->getTrueValue(), ReachingDecls, Visited);
  589. CollectReachingDeclsRec(SelI->getFalseValue(), ReachingDecls, Visited);
  590. } else if (dyn_cast<Argument>(pValue)) {
  591. ReachingDecls.emplace(pValue);
  592. } else {
  593. IFT(DXC_E_GENERAL_INTERNAL_ERROR);
  594. }
  595. }
  596. const DxilViewIdStateBuilder::ValueSetType &DxilViewIdStateBuilder::CollectStores(llvm::Value *pValue) {
  597. auto it = m_StoresPerDeclCache.emplace(pValue, ValueSetType());
  598. if (it.second) {
  599. // We have not seen this value before.
  600. ValueSetType Visited;
  601. CollectStoresRec(pValue, it.first->second, Visited);
  602. }
  603. return it.first->second;
  604. }
  605. void DxilViewIdStateBuilder::CollectStoresRec(llvm::Value *pValue, ValueSetType &Stores, ValueSetType &Visited) {
  606. if (Visited.find(pValue) != Visited.end())
  607. return;
  608. bool bInitialValue = Visited.size() == 0;
  609. Visited.emplace(pValue);
  610. if (!bInitialValue) {
  611. auto it = m_StoresPerDeclCache.find(pValue);
  612. if (it != m_StoresPerDeclCache.end()) {
  613. Stores.insert(it->second.begin(), it->second.end());
  614. return;
  615. }
  616. }
  617. if (isa<LoadInst>(pValue)) {
  618. return;
  619. } else if (isa<StoreInst>(pValue) ||
  620. isa<AtomicCmpXchgInst>(pValue) ||
  621. isa<AtomicRMWInst>(pValue)) {
  622. Stores.emplace(pValue);
  623. return;
  624. }
  625. for (auto *U : pValue->users()) {
  626. CollectStoresRec(U, Stores, Visited);
  627. }
  628. }
  629. void DxilViewIdStateBuilder::CreateViewIdSets(const std::unordered_map<unsigned, InstructionSetType> &ContributingInstructions,
  630. OutputsDependentOnViewIdType &OutputsDependentOnViewId,
  631. InputsContributingToOutputType &InputsContributingToOutputs,
  632. bool bPC) {
  633. const ShaderModel *pSM = m_pModule->GetShaderModel();
  634. for (auto &itOut : ContributingInstructions) {
  635. unsigned outIdx = itOut.first;
  636. for (Instruction *pInst : itOut.second) {
  637. // Set output dependence on ViewId.
  638. if (DxilInst_ViewID VID = DxilInst_ViewID(pInst)) {
  639. DXASSERT(m_bUsesViewId, "otherwise, DxilModule flag not set properly");
  640. OutputsDependentOnViewId[outIdx] = true;
  641. continue;
  642. }
  643. // Start setting output dependence on inputs.
  644. DxilSignatureElement *pSigElem = nullptr;
  645. bool bLoadOutputCPInHS = false;
  646. unsigned inpId = (unsigned)-1;
  647. int startRow = Semantic::kUndefinedRow, endRow = Semantic::kUndefinedRow;
  648. unsigned col = (unsigned)-1;
  649. if (DxilInst_LoadInput LI = DxilInst_LoadInput(pInst)) {
  650. GetUnsignedVal(LI.get_inputSigId(), &inpId);
  651. GetUnsignedVal(LI.get_colIndex(), &col);
  652. GetUnsignedVal(LI.get_rowIndex(), (uint32_t*)&startRow);
  653. pSigElem = &m_pModule->GetInputSignature().GetElement(inpId);
  654. if (pSM->IsDS() && bPC) {
  655. pSigElem = nullptr;
  656. }
  657. } else if (DxilInst_LoadOutputControlPoint LOCP = DxilInst_LoadOutputControlPoint(pInst)) {
  658. GetUnsignedVal(LOCP.get_inputSigId(), &inpId);
  659. GetUnsignedVal(LOCP.get_col(), &col);
  660. GetUnsignedVal(LOCP.get_row(), (uint32_t*)&startRow);
  661. if (pSM->IsHS()) {
  662. pSigElem = &m_pModule->GetOutputSignature().GetElement(inpId);
  663. bLoadOutputCPInHS = true;
  664. } else if (pSM->IsDS()) {
  665. if (!bPC) {
  666. pSigElem = &m_pModule->GetInputSignature().GetElement(inpId);
  667. }
  668. } else {
  669. DXASSERT_NOMSG(false);
  670. }
  671. } else if (DxilInst_LoadPatchConstant LPC = DxilInst_LoadPatchConstant(pInst)) {
  672. if (pSM->IsDS() && bPC) {
  673. GetUnsignedVal(LPC.get_inputSigId(), &inpId);
  674. GetUnsignedVal(LPC.get_col(), &col);
  675. GetUnsignedVal(LPC.get_row(), (uint32_t*)&startRow);
  676. pSigElem = &m_pModule->GetPatchConstantSignature().GetElement(inpId);
  677. }
  678. } else {
  679. continue;
  680. }
  681. // Finalize setting output dependence on inputs.
  682. if (pSigElem && pSigElem->IsAllocated()) {
  683. if (startRow != Semantic::kUndefinedRow) {
  684. endRow = startRow;
  685. } else {
  686. // The entire column contributes to output.
  687. startRow = 0;
  688. endRow = pSigElem->GetRows() - 1;
  689. }
  690. auto &ContributingInputs = InputsContributingToOutputs[outIdx];
  691. for (int row = startRow; row <= endRow; row++) {
  692. unsigned index = GetLinearIndex(*pSigElem, row, col);
  693. if (!bLoadOutputCPInHS) {
  694. ContributingInputs.emplace(index);
  695. } else {
  696. // This HS patch-constant output depends on an input value of LoadOutputControlPoint
  697. // that is the output value of the HS main (control-point) function.
  698. // Transitively update this (patch-constant) output dependence on main (control-point) output.
  699. DXASSERT_NOMSG(&OutputsDependentOnViewId == &m_PCOutputsDependentOnViewId);
  700. OutputsDependentOnViewId[outIdx] = OutputsDependentOnViewId[outIdx] || m_OutputsDependentOnViewId[0][index];
  701. const auto it = m_InputsContributingToOutputs[0].find(index);
  702. if (it != m_InputsContributingToOutputs[0].end()) {
  703. const std::set<unsigned> &LoadOutputCPInputsContributingToOutputs = it->second;
  704. ContributingInputs.insert(LoadOutputCPInputsContributingToOutputs.begin(),
  705. LoadOutputCPInputsContributingToOutputs.end());
  706. }
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. unsigned DxilViewIdStateBuilder::GetLinearIndex(DxilSignatureElement &SigElem, int row, unsigned col) const {
  714. DXASSERT_NOMSG(row >= 0 && col < kNumComps && SigElem.GetStartRow() != Semantic::kUndefinedRow);
  715. unsigned idx = (((unsigned)row) + SigElem.GetStartRow())*kNumComps + col + SigElem.GetStartCol();
  716. DXASSERT_NOMSG(idx < kMaxSigScalars);
  717. return idx;
  718. }
  719. void DxilViewIdStateBuilder::UpdateDynamicIndexUsageState() const {
  720. UpdateDynamicIndexUsageStateForSig(m_pModule->GetInputSignature(), m_InpSigDynIdxElems);
  721. UpdateDynamicIndexUsageStateForSig(m_pModule->GetOutputSignature(), m_OutSigDynIdxElems);
  722. UpdateDynamicIndexUsageStateForSig(m_pModule->GetPatchConstantSignature(), m_PCSigDynIdxElems);
  723. }
  724. void DxilViewIdStateBuilder::UpdateDynamicIndexUsageStateForSig(DxilSignature &Sig,
  725. const DynamicallyIndexedElemsType &DynIdxElems) const {
  726. for (auto it : DynIdxElems) {
  727. unsigned id = it.first;
  728. unsigned mask = it.second;
  729. DxilSignatureElement &E = Sig.GetElement(id);
  730. E.SetDynIdxCompMask(mask);
  731. }
  732. }
  733. namespace {
  734. class ComputeViewIdState : public ModulePass {
  735. public:
  736. static char ID; // Pass ID, replacement for typeid
  737. ComputeViewIdState();
  738. bool runOnModule(Module &M) override;
  739. void getAnalysisUsage(AnalysisUsage &AU) const override;
  740. };
  741. } // namespace
  742. char ComputeViewIdState::ID = 0;
  743. INITIALIZE_PASS_BEGIN(ComputeViewIdState, "viewid-state",
  744. "Compute information related to ViewID", true, true)
  745. INITIALIZE_PASS_END(ComputeViewIdState, "viewid-state",
  746. "Compute information related to ViewID", true, true)
  747. ComputeViewIdState::ComputeViewIdState() : ModulePass(ID) {
  748. }
  749. bool ComputeViewIdState::runOnModule(Module &M) {
  750. DxilModule &DxilModule = M.GetOrCreateDxilModule();
  751. const ShaderModel *pSM = DxilModule.GetShaderModel();
  752. if (!pSM->IsCS() && !pSM->IsLib()) {
  753. DxilViewIdState &ViewIdState = DxilModule.GetViewIdState();
  754. DxilViewIdStateBuilder Builder(ViewIdState, &DxilModule);
  755. Builder.Compute();
  756. return true;
  757. }
  758. return false;
  759. }
  760. void ComputeViewIdState::getAnalysisUsage(AnalysisUsage &AU) const {
  761. AU.setPreservesAll();
  762. }
  763. namespace llvm {
  764. ModulePass *createComputeViewIdStatePass() {
  765. return new ComputeViewIdState();
  766. }
  767. } // end of namespace llvm