Debian Codegraph @

tools/diff/diff.cpp

raw
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
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
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
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(path, &contents)) return {};
86
87 return spvtools::BuildModule(
88 kDefaultEnvironment, spvtools::utils::CLIMessageConsumer,
89 std::string(contents.begin(), contents.end()),
90 static_cast<spv_text_to_binary_options_t>(
91 spvtools::SpirvTools::kDefaultAssembleOption) |
92 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
93 }
94
95 std::vector<uint32_t> contents;
96 if (!ReadBinaryFile(path, &contents)) return {};
97
98 return spvtools::BuildModule(kDefaultEnvironment,
99 spvtools::utils::CLIMessageConsumer,
100 contents.data(), contents.size());
101 }
102
103 } // namespace
104
105 // clang-format off
106 FLAG_SHORT_bool(h, /* default_value= */ false, /* required= */ false);
107 FLAG_LONG_bool( help, /* default_value= */ false, /* required= */false);
108 FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
109 FLAG_LONG_bool( color, /* default_value= */ false, /* required= */ false);
110 FLAG_LONG_bool( no_color, /* default_value= */ false, /* required= */ false);
111 FLAG_LONG_bool( no_indent, /* default_value= */ false, /* required= */ false);
112 FLAG_LONG_bool( no_header, /* default_value= */ false, /* required= */ false);
113 FLAG_LONG_bool( with_id_map, /* default_value= */ false, /* required= */ false);
114 FLAG_LONG_bool( ignore_set_binding, /* default_value= */ false, /* required= */ false);
115 FLAG_LONG_bool( ignore_location, /* default_value= */ false, /* required= */ false);
116 // clang-format on
117
118 int main(int, const char* argv[]) {
119 if (!flags::Parse(argv)) {
120 return 1;
121 }
122
123 if (flags::h.value() || flags::help.value()) {
124 print_usage(argv[0]);
125 return 0;
126 }
127
128 if (flags::version.value()) {
129 printf("%s\n", spvSoftwareVersionDetailsString());
130 printf("Target: %s\n", spvTargetEnvDescription(kDefaultEnvironment));
131 return 0;
132 }
133
134 if (flags::positional_arguments.size() != 2) {
135 fprintf(stderr, "error: two input files required.\n");
136 return 1;
137 }
138
139 #if defined(_POSIX_VERSION)
140 const bool output_is_tty = isatty(fileno(stdout));
141 #else
142 const bool output_is_tty = true;
143 #endif
144
145 const std::string& src_file = flags::positional_arguments[0];
146 const std::string& dst_file = flags::positional_arguments[1];
147
148 spvtools::diff::Options options;
149 options.color_output = (output_is_tty || flags::color.value()) &&
150 !flags::no_color.value() && kColorIsPossible;
151 options.indent = !flags::no_indent.value();
152 options.no_header = flags::no_header.value();
153 options.dump_id_map = flags::with_id_map.value();
154 options.ignore_set_binding = flags::ignore_set_binding.value();
155 options.ignore_location = flags::ignore_location.value();
156
157 std::unique_ptr<spvtools::opt::IRContext> src = load_module(src_file.c_str());
158 std::unique_ptr<spvtools::opt::IRContext> dst = load_module(dst_file.c_str());
159
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
170 spvtools::diff::Diff(src.get(), dst.get(), std::cout, options);
171
172 return 0;
173 }