DxcTestUtils.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcTestUtils.h //
  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. // This file provides utility functions for testing dxc APIs. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <string>
  13. #include <vector>
  14. #include <map>
  15. #include "dxc/dxcapi.h"
  16. #include "dxc/Support/dxcapi.use.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/StringRef.h"
  19. namespace hlsl {
  20. namespace options {
  21. class DxcOpts;
  22. class MainArgs;
  23. }
  24. }
  25. /// Use this class to run a FileCheck invocation in memory.
  26. class FileCheckForTest {
  27. public:
  28. FileCheckForTest();
  29. std::string CheckFilename;
  30. /// File to check (defaults to stdin)
  31. std::string InputFilename;
  32. /// Prefix to use from check file (defaults to 'CHECK')
  33. std::vector<std::string> CheckPrefixes;
  34. /// Do not treat all horizontal whitespace as equivalent
  35. bool NoCanonicalizeWhiteSpace;
  36. /// Add an implicit negative check with this pattern to every
  37. /// positive check. This can be used to ensure that no instances of
  38. /// this pattern occur which are not matched by a positive pattern
  39. std::vector<std::string> ImplicitCheckNot;
  40. /// Allow the input file to be empty. This is useful when making
  41. /// checks that some error message does not occur, for example.
  42. bool AllowEmptyInput;
  43. /// String to read in place of standard input.
  44. std::string InputForStdin;
  45. /// Output stream.
  46. std::string test_outs;
  47. /// Output stream.
  48. std::string test_errs;
  49. int Run();
  50. };
  51. // The result of running a single command in a run pipeline
  52. struct FileRunCommandResult {
  53. CComPtr<IDxcOperationResult> OpResult; // The operation result, if any.
  54. std::string StdOut;
  55. std::string StdErr;
  56. int ExitCode = 0;
  57. bool AbortPipeline = false; // True to prevent running subsequent commands
  58. static inline FileRunCommandResult Success() {
  59. FileRunCommandResult result;
  60. result.ExitCode = 0;
  61. return std::move(result);
  62. }
  63. static inline FileRunCommandResult Success(std::string StdOut) {
  64. FileRunCommandResult result;
  65. result.ExitCode = 0;
  66. result.StdOut = std::move(StdOut);
  67. return std::move(result);
  68. }
  69. static inline FileRunCommandResult Error(int ExitCode, std::string StdErr) {
  70. FileRunCommandResult result;
  71. result.ExitCode = ExitCode;
  72. result.StdErr = std::move(StdErr);
  73. return std::move(result);
  74. }
  75. static inline FileRunCommandResult Error(std::string StdErr) {
  76. return Error(1, StdErr);
  77. }
  78. };
  79. typedef std::map<std::string, std::string> PluginToolsPaths;
  80. class FileRunCommandPart {
  81. public:
  82. FileRunCommandPart(const std::string &command, const std::string &arguments, LPCWSTR commandFileName);
  83. FileRunCommandPart(const FileRunCommandPart&) = default;
  84. FileRunCommandPart(FileRunCommandPart&&) = default;
  85. FileRunCommandResult Run(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior, PluginToolsPaths *pPluginToolsPaths = nullptr );
  86. FileRunCommandResult RunHashTests(dxc::DxcDllSupport &DllSupport);
  87. FileRunCommandResult ReadOptsForDxc(hlsl::options::MainArgs &argStrings, hlsl::options::DxcOpts &Opts, unsigned flagsToInclude = 0);
  88. std::string Command; // Command to run, eg %dxc
  89. std::string Arguments; // Arguments to command
  90. LPCWSTR CommandFileName; // File name replacement for %s
  91. private:
  92. FileRunCommandResult RunFileChecker(const FileRunCommandResult *Prior);
  93. FileRunCommandResult RunDxc(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior);
  94. FileRunCommandResult RunDxv(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior);
  95. FileRunCommandResult RunOpt(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior);
  96. FileRunCommandResult RunD3DReflect(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior);
  97. FileRunCommandResult RunDxr(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult *Prior);
  98. FileRunCommandResult RunTee(const FileRunCommandResult *Prior);
  99. FileRunCommandResult RunXFail(const FileRunCommandResult *Prior);
  100. FileRunCommandResult RunDxilVer(dxc::DxcDllSupport& DllSupport, const FileRunCommandResult* Prior);
  101. FileRunCommandResult RunDxcHashTest(dxc::DxcDllSupport &DllSupport);
  102. FileRunCommandResult RunFromPath(const std::string &path, const FileRunCommandResult *Prior);
  103. FileRunCommandResult RunFileCompareText(const FileRunCommandResult *Prior);
  104. #ifdef _WIN32
  105. FileRunCommandResult RunFxc(dxc::DxcDllSupport &DllSupport, const FileRunCommandResult* Prior);
  106. #endif
  107. void SubstituteFilenameVars(std::string &args);
  108. #ifdef _WIN32
  109. bool ReadFileContentToString(HANDLE hFile, std::string &str);
  110. #endif
  111. };
  112. void ParseCommandParts(LPCSTR commands, LPCWSTR fileName, std::vector<FileRunCommandPart> &parts);
  113. void ParseCommandPartsFromFile(LPCWSTR fileName, std::vector<FileRunCommandPart> &parts);
  114. class FileRunTestResult {
  115. public:
  116. std::string ErrorMessage;
  117. int RunResult;
  118. static FileRunTestResult RunHashTestFromFileCommands(LPCWSTR fileName);
  119. static FileRunTestResult RunFromFileCommands(LPCWSTR fileName, PluginToolsPaths *pPluginToolsPaths = nullptr);
  120. static FileRunTestResult RunFromFileCommands(LPCWSTR fileName, dxc::DxcDllSupport &dllSupport, PluginToolsPaths *pPluginToolsPaths = nullptr);
  121. };
  122. void AssembleToContainer(dxc::DxcDllSupport &dllSupport, IDxcBlob *pModule, IDxcBlob **pContainer);
  123. std::string BlobToUtf8(_In_ IDxcBlob *pBlob);
  124. std::wstring BlobToUtf16(_In_ IDxcBlob *pBlob);
  125. void CheckOperationSucceeded(IDxcOperationResult *pResult, IDxcBlob **ppBlob);
  126. bool CheckOperationResultMsgs(IDxcOperationResult *pResult,
  127. llvm::ArrayRef<LPCSTR> pErrorMsgs,
  128. bool maySucceedAnyway, bool bRegex);
  129. bool CheckOperationResultMsgs(IDxcOperationResult *pResult,
  130. const LPCSTR *pErrorMsgs, size_t errorMsgCount,
  131. bool maySucceedAnyway, bool bRegex);
  132. bool CheckMsgs(const LPCSTR pText, size_t TextCount, const LPCSTR *pErrorMsgs,
  133. size_t errorMsgCount, bool bRegex);
  134. bool CheckNotMsgs(const LPCSTR pText, size_t TextCount, const LPCSTR *pErrorMsgs,
  135. size_t errorMsgCount, bool bRegex);
  136. void GetDxilPart(dxc::DxcDllSupport &dllSupport, IDxcBlob *pProgram, IDxcBlob **pDxilPart);
  137. std::string DisassembleProgram(dxc::DxcDllSupport &dllSupport, IDxcBlob *pProgram);
  138. void SplitPassList(LPWSTR pPassesBuffer, std::vector<LPCWSTR> &passes);
  139. void MultiByteStringToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, UINT32 codePoint, _Outptr_ IDxcBlob **ppBlob);
  140. void MultiByteStringToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, UINT32 codePoint, _Outptr_ IDxcBlobEncoding **ppBlob);
  141. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, _Outptr_ IDxcBlob **ppBlob);
  142. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const std::string &val, _Outptr_ IDxcBlobEncoding **ppBlob);
  143. void Utf8ToBlob(dxc::DxcDllSupport &dllSupport, const char *pVal, _Outptr_ IDxcBlobEncoding **ppBlob);
  144. void Utf16ToBlob(dxc::DxcDllSupport &dllSupport, const std::wstring &val, _Outptr_ IDxcBlob **ppBlob);
  145. void Utf16ToBlob(dxc::DxcDllSupport &dllSupport, const std::wstring &val, _Outptr_ IDxcBlobEncoding **ppBlob);
  146. void VerifyCompileOK(dxc::DxcDllSupport &dllSupport, LPCSTR pText,
  147. LPWSTR pTargetProfile, LPCWSTR pArgs,
  148. _Outptr_ IDxcBlob **ppResult);
  149. void VerifyCompileOK(dxc::DxcDllSupport &dllSupport, LPCSTR pText,
  150. LPWSTR pTargetProfile, std::vector<LPCWSTR> &args,
  151. _Outptr_ IDxcBlob **ppResult);
  152. HRESULT GetVersion(dxc::DxcDllSupport& DllSupport, REFCLSID clsid, unsigned &Major, unsigned &Minor);
  153. bool ParseTargetProfile(llvm::StringRef targetProfile, llvm::StringRef &outStage, unsigned &outMajor, unsigned &outMinor);
  154. class VersionSupportInfo {
  155. private:
  156. bool m_CompilerIsDebugBuild;
  157. public:
  158. bool m_InternalValidator;
  159. unsigned m_DxilMajor, m_DxilMinor;
  160. unsigned m_ValMajor, m_ValMinor;
  161. VersionSupportInfo();
  162. // Initialize version info structure. TODO: add device shader model support
  163. void Initialize(dxc::DxcDllSupport &dllSupport);
  164. // Return true if IR sensitive test should be skipped, and log comment
  165. bool SkipIRSensitiveTest();
  166. // Return true if test requiring DXIL of given version should be skipped, and log comment
  167. bool SkipDxilVersion(unsigned major, unsigned minor);
  168. // Return true if out-of-memory test should be skipped, and log comment
  169. bool SkipOutOfMemoryTest();
  170. };