DxcPixVariables.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcPixVariables.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 DXC's PIX api for exposing llvm::DIVariables. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "dxc/Support/WinIncludes.h"
  12. #include "dxc/dxcpix.h"
  13. #include "dxc/Support/microcom.h"
  14. #include "dxc/Support/Global.h"
  15. #include "llvm/IR/DebugInfo.h"
  16. #include "llvm/IR/DebugInfoMetadata.h"
  17. #include "DxcPixBase.h"
  18. #include "DxcPixDxilDebugInfo.h"
  19. #include "DxcPixDxilStorage.h"
  20. #include "DxcPixLiveVariables.h"
  21. #include "DxcPixTypes.h"
  22. #include "DxilDiaSession.h"
  23. #include <set>
  24. #include <vector>
  25. namespace dxil_debug_info
  26. {
  27. template <typename T>
  28. class DxcPixVariable : public IDxcPixVariable
  29. {
  30. DXC_MICROCOM_TM_REF_FIELDS()
  31. CComPtr<DxcPixDxilDebugInfo> m_pDxilDebugInfo;
  32. T *m_pVariable;
  33. VariableInfo const *m_pVarInfo;
  34. llvm::DIType *m_pType;
  35. DxcPixVariable(
  36. IMalloc *pMalloc,
  37. DxcPixDxilDebugInfo *DxilDebugInfo,
  38. T *pVariable,
  39. VariableInfo const *pVarInfo)
  40. : m_pMalloc(pMalloc)
  41. , m_pDxilDebugInfo(DxilDebugInfo)
  42. , m_pVariable(pVariable)
  43. , m_pVarInfo(pVarInfo)
  44. {
  45. const llvm::DITypeIdentifierMap EmptyMap;
  46. m_pType = m_pVariable->getType().resolve(EmptyMap);
  47. }
  48. public:
  49. DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
  50. DXC_MICROCOM_TM_ALLOC(DxcPixVariable)
  51. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) {
  52. return DoBasicQueryInterface<IDxcPixVariable>(this, iid, ppvObject);
  53. }
  54. public:
  55. STDMETHODIMP GetName(
  56. _Outptr_result_z_ BSTR *Name) override;
  57. STDMETHODIMP GetType(
  58. _Outptr_result_z_ IDxcPixType** ppType) override;
  59. STDMETHODIMP GetStorage(
  60. _COM_Outptr_ IDxcPixDxilStorage** ppStorage) override;
  61. };
  62. } // namespace dxil_debug_info
  63. template <typename T>
  64. STDMETHODIMP dxil_debug_info::DxcPixVariable<T>::GetName(
  65. _Outptr_result_z_ BSTR* Name)
  66. {
  67. *Name = CComBSTR(CA2W(m_pVariable->getName().data())).Detach();
  68. return S_OK;
  69. }
  70. template <typename T>
  71. STDMETHODIMP dxil_debug_info::DxcPixVariable<T>::GetType(
  72. _Outptr_result_z_ IDxcPixType **ppType
  73. )
  74. {
  75. return dxil_debug_info::CreateDxcPixType(
  76. m_pDxilDebugInfo,
  77. m_pType,
  78. ppType);
  79. }
  80. template <typename T>
  81. STDMETHODIMP dxil_debug_info::DxcPixVariable<T>::GetStorage(
  82. _COM_Outptr_ IDxcPixDxilStorage **ppStorage
  83. )
  84. {
  85. const unsigned InitialOffsetInBits = 0;
  86. return CreateDxcPixStorage(
  87. m_pDxilDebugInfo,
  88. m_pType,
  89. m_pVarInfo,
  90. InitialOffsetInBits,
  91. ppStorage);
  92. }
  93. namespace dxil_debug_info
  94. {
  95. class DxcPixDxilLiveVariables : public IDxcPixDxilLiveVariables
  96. {
  97. private:
  98. DXC_MICROCOM_TM_REF_FIELDS();
  99. CComPtr<DxcPixDxilDebugInfo> m_pDxilDebugInfo;
  100. std::vector<const VariableInfo *> m_LiveVars;
  101. DxcPixDxilLiveVariables(
  102. IMalloc *pMalloc,
  103. DxcPixDxilDebugInfo *pDxilDebugInfo,
  104. std::vector<const VariableInfo *> LiveVars
  105. ) : m_pMalloc(pMalloc)
  106. , m_pDxilDebugInfo(pDxilDebugInfo)
  107. , m_LiveVars(std::move(LiveVars))
  108. {
  109. #ifndef NDEBUG
  110. for (auto VarAndInfo : m_LiveVars)
  111. {
  112. assert(llvm::isa<llvm::DIGlobalVariable>(VarAndInfo->m_Variable) ||
  113. llvm::isa<llvm::DILocalVariable>(VarAndInfo->m_Variable));
  114. }
  115. #endif // !NDEBUG
  116. }
  117. STDMETHODIMP CreateDxcPixVariable(
  118. IDxcPixVariable** ppVariable,
  119. VariableInfo const *VarInfo) const;
  120. public:
  121. DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL();
  122. DXC_MICROCOM_TM_ALLOC(DxcPixDxilLiveVariables);
  123. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) final {
  124. return DoBasicQueryInterface<IDxcPixDxilLiveVariables>(this, iid, ppvObject);
  125. }
  126. STDMETHODIMP GetCount(
  127. _Outptr_ DWORD *dwSize) override;
  128. STDMETHODIMP GetVariableByIndex(
  129. _In_ DWORD Index,
  130. _Outptr_result_z_ IDxcPixVariable** ppVariable) override;
  131. STDMETHODIMP GetVariableByName(
  132. _In_ LPCWSTR Name,
  133. _Outptr_result_z_ IDxcPixVariable** ppVariable) override;
  134. };
  135. } // namespace dxil_debug_info
  136. STDMETHODIMP dxil_debug_info::DxcPixDxilLiveVariables::GetCount(
  137. _Outptr_ DWORD *dwSize) {
  138. *dwSize = m_LiveVars.size();
  139. return S_OK;
  140. }
  141. STDMETHODIMP dxil_debug_info::DxcPixDxilLiveVariables::CreateDxcPixVariable(
  142. IDxcPixVariable** ppVariable,
  143. VariableInfo const* VarInfo) const
  144. {
  145. auto *Var = VarInfo->m_Variable;
  146. if (auto *DILV = llvm::dyn_cast<llvm::DILocalVariable>(Var))
  147. {
  148. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixVariable<llvm::DILocalVariable>>(
  149. ppVariable,
  150. m_pMalloc,
  151. m_pDxilDebugInfo,
  152. DILV,
  153. VarInfo);
  154. }
  155. else if (auto *DIGV = llvm::dyn_cast<llvm::DIGlobalVariable>(Var))
  156. {
  157. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixVariable<llvm::DIGlobalVariable>>(
  158. ppVariable,
  159. m_pMalloc,
  160. m_pDxilDebugInfo,
  161. DIGV,
  162. VarInfo);
  163. }
  164. return E_UNEXPECTED;
  165. }
  166. STDMETHODIMP dxil_debug_info::DxcPixDxilLiveVariables::GetVariableByIndex(
  167. _In_ DWORD Index,
  168. _Outptr_result_z_ IDxcPixVariable **ppVariable)
  169. {
  170. if (Index >= m_LiveVars.size())
  171. {
  172. return E_BOUNDS;
  173. }
  174. auto* VarInfo = m_LiveVars[Index];
  175. return CreateDxcPixVariable(ppVariable, VarInfo);
  176. }
  177. STDMETHODIMP dxil_debug_info::DxcPixDxilLiveVariables::GetVariableByName(
  178. _In_ LPCWSTR Name,
  179. _Outptr_result_z_ IDxcPixVariable **ppVariable)
  180. {
  181. std::string name = CW2A(Name);
  182. for (auto *VarInfo : m_LiveVars)
  183. {
  184. auto *Var = VarInfo->m_Variable;
  185. if (Var->getName() == name)
  186. {
  187. return CreateDxcPixVariable(ppVariable, VarInfo);
  188. }
  189. }
  190. return E_BOUNDS;
  191. }
  192. HRESULT dxil_debug_info::CreateDxilLiveVariables(
  193. DxcPixDxilDebugInfo *pDxilDebugInfo,
  194. std::vector<const VariableInfo *> &&LiveVariables,
  195. IDxcPixDxilLiveVariables **ppResult)
  196. {
  197. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixDxilLiveVariables>(
  198. ppResult,
  199. pDxilDebugInfo->GetMallocNoRef(),
  200. pDxilDebugInfo,
  201. std::move(LiveVariables));
  202. }