WholeFileTestFixture.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- unittests/SPIRV/WholeFileTestFixture.cpp - WholeFileTest impl ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include <fstream>
  10. #include "FileTestUtils.h"
  11. #include "WholeFileTestFixture.h"
  12. namespace clang {
  13. namespace spirv {
  14. namespace {
  15. const char hlslStartLabel[] = "// Run:";
  16. const char spirvStartLabel[] = "// CHECK-WHOLE-SPIR-V:";
  17. } // namespace
  18. bool WholeFileTest::parseInputFile() {
  19. bool foundRunCommand = false;
  20. bool parseSpirv = false;
  21. std::ostringstream outString;
  22. std::ifstream inputFile;
  23. inputFile.exceptions(std::ifstream::failbit);
  24. try {
  25. inputFile.open(inputFilePath);
  26. for (std::string line; std::getline(inputFile, line);) {
  27. if (line.find(hlslStartLabel) != std::string::npos) {
  28. foundRunCommand = true;
  29. if (!utils::processRunCommandArgs(line, &targetProfile, &entryPoint,
  30. &restArgs)) {
  31. // An error has occured when parsing the Run command.
  32. return false;
  33. }
  34. } else if (line.find(spirvStartLabel) != std::string::npos) {
  35. // HLSL source has ended.
  36. // SPIR-V source starts on the next line.
  37. parseSpirv = true;
  38. } else if (parseSpirv) {
  39. // Strip the leading "//" from the SPIR-V assembly (skip 2 characters)
  40. if (line.size() > 2u) {
  41. line = line.substr(2);
  42. }
  43. // Skip any leading whitespace
  44. size_t found = line.find_first_not_of(" \t");
  45. if (found != std::string::npos) {
  46. line = line.substr(found);
  47. }
  48. outString << line << std::endl;
  49. }
  50. }
  51. } catch (...) {
  52. if (!inputFile.eof()) {
  53. fprintf(
  54. stderr,
  55. "Error: Exception occurred while opening/reading the input file %s\n",
  56. inputFilePath.c_str());
  57. return false;
  58. }
  59. }
  60. if (!foundRunCommand) {
  61. fprintf(stderr, "Error: Missing \"Run:\" command.\n");
  62. return false;
  63. }
  64. if (!parseSpirv) {
  65. fprintf(stderr, "Error: Missing \"CHECK-WHOLE-SPIR-V:\" command.\n");
  66. return false;
  67. }
  68. // Reached the end of the file. SPIR-V source has ended. Store it for
  69. // comparison.
  70. expectedSpirvAsm = outString.str();
  71. // Close the input file.
  72. inputFile.close();
  73. // Everything was successful.
  74. return true;
  75. }
  76. void WholeFileTest::runWholeFileTest(llvm::StringRef filename,
  77. bool generateHeader,
  78. bool runSpirvValidation) {
  79. inputFilePath = utils::getAbsPathOfInputDataFile(filename);
  80. // Parse the input file.
  81. ASSERT_TRUE(parseInputFile());
  82. std::string errorMessages;
  83. // Feed the HLSL source into the Compiler.
  84. ASSERT_TRUE(utils::runCompilerWithSpirvGeneration(
  85. inputFilePath, entryPoint, targetProfile, restArgs, &generatedBinary,
  86. &errorMessages));
  87. // Disassemble the generated SPIR-V binary.
  88. ASSERT_TRUE(utils::disassembleSpirvBinary(generatedBinary, &generatedSpirvAsm,
  89. generateHeader));
  90. // Compare the expected and the generted SPIR-V code.
  91. EXPECT_EQ(expectedSpirvAsm, generatedSpirvAsm);
  92. // Run SPIR-V validation if requested.
  93. if (runSpirvValidation) {
  94. EXPECT_TRUE(utils::validateSpirvBinary(SPV_ENV_VULKAN_1_0, generatedBinary,
  95. /*relaxLogicalPointer=*/false));
  96. }
  97. }
  98. } // end namespace spirv
  99. } // end namespace clang