reduce_test_util.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "test/reduce/reduce_test_util.h"
  15. #include <iostream>
  16. #include "tools/io.h"
  17. namespace spvtools {
  18. namespace reduce {
  19. void CheckEqual(const spv_target_env env,
  20. const std::vector<uint32_t>& expected_binary,
  21. const std::vector<uint32_t>& actual_binary) {
  22. if (expected_binary != actual_binary) {
  23. SpirvTools t(env);
  24. std::string expected_disassembled;
  25. std::string actual_disassembled;
  26. ASSERT_TRUE(t.Disassemble(expected_binary, &expected_disassembled,
  27. kReduceDisassembleOption));
  28. ASSERT_TRUE(t.Disassemble(actual_binary, &actual_disassembled,
  29. kReduceDisassembleOption));
  30. ASSERT_EQ(expected_disassembled, actual_disassembled);
  31. }
  32. }
  33. void CheckEqual(const spv_target_env env, const std::string& expected_text,
  34. const std::vector<uint32_t>& actual_binary) {
  35. std::vector<uint32_t> expected_binary;
  36. SpirvTools t(env);
  37. ASSERT_TRUE(
  38. t.Assemble(expected_text, &expected_binary, kReduceAssembleOption));
  39. CheckEqual(env, expected_binary, actual_binary);
  40. }
  41. void CheckEqual(const spv_target_env env, const std::string& expected_text,
  42. const opt::IRContext* actual_ir) {
  43. std::vector<uint32_t> actual_binary;
  44. actual_ir->module()->ToBinary(&actual_binary, false);
  45. CheckEqual(env, expected_text, actual_binary);
  46. }
  47. void CheckValid(spv_target_env env, const opt::IRContext* ir) {
  48. std::vector<uint32_t> binary;
  49. ir->module()->ToBinary(&binary, false);
  50. SpirvTools t(env);
  51. ASSERT_TRUE(t.Validate(binary));
  52. }
  53. std::string ToString(spv_target_env env, const opt::IRContext* ir) {
  54. std::vector<uint32_t> binary;
  55. ir->module()->ToBinary(&binary, false);
  56. SpirvTools t(env);
  57. std::string result;
  58. t.Disassemble(binary, &result, kReduceDisassembleOption);
  59. return result;
  60. }
  61. void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/,
  62. const spv_position_t& /*position*/,
  63. const char* /*message*/) {}
  64. void CLIMessageConsumer(spv_message_level_t level, const char*,
  65. const spv_position_t& position, const char* message) {
  66. switch (level) {
  67. case SPV_MSG_FATAL:
  68. case SPV_MSG_INTERNAL_ERROR:
  69. case SPV_MSG_ERROR:
  70. std::cerr << "error: line " << position.index << ": " << message
  71. << std::endl;
  72. break;
  73. case SPV_MSG_WARNING:
  74. std::cout << "warning: line " << position.index << ": " << message
  75. << std::endl;
  76. break;
  77. case SPV_MSG_INFO:
  78. std::cout << "info: line " << position.index << ": " << message
  79. << std::endl;
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. void DumpShader(opt::IRContext* context, const char* filename) {
  86. std::vector<uint32_t> binary;
  87. context->module()->ToBinary(&binary, false);
  88. DumpShader(binary, filename);
  89. }
  90. void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
  91. auto write_file_succeeded =
  92. WriteFile(filename, "wb", &binary[0], binary.size());
  93. if (!write_file_succeeded) {
  94. std::cerr << "Failed to dump shader" << std::endl;
  95. }
  96. }
  97. } // namespace reduce
  98. } // namespace spvtools