• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 Google 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 #include <cstring>
16 #include <algorithm>
17 #include <iostream>
18 #include <memory>
19 #include <sstream>
20 #include <vector>
21 
22 #include "opt/set_spec_constant_default_value_pass.h"
23 #include "spirv-tools/optimizer.hpp"
24 
25 #include "message.h"
26 #include "tools/io.h"
27 
28 using namespace spvtools;
29 
PrintUsage(const char * program)30 void PrintUsage(const char* program) {
31   printf(
32       R"(%s - Optimize a SPIR-V binary file.
33 
34 USAGE: %s [options] [<input>] -o <output>
35 
36 The SPIR-V binary is read from <input>. If no file is specified,
37 or if <input> is "-", then the binary is read from standard input.
38 if <output> is "-", then the optimized output is written to
39 standard output.
40 
41 NOTE: The optimizer is a work in progress.
42 
43 Options:
44   --strip-debug
45                Remove all debug instructions.
46   --freeze-spec-const
47                Freeze the values of specialization constants to their default
48                values.
49   --eliminate-dead-const
50                Eliminate dead constants.
51   --fold-spec-const-op-composite
52                Fold the spec constants defined by OpSpecConstantOp or
53                OpSpecConstantComposite instructions to front-end constants
54                when possible.
55   --set-spec-const-default-value "<spec id>:<default value> ..."
56                Set the default values of the specialization constants with
57                <spec id>:<default value> pairs specified in a double-quoted
58                string. <spec id>:<default value> pairs must be separated by
59                blank spaces, and in each pair, spec id and default value must
60                be separated with colon ':' without any blank spaces in between.
61                e.g.: --set-spec-const-default-value "1:100 2:400"
62   --unify-const
63                Remove the duplicated constants.
64   --inline-entry-points-exhaustive
65                Exhaustively inline all function calls in entry point functions.
66                Currently does not inline calls to functions with multiple
67                returns.
68   --flatten-decorations
69                Replace decoration groups with repeated OpDecorate and
70                OpMemberDecorate instructions.
71   --compact-ids
72                Remap result ids to a compact range starting from %%1 and without
73                any gaps.
74   -h, --help   Print this help.
75   --version    Display optimizer version information.
76 )",
77       program, program);
78 }
79 
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81   const char* in_file = nullptr;
82   const char* out_file = nullptr;
83 
84   spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
85 
86   spvtools::Optimizer optimizer(target_env);
87   optimizer.SetMessageConsumer([](spv_message_level_t level, const char* source,
88                                   const spv_position_t& position,
89                                   const char* message) {
90     std::cerr << StringifyMessage(level, source, position, message)
91               << std::endl;
92   });
93 
94   for (int argi = 1; argi < argc; ++argi) {
95     const char* cur_arg = argv[argi];
96     if ('-' == cur_arg[0]) {
97       if (0 == strcmp(cur_arg, "--version")) {
98         printf("%s\n", spvSoftwareVersionDetailsString());
99         return 0;
100       } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
101         PrintUsage(argv[0]);
102         return 0;
103       } else if (0 == strcmp(cur_arg, "-o")) {
104         if (!out_file && argi + 1 < argc) {
105           out_file = argv[++argi];
106         } else {
107           PrintUsage(argv[0]);
108           return 1;
109         }
110       } else if (0 == strcmp(cur_arg, "--strip-debug")) {
111         optimizer.RegisterPass(CreateStripDebugInfoPass());
112       } else if (0 == strcmp(cur_arg, "--set-spec-const-default-value")) {
113         if (++argi < argc) {
114           auto spec_ids_vals =
115               opt::SetSpecConstantDefaultValuePass::ParseDefaultValuesString(
116                   argv[argi]);
117           if (!spec_ids_vals) {
118             fprintf(stderr,
119                     "error: Invalid argument for "
120                     "--set-spec-const-default-value: %s\n",
121                     argv[argi]);
122             return 1;
123           }
124           optimizer.RegisterPass(
125               CreateSetSpecConstantDefaultValuePass(std::move(*spec_ids_vals)));
126         } else {
127           fprintf(
128               stderr,
129               "error: Expected a string of <spec id>:<default value> pairs.");
130           return 1;
131         }
132       } else if (0 == strcmp(cur_arg, "--freeze-spec-const")) {
133         optimizer.RegisterPass(CreateFreezeSpecConstantValuePass());
134       } else if (0 == strcmp(cur_arg, "--inline-entry-points-exhaustive")) {
135         optimizer.RegisterPass(CreateInlinePass());
136       } else if (0 == strcmp(cur_arg, "--convert-local-access-chains")) {
137         optimizer.RegisterPass(CreateLocalAccessChainConvertPass());
138       } else if (0 == strcmp(cur_arg, "--eliminate-dead-code-aggressive")) {
139         optimizer.RegisterPass(CreateAggressiveDCEPass());
140       } else if (0 == strcmp(cur_arg, "--eliminate-insert-extract")) {
141         optimizer.RegisterPass(CreateInsertExtractElimPass());
142       } else if (0 == strcmp(cur_arg, "--eliminate-local-single-block")) {
143         optimizer.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass());
144       } else if (0 == strcmp(cur_arg, "--eliminate-local-single-store")) {
145         optimizer.RegisterPass(CreateLocalSingleStoreElimPass());
146       } else if (0 == strcmp(cur_arg, "--merge-blocks")) {
147         optimizer.RegisterPass(CreateBlockMergePass());
148       } else if (0 == strcmp(cur_arg, "--eliminate-dead-branches")) {
149         optimizer.RegisterPass(CreateDeadBranchElimPass());
150       } else if (0 == strcmp(cur_arg, "--eliminate-local-multi-store")) {
151         optimizer.RegisterPass(CreateLocalMultiStoreElimPass());
152       } else if (0 == strcmp(cur_arg, "--eliminate-dead-const")) {
153         optimizer.RegisterPass(CreateEliminateDeadConstantPass());
154       } else if (0 == strcmp(cur_arg, "--fold-spec-const-op-composite")) {
155         optimizer.RegisterPass(CreateFoldSpecConstantOpAndCompositePass());
156       } else if (0 == strcmp(cur_arg, "--unify-const")) {
157         optimizer.RegisterPass(CreateUnifyConstantPass());
158       } else if (0 == strcmp(cur_arg, "--flatten-decorations")) {
159         optimizer.RegisterPass(CreateFlattenDecorationPass());
160       } else if (0 == strcmp(cur_arg, "--compact-ids")) {
161         optimizer.RegisterPass(CreateCompactIdsPass());
162       } else if ('\0' == cur_arg[1]) {
163         // Setting a filename of "-" to indicate stdin.
164         if (!in_file) {
165           in_file = cur_arg;
166         } else {
167           fprintf(stderr, "error: More than one input file specified\n");
168           return 1;
169         }
170       } else {
171         PrintUsage(argv[0]);
172         return 1;
173       }
174     } else {
175       if (!in_file) {
176         in_file = cur_arg;
177       } else {
178         fprintf(stderr, "error: More than one input file specified\n");
179         return 1;
180       }
181     }
182   }
183 
184   if (out_file == nullptr) {
185     fprintf(stderr, "error: -o required\n");
186     return 1;
187   }
188 
189   std::vector<uint32_t> binary;
190   if (!ReadFile<uint32_t>(in_file, "rb", &binary)) return 1;
191 
192   // Let's do validation first.
193   spv_context context = spvContextCreate(target_env);
194   spv_diagnostic diagnostic = nullptr;
195   spv_const_binary_t binary_struct = {binary.data(), binary.size()};
196   spv_result_t error = spvValidate(context, &binary_struct, &diagnostic);
197   if (error) {
198     spvDiagnosticPrint(diagnostic);
199     spvDiagnosticDestroy(diagnostic);
200     spvContextDestroy(context);
201     return error;
202   }
203   spvDiagnosticDestroy(diagnostic);
204   spvContextDestroy(context);
205 
206   // By using the same vector as input and output, we save time in the case
207   // that there was no change.
208   bool ok = optimizer.Run(binary.data(), binary.size(), &binary);
209 
210   if (!WriteFile<uint32_t>(out_file, "wb", binary.data(), binary.size())) {
211     return 1;
212   }
213 
214   return ok ? 0 : 1;
215 }
216