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 // Tests for unique type declaration rules validator.
16
17 #include <sstream>
18 #include <string>
19
20 #include "source/latest_version_spirv_header.h"
21 #include "test/test_fixture.h"
22 #include "tools/stats/stats_analyzer.h"
23
24 namespace spvtools {
25 namespace stats {
26 namespace {
27
28 // Fills |stats| with some synthetic header stats, as if aggregated from 100
29 // modules (100 used for simpler percentage evaluation).
FillDefaultStats(SpirvStats * stats)30 void FillDefaultStats(SpirvStats* stats) {
31 *stats = SpirvStats();
32 stats->version_hist[0x00010000] = 40;
33 stats->version_hist[0x00010100] = 60;
34 stats->generator_hist[0x00000000] = 64;
35 stats->generator_hist[0x00010000] = 1;
36 stats->generator_hist[0x00020000] = 2;
37 stats->generator_hist[0x00030000] = 3;
38 stats->generator_hist[0x00040000] = 4;
39 stats->generator_hist[0x00050000] = 5;
40 stats->generator_hist[0x00060000] = 6;
41 stats->generator_hist[0x00070000] = 7;
42 stats->generator_hist[0x00080000] = 8;
43
44 int num_version_entries = 0;
45 for (const auto& pair : stats->version_hist) {
46 num_version_entries += pair.second;
47 }
48
49 int num_generator_entries = 0;
50 for (const auto& pair : stats->generator_hist) {
51 num_generator_entries += pair.second;
52 }
53
54 EXPECT_EQ(num_version_entries, num_generator_entries);
55 }
56
TEST(StatsAnalyzer,Version)57 TEST(StatsAnalyzer, Version) {
58 SpirvStats stats;
59 FillDefaultStats(&stats);
60
61 StatsAnalyzer analyzer(stats);
62
63 std::stringstream ss;
64 analyzer.WriteVersion(ss);
65 const std::string output = ss.str();
66 const std::string expected_output = "Version 1.1 60%\nVersion 1.0 40%\n";
67
68 EXPECT_EQ(expected_output, output);
69 }
70
TEST(StatsAnalyzer,Generator)71 TEST(StatsAnalyzer, Generator) {
72 SpirvStats stats;
73 FillDefaultStats(&stats);
74
75 StatsAnalyzer analyzer(stats);
76
77 std::stringstream ss;
78 analyzer.WriteGenerator(ss);
79 const std::string output = ss.str();
80 const std::string expected_output =
81 "Khronos 64%\nKhronos Glslang Reference Front End 8%\n"
82 "Khronos SPIR-V Tools Assembler 7%\nKhronos LLVM/SPIR-V Translator 6%"
83 "\nARM 5%\nNVIDIA 4%\nCodeplay 3%\nValve 2%\nLunarG 1%\n";
84
85 EXPECT_EQ(expected_output, output);
86 }
87
TEST(StatsAnalyzer,Capability)88 TEST(StatsAnalyzer, Capability) {
89 SpirvStats stats;
90 FillDefaultStats(&stats);
91
92 stats.capability_hist[SpvCapabilityShader] = 25;
93 stats.capability_hist[SpvCapabilityKernel] = 75;
94
95 StatsAnalyzer analyzer(stats);
96
97 std::stringstream ss;
98 analyzer.WriteCapability(ss);
99 const std::string output = ss.str();
100 const std::string expected_output = "Kernel 75%\nShader 25%\n";
101
102 EXPECT_EQ(expected_output, output);
103 }
104
TEST(StatsAnalyzer,Extension)105 TEST(StatsAnalyzer, Extension) {
106 SpirvStats stats;
107 FillDefaultStats(&stats);
108
109 stats.extension_hist["greatest_extension_ever"] = 1;
110 stats.extension_hist["worst_extension_ever"] = 10;
111
112 StatsAnalyzer analyzer(stats);
113
114 std::stringstream ss;
115 analyzer.WriteExtension(ss);
116 const std::string output = ss.str();
117 const std::string expected_output =
118 "worst_extension_ever 10%\ngreatest_extension_ever 1%\n";
119
120 EXPECT_EQ(expected_output, output);
121 }
122
TEST(StatsAnalyzer,Opcode)123 TEST(StatsAnalyzer, Opcode) {
124 SpirvStats stats;
125 FillDefaultStats(&stats);
126
127 stats.opcode_hist[SpvOpCapability] = 20;
128 stats.opcode_hist[SpvOpConstant] = 80;
129 stats.opcode_hist[SpvOpDecorate] = 100;
130
131 StatsAnalyzer analyzer(stats);
132
133 std::stringstream ss;
134 analyzer.WriteOpcode(ss);
135 const std::string output = ss.str();
136 const std::string expected_output =
137 "Total unique opcodes used: 3\nDecorate 50%\n"
138 "Constant 40%\nCapability 10%\n";
139
140 EXPECT_EQ(expected_output, output);
141 }
142
TEST(StatsAnalyzer,OpcodeMarkov)143 TEST(StatsAnalyzer, OpcodeMarkov) {
144 SpirvStats stats;
145 FillDefaultStats(&stats);
146
147 stats.opcode_hist[SpvOpFMul] = 400;
148 stats.opcode_hist[SpvOpFAdd] = 200;
149 stats.opcode_hist[SpvOpFSub] = 400;
150
151 stats.opcode_markov_hist.resize(1);
152 auto& hist = stats.opcode_markov_hist[0];
153 hist[SpvOpFMul][SpvOpFAdd] = 100;
154 hist[SpvOpFMul][SpvOpFSub] = 300;
155 hist[SpvOpFAdd][SpvOpFMul] = 100;
156 hist[SpvOpFAdd][SpvOpFAdd] = 100;
157
158 StatsAnalyzer analyzer(stats);
159
160 std::stringstream ss;
161 analyzer.WriteOpcodeMarkov(ss);
162 const std::string output = ss.str();
163 const std::string expected_output =
164 "FMul -> FSub 75% (base rate 40%, pair occurrences 300)\n"
165 "FMul -> FAdd 25% (base rate 20%, pair occurrences 100)\n"
166 "FAdd -> FAdd 50% (base rate 20%, pair occurrences 100)\n"
167 "FAdd -> FMul 50% (base rate 40%, pair occurrences 100)\n";
168
169 EXPECT_EQ(expected_output, output);
170 }
171
172 } // namespace
173 } // namespace stats
174 } // namespace spvtools
175