DxcPixDxilStorage.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcPixDxilStorage.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 DxcPixDxilStorage API. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "DxcPixDxilStorage.h"
  12. #include "dxc/Support/WinIncludes.h"
  13. #include "dxc/DxilPIXPasses/DxilPIXVirtualRegisters.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "DxcPixBase.h"
  16. #include "DxcPixLiveVariables.h"
  17. #include "DxcPixTypes.h"
  18. #include "DxilDiaSession.h"
  19. static HRESULT UnAliasType(
  20. IDxcPixType *MaybeAlias,
  21. IDxcPixType **OriginalType
  22. )
  23. {
  24. CComPtr<IDxcPixType> Tmp(MaybeAlias);
  25. HRESULT hr = E_FAIL;
  26. *OriginalType = nullptr;
  27. do
  28. {
  29. CComPtr<IDxcPixType> Other;
  30. hr = Tmp->UnAlias(&Other);
  31. IFR(hr);
  32. if (hr == S_FALSE)
  33. {
  34. break;
  35. }
  36. Tmp = Other;
  37. } while (true);
  38. *OriginalType = Tmp.Detach();
  39. return S_OK;
  40. }
  41. HRESULT CreateDxcPixStorageImpl(
  42. dxil_debug_info::DxcPixDxilDebugInfo *pDxilDebugInfo,
  43. IDxcPixType* OriginalType,
  44. const dxil_debug_info::VariableInfo* VarInfo,
  45. unsigned CurrentOffsetInBits,
  46. IDxcPixDxilStorage** ppStorage)
  47. {
  48. CComPtr<IDxcPixArrayType> ArrayTy;
  49. CComPtr<IDxcPixStructType> StructTy;
  50. CComPtr<IDxcPixScalarType> ScalarTy;
  51. CComPtr<IDxcPixType> UnalisedType;
  52. IFR(UnAliasType(OriginalType, &UnalisedType));
  53. if (UnalisedType->QueryInterface(&ArrayTy) == S_OK)
  54. {
  55. return dxil_debug_info::NewDxcPixDxilDebugInfoObjectOrThrow<dxil_debug_info::DxcPixDxilArrayStorage>(
  56. ppStorage,
  57. pDxilDebugInfo->GetMallocNoRef(),
  58. pDxilDebugInfo,
  59. OriginalType,
  60. ArrayTy,
  61. VarInfo,
  62. CurrentOffsetInBits);
  63. }
  64. else if (UnalisedType->QueryInterface(&StructTy) == S_OK)
  65. {
  66. return dxil_debug_info::NewDxcPixDxilDebugInfoObjectOrThrow<dxil_debug_info::DxcPixDxilStructStorage>(
  67. ppStorage,
  68. pDxilDebugInfo->GetMallocNoRef(),
  69. pDxilDebugInfo,
  70. OriginalType,
  71. StructTy,
  72. VarInfo,
  73. CurrentOffsetInBits);
  74. }
  75. else if (UnalisedType->QueryInterface(&ScalarTy) == S_OK)
  76. {
  77. return dxil_debug_info::NewDxcPixDxilDebugInfoObjectOrThrow<dxil_debug_info::DxcPixDxilScalarStorage>(
  78. ppStorage,
  79. pDxilDebugInfo->GetMallocNoRef(),
  80. pDxilDebugInfo,
  81. OriginalType,
  82. ScalarTy,
  83. VarInfo,
  84. CurrentOffsetInBits);
  85. }
  86. return E_UNEXPECTED;
  87. }
  88. STDMETHODIMP dxil_debug_info::DxcPixDxilArrayStorage::AccessField(
  89. _In_ LPCWSTR Name,
  90. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  91. {
  92. return E_FAIL;
  93. }
  94. STDMETHODIMP dxil_debug_info::DxcPixDxilArrayStorage::Index(
  95. _In_ DWORD Index,
  96. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  97. {
  98. CComPtr<IDxcPixType> IndexedType;
  99. IFR(m_pType->GetIndexedType(&IndexedType));
  100. DWORD NumElements;
  101. IFR(m_pType->GetNumElements(&NumElements));
  102. if (Index >= NumElements)
  103. {
  104. return E_BOUNDS;
  105. }
  106. DWORD IndexedTypeSizeInBits;
  107. IFR(IndexedType->GetSizeInBits(&IndexedTypeSizeInBits));
  108. const unsigned NewOffsetInBits =
  109. m_OffsetFromStorageStartInBits + Index * IndexedTypeSizeInBits;
  110. return CreateDxcPixStorageImpl(
  111. m_pDxilDebugInfo,
  112. IndexedType,
  113. m_pVarInfo,
  114. NewOffsetInBits,
  115. ppResult);
  116. }
  117. STDMETHODIMP dxil_debug_info::DxcPixDxilArrayStorage::GetRegisterNumber(
  118. _Outptr_result_z_ DWORD* pRegisterNumber)
  119. {
  120. return E_FAIL;
  121. }
  122. STDMETHODIMP dxil_debug_info::DxcPixDxilArrayStorage::GetIsAlive()
  123. {
  124. for (auto OffsetAndRegister : m_pVarInfo->m_ValueLocationMap)
  125. {
  126. if (OffsetAndRegister.second.m_V != nullptr)
  127. {
  128. return S_OK;
  129. }
  130. }
  131. return E_FAIL;
  132. }
  133. STDMETHODIMP dxil_debug_info::DxcPixDxilArrayStorage::GetType(
  134. _Outptr_result_z_ IDxcPixType** ppType)
  135. {
  136. *ppType = m_pOriginalType;
  137. (*ppType)->AddRef();
  138. return S_OK;
  139. }
  140. STDMETHODIMP dxil_debug_info::DxcPixDxilStructStorage::AccessField(
  141. _In_ LPCWSTR Name,
  142. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  143. {
  144. CComPtr<IDxcPixStructField> Field;
  145. IFR(m_pType->GetFieldByName(Name, &Field));
  146. CComPtr<IDxcPixType> FieldType;
  147. IFR(Field->GetType(&FieldType));
  148. DWORD FieldOffsetInBits;
  149. IFR(Field->GetOffsetInBits(&FieldOffsetInBits));
  150. const unsigned NewOffsetInBits =
  151. m_OffsetFromStorageStartInBits + FieldOffsetInBits;
  152. return CreateDxcPixStorageImpl(
  153. m_pDxilDebugInfo,
  154. FieldType,
  155. m_pVarInfo,
  156. NewOffsetInBits,
  157. ppResult);
  158. }
  159. STDMETHODIMP dxil_debug_info::DxcPixDxilStructStorage::Index(
  160. _In_ DWORD Index,
  161. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  162. {
  163. return E_FAIL;
  164. }
  165. STDMETHODIMP dxil_debug_info::DxcPixDxilStructStorage::GetRegisterNumber(
  166. _Outptr_result_z_ DWORD* pRegisterNumber)
  167. {
  168. return E_FAIL;
  169. }
  170. STDMETHODIMP dxil_debug_info::DxcPixDxilStructStorage::GetIsAlive()
  171. {
  172. for (auto OffsetAndRegister : m_pVarInfo->m_ValueLocationMap)
  173. {
  174. if (OffsetAndRegister.second.m_V != nullptr)
  175. {
  176. return S_OK;
  177. }
  178. }
  179. return E_FAIL;
  180. }
  181. STDMETHODIMP dxil_debug_info::DxcPixDxilStructStorage::GetType(
  182. _Outptr_result_z_ IDxcPixType** ppType)
  183. {
  184. *ppType = m_pOriginalType;
  185. (*ppType)->AddRef();
  186. return S_OK;
  187. }
  188. STDMETHODIMP dxil_debug_info::DxcPixDxilScalarStorage::AccessField(
  189. _In_ LPCWSTR Name,
  190. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  191. {
  192. return E_FAIL;
  193. }
  194. STDMETHODIMP dxil_debug_info::DxcPixDxilScalarStorage::Index(
  195. _In_ DWORD Index,
  196. _COM_Outptr_ IDxcPixDxilStorage** ppResult)
  197. {
  198. return E_FAIL;
  199. }
  200. STDMETHODIMP dxil_debug_info::DxcPixDxilScalarStorage::GetRegisterNumber(
  201. _Outptr_result_z_ DWORD* pRegisterNumber)
  202. {
  203. const auto &ValueLocationMap = m_pVarInfo->m_ValueLocationMap;
  204. auto RegIt = ValueLocationMap.find(
  205. m_OffsetFromStorageStartInBits);
  206. if (RegIt == ValueLocationMap.end())
  207. {
  208. return E_FAIL;
  209. }
  210. if (auto *AllocaReg = llvm::dyn_cast<llvm::AllocaInst>(RegIt->second.m_V))
  211. {
  212. uint32_t RegNum;
  213. uint32_t RegSize;
  214. if (!pix_dxil::PixAllocaReg::FromInst(AllocaReg, &RegNum, &RegSize))
  215. {
  216. return E_FAIL;
  217. }
  218. *pRegisterNumber = RegNum + RegIt->second.m_FragmentIndex;
  219. }
  220. else
  221. {
  222. return E_FAIL;
  223. }
  224. return S_OK;
  225. }
  226. STDMETHODIMP dxil_debug_info::DxcPixDxilScalarStorage::GetIsAlive()
  227. {
  228. const auto &ValueLocationMap = m_pVarInfo->m_ValueLocationMap;
  229. auto RegIt = ValueLocationMap.find(
  230. m_OffsetFromStorageStartInBits);
  231. return RegIt == ValueLocationMap.end() ? E_FAIL : S_OK;
  232. }
  233. STDMETHODIMP dxil_debug_info::DxcPixDxilScalarStorage::GetType(
  234. _Outptr_result_z_ IDxcPixType **ppType)
  235. {
  236. *ppType = m_pOriginalType;
  237. (*ppType)->AddRef();
  238. return S_OK;
  239. }
  240. HRESULT dxil_debug_info::CreateDxcPixStorage(
  241. DxcPixDxilDebugInfo *pDxilDebugInfo,
  242. llvm::DIType *diType,
  243. const VariableInfo *VarInfo,
  244. unsigned CurrentOffsetInBits,
  245. IDxcPixDxilStorage **ppStorage)
  246. {
  247. CComPtr<IDxcPixType> OriginalType;
  248. IFR(dxil_debug_info::CreateDxcPixType(pDxilDebugInfo, diType, &OriginalType));
  249. return CreateDxcPixStorageImpl(
  250. pDxilDebugInfo,
  251. OriginalType,
  252. VarInfo,
  253. CurrentOffsetInBits,
  254. ppStorage);
  255. }