diff.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (c) 2022 The Khronos Group Inc.
  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. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  15. #include <unistd.h>
  16. #endif
  17. #include "source/diff/diff.h"
  18. #include "source/opt/build_module.h"
  19. #include "source/opt/ir_context.h"
  20. #include "spirv-tools/libspirv.hpp"
  21. #include "tools/io.h"
  22. #include "tools/util/cli_consumer.h"
  23. static void print_usage(char* argv0) {
  24. printf(R"(%s - Compare two SPIR-V files
  25. Usage: %s <src_filename> <dst_filename>
  26. The SPIR-V binary is read from <src_filename> and <dst_filename>. If either
  27. file ends in .spvasm, the SPIR-V is read as text and disassembled.
  28. The contents of the SPIR-V modules are analyzed and a diff is produced showing a
  29. logical transformation from src to dst, in src's id-space.
  30. -h, --help Print this help.
  31. --version Display diff version information.
  32. --color Force color output. The default when printing to a terminal.
  33. Overrides a previous --no-color option.
  34. --no-color Don't print in color. Overrides a previous --color option.
  35. The default when output goes to something other than a
  36. terminal (e.g. a pipe, or a shell redirection).
  37. --no-indent Don't indent instructions.
  38. --no-header Don't output the header as leading comments.
  39. --with-id-map Also output the mapping between src and dst outputs.
  40. --ignore-set-binding
  41. Don't use set/binding decorations for variable matching.
  42. --ignore-location
  43. Don't use location decorations for variable matching.
  44. )",
  45. argv0, argv0);
  46. }
  47. static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;
  48. static bool is_assembly(const char* path) {
  49. const char* suffix = strrchr(path, '.');
  50. if (suffix == nullptr) {
  51. return false;
  52. }
  53. return strcmp(suffix, ".spvasm") == 0;
  54. }
  55. static std::unique_ptr<spvtools::opt::IRContext> load_module(const char* path) {
  56. if (is_assembly(path)) {
  57. std::vector<char> contents;
  58. if (!ReadTextFile<char>(path, &contents)) return {};
  59. return spvtools::BuildModule(
  60. kDefaultEnvironment, spvtools::utils::CLIMessageConsumer,
  61. std::string(contents.begin(), contents.end()),
  62. spvtools::SpirvTools::kDefaultAssembleOption |
  63. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  64. }
  65. std::vector<uint32_t> contents;
  66. if (!ReadBinaryFile<uint32_t>(path, &contents)) return {};
  67. return spvtools::BuildModule(kDefaultEnvironment,
  68. spvtools::utils::CLIMessageConsumer,
  69. contents.data(), contents.size());
  70. }
  71. int main(int argc, char** argv) {
  72. const char* src_file = nullptr;
  73. const char* dst_file = nullptr;
  74. bool color_is_possible =
  75. #if SPIRV_COLOR_TERMINAL
  76. true;
  77. #else
  78. false;
  79. #endif
  80. bool force_color = false;
  81. bool force_no_color = false;
  82. bool allow_indent = true;
  83. bool no_header = false;
  84. bool dump_id_map = false;
  85. bool ignore_set_binding = false;
  86. bool ignore_location = false;
  87. for (int argi = 1; argi < argc; ++argi) {
  88. if ('-' == argv[argi][0]) {
  89. switch (argv[argi][1]) {
  90. case 'h':
  91. print_usage(argv[0]);
  92. return 0;
  93. case '-': {
  94. // Long options
  95. if (strcmp(argv[argi], "--no-color") == 0) {
  96. force_no_color = true;
  97. force_color = false;
  98. } else if (strcmp(argv[argi], "--color") == 0) {
  99. force_no_color = false;
  100. force_color = true;
  101. } else if (strcmp(argv[argi], "--no-indent") == 0) {
  102. allow_indent = false;
  103. } else if (strcmp(argv[argi], "--no-header") == 0) {
  104. no_header = true;
  105. } else if (strcmp(argv[argi], "--with-id-map") == 0) {
  106. dump_id_map = true;
  107. } else if (strcmp(argv[argi], "--ignore-set-binding") == 0) {
  108. ignore_set_binding = true;
  109. } else if (strcmp(argv[argi], "--ignore-location") == 0) {
  110. ignore_location = true;
  111. } else if (strcmp(argv[argi], "--help") == 0) {
  112. print_usage(argv[0]);
  113. return 0;
  114. } else if (strcmp(argv[argi], "--version") == 0) {
  115. printf("%s\n", spvSoftwareVersionDetailsString());
  116. printf("Target: %s\n",
  117. spvTargetEnvDescription(kDefaultEnvironment));
  118. return 0;
  119. } else {
  120. print_usage(argv[0]);
  121. return 1;
  122. }
  123. } break;
  124. default:
  125. print_usage(argv[0]);
  126. return 1;
  127. }
  128. } else {
  129. if (src_file == nullptr) {
  130. src_file = argv[argi];
  131. } else if (dst_file == nullptr) {
  132. dst_file = argv[argi];
  133. } else {
  134. fprintf(stderr, "error: More than two input files specified\n");
  135. return 1;
  136. }
  137. }
  138. }
  139. if (src_file == nullptr || dst_file == nullptr) {
  140. print_usage(argv[0]);
  141. return 1;
  142. }
  143. spvtools::diff::Options options;
  144. if (allow_indent) options.indent = true;
  145. if (no_header) options.no_header = true;
  146. if (dump_id_map) options.dump_id_map = true;
  147. if (ignore_set_binding) options.ignore_set_binding = true;
  148. if (ignore_location) options.ignore_location = true;
  149. if (color_is_possible && !force_no_color) {
  150. bool output_is_tty = true;
  151. #if defined(_POSIX_VERSION)
  152. output_is_tty = isatty(fileno(stdout));
  153. #endif
  154. if (output_is_tty || force_color) {
  155. options.color_output = true;
  156. }
  157. }
  158. std::unique_ptr<spvtools::opt::IRContext> src = load_module(src_file);
  159. std::unique_ptr<spvtools::opt::IRContext> dst = load_module(dst_file);
  160. if (!src) {
  161. fprintf(stderr, "error: Loading src file\n");
  162. }
  163. if (!dst) {
  164. fprintf(stderr, "error: Loading dst file\n");
  165. }
  166. if (!src || !dst) {
  167. return 1;
  168. }
  169. spvtools::diff::Diff(src.get(), dst.get(), std::cout, options);
  170. return 0;
  171. }