1 // Copyright (c) 2017 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 <cassert>
16 #include <cstring>
17 #include <fstream>
18 #include <iostream>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "spirv-tools/libspirv.h"
23 #include "tools/io.h"
24 #include "tools/stats/spirv_stats.h"
25 #include "tools/stats/stats_analyzer.h"
26 
27 namespace {
28 
PrintUsage(char * argv0)29 void PrintUsage(char* argv0) {
30   printf(
31       R"(%s - Collect statistics from one or more SPIR-V binary file(s).
32 
33 USAGE: %s [options] [<filepaths>]
34 
35 TIP: In order to collect statistics from all .spv files under current dir use
36 find . -name "*.spv" -print0 | xargs -0 -s 2000000 %s
37 
38 Options:
39   -h, --help
40                    Print this help.
41 
42   -v, --verbose
43                    Print additional info to stderr.
44 )",
45       argv0, argv0, argv0);
46 }
47 
DiagnosticsMessageHandler(spv_message_level_t level,const char *,const spv_position_t & position,const char * message)48 void DiagnosticsMessageHandler(spv_message_level_t level, const char*,
49                                const spv_position_t& position,
50                                const char* message) {
51   switch (level) {
52     case SPV_MSG_FATAL:
53     case SPV_MSG_INTERNAL_ERROR:
54     case SPV_MSG_ERROR:
55       std::cerr << "error: " << position.index << ": " << message << std::endl;
56       break;
57     case SPV_MSG_WARNING:
58       std::cout << "warning: " << position.index << ": " << message
59                 << std::endl;
60       break;
61     case SPV_MSG_INFO:
62       std::cout << "info: " << position.index << ": " << message << std::endl;
63       break;
64     default:
65       break;
66   }
67 }
68 
69 }  // namespace
70 
main(int argc,char ** argv)71 int main(int argc, char** argv) {
72   bool continue_processing = true;
73   int return_code = 0;
74 
75   bool expect_output_path = false;
76   bool verbose = false;
77 
78   std::vector<const char*> paths;
79   const char* output_path = nullptr;
80 
81   for (int argi = 1; continue_processing && argi < argc; ++argi) {
82     const char* cur_arg = argv[argi];
83     if ('-' == cur_arg[0]) {
84       if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
85         PrintUsage(argv[0]);
86         continue_processing = false;
87         return_code = 0;
88       } else if (0 == strcmp(cur_arg, "--verbose") ||
89                  0 == strcmp(cur_arg, "-v")) {
90         verbose = true;
91       } else if (0 == strcmp(cur_arg, "--output") ||
92                  0 == strcmp(cur_arg, "-o")) {
93         expect_output_path = true;
94       } else {
95         PrintUsage(argv[0]);
96         continue_processing = false;
97         return_code = 1;
98       }
99     } else {
100       if (expect_output_path) {
101         output_path = cur_arg;
102         expect_output_path = false;
103       } else {
104         paths.push_back(cur_arg);
105       }
106     }
107   }
108 
109   // Exit if command line parsing was not successful.
110   if (!continue_processing) {
111     return return_code;
112   }
113 
114   std::cerr << "Processing " << paths.size() << " files..." << std::endl;
115 
116   spvtools::Context ctx(SPV_ENV_UNIVERSAL_1_1);
117   ctx.SetMessageConsumer(DiagnosticsMessageHandler);
118 
119   spvtools::stats::SpirvStats stats;
120   stats.opcode_markov_hist.resize(1);
121 
122   for (size_t index = 0; index < paths.size(); ++index) {
123     const size_t kMilestonePeriod = 1000;
124     if (verbose) {
125       if (index % kMilestonePeriod == kMilestonePeriod - 1)
126         std::cerr << "Processed " << index + 1 << " files..." << std::endl;
127     }
128 
129     const char* path = paths[index];
130     std::vector<uint32_t> contents;
131     if (!ReadFile<uint32_t>(path, "rb", &contents)) return 1;
132 
133     if (SPV_SUCCESS !=
134         spvtools::stats::AggregateStats(ctx.CContext(), contents.data(),
135                                         contents.size(), nullptr, &stats)) {
136       std::cerr << "error: Failed to aggregate stats for " << path << std::endl;
137       return 1;
138     }
139   }
140 
141   spvtools::stats::StatsAnalyzer analyzer(stats);
142 
143   std::ofstream fout;
144   if (output_path) {
145     fout.open(output_path);
146     if (!fout.is_open()) {
147       std::cerr << "error: Failed to open " << output_path << std::endl;
148       return 1;
149     }
150   }
151 
152   std::ostream& out = fout.is_open() ? fout : std::cout;
153   out << std::endl;
154   analyzer.WriteVersion(out);
155   analyzer.WriteGenerator(out);
156 
157   out << std::endl;
158   analyzer.WriteCapability(out);
159 
160   out << std::endl;
161   analyzer.WriteExtension(out);
162 
163   out << std::endl;
164   analyzer.WriteOpcode(out);
165 
166   out << std::endl;
167   analyzer.WriteOpcodeMarkov(out);
168 
169   out << std::endl;
170   analyzer.WriteConstantLiterals(out);
171 
172   return 0;
173 }
174