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