DxcPixLiveVariables.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcPixLiveVariables.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. // Defines the mapping between instructions and the set of live variables //
  9. // for it. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "dxc/Support/WinIncludes.h"
  13. #include "DxcPixDxilDebugInfo.h"
  14. #include "DxcPixLiveVariables.h"
  15. #include "DxcPixLiveVariables_FragmentIterator.h"
  16. #include "DxilDiaSession.h"
  17. #include "dxc/Support/Global.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/IR/DebugInfo.h"
  20. #include "llvm/IR/DebugInfoMetadata.h"
  21. #include "llvm/IR/Dominators.h"
  22. #include "llvm/IR/Instructions.h"
  23. #include "llvm/IR/Intrinsics.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Module.h"
  26. #include <unordered_map>
  27. // ValidateDbgDeclare ensures that all of the bits in
  28. // [FragmentSizeInBits, FragmentOffsetInBits) are currently
  29. // not assigned to a dxil alloca register -- i.e., it
  30. // tries to find overlapping alloca registers -- which should
  31. // never happen -- to report the issue.
  32. void ValidateDbgDeclare(
  33. dxil_debug_info::VariableInfo *VarInfo,
  34. unsigned FragmentSizeInBits,
  35. unsigned FragmentOffsetInBits
  36. )
  37. {
  38. #ifndef NDEBUG
  39. for (unsigned i = 0; i < FragmentSizeInBits; ++i)
  40. {
  41. const unsigned BitNum = FragmentOffsetInBits + i;
  42. VarInfo->m_DbgDeclareValidation.resize(
  43. std::max<unsigned>(VarInfo->m_DbgDeclareValidation.size(),
  44. BitNum + 1));
  45. assert(!VarInfo->m_DbgDeclareValidation[BitNum]);
  46. VarInfo->m_DbgDeclareValidation[BitNum] = true;
  47. }
  48. #endif // !NDEBUG
  49. }
  50. struct dxil_debug_info::LiveVariables::Impl
  51. {
  52. using VariableInfoMap = std::unordered_map<
  53. llvm::DIVariable *,
  54. std::unique_ptr<VariableInfo>>;
  55. using LiveVarsMap =
  56. std::unordered_map<llvm::DIScope*, VariableInfoMap>;
  57. IMalloc *m_pMalloc;
  58. DxcPixDxilDebugInfo *m_pDxilDebugInfo;
  59. llvm::Module *m_pModule;
  60. LiveVarsMap m_LiveVarsDbgDeclare;
  61. void Init(
  62. IMalloc *pMalloc,
  63. DxcPixDxilDebugInfo *pDxilDebugInfo,
  64. llvm::Module *pModule);
  65. void Init_DbgDeclare(llvm::DbgDeclareInst *DbgDeclare);
  66. VariableInfo *AssignValueToOffset(
  67. VariableInfoMap *VarInfoMap,
  68. llvm::DIVariable *Var,
  69. llvm::Value *Address,
  70. unsigned FragmentIndex,
  71. unsigned FragmentOffsetInBits);
  72. };
  73. void dxil_debug_info::LiveVariables::Impl::Init(
  74. IMalloc *pMalloc,
  75. DxcPixDxilDebugInfo *pDxilDebugInfo,
  76. llvm::Module *pModule)
  77. {
  78. m_pMalloc = pMalloc;
  79. m_pDxilDebugInfo = pDxilDebugInfo;
  80. m_pModule = pModule;
  81. llvm::Function* DbgDeclareFn = llvm::Intrinsic::getDeclaration(
  82. m_pModule,
  83. llvm::Intrinsic::dbg_declare);
  84. for (llvm::User* U : DbgDeclareFn->users())
  85. {
  86. if (auto* DbgDeclare = llvm::dyn_cast<llvm::DbgDeclareInst>(U))
  87. {
  88. Init_DbgDeclare(DbgDeclare);
  89. }
  90. }
  91. }
  92. void dxil_debug_info::LiveVariables::Impl::Init_DbgDeclare(
  93. llvm::DbgDeclareInst *DbgDeclare
  94. )
  95. {
  96. llvm::Value *Address = DbgDeclare->getAddress();
  97. auto *Variable = DbgDeclare->getVariable();
  98. auto *Expression = DbgDeclare->getExpression();
  99. if (Address == nullptr || Variable == nullptr || Expression == nullptr)
  100. {
  101. return;
  102. }
  103. auto* AddressAsAlloca = llvm::dyn_cast<llvm::AllocaInst>(Address);
  104. if (AddressAsAlloca == nullptr)
  105. {
  106. return;
  107. }
  108. auto *S = Variable->getScope();
  109. if (S == nullptr)
  110. {
  111. return;
  112. }
  113. auto Iter = CreateMemberIterator(
  114. DbgDeclare,
  115. m_pModule->getDataLayout(),
  116. AddressAsAlloca,
  117. Expression);
  118. if (!Iter)
  119. {
  120. // MemberIterator creation failure, this skip this var.
  121. return;
  122. }
  123. unsigned FragmentIndex;
  124. while (Iter->Next(&FragmentIndex))
  125. {
  126. const unsigned FragmentSizeInBits =
  127. Iter->SizeInBits(FragmentIndex);
  128. const unsigned FragmentOffsetInBits =
  129. Iter->OffsetInBits(FragmentIndex);
  130. VariableInfo* VarInfo = AssignValueToOffset(
  131. &m_LiveVarsDbgDeclare[S],
  132. Variable,
  133. Address,
  134. FragmentIndex,
  135. FragmentOffsetInBits);
  136. // SROA can split structs so that multiple allocas back the same variable.
  137. // In this case the expression will be empty
  138. if (Expression->getNumElements() != 0)
  139. {
  140. ValidateDbgDeclare(
  141. VarInfo,
  142. FragmentSizeInBits,
  143. FragmentOffsetInBits);
  144. }
  145. }
  146. }
  147. dxil_debug_info::VariableInfo* dxil_debug_info::LiveVariables::Impl::AssignValueToOffset(
  148. VariableInfoMap *VarInfoMap,
  149. llvm::DIVariable *Variable,
  150. llvm::Value *Address,
  151. unsigned FragmentIndex,
  152. unsigned FragmentOffsetInBits
  153. )
  154. {
  155. // FragmentIndex is the index within the alloca'd value
  156. // FragmentOffsetInBits is the offset within the HLSL variable
  157. // that maps to Address[FragmentIndex]
  158. auto it = VarInfoMap->find(Variable);
  159. if (it == VarInfoMap->end())
  160. {
  161. auto InsertIt = VarInfoMap->emplace(
  162. Variable,
  163. std::make_unique<VariableInfo>(Variable));
  164. assert(InsertIt.second);
  165. it = InsertIt.first;
  166. }
  167. auto *VarInfo = it->second.get();
  168. auto &FragmentLocation =
  169. VarInfo->m_ValueLocationMap[FragmentOffsetInBits];
  170. FragmentLocation.m_V = Address;
  171. FragmentLocation.m_FragmentIndex = FragmentIndex;
  172. return VarInfo;
  173. }
  174. dxil_debug_info::LiveVariables::LiveVariables() = default;
  175. dxil_debug_info::LiveVariables::~LiveVariables() = default;
  176. HRESULT dxil_debug_info::LiveVariables::Init(DxcPixDxilDebugInfo *pDxilDebugInfo) {
  177. Clear();
  178. m_pImpl->Init(pDxilDebugInfo->GetMallocNoRef(), pDxilDebugInfo, pDxilDebugInfo->GetModuleRef());
  179. return S_OK;
  180. }
  181. void dxil_debug_info::LiveVariables::Clear() {
  182. m_pImpl.reset(new dxil_debug_info::LiveVariables::Impl());
  183. }
  184. HRESULT dxil_debug_info::LiveVariables::GetLiveVariablesAtInstruction(
  185. llvm::Instruction *IP,
  186. IDxcPixDxilLiveVariables **ppResult) const {
  187. DXASSERT(IP != nullptr, "else IP should not be nullptr");
  188. DXASSERT(ppResult != nullptr, "else Result should not be nullptr");
  189. std::vector<const VariableInfo *> LiveVars;
  190. std::set<std::string> LiveVarsName;
  191. const llvm::DebugLoc &DL = IP->getDebugLoc();
  192. if (!DL)
  193. {
  194. return E_FAIL;
  195. }
  196. llvm::DIScope *S = DL->getScope();
  197. if (S == nullptr)
  198. {
  199. return E_FAIL;
  200. }
  201. const llvm::DITypeIdentifierMap EmptyMap;
  202. while (S != nullptr)
  203. {
  204. auto it = m_pImpl->m_LiveVarsDbgDeclare.find(S);
  205. if (it != m_pImpl->m_LiveVarsDbgDeclare.end())
  206. {
  207. for (const auto &VarAndInfo : it->second)
  208. {
  209. auto *Var = VarAndInfo.first;
  210. llvm::StringRef VarName = Var->getName();
  211. if (Var->getLine() > DL.getLine())
  212. {
  213. // Defined later in the HLSL source.
  214. continue;
  215. }
  216. if (VarName.empty())
  217. {
  218. // No name?...
  219. continue;
  220. }
  221. if (!LiveVarsName.insert(VarName).second)
  222. {
  223. // There's a variable with the same name; use the
  224. // previous one instead.
  225. continue;
  226. }
  227. LiveVars.emplace_back(VarAndInfo.second.get());
  228. }
  229. }
  230. S = S->getScope().resolve(EmptyMap);
  231. }
  232. return CreateDxilLiveVariables(
  233. m_pImpl->m_pDxilDebugInfo,
  234. std::move(LiveVars),
  235. ppResult);
  236. }