reduce_test_util.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. const spvtools::MessageConsumer kConsoleMessageConsumer =
  20. [](spv_message_level_t level, const char*, const spv_position_t& position,
  21. const char* message) -> void {
  22. switch (level) {
  23. case SPV_MSG_FATAL:
  24. case SPV_MSG_INTERNAL_ERROR:
  25. case SPV_MSG_ERROR:
  26. std::cerr << "error: line " << position.index << ": " << message
  27. << std::endl;
  28. break;
  29. case SPV_MSG_WARNING:
  30. std::cout << "warning: line " << position.index << ": " << message
  31. << std::endl;
  32. break;
  33. case SPV_MSG_INFO:
  34. std::cout << "info: line " << position.index << ": " << message
  35. << std::endl;
  36. break;
  37. default:
  38. break;
  39. }
  40. };
  41. void CheckEqual(const spv_target_env env,
  42. const std::vector<uint32_t>& expected_binary,
  43. const std::vector<uint32_t>& actual_binary) {
  44. if (expected_binary != actual_binary) {
  45. SpirvTools t(env);
  46. std::string expected_disassembled;
  47. std::string actual_disassembled;
  48. ASSERT_TRUE(t.Disassemble(expected_binary, &expected_disassembled,
  49. kReduceDisassembleOption));
  50. ASSERT_TRUE(t.Disassemble(actual_binary, &actual_disassembled,
  51. kReduceDisassembleOption));
  52. ASSERT_EQ(expected_disassembled, actual_disassembled);
  53. }
  54. }
  55. void CheckEqual(const spv_target_env env, const std::string& expected_text,
  56. const std::vector<uint32_t>& actual_binary) {
  57. std::vector<uint32_t> expected_binary;
  58. SpirvTools t(env);
  59. ASSERT_TRUE(
  60. t.Assemble(expected_text, &expected_binary, kReduceAssembleOption));
  61. CheckEqual(env, expected_binary, actual_binary);
  62. }
  63. void CheckEqual(const spv_target_env env, const std::string& expected_text,
  64. const opt::IRContext* actual_ir) {
  65. std::vector<uint32_t> actual_binary;
  66. actual_ir->module()->ToBinary(&actual_binary, false);
  67. CheckEqual(env, expected_text, actual_binary);
  68. }
  69. void CheckValid(spv_target_env env, const opt::IRContext* ir) {
  70. std::vector<uint32_t> binary;
  71. ir->module()->ToBinary(&binary, false);
  72. SpirvTools tools(env);
  73. tools.SetMessageConsumer(kConsoleMessageConsumer);
  74. ASSERT_TRUE(tools.Validate(binary));
  75. }
  76. std::string ToString(spv_target_env env, const opt::IRContext* ir) {
  77. std::vector<uint32_t> binary;
  78. ir->module()->ToBinary(&binary, false);
  79. SpirvTools t(env);
  80. std::string result;
  81. t.Disassemble(binary, &result, kReduceDisassembleOption);
  82. return result;
  83. }
  84. void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/,
  85. const spv_position_t& /*position*/,
  86. const char* /*message*/) {}
  87. void CLIMessageConsumer(spv_message_level_t level, const char*,
  88. const spv_position_t& position, const char* message) {
  89. switch (level) {
  90. case SPV_MSG_FATAL:
  91. case SPV_MSG_INTERNAL_ERROR:
  92. case SPV_MSG_ERROR:
  93. std::cerr << "error: line " << position.index << ": " << message
  94. << std::endl;
  95. break;
  96. case SPV_MSG_WARNING:
  97. std::cout << "warning: line " << position.index << ": " << message
  98. << std::endl;
  99. break;
  100. case SPV_MSG_INFO:
  101. std::cout << "info: line " << position.index << ": " << message
  102. << std::endl;
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. void DumpShader(opt::IRContext* context, const char* filename) {
  109. std::vector<uint32_t> binary;
  110. context->module()->ToBinary(&binary, false);
  111. DumpShader(binary, filename);
  112. }
  113. void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
  114. auto write_file_succeeded =
  115. WriteFile(filename, "wb", &binary[0], binary.size());
  116. if (!write_file_succeeded) {
  117. std::cerr << "Failed to dump shader" << std::endl;
  118. }
  119. }
  120. } // namespace reduce
  121. } // namespace spvtools