DxcPixCompilationInfo.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcPixCompilationInfo.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 all of the entrypoints for DXC's PIX interfaces for returning //
  9. // compilation parameters such as target profile, entry point, etc. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "dxc/Support/WinIncludes.h"
  13. #include "dxc/dxcapi.h"
  14. #include "dxc/dxcpix.h"
  15. #include "dxc/Support/FileIOHelper.h"
  16. #include "dxc/Support/Global.h"
  17. #include "dxc/Support/microcom.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/MSFileSystem.h"
  20. #include "DxcPixBase.h"
  21. #include "DxcPixDxilDebugInfo.h"
  22. #include <functional>
  23. #include "dxc/Support/WinIncludes.h"
  24. #include "DxcPixCompilationInfo.h"
  25. #include "DxcPixDxilDebugInfo.h"
  26. #include "dxc/Support/Global.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. #include "llvm/IR/DebugInfo.h"
  29. #include "llvm/IR/DebugInfoMetadata.h"
  30. #include "llvm/IR/Dominators.h"
  31. #include "llvm/IR/Instructions.h"
  32. #include "llvm/IR/IntrinsicInst.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/IR/Module.h"
  35. #include "DxilDiaSession.h"
  36. #include <unordered_map>
  37. namespace dxil_debug_info {
  38. struct CompilationInfo : public IDxcPixCompilationInfo {
  39. private:
  40. DXC_MICROCOM_TM_REF_FIELDS();
  41. dxil_dia::Session *m_pSession;
  42. llvm::NamedMDNode *m_contents;
  43. llvm::NamedMDNode *m_defines;
  44. llvm::NamedMDNode *m_mainFileName;
  45. llvm::NamedMDNode *m_arguments;
  46. public:
  47. CompilationInfo(IMalloc *pMalloc, dxil_dia::Session *pSession);
  48. DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL();
  49. DXC_MICROCOM_TM_ALLOC(CompilationInfo);
  50. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) final {
  51. return DoBasicQueryInterface<IDxcPixCompilationInfo>(this, iid, ppvObject);
  52. }
  53. virtual STDMETHODIMP
  54. GetSourceFile(_In_ DWORD SourceFileOrdinal,
  55. _Outptr_result_z_ BSTR *pSourceName,
  56. _Outptr_result_z_ BSTR *pSourceContents) override;
  57. virtual STDMETHODIMP
  58. GetArguments(_Outptr_result_z_ BSTR *pArguments) override;
  59. virtual STDMETHODIMP
  60. GetMacroDefinitions(_Outptr_result_z_ BSTR *pMacroDefinitions) override;
  61. virtual STDMETHODIMP
  62. GetEntryPointFile(_Outptr_result_z_ BSTR *pEntryPointFile) override;
  63. virtual STDMETHODIMP
  64. GetHlslTarget(_Outptr_result_z_ BSTR *pHlslTarget) override;
  65. virtual STDMETHODIMP
  66. GetEntryPoint(_Outptr_result_z_ BSTR *pEntryPoint) override;
  67. };
  68. CompilationInfo::CompilationInfo(IMalloc *pMalloc, dxil_dia::Session *pSession)
  69. : m_pSession(pSession), m_pMalloc(pMalloc) {
  70. auto *Module = m_pSession->DxilModuleRef().GetModule();
  71. m_contents =
  72. Module->getNamedMetadata(hlsl::DxilMDHelper::kDxilSourceContentsMDName);
  73. if (!m_contents)
  74. m_contents = Module->getNamedMetadata("llvm.dbg.contents");
  75. m_defines =
  76. Module->getNamedMetadata(hlsl::DxilMDHelper::kDxilSourceDefinesMDName);
  77. if (!m_defines)
  78. m_defines = Module->getNamedMetadata("llvm.dbg.defines");
  79. m_mainFileName = Module->getNamedMetadata(
  80. hlsl::DxilMDHelper::kDxilSourceMainFileNameMDName);
  81. if (!m_mainFileName)
  82. m_mainFileName = Module->getNamedMetadata("llvm.dbg.mainFileName");
  83. m_arguments =
  84. Module->getNamedMetadata(hlsl::DxilMDHelper::kDxilSourceArgsMDName);
  85. if (!m_arguments)
  86. m_arguments = Module->getNamedMetadata("llvm.dbg.args");
  87. }
  88. static void MDStringOperandToBSTR(llvm::MDOperand const &mdOperand,
  89. BSTR *pBStr) {
  90. llvm::StringRef MetadataAsStringRef =
  91. llvm::dyn_cast<llvm::MDString>(mdOperand)->getString();
  92. std::string StringWithTerminator(MetadataAsStringRef.begin(),
  93. MetadataAsStringRef.size());
  94. CA2W cv(StringWithTerminator.c_str(), CP_UTF8);
  95. CComBSTR BStr;
  96. BStr.Append(cv);
  97. BStr.Append(L"\0", 1);
  98. *pBStr = BStr.Detach();
  99. }
  100. STDMETHODIMP
  101. CompilationInfo::GetSourceFile(_In_ DWORD SourceFileOrdinal,
  102. _Outptr_result_z_ BSTR *pSourceName,
  103. _Outptr_result_z_ BSTR *pSourceContents) {
  104. *pSourceName = nullptr;
  105. *pSourceContents = nullptr;
  106. if (SourceFileOrdinal >= m_contents->getNumOperands()) {
  107. return E_INVALIDARG;
  108. }
  109. llvm::MDTuple *FileTuple =
  110. llvm::cast<llvm::MDTuple>(m_contents->getOperand(SourceFileOrdinal));
  111. MDStringOperandToBSTR(FileTuple->getOperand(0), pSourceName);
  112. MDStringOperandToBSTR(FileTuple->getOperand(1), pSourceContents);
  113. return S_OK;
  114. }
  115. STDMETHODIMP CompilationInfo::GetArguments(_Outptr_result_z_ BSTR *pArguments) {
  116. llvm::MDNode *argsNode = m_arguments->getOperand(0);
  117. // Don't return any arguments that denote things that are returned via
  118. // other methods in this class (and that PIX isn't expecting to see
  119. // in the arguments list):
  120. const char *specialCases[] = {
  121. "/T", "-T", "-D", "/D", "-E", "/E",
  122. };
  123. // Concatenate arguments into one string
  124. CComBSTR pBSTR;
  125. for (llvm::MDNode::op_iterator it = argsNode->op_begin();
  126. it != argsNode->op_end(); ++it) {
  127. llvm::StringRef strRef = llvm::dyn_cast<llvm::MDString>(*it)->getString();
  128. bool skip = false;
  129. bool skipTwice = false;
  130. for (unsigned i = 0; i < _countof(specialCases); i++) {
  131. // It's legal for users to specify, for example, /Emain or /E main:
  132. if (strRef == specialCases[i]) {
  133. skipTwice = true;
  134. skip = true;
  135. break;
  136. } else if (strRef.startswith(specialCases[i])) {
  137. skip = true;
  138. break;
  139. }
  140. }
  141. if (skip) {
  142. if (skipTwice)
  143. ++it;
  144. continue;
  145. }
  146. std::string str(strRef.begin(), strRef.size());
  147. CA2W cv(str.c_str(), CP_UTF8);
  148. pBSTR.Append(cv);
  149. pBSTR.Append(L" ", 1);
  150. }
  151. pBSTR.Append(L"\0", 1);
  152. *pArguments = pBSTR.Detach();
  153. return S_OK;
  154. }
  155. STDMETHODIMP CompilationInfo::GetMacroDefinitions(
  156. _Outptr_result_z_ BSTR *pMacroDefinitions) {
  157. llvm::MDNode *definesNode = m_defines->getOperand(0);
  158. // Concatenate definitions into one string separated by spaces
  159. CComBSTR pBSTR;
  160. for (llvm::MDNode::op_iterator it = definesNode->op_begin();
  161. it != definesNode->op_end(); ++it) {
  162. llvm::StringRef strRef = llvm::dyn_cast<llvm::MDString>(*it)->getString();
  163. std::string str(strRef.begin(), strRef.size());
  164. // PIX is expecting quoted strings as the definitions. So if no quotes were
  165. // given, add them now:
  166. auto findEquals = str.find_first_of("=");
  167. if (findEquals != std::string::npos && findEquals + 1 < str.length()) {
  168. std::string definition = str.substr(findEquals + 1);
  169. if (definition.front() != '\"') {
  170. definition.insert(definition.begin(), '\"');
  171. }
  172. if (definition.back() != '\"') {
  173. definition.push_back('\"');
  174. }
  175. std::string name = str.substr(0, findEquals);
  176. str = name + "=" + definition;
  177. }
  178. CA2W cv(str.c_str(), CP_UTF8);
  179. pBSTR.Append(L"-D", 2);
  180. pBSTR.Append(cv);
  181. pBSTR.Append(L" ", 1);
  182. }
  183. pBSTR.Append(L"\0", 1);
  184. *pMacroDefinitions = pBSTR.Detach();
  185. return S_OK;
  186. }
  187. STDMETHODIMP
  188. CompilationInfo::GetEntryPointFile(_Outptr_result_z_ BSTR *pEntryPointFile) {
  189. llvm::StringRef strRef = llvm::dyn_cast<llvm::MDString>(
  190. m_mainFileName->getOperand(0)->getOperand(0))
  191. ->getString();
  192. std::string str(strRef.begin(),
  193. strRef.size()); // To make sure str is null terminated
  194. CA2W cv(str.c_str(), CP_UTF8);
  195. CComBSTR pBSTR;
  196. pBSTR.Append(cv);
  197. *pEntryPointFile = pBSTR.Detach();
  198. return S_OK;
  199. }
  200. STDMETHODIMP
  201. CompilationInfo::GetHlslTarget(_Outptr_result_z_ BSTR *pHlslTarget) {
  202. CA2W cv(m_pSession->DxilModuleRef().GetShaderModel()->GetName(), CP_UTF8);
  203. CComBSTR pBSTR;
  204. pBSTR.Append(cv);
  205. *pHlslTarget = pBSTR.Detach();
  206. return S_OK;
  207. }
  208. STDMETHODIMP
  209. CompilationInfo::GetEntryPoint(_Outptr_result_z_ BSTR *pEntryPoint) {
  210. auto name = m_pSession->DxilModuleRef().GetEntryFunctionName();
  211. CA2W cv(name.c_str(), CP_UTF8);
  212. CComBSTR pBSTR;
  213. pBSTR.Append(cv);
  214. *pEntryPoint = pBSTR.Detach();
  215. return S_OK;
  216. }
  217. } // namespace dxil_debug_info
  218. HRESULT
  219. dxil_debug_info::CreateDxilCompilationInfo(IMalloc *pMalloc,
  220. dxil_dia::Session *pSession,
  221. IDxcPixCompilationInfo **ppResult) {
  222. return NewDxcPixDxilDebugInfoObjectOrThrow<CompilationInfo>(ppResult, pMalloc,
  223. pSession);
  224. }