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