Debian Codegraph @

third_party/spirv-tools/test/fuzz/transformation_add_constant_scalar_test.cpp

raw
1 // Copyright (c) 2019 Google LLC
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 "source/fuzz/transformation_add_constant_scalar.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "test/fuzz/fuzz_test_util.h"
20
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24
25 TEST(TransformationAddConstantScalarTest, IsApplicable) {
26 std::string reference_shader = R"(
27 OpCapability Shader
28 OpCapability Int64
29 OpCapability Float64
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Vertex %17 "main"
33
34 ; Types
35
36 ; 32-bit types
37 %2 = OpTypeInt 32 0
38 %3 = OpTypeInt 32 1
39 %4 = OpTypeFloat 32
40
41 ; 64-bit types
42 %5 = OpTypeInt 64 0
43 %6 = OpTypeInt 64 1
44 %7 = OpTypeFloat 64
45
46 %8 = OpTypePointer Private %2
47 %9 = OpTypeVoid
48 %10 = OpTypeFunction %9
49
50 ; Constants
51
52 ; 32-bit constants
53 %11 = OpConstant %2 1
54 %12 = OpConstant %3 2
55 %13 = OpConstant %4 3
56
57 ; 64-bit constants
58 %14 = OpConstant %5 1
59 %15 = OpConstant %6 2
60 %16 = OpConstant %7 3
61
62 ; main function
63 %17 = OpFunction %9 None %10
64 %18 = OpLabel
65 OpReturn
66 OpFunctionEnd
67 )";
68
69 const auto env = SPV_ENV_UNIVERSAL_1_3;
70 const auto consumer = nullptr;
71 const auto context =
72 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
73 spvtools::ValidatorOptions validator_options;
74 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
75 kConsoleMessageConsumer));
76 TransformationContext transformation_context(
77 MakeUnique<FactManager>(context.get()), validator_options);
78 // Tests |fresh_id| being non-fresh.
79 auto transformation = TransformationAddConstantScalar(18, 2, {0}, false);
80 ASSERT_FALSE(
81 transformation.IsApplicable(context.get(), transformation_context));
82
83 // Tests undefined |type_id|.
84 transformation = TransformationAddConstantScalar(19, 20, {0}, false);
85 ASSERT_FALSE(
86 transformation.IsApplicable(context.get(), transformation_context));
87
88 // Tests |type_id| not representing a type instruction.
89 transformation = TransformationAddConstantScalar(19, 11, {0}, false);
90 ASSERT_FALSE(
91 transformation.IsApplicable(context.get(), transformation_context));
92
93 // Tests |type_id| representing an OpTypePointer instruction.
94 transformation = TransformationAddConstantScalar(19, 8, {0}, false);
95 ASSERT_FALSE(
96 transformation.IsApplicable(context.get(), transformation_context));
97
98 // Tests |type_id| representing an OpTypeVoid instruction.
99 transformation = TransformationAddConstantScalar(19, 9, {0}, false);
100 ASSERT_FALSE(
101 transformation.IsApplicable(context.get(), transformation_context));
102
103 // Tests |words| having no words.
104 transformation = TransformationAddConstantScalar(19, 2, {}, false);
105 ASSERT_FALSE(
106 transformation.IsApplicable(context.get(), transformation_context));
107
108 // Tests |words| having 2 words for a 32-bit type.
109 transformation = TransformationAddConstantScalar(19, 2, {0, 1}, false);
110 ASSERT_FALSE(
111 transformation.IsApplicable(context.get(), transformation_context));
112
113 // Tests |words| having 3 words for a 64-bit type.
114 transformation = TransformationAddConstantScalar(19, 5, {0, 1, 2}, false);
115 ASSERT_FALSE(
116 transformation.IsApplicable(context.get(), transformation_context));
117 }
118
119 TEST(TransformationAddConstantScalarTest, Apply) {
120 std::string reference_shader = R"(
121 OpCapability Shader
122 OpCapability Int64
123 OpCapability Float64
124 %1 = OpExtInstImport "GLSL.std.450"
125 OpMemoryModel Logical GLSL450
126 OpEntryPoint Vertex %17 "main"
127
128 ; Types
129
130 ; 32-bit types
131 %2 = OpTypeInt 32 0
132 %3 = OpTypeInt 32 1
133 %4 = OpTypeFloat 32
134
135 ; 64-bit types
136 %5 = OpTypeInt 64 0
137 %6 = OpTypeInt 64 1
138 %7 = OpTypeFloat 64
139
140 %8 = OpTypePointer Private %2
141 %9 = OpTypeVoid
142 %10 = OpTypeFunction %9
143
144 ; Constants
145
146 ; 32-bit constants
147 %11 = OpConstant %2 1
148 %12 = OpConstant %3 2
149 %13 = OpConstant %4 3
150
151 ; 64-bit constants
152 %14 = OpConstant %5 1
153 %15 = OpConstant %6 2
154 %16 = OpConstant %7 3
155
156 ; main function
157 %17 = OpFunction %9 None %10
158 %18 = OpLabel
159 OpReturn
160 OpFunctionEnd
161 )";
162
163 const auto env = SPV_ENV_UNIVERSAL_1_3;
164 const auto consumer = nullptr;
165 const auto context =
166 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
167 spvtools::ValidatorOptions validator_options;
168 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
169 kConsoleMessageConsumer));
170 TransformationContext transformation_context(
171 MakeUnique<FactManager>(context.get()), validator_options);
172 // Adds 32-bit unsigned integer (1 logical operand with 1 word).
173 auto transformation = TransformationAddConstantScalar(19, 2, {4}, false);
174 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
175 auto* constant_instruction = context->get_def_use_mgr()->GetDef(19);
176 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
177 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
178 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
179 kConsoleMessageConsumer));
180
181 // Adds 32-bit signed integer (1 logical operand with 1 word).
182 transformation = TransformationAddConstantScalar(20, 3, {5}, false);
183 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
184 constant_instruction = context->get_def_use_mgr()->GetDef(20);
185 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
186 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
187 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
188 kConsoleMessageConsumer));
189
190 // Adds 32-bit float (1 logical operand with 1 word).
191 transformation = TransformationAddConstantScalar(
192 21, 4, {0b01000000110000000000000000000000}, false);
193 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
194 constant_instruction = context->get_def_use_mgr()->GetDef(21);
195 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
196 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
197 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
198 kConsoleMessageConsumer));
199
200 // Adds 64-bit unsigned integer (1 logical operand with 2 words).
201 transformation = TransformationAddConstantScalar(22, 5, {7, 0}, false);
202 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
203 constant_instruction = context->get_def_use_mgr()->GetDef(22);
204 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
205 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
206 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
207 kConsoleMessageConsumer));
208
209 // Adds 64-bit signed integer (1 logical operand with 2 words).
210 transformation = TransformationAddConstantScalar(23, 6, {8, 0}, false);
211 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
212 constant_instruction = context->get_def_use_mgr()->GetDef(23);
213 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
214 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
215 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
216 kConsoleMessageConsumer));
217
218 // Adds 64-bit float (1 logical operand with 2 words).
219 transformation = TransformationAddConstantScalar(
220 24, 7, {0, 0b01000000001000100000000000000000}, false);
221 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
222 constant_instruction = context->get_def_use_mgr()->GetDef(24);
223 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
224 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
225 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
226 kConsoleMessageConsumer));
227
228 // Adds irrelevant 32-bit unsigned integer (1 logical operand with 1 word).
229 transformation = TransformationAddConstantScalar(25, 2, {10}, true);
230 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
231 constant_instruction = context->get_def_use_mgr()->GetDef(25);
232 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
233 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
234 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
235 kConsoleMessageConsumer));
236
237 // Adds irrelevant 32-bit signed integer (1 logical operand with 1 word).
238 transformation = TransformationAddConstantScalar(26, 3, {11}, true);
239 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
240 constant_instruction = context->get_def_use_mgr()->GetDef(26);
241 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
242 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
243 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
244 kConsoleMessageConsumer));
245
246 // Adds irrelevant 32-bit float (1 logical operand with 1 word).
247 transformation = TransformationAddConstantScalar(
248 27, 4, {0b01000001010000000000000000000000}, true);
249 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
250 constant_instruction = context->get_def_use_mgr()->GetDef(27);
251 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
252 EXPECT_EQ(constant_instruction->NumInOperandWords(), 1);
253 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
254 kConsoleMessageConsumer));
255
256 // Adds irrelevant 64-bit unsigned integer (1 logical operand with 2 words).
257 transformation = TransformationAddConstantScalar(28, 5, {13, 0}, true);
258 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
259 constant_instruction = context->get_def_use_mgr()->GetDef(28);
260 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
261 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
262 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
263 kConsoleMessageConsumer));
264
265 // Adds irrelevant 64-bit signed integer (1 logical operand with 2 words).
266 transformation = TransformationAddConstantScalar(29, 6, {14, 0}, true);
267 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
268 constant_instruction = context->get_def_use_mgr()->GetDef(29);
269 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
270 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
271 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
272 kConsoleMessageConsumer));
273
274 // Adds irrelevant 64-bit float (1 logical operand with 2 words).
275 transformation = TransformationAddConstantScalar(
276 30, 7, {0, 0b01000000001011100000000000000000}, true);
277 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
278 constant_instruction = context->get_def_use_mgr()->GetDef(30);
279 EXPECT_EQ(constant_instruction->NumInOperands(), 1);
280 EXPECT_EQ(constant_instruction->NumInOperandWords(), 2);
281 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
282 kConsoleMessageConsumer));
283
284 for (uint32_t result_id = 19; result_id <= 24; ++result_id) {
285 ASSERT_FALSE(
286 transformation_context.GetFactManager()->IdIsIrrelevant(result_id));
287 }
288
289 for (uint32_t result_id = 25; result_id <= 30; ++result_id) {
290 ASSERT_TRUE(
291 transformation_context.GetFactManager()->IdIsIrrelevant(result_id));
292 }
293
294 std::string variant_shader = R"(
295 OpCapability Shader
296 OpCapability Int64
297 OpCapability Float64
298 %1 = OpExtInstImport "GLSL.std.450"
299 OpMemoryModel Logical GLSL450
300 OpEntryPoint Vertex %17 "main"
301
302 ; Types
303
304 ; 32-bit types
305 %2 = OpTypeInt 32 0
306 %3 = OpTypeInt 32 1
307 %4 = OpTypeFloat 32
308
309 ; 64-bit types
310 %5 = OpTypeInt 64 0
311 %6 = OpTypeInt 64 1
312 %7 = OpTypeFloat 64
313
314 %8 = OpTypePointer Private %2
315 %9 = OpTypeVoid
316 %10 = OpTypeFunction %9
317
318 ; Constants
319
320 ; 32-bit constants
321 %11 = OpConstant %2 1
322 %12 = OpConstant %3 2
323 %13 = OpConstant %4 3
324
325 ; 64-bit constants
326 %14 = OpConstant %5 1
327 %15 = OpConstant %6 2
328 %16 = OpConstant %7 3
329
330 ; added constants
331 %19 = OpConstant %2 4
332 %20 = OpConstant %3 5
333 %21 = OpConstant %4 6
334 %22 = OpConstant %5 7
335 %23 = OpConstant %6 8
336 %24 = OpConstant %7 9
337 %25 = OpConstant %2 10
338 %26 = OpConstant %3 11
339 %27 = OpConstant %4 12
340 %28 = OpConstant %5 13
341 %29 = OpConstant %6 14
342 %30 = OpConstant %7 15
343
344 ; main function
345 %17 = OpFunction %9 None %10
346 %18 = OpLabel
347 OpReturn
348 OpFunctionEnd
349 )";
350
351 ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
352 }
353
354 } // namespace
355 } // namespace fuzz
356 } // namespace spvtools