DxilModuleTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Copyright (C) Microsoft Corporation. All rights reserved. //
  4. // DxilModuleTest.cpp //
  5. // //
  6. // Provides unit tests for DxilModule. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "CompilationResult.h"
  10. #include "HlslTestUtils.h"
  11. #include "DxcTestUtils.h"
  12. #include "dxc/Support/microcom.h"
  13. #include "dxc/dxcapi.internal.h"
  14. #include "dxc/HLSL/HLOperationLowerExtension.h"
  15. #include "dxc/HlslIntrinsicOp.h"
  16. #include "dxc/DXIL/DxilOperations.h"
  17. #include "dxc/DXIL/DxilInstructions.h"
  18. #include "dxc/DxilContainer/DxilContainer.h"
  19. #include "dxc/DXIL/DxilModule.h"
  20. #include "llvm/Support/Regex.h"
  21. #include "llvm/Support/MSFileSystem.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/ErrorOr.h"
  25. #include "llvm/Bitcode/ReaderWriter.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/InstIterator.h"
  28. using namespace hlsl;
  29. using namespace llvm;
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // DxilModule unit tests.
  32. #ifdef _WIN32
  33. class DxilModuleTest {
  34. #else
  35. class DxilModuleTest : public ::testing::Test {
  36. #endif
  37. public:
  38. BEGIN_TEST_CLASS(DxilModuleTest)
  39. TEST_CLASS_PROPERTY(L"Parallel", L"true")
  40. TEST_METHOD_PROPERTY(L"Priority", L"0")
  41. END_TEST_CLASS()
  42. TEST_CLASS_SETUP(InitSupport);
  43. dxc::DxcDllSupport m_dllSupport;
  44. // Basic loading tests.
  45. TEST_METHOD(LoadDxilModule_1_0)
  46. TEST_METHOD(LoadDxilModule_1_1)
  47. TEST_METHOD(LoadDxilModule_1_2)
  48. // Precise query tests.
  49. TEST_METHOD(Precise1)
  50. TEST_METHOD(Precise2)
  51. TEST_METHOD(Precise3)
  52. TEST_METHOD(Precise4)
  53. TEST_METHOD(Precise5)
  54. TEST_METHOD(Precise6)
  55. TEST_METHOD(Precise7)
  56. TEST_METHOD(SetValidatorVersion)
  57. void VerifyValidatorVersionFails(
  58. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  59. const std::vector<LPCSTR> &expectedErrors);
  60. void VerifyValidatorVersionMatches(
  61. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  62. unsigned expectedMajor = UINT_MAX, unsigned expectedMinor = UINT_MAX);
  63. };
  64. bool DxilModuleTest::InitSupport() {
  65. if (!m_dllSupport.IsEnabled()) {
  66. VERIFY_SUCCEEDED(m_dllSupport.Initialize());
  67. }
  68. return true;
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Compilation and dxil module loading support.
  72. namespace {
  73. class Compiler {
  74. public:
  75. Compiler(dxc::DxcDllSupport &dll)
  76. : m_dllSupport(dll)
  77. , m_msf(CreateMSFileSystem())
  78. , m_pts(m_msf.get())
  79. {
  80. m_ver.Initialize(m_dllSupport);
  81. VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
  82. }
  83. bool SkipDxil_Test(unsigned major, unsigned minor) {
  84. return m_ver.SkipDxilVersion(major, minor);
  85. }
  86. IDxcOperationResult *Compile(const char *program, LPCWSTR shaderModel = L"ps_6_0") {
  87. return Compile(program, shaderModel, {}, {});
  88. }
  89. IDxcOperationResult *Compile(const char *program, LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments, const std::vector<DxcDefine> defs ) {
  90. Utf8ToBlob(m_dllSupport, program, &pCodeBlob);
  91. VERIFY_SUCCEEDED(pCompiler->Compile(pCodeBlob, L"hlsl.hlsl", L"main",
  92. shaderModel,
  93. const_cast<LPCWSTR *>(arguments.data()), arguments.size(),
  94. defs.data(), defs.size(),
  95. nullptr, &pCompileResult));
  96. return pCompileResult;
  97. }
  98. std::string Disassemble() {
  99. CComPtr<IDxcBlob> pBlob;
  100. CheckOperationSucceeded(pCompileResult, &pBlob);
  101. return DisassembleProgram(m_dllSupport, pBlob);
  102. }
  103. DxilModule &GetDxilModule() {
  104. // Make sure we compiled successfully.
  105. CComPtr<IDxcBlob> pBlob;
  106. CheckOperationSucceeded(pCompileResult, &pBlob);
  107. // Verify we have a valid dxil container.
  108. const DxilContainerHeader *pContainer =
  109. IsDxilContainerLike(pBlob->GetBufferPointer(), pBlob->GetBufferSize());
  110. VERIFY_IS_NOT_NULL(pContainer);
  111. VERIFY_IS_TRUE(IsValidDxilContainer(pContainer, pBlob->GetBufferSize()));
  112. // Get Dxil part from container.
  113. DxilPartIterator it = std::find_if(begin(pContainer), end(pContainer), DxilPartIsType(DFCC_DXIL));
  114. VERIFY_IS_FALSE(it == end(pContainer));
  115. const DxilProgramHeader *pProgramHeader =
  116. reinterpret_cast<const DxilProgramHeader *>(GetDxilPartData(*it));
  117. VERIFY_IS_TRUE(IsValidDxilProgramHeader(pProgramHeader, (*it)->PartSize));
  118. // Get a pointer to the llvm bitcode.
  119. const char *pIL;
  120. uint32_t pILLength;
  121. GetDxilProgramBitcode(pProgramHeader, &pIL, &pILLength);
  122. // Parse llvm bitcode into a module.
  123. std::unique_ptr<llvm::MemoryBuffer> pBitcodeBuf(
  124. llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(pIL, pILLength), "", false));
  125. llvm::ErrorOr<std::unique_ptr<llvm::Module>>
  126. pModule(llvm::parseBitcodeFile(pBitcodeBuf->getMemBufferRef(), m_llvmContext));
  127. if (std::error_code ec = pModule.getError()) {
  128. VERIFY_FAIL();
  129. }
  130. m_module = std::move(pModule.get());
  131. // Grab the dxil module;
  132. DxilModule *DM = DxilModule::TryGetDxilModule(m_module.get());
  133. VERIFY_IS_NOT_NULL(DM);
  134. return *DM;
  135. }
  136. public:
  137. static ::llvm::sys::fs::MSFileSystem *CreateMSFileSystem() {
  138. ::llvm::sys::fs::MSFileSystem *msfPtr;
  139. VERIFY_SUCCEEDED(CreateMSFileSystemForDisk(&msfPtr));
  140. return msfPtr;
  141. }
  142. dxc::DxcDllSupport &m_dllSupport;
  143. VersionSupportInfo m_ver;
  144. CComPtr<IDxcCompiler> pCompiler;
  145. CComPtr<IDxcBlobEncoding> pCodeBlob;
  146. CComPtr<IDxcOperationResult> pCompileResult;
  147. llvm::LLVMContext m_llvmContext;
  148. std::unique_ptr<llvm::Module> m_module;
  149. std::unique_ptr<::llvm::sys::fs::MSFileSystem> m_msf;
  150. ::llvm::sys::fs::AutoPerThreadSystem m_pts;
  151. };
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////
  154. // Unit Test Implementation
  155. TEST_F(DxilModuleTest, LoadDxilModule_1_0) {
  156. Compiler c(m_dllSupport);
  157. c.Compile(
  158. "float4 main() : SV_Target {\n"
  159. " return 0;\n"
  160. "}\n"
  161. ,
  162. L"ps_6_0"
  163. );
  164. // Basic sanity check on dxil version in dxil module.
  165. DxilModule &DM = c.GetDxilModule();
  166. unsigned vMajor, vMinor;
  167. DM.GetDxilVersion(vMajor, vMinor);
  168. VERIFY_IS_TRUE(vMajor == 1);
  169. VERIFY_IS_TRUE(vMinor == 0);
  170. }
  171. TEST_F(DxilModuleTest, LoadDxilModule_1_1) {
  172. Compiler c(m_dllSupport);
  173. if (c.SkipDxil_Test(1,1)) return;
  174. c.Compile(
  175. "float4 main() : SV_Target {\n"
  176. " return 0;\n"
  177. "}\n"
  178. ,
  179. L"ps_6_1"
  180. );
  181. // Basic sanity check on dxil version in dxil module.
  182. DxilModule &DM = c.GetDxilModule();
  183. unsigned vMajor, vMinor;
  184. DM.GetDxilVersion(vMajor, vMinor);
  185. VERIFY_IS_TRUE(vMajor == 1);
  186. VERIFY_IS_TRUE(vMinor == 1);
  187. }
  188. TEST_F(DxilModuleTest, LoadDxilModule_1_2) {
  189. Compiler c(m_dllSupport);
  190. if (c.SkipDxil_Test(1,2)) return;
  191. c.Compile(
  192. "float4 main() : SV_Target {\n"
  193. " return 0;\n"
  194. "}\n"
  195. ,
  196. L"ps_6_2"
  197. );
  198. // Basic sanity check on dxil version in dxil module.
  199. DxilModule &DM = c.GetDxilModule();
  200. unsigned vMajor, vMinor;
  201. DM.GetDxilVersion(vMajor, vMinor);
  202. VERIFY_IS_TRUE(vMajor == 1);
  203. VERIFY_IS_TRUE(vMinor == 2);
  204. }
  205. TEST_F(DxilModuleTest, Precise1) {
  206. Compiler c(m_dllSupport);
  207. c.Compile(
  208. "precise float main(float x : X, float y : Y) : SV_Target {\n"
  209. " return sqrt(x) + y;\n"
  210. "}\n"
  211. );
  212. // Make sure sqrt and add are marked precise.
  213. DxilModule &DM = c.GetDxilModule();
  214. Function *F = DM.GetEntryFunction();
  215. int numChecks = 0;
  216. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  217. Instruction *Inst = &*I;
  218. if (DxilInst_Sqrt(Inst)) {
  219. numChecks++;
  220. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  221. }
  222. else if (LlvmInst_FAdd(Inst)) {
  223. numChecks++;
  224. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  225. }
  226. }
  227. VERIFY_ARE_EQUAL(numChecks, 2);
  228. }
  229. TEST_F(DxilModuleTest, Precise2) {
  230. Compiler c(m_dllSupport);
  231. c.Compile(
  232. "float main(float x : X, float y : Y) : SV_Target {\n"
  233. " return sqrt(x) + y;\n"
  234. "}\n"
  235. );
  236. // Make sure sqrt and add are not marked precise.
  237. DxilModule &DM = c.GetDxilModule();
  238. Function *F = DM.GetEntryFunction();
  239. int numChecks = 0;
  240. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  241. Instruction *Inst = &*I;
  242. if (DxilInst_Sqrt(Inst)) {
  243. numChecks++;
  244. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  245. }
  246. else if (LlvmInst_FAdd(Inst)) {
  247. numChecks++;
  248. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  249. }
  250. }
  251. VERIFY_ARE_EQUAL(numChecks, 2);
  252. }
  253. TEST_F(DxilModuleTest, Precise3) {
  254. // TODO: Enable this test when precise metadata is inserted for Gis.
  255. if (const bool GisIsBroken = true) return;
  256. Compiler c(m_dllSupport);
  257. c.Compile(
  258. "float main(float x : X, float y : Y) : SV_Target {\n"
  259. " return sqrt(x) + y;\n"
  260. "}\n",
  261. L"ps_6_0",
  262. { L"/Gis" }, {}
  263. );
  264. // Make sure sqrt and add are marked precise.
  265. DxilModule &DM = c.GetDxilModule();
  266. Function *F = DM.GetEntryFunction();
  267. int numChecks = 0;
  268. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  269. Instruction *Inst = &*I;
  270. if (DxilInst_Sqrt(Inst)) {
  271. numChecks++;
  272. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  273. }
  274. else if (LlvmInst_FAdd(Inst)) {
  275. numChecks++;
  276. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  277. }
  278. }
  279. VERIFY_ARE_EQUAL(numChecks, 2);
  280. }
  281. TEST_F(DxilModuleTest, Precise4) {
  282. Compiler c(m_dllSupport);
  283. c.Compile(
  284. "float main(float x : X, float y : Y) : SV_Target {\n"
  285. " precise float sx = 1 / sqrt(x);\n"
  286. " return sx + y;\n"
  287. "}\n"
  288. );
  289. // Make sure sqrt and div are marked precise, and add is not.
  290. DxilModule &DM = c.GetDxilModule();
  291. Function *F = DM.GetEntryFunction();
  292. int numChecks = 0;
  293. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  294. Instruction *Inst = &*I;
  295. if (DxilInst_Sqrt(Inst)) {
  296. numChecks++;
  297. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  298. }
  299. else if (LlvmInst_FDiv(Inst)) {
  300. numChecks++;
  301. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  302. }
  303. else if (LlvmInst_FAdd(Inst)) {
  304. numChecks++;
  305. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  306. }
  307. }
  308. VERIFY_ARE_EQUAL(numChecks, 3);
  309. }
  310. TEST_F(DxilModuleTest, Precise5) {
  311. Compiler c(m_dllSupport);
  312. c.Compile(
  313. "float C[10];\n"
  314. "float main(float x : X, float y : Y, int i : I) : SV_Target {\n"
  315. " float A[2];\n"
  316. " A[0] = x;\n"
  317. " A[1] = y;\n"
  318. " return A[i] + C[i];\n"
  319. "}\n"
  320. );
  321. // Make sure load and extract value are not reported as precise.
  322. DxilModule &DM = c.GetDxilModule();
  323. Function *F = DM.GetEntryFunction();
  324. int numChecks = 0;
  325. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  326. Instruction *Inst = &*I;
  327. if (LlvmInst_ExtractValue(Inst)) {
  328. numChecks++;
  329. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  330. }
  331. else if (LlvmInst_Load(Inst)) {
  332. numChecks++;
  333. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  334. }
  335. else if (LlvmInst_FAdd(Inst)) {
  336. numChecks++;
  337. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  338. }
  339. }
  340. VERIFY_ARE_EQUAL(numChecks, 3);
  341. }
  342. TEST_F(DxilModuleTest, Precise6) {
  343. Compiler c(m_dllSupport);
  344. c.Compile(
  345. "precise float2 main(float2 x : A, float2 y : B) : SV_Target {\n"
  346. " return sqrt(x * y);\n"
  347. "}\n"
  348. );
  349. // Make sure sqrt and mul are marked precise.
  350. DxilModule &DM = c.GetDxilModule();
  351. Function *F = DM.GetEntryFunction();
  352. int numChecks = 0;
  353. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  354. Instruction *Inst = &*I;
  355. if (DxilInst_Sqrt(Inst)) {
  356. numChecks++;
  357. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  358. }
  359. else if (LlvmInst_FMul(Inst)) {
  360. numChecks++;
  361. VERIFY_IS_TRUE(DM.IsPrecise(Inst));
  362. }
  363. }
  364. VERIFY_ARE_EQUAL(numChecks, 4);
  365. }
  366. TEST_F(DxilModuleTest, Precise7) {
  367. Compiler c(m_dllSupport);
  368. c.Compile(
  369. "float2 main(float2 x : A, float2 y : B) : SV_Target {\n"
  370. " return sqrt(x * y);\n"
  371. "}\n"
  372. );
  373. // Make sure sqrt and mul are not marked precise.
  374. DxilModule &DM = c.GetDxilModule();
  375. Function *F = DM.GetEntryFunction();
  376. int numChecks = 0;
  377. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  378. Instruction *Inst = &*I;
  379. if (DxilInst_Sqrt(Inst)) {
  380. numChecks++;
  381. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  382. }
  383. else if (LlvmInst_FMul(Inst)) {
  384. numChecks++;
  385. VERIFY_IS_FALSE(DM.IsPrecise(Inst));
  386. }
  387. }
  388. VERIFY_ARE_EQUAL(numChecks, 4);
  389. }
  390. void DxilModuleTest::VerifyValidatorVersionFails(
  391. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  392. const std::vector<LPCSTR> &expectedErrors) {
  393. LPCSTR shader =
  394. "[shader(\"pixel\")]"
  395. "float4 main() : SV_Target {\n"
  396. " return 0;\n"
  397. "}\n";
  398. Compiler c(m_dllSupport);
  399. c.Compile(shader, shaderModel, arguments, {});
  400. CheckOperationResultMsgs(c.pCompileResult, expectedErrors, false, false);
  401. }
  402. void DxilModuleTest::VerifyValidatorVersionMatches(
  403. LPCWSTR shaderModel, const std::vector<LPCWSTR> &arguments,
  404. unsigned expectedMajor, unsigned expectedMinor) {
  405. LPCSTR shader =
  406. "[shader(\"pixel\")]"
  407. "float4 main() : SV_Target {\n"
  408. " return 0;\n"
  409. "}\n";
  410. Compiler c(m_dllSupport);
  411. c.Compile(shader, shaderModel, arguments, {});
  412. DxilModule &DM = c.GetDxilModule();
  413. unsigned vMajor, vMinor;
  414. DM.GetValidatorVersion(vMajor, vMinor);
  415. if (expectedMajor == UINT_MAX) {
  416. // Expect current version
  417. VERIFY_ARE_EQUAL(vMajor, c.m_ver.m_ValMajor);
  418. VERIFY_ARE_EQUAL(vMinor, c.m_ver.m_ValMinor);
  419. } else {
  420. VERIFY_ARE_EQUAL(vMajor, expectedMajor);
  421. VERIFY_ARE_EQUAL(vMinor, expectedMinor);
  422. }
  423. }
  424. TEST_F(DxilModuleTest, SetValidatorVersion) {
  425. Compiler c(m_dllSupport);
  426. if (c.SkipDxil_Test(1, 4)) return;
  427. // Current version
  428. VerifyValidatorVersionMatches(L"ps_6_2", {});
  429. VerifyValidatorVersionMatches(L"lib_6_3", {});
  430. // Current version, with validation disabled
  431. VerifyValidatorVersionMatches(L"ps_6_2", {L"-Vd"});
  432. VerifyValidatorVersionMatches(L"lib_6_3", {L"-Vd"});
  433. // Override validator version
  434. VerifyValidatorVersionMatches(L"ps_6_2", {L"-validator-version", L"1.2"}, 1,2);
  435. VerifyValidatorVersionMatches(L"lib_6_3", {L"-validator-version", L"1.3"}, 1,3);
  436. // Override validator version, with validation disabled
  437. VerifyValidatorVersionMatches(L"ps_6_2", {L"-Vd", L"-validator-version", L"1.2"}, 1,2);
  438. VerifyValidatorVersionMatches(L"lib_6_3", {L"-Vd", L"-validator-version", L"1.3"}, 1,3);
  439. // Never can validate (version 0,0):
  440. VerifyValidatorVersionMatches(L"lib_6_1", {L"-Vd"}, 0, 0);
  441. VerifyValidatorVersionMatches(L"lib_6_2", {L"-Vd"}, 0, 0);
  442. VerifyValidatorVersionMatches(L"lib_6_2", {L"-Vd", L"-validator-version", L"0.0"}, 0, 0);
  443. VerifyValidatorVersionMatches(L"lib_6_x", {}, 0, 0);
  444. VerifyValidatorVersionMatches(L"lib_6_x", {L"-validator-version", L"0.0"}, 0, 0);
  445. // Failure cases:
  446. VerifyValidatorVersionFails(L"ps_6_2", {L"-validator-version", L"1.1"}, {
  447. "validator version 1,1 does not support target profile."});
  448. VerifyValidatorVersionFails(L"lib_6_2", {L"-Tlib_6_2"}, {
  449. "Must disable validation for unsupported lib_6_1 or lib_6_2 targets"});
  450. VerifyValidatorVersionFails(L"lib_6_2", {L"-Vd", L"-validator-version", L"1.2"}, {
  451. "-validator-version cannot be used with library profiles lib_6_1 or lib_6_2."});
  452. VerifyValidatorVersionFails(L"lib_6_x", {L"-validator-version", L"1.3"}, {
  453. "Offline library profile cannot be used with non-zero -validator-version."});
  454. }