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 <sstream>
16 #include <string>
17 #include <vector>
18
19 #include "test/opt/assembly_builder.h"
20 #include "test/opt/pass_fixture.h"
21 #include "test/opt/pass_utils.h"
22
23 namespace spvtools {
24 namespace opt {
25 namespace {
26
27 using FoldSpecConstantOpAndCompositePassBasicTest = PassTest<::testing::Test>;
28
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,Empty)29 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, Empty) {
30 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
31 "", "", /* skip_nop = */ true);
32 }
33
34 // A test of the basic functionality of FoldSpecConstantOpAndCompositePass.
35 // A spec constant defined with an integer addition operation should be folded
36 // to a normal constant with fixed value.
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,Basic)37 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, Basic) {
38 AssemblyBuilder builder;
39 builder.AppendTypesConstantsGlobals({
40 // clang-format off
41 "%int = OpTypeInt 32 1",
42 "%frozen_spec_const_int = OpConstant %int 1",
43 "%const_int = OpConstant %int 2",
44 // Folding target:
45 "%spec_add = OpSpecConstantOp %int IAdd %frozen_spec_const_int %const_int",
46 // clang-format on
47 });
48
49 std::vector<const char*> expected = {
50 // clang-format off
51 "OpCapability Shader",
52 "OpCapability Float64",
53 "%1 = OpExtInstImport \"GLSL.std.450\"",
54 "OpMemoryModel Logical GLSL450",
55 "OpEntryPoint Vertex %main \"main\"",
56 "OpName %void \"void\"",
57 "OpName %main_func_type \"main_func_type\"",
58 "OpName %main \"main\"",
59 "OpName %main_func_entry_block \"main_func_entry_block\"",
60 "OpName %int \"int\"",
61 "OpName %frozen_spec_const_int \"frozen_spec_const_int\"",
62 "OpName %const_int \"const_int\"",
63 "OpName %spec_add \"spec_add\"",
64 "%void = OpTypeVoid",
65 "%main_func_type = OpTypeFunction %void",
66 "%int = OpTypeInt 32 1",
67 "%frozen_spec_const_int = OpConstant %int 1",
68 "%const_int = OpConstant %int 2",
69 // The SpecConstantOp IAdd instruction should be replace by OpConstant
70 // instruction:
71 "%spec_add = OpConstant %int 3",
72 "%main = OpFunction %void None %main_func_type",
73 "%main_func_entry_block = OpLabel",
74 "OpReturn",
75 "OpFunctionEnd",
76 // clang-format on
77 };
78 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
79 builder.GetCode(), JoinAllInsts(expected), /* skip_nop = */ true);
80 }
81
82 // A test of skipping folding an instruction when the instruction result type
83 // has decorations.
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,SkipWhenTypeHasDecorations)84 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
85 SkipWhenTypeHasDecorations) {
86 AssemblyBuilder builder;
87 builder
88 .AppendAnnotations({
89 // clang-format off
90 "OpDecorate %int RelaxedPrecision",
91 // clang-format on
92 })
93 .AppendTypesConstantsGlobals({
94 // clang-format off
95 "%int = OpTypeInt 32 1",
96 "%frozen_spec_const_int = OpConstant %int 1",
97 "%const_int = OpConstant %int 2",
98 // The following spec constant should not be folded as the result type
99 // has relaxed precision decoration.
100 "%spec_add = OpSpecConstantOp %int IAdd %frozen_spec_const_int %const_int",
101 // clang-format on
102 });
103
104 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
105 builder.GetCode(), builder.GetCode(), /* skip_nop = */ true);
106 }
107
108 // Test where OpSpecConstantOp depends on another OpSpecConstantOp with
109 // CompositeExtract
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,StackedCompositeExtract)110 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, StackedCompositeExtract) {
111 AssemblyBuilder builder;
112 builder.AppendTypesConstantsGlobals({
113 // clang-format off
114 "%uint = OpTypeInt 32 0",
115 "%v3uint = OpTypeVector %uint 3",
116 "%uint_2 = OpConstant %uint 2",
117 "%uint_3 = OpConstant %uint 3",
118 // Folding target:
119 "%composite_0 = OpSpecConstantComposite %v3uint %uint_2 %uint_3 %uint_2",
120 "%op_0 = OpSpecConstantOp %uint CompositeExtract %composite_0 0",
121 "%op_1 = OpSpecConstantOp %uint CompositeExtract %composite_0 1",
122 "%op_2 = OpSpecConstantOp %uint IMul %op_0 %op_1",
123 "%composite_1 = OpSpecConstantComposite %v3uint %op_0 %op_1 %op_2",
124 "%op_3 = OpSpecConstantOp %uint CompositeExtract %composite_1 0",
125 "%op_4 = OpSpecConstantOp %uint IMul %op_2 %op_3",
126 // clang-format on
127 });
128
129 std::vector<const char*> expected = {
130 // clang-format off
131 "OpCapability Shader",
132 "OpCapability Float64",
133 "%1 = OpExtInstImport \"GLSL.std.450\"",
134 "OpMemoryModel Logical GLSL450",
135 "OpEntryPoint Vertex %main \"main\"",
136 "OpName %void \"void\"",
137 "OpName %main_func_type \"main_func_type\"",
138 "OpName %main \"main\"",
139 "OpName %main_func_entry_block \"main_func_entry_block\"",
140 "OpName %uint \"uint\"",
141 "OpName %v3uint \"v3uint\"",
142 "OpName %uint_2 \"uint_2\"",
143 "OpName %uint_3 \"uint_3\"",
144 "OpName %composite_0 \"composite_0\"",
145 "OpName %op_0 \"op_0\"",
146 "OpName %op_1 \"op_1\"",
147 "OpName %op_2 \"op_2\"",
148 "OpName %composite_1 \"composite_1\"",
149 "OpName %op_3 \"op_3\"",
150 "OpName %op_4 \"op_4\"",
151 "%void = OpTypeVoid",
152 "%main_func_type = OpTypeFunction %void",
153 "%uint = OpTypeInt 32 0",
154 "%v3uint = OpTypeVector %uint 3",
155 "%uint_2 = OpConstant %uint 2",
156 "%uint_3 = OpConstant %uint 3",
157 "%composite_0 = OpConstantComposite %v3uint %uint_2 %uint_3 %uint_2",
158 "%op_0 = OpConstant %uint 2",
159 "%op_1 = OpConstant %uint 3",
160 "%op_2 = OpConstant %uint 6",
161 "%composite_1 = OpConstantComposite %v3uint %op_0 %op_1 %op_2",
162 "%op_3 = OpConstant %uint 2",
163 "%op_4 = OpConstant %uint 12",
164 "%main = OpFunction %void None %main_func_type",
165 "%main_func_entry_block = OpLabel",
166 "OpReturn",
167 "OpFunctionEnd",
168 // clang-format on
169 };
170 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
171 builder.GetCode(), JoinAllInsts(expected), /* skip_nop = */ true);
172 }
173
174 // Test where OpSpecConstantOp depends on another OpSpecConstantOp with
175 // VectorShuffle
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,StackedVectorShuffle)176 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, StackedVectorShuffle) {
177 AssemblyBuilder builder;
178 builder.AppendTypesConstantsGlobals({
179 // clang-format off
180 "%uint = OpTypeInt 32 0",
181 "%v3uint = OpTypeVector %uint 3",
182 "%uint_1 = OpConstant %uint 1",
183 "%uint_2 = OpConstant %uint 2",
184 "%uint_3 = OpConstant %uint 3",
185 "%uint_4 = OpConstant %uint 4",
186 "%uint_5 = OpConstant %uint 5",
187 "%uint_6 = OpConstant %uint 6",
188 // Folding target:
189 "%composite_0 = OpSpecConstantComposite %v3uint %uint_1 %uint_2 %uint_3",
190 "%composite_1 = OpSpecConstantComposite %v3uint %uint_4 %uint_5 %uint_6",
191 "%vecshuffle = OpSpecConstantOp %v3uint VectorShuffle %composite_0 %composite_1 0 5 3",
192 "%op = OpSpecConstantOp %uint CompositeExtract %vecshuffle 1",
193 // clang-format on
194 });
195
196 std::vector<const char*> expected = {
197 // clang-format off
198 "OpCapability Shader",
199 "OpCapability Float64",
200 "%1 = OpExtInstImport \"GLSL.std.450\"",
201 "OpMemoryModel Logical GLSL450",
202 "OpEntryPoint Vertex %main \"main\"",
203 "OpName %void \"void\"",
204 "OpName %main_func_type \"main_func_type\"",
205 "OpName %main \"main\"",
206 "OpName %main_func_entry_block \"main_func_entry_block\"",
207 "OpName %uint \"uint\"",
208 "OpName %v3uint \"v3uint\"",
209 "OpName %uint_1 \"uint_1\"",
210 "OpName %uint_2 \"uint_2\"",
211 "OpName %uint_3 \"uint_3\"",
212 "OpName %uint_4 \"uint_4\"",
213 "OpName %uint_5 \"uint_5\"",
214 "OpName %uint_6 \"uint_6\"",
215 "OpName %composite_0 \"composite_0\"",
216 "OpName %composite_1 \"composite_1\"",
217 "OpName %vecshuffle \"vecshuffle\"",
218 "OpName %op \"op\"",
219 "%void = OpTypeVoid",
220 "%main_func_type = OpTypeFunction %void",
221 "%uint = OpTypeInt 32 0",
222 "%v3uint = OpTypeVector %uint 3",
223 "%uint_1 = OpConstant %uint 1",
224 "%uint_2 = OpConstant %uint 2",
225 "%uint_3 = OpConstant %uint 3",
226 "%uint_4 = OpConstant %uint 4",
227 "%uint_5 = OpConstant %uint 5",
228 "%uint_6 = OpConstant %uint 6",
229 "%composite_0 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3",
230 "%composite_1 = OpConstantComposite %v3uint %uint_4 %uint_5 %uint_6",
231 "%vecshuffle = OpConstantComposite %v3uint %uint_1 %uint_6 %uint_4",
232 "%op = OpConstant %uint 6",
233 "%main = OpFunction %void None %main_func_type",
234 "%main_func_entry_block = OpLabel",
235 "OpReturn",
236 "OpFunctionEnd",
237 // clang-format on
238 };
239 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
240 builder.GetCode(), JoinAllInsts(expected), /* skip_nop = */ true);
241 }
242
243 // Test CompositeExtract with matrix
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeExtractMaxtrix)244 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeExtractMaxtrix) {
245 AssemblyBuilder builder;
246 builder.AppendTypesConstantsGlobals({
247 // clang-format off
248 "%uint = OpTypeInt 32 0",
249 "%v3uint = OpTypeVector %uint 3",
250 "%mat3x3 = OpTypeMatrix %v3uint 3",
251 "%uint_1 = OpConstant %uint 1",
252 "%uint_2 = OpConstant %uint 2",
253 "%uint_3 = OpConstant %uint 3",
254 // Folding target:
255 "%a = OpSpecConstantComposite %v3uint %uint_1 %uint_1 %uint_1",
256 "%b = OpSpecConstantComposite %v3uint %uint_1 %uint_1 %uint_3",
257 "%c = OpSpecConstantComposite %v3uint %uint_1 %uint_2 %uint_1",
258 "%op = OpSpecConstantComposite %mat3x3 %a %b %c",
259 "%x = OpSpecConstantOp %uint CompositeExtract %op 2 1",
260 "%y = OpSpecConstantOp %uint CompositeExtract %op 1 2",
261 // clang-format on
262 });
263
264 std::vector<const char*> expected = {
265 // clang-format off
266 "OpCapability Shader",
267 "OpCapability Float64",
268 "%1 = OpExtInstImport \"GLSL.std.450\"",
269 "OpMemoryModel Logical GLSL450",
270 "OpEntryPoint Vertex %main \"main\"",
271 "OpName %void \"void\"",
272 "OpName %main_func_type \"main_func_type\"",
273 "OpName %main \"main\"",
274 "OpName %main_func_entry_block \"main_func_entry_block\"",
275 "OpName %uint \"uint\"",
276 "OpName %v3uint \"v3uint\"",
277 "OpName %mat3x3 \"mat3x3\"",
278 "OpName %uint_1 \"uint_1\"",
279 "OpName %uint_2 \"uint_2\"",
280 "OpName %uint_3 \"uint_3\"",
281 "OpName %a \"a\"",
282 "OpName %b \"b\"",
283 "OpName %c \"c\"",
284 "OpName %op \"op\"",
285 "OpName %x \"x\"",
286 "OpName %y \"y\"",
287 "%void = OpTypeVoid",
288 "%main_func_type = OpTypeFunction %void",
289 "%uint = OpTypeInt 32 0",
290 "%v3uint = OpTypeVector %uint 3",
291 "%mat3x3 = OpTypeMatrix %v3uint 3",
292 "%uint_1 = OpConstant %uint 1",
293 "%uint_2 = OpConstant %uint 2",
294 "%uint_3 = OpConstant %uint 3",
295 "%a = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1",
296 "%b = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_3",
297 "%c = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_1",
298 "%op = OpConstantComposite %mat3x3 %a %b %c",
299 "%x = OpConstant %uint 2",
300 "%y = OpConstant %uint 3",
301 "%main = OpFunction %void None %main_func_type",
302 "%main_func_entry_block = OpLabel",
303 "OpReturn",
304 "OpFunctionEnd",
305 // clang-format on
306 };
307 SinglePassRunAndCheck<FoldSpecConstantOpAndCompositePass>(
308 builder.GetCode(), JoinAllInsts(expected), /* skip_nop = */ true);
309 }
310
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVector)311 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertVector) {
312 const std::string test =
313 R"(
314 OpCapability Shader
315 OpMemoryModel Logical GLSL450
316 OpEntryPoint GLCompute %1 "main"
317 OpExecutionMode %1 LocalSize 1 1 1
318 %void = OpTypeVoid
319 %3 = OpTypeFunction %void
320 %uint = OpTypeInt 32 0
321 %v3uint = OpTypeVector %uint 3
322 %uint_2 = OpConstant %uint 2
323 %uint_3 = OpConstant %uint 3
324 %8 = OpConstantNull %uint
325 %9 = OpSpecConstantComposite %v3uint %uint_2 %uint_2 %uint_2
326 ; CHECK: %15 = OpConstantComposite %v3uint %uint_3 %uint_2 %uint_2
327 ; CHECK: %uint_3_0 = OpConstant %uint 3
328 ; CHECK: %17 = OpConstantComposite %v3uint %8 %uint_2 %uint_2
329 ; CHECK: %18 = OpConstantNull %uint
330 %10 = OpSpecConstantOp %v3uint CompositeInsert %uint_3 %9 0
331 %11 = OpSpecConstantOp %uint CompositeExtract %10 0
332 %12 = OpSpecConstantOp %v3uint CompositeInsert %8 %9 0
333 %13 = OpSpecConstantOp %uint CompositeExtract %12 0
334 %1 = OpFunction %void None %3
335 %14 = OpLabel
336 OpReturn
337 OpFunctionEnd
338 )";
339
340 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
341 }
342
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVectorIntoMatrix)343 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
344 CompositeInsertVectorIntoMatrix) {
345 const std::string test =
346 R"(
347 OpCapability Shader
348 OpMemoryModel Logical GLSL450
349 OpEntryPoint GLCompute %1 "main"
350 OpExecutionMode %1 LocalSize 1 1 1
351 %void = OpTypeVoid
352 %3 = OpTypeFunction %void
353 %float = OpTypeFloat 32
354 %v2float = OpTypeVector %float 2
355 %mat2v2float = OpTypeMatrix %v2float 2
356 %float_0 = OpConstant %float 0
357 %float_1 = OpConstant %float 1
358 %float_2 = OpConstant %float 2
359 %v2float_01 = OpConstantComposite %v2float %float_0 %float_1
360 %v2float_12 = OpConstantComposite %v2float %float_1 %float_2
361
362 ; CHECK: %10 = OpConstantComposite %v2float %float_0 %float_1
363 ; CHECK: %11 = OpConstantComposite %v2float %float_1 %float_2
364 ; CHECK: %12 = OpConstantComposite %mat2v2float %11 %11
365 %mat2v2float_1212 = OpConstantComposite %mat2v2float %v2float_12 %v2float_12
366
367 ; CHECK: %15 = OpConstantComposite %mat2v2float %10 %11
368 %spec_0 = OpSpecConstantOp %mat2v2float CompositeInsert %v2float_01 %mat2v2float_1212 0
369 %1 = OpFunction %void None %3
370 %label = OpLabel
371 OpReturn
372 OpFunctionEnd
373 )";
374
375 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
376 }
377
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertMatrix)378 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertMatrix) {
379 const std::string test =
380 R"(
381 OpCapability Shader
382 OpMemoryModel Logical GLSL450
383 OpEntryPoint GLCompute %1 "main"
384 OpExecutionMode %1 LocalSize 1 1 1
385 %void = OpTypeVoid
386 %3 = OpTypeFunction %void
387 %float = OpTypeFloat 32
388 %v3float = OpTypeVector %float 3
389 %mat3v3float = OpTypeMatrix %v3float 3
390 %float_1 = OpConstant %float 1
391 %float_2 = OpConstant %float 2
392 %9 = OpSpecConstantComposite %v3float %float_1 %float_1 %float_1
393 %10 = OpSpecConstantComposite %v3float %float_1 %float_1 %float_1
394 %11 = OpSpecConstantComposite %v3float %float_1 %float_2 %float_1
395 %12 = OpSpecConstantComposite %mat3v3float %9 %10 %11
396 ; CHECK: %float_2_0 = OpConstant %float 2
397 ; CHECK: %18 = OpConstantComposite %v3float %float_1 %float_1 %float_2
398 ; CHECK: %19 = OpConstantComposite %mat3v3float %9 %18 %11
399 ; CHECK: %float_2_1 = OpConstant %float 2
400 %13 = OpSpecConstantOp %float CompositeExtract %12 2 1
401 %14 = OpSpecConstantOp %mat3v3float CompositeInsert %13 %12 1 2
402 %15 = OpSpecConstantOp %float CompositeExtract %14 1 2
403 %1 = OpFunction %void None %3
404 %16 = OpLabel
405 OpReturn
406 OpFunctionEnd
407 )";
408
409 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
410 }
411
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertFloatNull)412 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertFloatNull) {
413 const std::string test =
414 R"(
415 OpCapability Shader
416 OpMemoryModel Logical GLSL450
417 OpEntryPoint GLCompute %1 "main"
418 OpExecutionMode %1 LocalSize 1 1 1
419 %void = OpTypeVoid
420 %3 = OpTypeFunction %void
421 %float = OpTypeFloat 32
422 %v3float = OpTypeVector %float 3
423 %float_1 = OpConstant %float 1
424
425 ; CHECK: %7 = OpConstantNull %float
426 ; CHECK: %8 = OpConstantComposite %v3float %7 %7 %7
427 ; CHECK: %12 = OpConstantComposite %v3float %7 %7 %float_1
428 %null = OpConstantNull %float
429 %spec_0 = OpConstantComposite %v3float %null %null %null
430 %spec_1 = OpSpecConstantOp %v3float CompositeInsert %float_1 %spec_0 2
431
432 ; CHECK: %float_1_0 = OpConstant %float 1
433 %spec_2 = OpSpecConstantOp %float CompositeExtract %spec_1 2
434 %1 = OpFunction %void None %3
435 %label = OpLabel
436 OpReturn
437 OpFunctionEnd
438 )";
439
440 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
441 }
442
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertFloatSetNull)443 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
444 CompositeInsertFloatSetNull) {
445 const std::string test =
446 R"(
447 OpCapability Shader
448 OpMemoryModel Logical GLSL450
449 OpEntryPoint GLCompute %1 "main"
450 OpExecutionMode %1 LocalSize 1 1 1
451 %void = OpTypeVoid
452 %3 = OpTypeFunction %void
453 %float = OpTypeFloat 32
454 %v3float = OpTypeVector %float 3
455 %float_1 = OpConstant %float 1
456
457 ; CHECK: %7 = OpConstantNull %float
458 ; CHECK: %8 = OpConstantComposite %v3float %7 %7 %float_1
459 ; CHECK: %12 = OpConstantComposite %v3float %7 %7 %7
460 %null = OpConstantNull %float
461 %spec_0 = OpConstantComposite %v3float %null %null %float_1
462 %spec_1 = OpSpecConstantOp %v3float CompositeInsert %null %spec_0 2
463
464 ; CHECK: %13 = OpConstantNull %float
465 %spec_2 = OpSpecConstantOp %float CompositeExtract %spec_1 2
466 %1 = OpFunction %void None %3
467 %label = OpLabel
468 OpReturn
469 OpFunctionEnd
470 )";
471
472 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
473 }
474
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVectorNull)475 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertVectorNull) {
476 const std::string test =
477 R"(
478 OpCapability Shader
479 OpMemoryModel Logical GLSL450
480 OpEntryPoint GLCompute %1 "main"
481 OpExecutionMode %1 LocalSize 1 1 1
482 %void = OpTypeVoid
483 %3 = OpTypeFunction %void
484 %float = OpTypeFloat 32
485 %v3float = OpTypeVector %float 3
486 %float_1 = OpConstant %float 1
487 %null = OpConstantNull %v3float
488
489 ; CHECK: %11 = OpConstantNull %float
490 ; CHECK: %12 = OpConstantComposite %v3float %11 %11 %float_1
491 %spec_0 = OpSpecConstantOp %v3float CompositeInsert %float_1 %null 2
492
493
494 ; CHECK: %float_1_0 = OpConstant %float 1
495 %spec_1 = OpSpecConstantOp %float CompositeExtract %spec_0 2
496 %1 = OpFunction %void None %3
497 %label = OpLabel
498 OpReturn
499 OpFunctionEnd
500 )";
501
502 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
503 }
504
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertNullVectorIntoMatrix)505 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
506 CompositeInsertNullVectorIntoMatrix) {
507 const std::string test =
508 R"(
509 OpCapability Shader
510 OpMemoryModel Logical GLSL450
511 OpEntryPoint GLCompute %1 "main"
512 OpExecutionMode %1 LocalSize 1 1 1
513 %void = OpTypeVoid
514 %3 = OpTypeFunction %void
515 %float = OpTypeFloat 32
516 %v2float = OpTypeVector %float 2
517 %mat2v2float = OpTypeMatrix %v2float 2
518 %null = OpConstantNull %mat2v2float
519 %float_1 = OpConstant %float 1
520 %float_2 = OpConstant %float 2
521 %v2float_12 = OpConstantComposite %v2float %float_1 %float_2
522
523 ; CHECK: %13 = OpConstantNull %v2float
524 ; CHECK: %14 = OpConstantComposite %mat2v2float %10 %13
525 %spec_0 = OpSpecConstantOp %mat2v2float CompositeInsert %v2float_12 %null 0
526 %1 = OpFunction %void None %3
527 %label = OpLabel
528 OpReturn
529 OpFunctionEnd
530 )";
531
532 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
533 }
534
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVectorKeepNull)535 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
536 CompositeInsertVectorKeepNull) {
537 const std::string test =
538 R"(
539 OpCapability Shader
540 OpMemoryModel Logical GLSL450
541 OpEntryPoint GLCompute %1 "main"
542 OpExecutionMode %1 LocalSize 1 1 1
543 %void = OpTypeVoid
544 %3 = OpTypeFunction %void
545 %float = OpTypeFloat 32
546 %v3float = OpTypeVector %float 3
547 %float_0 = OpConstant %float 0
548 %null_float = OpConstantNull %float
549 %null_vec = OpConstantNull %v3float
550
551 ; CHECK: %15 = OpConstantComposite %v3float %7 %7 %float_0
552 %spec_0 = OpSpecConstantOp %v3float CompositeInsert %float_0 %null_vec 2
553
554 ; CHECK: %float_0_0 = OpConstant %float 0
555 %spec_1 = OpSpecConstantOp %float CompositeExtract %spec_0 2
556
557 ; CHECK: %17 = OpConstantComposite %v3float %7 %7 %7
558 %spec_2 = OpSpecConstantOp %v3float CompositeInsert %null_float %null_vec 2
559
560 ; CHECK: %18 = OpConstantNull %float
561 %spec_3 = OpSpecConstantOp %float CompositeExtract %spec_2 2
562 %1 = OpFunction %void None %3
563 %label = OpLabel
564 %add = OpFAdd %float %spec_3 %spec_3
565 OpReturn
566 OpFunctionEnd
567 )";
568
569 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
570 }
571
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVectorChainNull)572 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
573 CompositeInsertVectorChainNull) {
574 const std::string test =
575 R"(
576 OpCapability Shader
577 OpMemoryModel Logical GLSL450
578 OpEntryPoint GLCompute %1 "main"
579 OpExecutionMode %1 LocalSize 1 1 1
580 %void = OpTypeVoid
581 %3 = OpTypeFunction %void
582 %float = OpTypeFloat 32
583 %v3float = OpTypeVector %float 3
584 %float_1 = OpConstant %float 1
585 %null = OpConstantNull %v3float
586
587 ; CHECK: %15 = OpConstantNull %float
588 ; CHECK: %16 = OpConstantComposite %v3float %15 %15 %float_1
589 ; CHECK: %17 = OpConstantComposite %v3float %15 %float_1 %float_1
590 ; CHECK: %18 = OpConstantComposite %v3float %float_1 %float_1 %float_1
591 %spec_0 = OpSpecConstantOp %v3float CompositeInsert %float_1 %null 2
592 %spec_1 = OpSpecConstantOp %v3float CompositeInsert %float_1 %spec_0 1
593 %spec_2 = OpSpecConstantOp %v3float CompositeInsert %float_1 %spec_1 0
594
595 ; CHECK: %float_1_0 = OpConstant %float 1
596 ; CHECK: %float_1_1 = OpConstant %float 1
597 ; CHECK: %float_1_2 = OpConstant %float 1
598 %spec_3 = OpSpecConstantOp %float CompositeExtract %spec_2 0
599 %spec_4 = OpSpecConstantOp %float CompositeExtract %spec_2 1
600 %spec_5 = OpSpecConstantOp %float CompositeExtract %spec_2 2
601 %1 = OpFunction %void None %3
602 %label = OpLabel
603 OpReturn
604 OpFunctionEnd
605 )";
606
607 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
608 }
609
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertVectorChainReset)610 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,
611 CompositeInsertVectorChainReset) {
612 const std::string test =
613 R"(
614 OpCapability Shader
615 OpMemoryModel Logical GLSL450
616 OpEntryPoint GLCompute %1 "main"
617 OpExecutionMode %1 LocalSize 1 1 1
618 %void = OpTypeVoid
619 %3 = OpTypeFunction %void
620 %float = OpTypeFloat 32
621 %v3float = OpTypeVector %float 3
622 %float_1 = OpConstant %float 1
623 %null = OpConstantNull %float
624 ; CHECK: %8 = OpConstantComposite %v3float %7 %7 %float_1
625 %spec_0 = OpConstantComposite %v3float %null %null %float_1
626
627 ; set to null
628 ; CHECK: %13 = OpConstantComposite %v3float %7 %7 %7
629 %spec_1 = OpSpecConstantOp %v3float CompositeInsert %null %spec_0 2
630
631 ; set to back to original value
632 ; CHECK: %14 = OpConstantComposite %v3float %7 %7 %float_1
633 %spec_2 = OpSpecConstantOp %v3float CompositeInsert %float_1 %spec_1 2
634
635 ; CHECK: %float_1_0 = OpConstant %float 1
636 %spec_3 = OpSpecConstantOp %float CompositeExtract %spec_2 2
637 %1 = OpFunction %void None %3
638 %label = OpLabel
639 OpReturn
640 OpFunctionEnd
641 )";
642
643 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
644 }
645
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,CompositeInsertMatrixNull)646 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, CompositeInsertMatrixNull) {
647 const std::string test =
648 R"(
649 OpCapability Shader
650 OpMemoryModel Logical GLSL450
651 OpEntryPoint GLCompute %main "main"
652 OpExecutionMode %main LocalSize 1 1 1
653 %void = OpTypeVoid
654 %func = OpTypeFunction %void
655 %float = OpTypeFloat 32
656 %int = OpTypeInt 32 0
657 %v2float = OpTypeVector %float 2
658 %mat2v2float = OpTypeMatrix %v2float 2
659 %null = OpConstantNull %mat2v2float
660 %float_1 = OpConstant %float 1
661 ; CHECK: %13 = OpConstantNull %v2float
662 ; CHECK: %14 = OpConstantNull %float
663 ; CHECK: %15 = OpConstantComposite %v2float %float_1 %14
664 ; CHECK: %16 = OpConstantComposite %mat2v2float %13 %15
665 %spec = OpSpecConstantOp %mat2v2float CompositeInsert %float_1 %null 1 0
666 ; extra type def to make sure new type def are not just thrown at end
667 %v2int = OpTypeVector %int 2
668 %main = OpFunction %void None %func
669 %label = OpLabel
670 OpReturn
671 OpFunctionEnd
672 )";
673
674 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
675 }
676
677 // Silently ignore spec constants that cannot be folded
TEST_F(FoldSpecConstantOpAndCompositePassBasicTest,UnfoldableOp)678 TEST_F(FoldSpecConstantOpAndCompositePassBasicTest, UnfoldableOp) {
679 const std::string test = R"(
680 OpCapability Shader
681 OpCapability SignedZeroInfNanPreserve
682 OpExtension "SPV_KHR_float_controls"
683 OpMemoryModel Logical GLSL450
684 OpEntryPoint Vertex %main "main"
685 OpSource GLSL 450
686 OpDecorate %v SpecId 1
687 %void = OpTypeVoid
688 %3 = OpTypeFunction %void
689 %float = OpTypeFloat 32
690 %v = OpConstant %float 0x1p-1
691 %c = OpSpecConstantOp %float QuantizeToF16 %v
692 ;CHECK: {{%\w+}} = OpSpecConstantOp {{%\w+}} QuantizeToF16 {{%\w+}}
693 %main = OpFunction %void None %3
694 %5 = OpLabel
695 OpReturn
696 OpFunctionEnd
697 )";
698
699 SinglePassRunAndMatch<FoldSpecConstantOpAndCompositePass>(test, false);
700 }
701
702 // All types and some common constants that are potentially required in
703 // FoldSpecConstantOpAndCompositeTest.
CommonTypesAndConstants()704 std::vector<std::string> CommonTypesAndConstants() {
705 return std::vector<std::string>{
706 // clang-format off
707 // scalar types
708 "%bool = OpTypeBool",
709 "%ushort = OpTypeInt 16 0",
710 "%short = OpTypeInt 16 1",
711 "%uint = OpTypeInt 32 0",
712 "%int = OpTypeInt 32 1",
713 "%ulong = OpTypeInt 64 0",
714 "%long = OpTypeInt 64 1",
715 "%float = OpTypeFloat 32",
716 "%double = OpTypeFloat 64",
717 // vector types
718 "%v2bool = OpTypeVector %bool 2",
719 "%v2uint = OpTypeVector %uint 2",
720 "%v2int = OpTypeVector %int 2",
721 "%v3int = OpTypeVector %int 3",
722 "%v4int = OpTypeVector %int 4",
723 "%v2long = OpTypeVector %long 2",
724 "%v2ulong = OpTypeVector %ulong 2",
725 "%v2float = OpTypeVector %float 2",
726 "%v2double = OpTypeVector %double 2",
727 // variable pointer types
728 "%_pf_bool = OpTypePointer Function %bool",
729 "%_pf_uint = OpTypePointer Function %uint",
730 "%_pf_int = OpTypePointer Function %int",
731 "%_pf_float = OpTypePointer Function %float",
732 "%_pf_double = OpTypePointer Function %double",
733 "%_pf_v2int = OpTypePointer Function %v2int",
734 "%_pf_v2float = OpTypePointer Function %v2float",
735 "%_pf_v2double = OpTypePointer Function %v2double",
736 // struct types
737 "%inner_struct = OpTypeStruct %bool %int %float",
738 "%outer_struct = OpTypeStruct %inner_struct %int",
739 "%flat_struct = OpTypeStruct %bool %int %float",
740
741 // common constants
742 // scalar constants:
743 "%bool_true = OpConstantTrue %bool",
744 "%bool_false = OpConstantFalse %bool",
745 "%bool_null = OpConstantNull %bool",
746 "%signed_zero = OpConstant %int 0",
747 "%unsigned_zero = OpConstant %uint 0",
748 "%long_zero = OpConstant %long 0",
749 "%ulong_zero = OpConstant %ulong 0",
750 "%signed_one = OpConstant %int 1",
751 "%unsigned_one = OpConstant %uint 1",
752 "%signed_two = OpConstant %int 2",
753 "%unsigned_two = OpConstant %uint 2",
754 "%signed_three = OpConstant %int 3",
755 "%unsigned_three = OpConstant %uint 3",
756 "%signed_null = OpConstantNull %int",
757 "%unsigned_null = OpConstantNull %uint",
758 "%signed_minus_one = OpConstant %int -1",
759 // vector constants:
760 "%bool_true_vec = OpConstantComposite %v2bool %bool_true %bool_true",
761 "%bool_false_vec = OpConstantComposite %v2bool %bool_false %bool_false",
762 "%bool_null_vec = OpConstantNull %v2bool",
763 "%signed_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
764 "%unsigned_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
765 "%signed_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
766 "%unsigned_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
767 "%signed_two_vec = OpConstantComposite %v2int %signed_two %signed_two",
768 "%unsigned_two_vec = OpConstantComposite %v2uint %unsigned_two %unsigned_two",
769 "%signed_three_vec = OpConstantComposite %v2int %signed_three %signed_three",
770 "%unsigned_three_vec = OpConstantComposite %v2uint %unsigned_three %unsigned_three",
771 "%signed_null_vec = OpConstantNull %v2int",
772 "%unsigned_null_vec = OpConstantNull %v2uint",
773 "%signed_minus_one_vec = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
774 "%v4int_0_1_2_3 = OpConstantComposite %v4int %signed_zero %signed_one %signed_two %signed_three",
775 // clang-format on
776 };
777 }
778
779 // A helper function to strip OpName instructions from the given string of
780 // disassembly code. Returns the string with all OpName instruction stripped.
StripOpNameInstructions(const std::string & str)781 std::string StripOpNameInstructions(const std::string& str) {
782 std::stringstream ss(str);
783 std::ostringstream oss;
784 std::string inst_str;
785 while (std::getline(ss, inst_str, '\n')) {
786 if (inst_str.find("OpName %") == std::string::npos) {
787 oss << inst_str << '\n';
788 }
789 }
790 return oss.str();
791 }
792
793 struct FoldSpecConstantOpAndCompositePassTestCase {
794 // Original constants with unfolded spec constants.
795 std::vector<std::string> original;
796 // Expected constant after folding.
797 std::vector<std::string> expected;
798 };
799
800 using FoldSpecConstantOpAndCompositePassTest = PassTest<
801 ::testing::TestWithParam<FoldSpecConstantOpAndCompositePassTestCase>>;
802
TEST_P(FoldSpecConstantOpAndCompositePassTest,ParamTestCase)803 TEST_P(FoldSpecConstantOpAndCompositePassTest, ParamTestCase) {
804 AssemblyBuilder test_code_builder, expected_code_builder;
805 const auto& tc = GetParam();
806 test_code_builder.AppendTypesConstantsGlobals(CommonTypesAndConstants());
807 test_code_builder.AppendTypesConstantsGlobals(tc.original);
808 expected_code_builder.AppendTypesConstantsGlobals(CommonTypesAndConstants());
809 expected_code_builder.AppendTypesConstantsGlobals(tc.expected);
810 const std::string original = test_code_builder.GetCode();
811 const std::string expected = expected_code_builder.GetCode();
812
813 // Run the optimization and get the result code in disassembly.
814 std::string optimized;
815 auto status = Pass::Status::SuccessWithoutChange;
816 std::tie(optimized, status) =
817 SinglePassRunAndDisassemble<FoldSpecConstantOpAndCompositePass>(
818 original, /* skip_nop = */ true, /* do_validation = */ false);
819
820 // Check the optimized code, but ignore the OpName instructions.
821 EXPECT_NE(Pass::Status::Failure, status);
822 EXPECT_EQ(
823 StripOpNameInstructions(expected) == StripOpNameInstructions(original),
824 status == Pass::Status::SuccessWithoutChange);
825 EXPECT_EQ(StripOpNameInstructions(expected),
826 StripOpNameInstructions(optimized));
827 }
828
829 // Tests that OpSpecConstantComposite opcodes are replace with
830 // OpConstantComposite correctly.
831 INSTANTIATE_TEST_SUITE_P(
832 Composite, FoldSpecConstantOpAndCompositePassTest,
833 ::testing::ValuesIn(std::vector<
834 FoldSpecConstantOpAndCompositePassTestCase>({
835 // clang-format off
836 // normal vector
837 {
838 // original
839 {
840 "%spec_v2bool = OpSpecConstantComposite %v2bool %bool_true %bool_false",
841 "%spec_v2uint = OpSpecConstantComposite %v2uint %unsigned_one %unsigned_one",
842 "%spec_v2int_a = OpSpecConstantComposite %v2int %signed_one %signed_two",
843 // Spec constants whose value can not be fully resolved should
844 // not be processed.
845 "%spec_int = OpSpecConstant %int 99",
846 "%spec_v2int_b = OpSpecConstantComposite %v2int %signed_one %spec_int",
847 },
848 // expected
849 {
850 "%spec_v2bool = OpConstantComposite %v2bool %bool_true %bool_false",
851 "%spec_v2uint = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
852 "%spec_v2int_a = OpConstantComposite %v2int %signed_one %signed_two",
853 "%spec_int = OpSpecConstant %int 99",
854 "%spec_v2int_b = OpSpecConstantComposite %v2int %signed_one %spec_int",
855 },
856 },
857 // vector with null constants
858 {
859 // original
860 {
861 "%null_bool = OpConstantNull %bool",
862 "%null_int = OpConstantNull %int",
863 "%spec_v2bool = OpSpecConstantComposite %v2bool %null_bool %null_bool",
864 "%spec_v3int = OpSpecConstantComposite %v3int %null_int %null_int %null_int",
865 "%spec_v4int = OpSpecConstantComposite %v4int %null_int %null_int %null_int %null_int",
866 },
867 // expected
868 {
869 "%null_bool = OpConstantNull %bool",
870 "%null_int = OpConstantNull %int",
871 "%spec_v2bool = OpConstantComposite %v2bool %null_bool %null_bool",
872 "%spec_v3int = OpConstantComposite %v3int %null_int %null_int %null_int",
873 "%spec_v4int = OpConstantComposite %v4int %null_int %null_int %null_int %null_int",
874 },
875 },
876 // flat struct
877 {
878 // original
879 {
880 "%float_1 = OpConstant %float 1",
881 "%flat_1 = OpSpecConstantComposite %flat_struct %bool_true %signed_null %float_1",
882 // following struct should not be folded as the value of
883 // %spec_float is not determined.
884 "%spec_float = OpSpecConstant %float 1",
885 "%flat_2 = OpSpecConstantComposite %flat_struct %bool_true %signed_one %spec_float",
886 },
887 // expected
888 {
889 "%float_1 = OpConstant %float 1",
890 "%flat_1 = OpConstantComposite %flat_struct %bool_true %signed_null %float_1",
891 "%spec_float = OpSpecConstant %float 1",
892 "%flat_2 = OpSpecConstantComposite %flat_struct %bool_true %signed_one %spec_float",
893 }
894 },
895 // nested struct
896 {
897 // original
898 {
899 "%float_1 = OpConstant %float 1",
900 "%inner_1 = OpSpecConstantComposite %inner_struct %bool_true %signed_null %float_1",
901 "%outer_1 = OpSpecConstantComposite %outer_struct %inner_1 %signed_one",
902 // following structs should not be folded as the value of
903 // %spec_float is not determined.
904 "%spec_float = OpSpecConstant %float 1",
905 "%inner_2 = OpSpecConstantComposite %inner_struct %bool_true %signed_null %spec_float",
906 "%outer_2 = OpSpecConstantComposite %outer_struct %inner_2 %signed_one",
907 },
908 // expected
909 {
910 "%float_1 = OpConstant %float 1",
911 "%inner_1 = OpConstantComposite %inner_struct %bool_true %signed_null %float_1",
912 "%outer_1 = OpConstantComposite %outer_struct %inner_1 %signed_one",
913 "%spec_float = OpSpecConstant %float 1",
914 "%inner_2 = OpSpecConstantComposite %inner_struct %bool_true %signed_null %spec_float",
915 "%outer_2 = OpSpecConstantComposite %outer_struct %inner_2 %signed_one",
916 }
917 },
918 // composite constants touched by OpUndef should be skipped
919 {
920 // original
921 {
922 "%undef = OpUndef %float",
923 "%inner = OpConstantComposite %inner_struct %bool_true %signed_one %undef",
924 "%outer = OpSpecConstantComposite %outer_struct %inner %signed_one",
925 },
926 // expected
927 {
928 "%undef = OpUndef %float",
929 "%inner = OpConstantComposite %inner_struct %bool_true %signed_one %undef",
930 "%outer = OpSpecConstantComposite %outer_struct %inner %signed_one",
931 },
932 },
933 // Fold an QuantizetoF16 instruction
934 {
935 // original
936 {
937 "%float_1 = OpConstant %float 1",
938 "%quant_float = OpSpecConstantOp %float QuantizeToF16 %float_1",
939 },
940 // expected
941 {
942 "%float_1 = OpConstant %float 1",
943 "%quant_float = OpConstant %float 1",
944 },
945 }
946 // clang-format on
947 })));
948
949 // Tests for operations that resulting in different types.
950 INSTANTIATE_TEST_SUITE_P(
951 Cast, FoldSpecConstantOpAndCompositePassTest,
952 ::testing::ValuesIn(std::vector<
953 FoldSpecConstantOpAndCompositePassTestCase>({
954 // clang-format off
955 // int -> bool scalar
956 {
957 // original
958 {
959 "%spec_bool_t = OpSpecConstantOp %bool INotEqual %signed_three %signed_zero",
960 "%spec_bool_f = OpSpecConstantOp %bool INotEqual %signed_zero %signed_zero",
961 "%spec_bool_from_null = OpSpecConstantOp %bool INotEqual %signed_null %signed_zero",
962 },
963 // expected
964 {
965 "%spec_bool_t = OpConstantTrue %bool",
966 "%spec_bool_f = OpConstantFalse %bool",
967 "%spec_bool_from_null = OpConstantFalse %bool",
968 },
969 },
970
971 // uint -> bool scalar
972 {
973 // original
974 {
975 "%spec_bool_t = OpSpecConstantOp %bool INotEqual %unsigned_three %unsigned_zero",
976 "%spec_bool_f = OpSpecConstantOp %bool INotEqual %unsigned_zero %unsigned_zero",
977 "%spec_bool_from_null = OpSpecConstantOp %bool INotEqual %unsigned_null %unsigned_zero",
978 },
979 // expected
980 {
981 "%spec_bool_t = OpConstantTrue %bool",
982 "%spec_bool_f = OpConstantFalse %bool",
983 "%spec_bool_from_null = OpConstantFalse %bool",
984 },
985 },
986
987 // bool -> int scalar
988 {
989 // original
990 {
991 "%spec_int_one = OpSpecConstantOp %int Select %bool_true %signed_one %signed_zero",
992 "%spec_int_zero = OpSpecConstantOp %int Select %bool_false %signed_one %signed_zero",
993 "%spec_int_from_null = OpSpecConstantOp %int Select %bool_null %signed_one %signed_zero",
994 },
995 // expected
996 {
997 "%spec_int_one = OpConstant %int 1",
998 "%spec_int_zero = OpConstant %int 0",
999 "%spec_int_from_null = OpConstant %int 0",
1000 },
1001 },
1002
1003 // uint -> int scalar
1004 {
1005 // original
1006 {
1007 "%spec_int_one = OpSpecConstantOp %int IAdd %unsigned_one %signed_zero",
1008 "%spec_int_zero = OpSpecConstantOp %int IAdd %unsigned_zero %signed_zero",
1009 "%spec_int_from_null = OpSpecConstantOp %int IAdd %unsigned_null %unsigned_zero",
1010 },
1011 // expected
1012 {
1013 "%spec_int_one = OpConstant %int 1",
1014 "%spec_int_zero = OpConstant %int 0",
1015 "%spec_int_from_null = OpConstant %int 0",
1016 },
1017 },
1018
1019 // bool -> uint scalar
1020 {
1021 // original
1022 {
1023 "%spec_uint_one = OpSpecConstantOp %uint Select %bool_true %unsigned_one %unsigned_zero",
1024 "%spec_uint_zero = OpSpecConstantOp %uint Select %bool_false %unsigned_one %unsigned_zero",
1025 "%spec_uint_from_null = OpSpecConstantOp %uint Select %bool_null %unsigned_one %unsigned_zero",
1026 },
1027 // expected
1028 {
1029 "%spec_uint_one = OpConstant %uint 1",
1030 "%spec_uint_zero = OpConstant %uint 0",
1031 "%spec_uint_from_null = OpConstant %uint 0",
1032 },
1033 },
1034
1035 // int -> uint scalar
1036 {
1037 // original
1038 {
1039 "%spec_uint_one = OpSpecConstantOp %uint IAdd %signed_one %unsigned_zero",
1040 "%spec_uint_zero = OpSpecConstantOp %uint IAdd %signed_zero %unsigned_zero",
1041 "%spec_uint_from_null = OpSpecConstantOp %uint IAdd %signed_null %unsigned_zero",
1042 },
1043 // expected
1044 {
1045 "%spec_uint_one = OpConstant %uint 1",
1046 "%spec_uint_zero = OpConstant %uint 0",
1047 "%spec_uint_from_null = OpConstant %uint 0",
1048 },
1049 },
1050
1051 // int -> bool vector
1052 {
1053 // original
1054 {
1055 "%spec_bool_t_vec = OpSpecConstantOp %v2bool INotEqual %signed_three_vec %signed_zero_vec",
1056 "%spec_bool_f_vec = OpSpecConstantOp %v2bool INotEqual %signed_zero_vec %signed_zero_vec",
1057 "%spec_bool_from_null = OpSpecConstantOp %v2bool INotEqual %signed_null_vec %signed_zero_vec",
1058 },
1059 // expected
1060 {
1061 "%spec_bool_t_vec = OpConstantComposite %v2bool %bool_true %bool_true",
1062 "%spec_bool_f_vec = OpConstantComposite %v2bool %bool_false %bool_false",
1063 "%spec_bool_from_null = OpConstantComposite %v2bool %bool_false %bool_false",
1064 },
1065 },
1066
1067 // uint -> bool vector
1068 {
1069 // original
1070 {
1071 "%spec_bool_t_vec = OpSpecConstantOp %v2bool INotEqual %unsigned_three_vec %unsigned_zero_vec",
1072 "%spec_bool_f_vec = OpSpecConstantOp %v2bool INotEqual %unsigned_zero_vec %unsigned_zero_vec",
1073 "%spec_bool_from_null = OpSpecConstantOp %v2bool INotEqual %unsigned_null_vec %unsigned_zero_vec",
1074 },
1075 // expected
1076 {
1077 "%spec_bool_t_vec = OpConstantComposite %v2bool %bool_true %bool_true",
1078 "%spec_bool_f_vec = OpConstantComposite %v2bool %bool_false %bool_false",
1079 "%spec_bool_from_null = OpConstantComposite %v2bool %bool_false %bool_false",
1080 },
1081 },
1082
1083 // bool -> int vector
1084 {
1085 // original
1086 {
1087 "%spec_int_one_vec = OpSpecConstantOp %v2int Select %bool_true_vec %signed_one_vec %signed_zero_vec",
1088 "%spec_int_zero_vec = OpSpecConstantOp %v2int Select %bool_false_vec %signed_one_vec %signed_zero_vec",
1089 "%spec_int_from_null = OpSpecConstantOp %v2int Select %bool_null_vec %signed_one_vec %signed_zero_vec",
1090 },
1091 // expected
1092 {
1093 "%spec_int_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
1094 "%spec_int_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
1095 "%spec_int_from_null = OpConstantComposite %v2int %signed_zero %signed_zero",
1096 },
1097 },
1098
1099 // uint -> int vector
1100 {
1101 // original
1102 {
1103 "%spec_int_one_vec = OpSpecConstantOp %v2int IAdd %unsigned_one_vec %signed_zero_vec",
1104 "%spec_int_zero_vec = OpSpecConstantOp %v2int IAdd %unsigned_zero_vec %signed_zero_vec",
1105 "%spec_int_from_null = OpSpecConstantOp %v2int IAdd %unsigned_null_vec %signed_zero_vec",
1106 },
1107 // expected
1108 {
1109 "%spec_int_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
1110 "%spec_int_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
1111 "%spec_int_from_null = OpConstantComposite %v2int %signed_zero %signed_zero",
1112 },
1113 },
1114
1115 // bool -> uint vector
1116 {
1117 // original
1118 {
1119 "%spec_uint_one_vec = OpSpecConstantOp %v2uint Select %bool_true_vec %unsigned_one_vec %unsigned_zero_vec",
1120 "%spec_uint_zero_vec = OpSpecConstantOp %v2uint Select %bool_false_vec %unsigned_one_vec %unsigned_zero_vec",
1121 "%spec_uint_from_null = OpSpecConstantOp %v2uint Select %bool_null_vec %unsigned_one_vec %unsigned_zero_vec",
1122 },
1123 // expected
1124 {
1125 "%spec_uint_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1126 "%spec_uint_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1127 "%spec_uint_from_null = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1128 },
1129 },
1130
1131 // int -> uint vector
1132 {
1133 // original
1134 {
1135 "%spec_uint_one_vec = OpSpecConstantOp %v2uint IAdd %signed_one_vec %unsigned_zero_vec",
1136 "%spec_uint_zero_vec = OpSpecConstantOp %v2uint IAdd %signed_zero_vec %unsigned_zero_vec",
1137 "%spec_uint_from_null = OpSpecConstantOp %v2uint IAdd %signed_null_vec %unsigned_zero_vec",
1138 },
1139 // expected
1140 {
1141 "%spec_uint_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1142 "%spec_uint_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1143 "%spec_uint_from_null = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
1144 },
1145 },
1146
1147 // UConvert scalar
1148 {
1149 // original
1150 {
1151 "%spec_ulong_zero = OpSpecConstantOp %ulong UConvert %unsigned_zero",
1152 "%spec_ulong_one = OpSpecConstantOp %ulong UConvert %unsigned_one",
1153 "%spec_short_zero = OpSpecConstantOp %ushort UConvert %unsigned_zero",
1154 "%spec_short_one = OpSpecConstantOp %ushort UConvert %unsigned_one",
1155 "%uint_max = OpConstant %uint 4294967295",
1156 "%spec_ushort_max = OpSpecConstantOp %ushort UConvert %uint_max",
1157 "%uint_0xDDDDDDDD = OpConstant %uint 3722304989",
1158 "%spec_ushort_0xDDDD = OpSpecConstantOp %ushort UConvert %uint_0xDDDDDDDD",
1159 },
1160 // expected
1161 {
1162 "%spec_ulong_zero = OpConstant %ulong 0",
1163 "%spec_ulong_one = OpConstant %ulong 1",
1164 "%spec_short_zero = OpConstant %ushort 0",
1165 "%spec_short_one = OpConstant %ushort 1",
1166 "%uint_max = OpConstant %uint 4294967295",
1167 "%spec_ushort_max = OpConstant %ushort 65535",
1168 "%uint_0xDDDDDDDD = OpConstant %uint 3722304989",
1169 "%spec_ushort_0xDDDD = OpConstant %ushort 56797",
1170 },
1171 },
1172
1173 // SConvert scalar
1174 {
1175 // original
1176 {
1177 "%spec_long_zero = OpSpecConstantOp %long SConvert %signed_zero",
1178 "%spec_long_one = OpSpecConstantOp %long SConvert %signed_one",
1179 "%spec_long_minus_one = OpSpecConstantOp %long SConvert %signed_minus_one",
1180 "%spec_short_minus_one_trunc = OpSpecConstantOp %short SConvert %signed_minus_one",
1181 "%int_2_to_17_minus_one = OpConstant %int 131071",
1182 "%spec_short_minus_one_trunc2 = OpSpecConstantOp %short SConvert %int_2_to_17_minus_one",
1183 },
1184 // expected
1185 {
1186 "%spec_long_zero = OpConstant %long 0",
1187 "%spec_long_one = OpConstant %long 1",
1188 "%spec_long_minus_one = OpConstant %long -1",
1189 "%spec_short_minus_one_trunc = OpConstant %short -1",
1190 "%int_2_to_17_minus_one = OpConstant %int 131071",
1191 "%spec_short_minus_one_trunc2 = OpConstant %short -1",
1192 },
1193 },
1194
1195 // UConvert vector
1196 {
1197 // original
1198 {
1199 "%spec_v2ulong_zero = OpSpecConstantOp %v2ulong UConvert %unsigned_zero_vec",
1200 "%spec_v2ulong_one = OpSpecConstantOp %v2ulong UConvert %unsigned_one_vec",
1201 },
1202 // expected
1203 {
1204 "%spec_v2ulong_zero = OpConstantComposite %v2ulong %ulong_zero %ulong_zero",
1205 "%ulong_1 = OpConstant %ulong 1",
1206 "%spec_v2ulong_one = OpConstantComposite %v2ulong %ulong_1 %ulong_1",
1207 },
1208 },
1209
1210 // SConvert vector
1211 {
1212 // original
1213 {
1214 "%spec_v2long_zero = OpSpecConstantOp %v2long SConvert %signed_zero_vec",
1215 "%spec_v2long_one = OpSpecConstantOp %v2long SConvert %signed_one_vec",
1216 "%spec_v2long_minus_one = OpSpecConstantOp %v2long SConvert %signed_minus_one_vec",
1217 },
1218 // expected
1219 {
1220 "%spec_v2long_zero = OpConstantComposite %v2long %long_zero %long_zero",
1221 "%long_1 = OpConstant %long 1",
1222 "%spec_v2long_one = OpConstantComposite %v2long %long_1 %long_1",
1223 "%long_n1 = OpConstant %long -1",
1224 "%spec_v2long_minus_one = OpConstantComposite %v2long %long_n1 %long_n1",
1225 },
1226 },
1227 // clang-format on
1228 })));
1229
1230 // Tests about boolean scalar logical operations and comparison operations with
1231 // scalar int/uint type.
1232 INSTANTIATE_TEST_SUITE_P(
1233 Logical, FoldSpecConstantOpAndCompositePassTest,
1234 ::testing::ValuesIn(std::vector<
1235 FoldSpecConstantOpAndCompositePassTestCase>({
1236 // clang-format off
1237 // scalar integer comparison
1238 {
1239 // original
1240 {
1241 "%int_minus_1 = OpConstant %int -1",
1242
1243 "%slt_0_1 = OpSpecConstantOp %bool SLessThan %signed_zero %signed_one",
1244 "%sgt_0_1 = OpSpecConstantOp %bool SGreaterThan %signed_zero %signed_one",
1245 "%sle_2_2 = OpSpecConstantOp %bool SLessThanEqual %signed_two %signed_two",
1246 "%sge_2_1 = OpSpecConstantOp %bool SGreaterThanEqual %signed_two %signed_one",
1247 "%sge_2_null = OpSpecConstantOp %bool SGreaterThanEqual %signed_two %signed_null",
1248 "%sge_minus_1_null = OpSpecConstantOp %bool SGreaterThanEqual %int_minus_1 %signed_null",
1249
1250 "%ult_0_1 = OpSpecConstantOp %bool ULessThan %unsigned_zero %unsigned_one",
1251 "%ugt_0_1 = OpSpecConstantOp %bool UGreaterThan %unsigned_zero %unsigned_one",
1252 "%ule_2_3 = OpSpecConstantOp %bool ULessThanEqual %unsigned_two %unsigned_three",
1253 "%uge_1_1 = OpSpecConstantOp %bool UGreaterThanEqual %unsigned_one %unsigned_one",
1254 "%uge_2_null = OpSpecConstantOp %bool UGreaterThanEqual %unsigned_two %unsigned_null",
1255 "%uge_minus_1_null = OpSpecConstantOp %bool UGreaterThanEqual %int_minus_1 %unsigned_null",
1256 },
1257 // expected
1258 {
1259 "%int_minus_1 = OpConstant %int -1",
1260
1261 "%slt_0_1 = OpConstantTrue %bool",
1262 "%sgt_0_1 = OpConstantFalse %bool",
1263 "%sle_2_2 = OpConstantTrue %bool",
1264 "%sge_2_1 = OpConstantTrue %bool",
1265 "%sge_2_null = OpConstantTrue %bool",
1266 "%sge_minus_1_null = OpConstantFalse %bool",
1267
1268 "%ult_0_1 = OpConstantTrue %bool",
1269 "%ugt_0_1 = OpConstantFalse %bool",
1270 "%ule_2_3 = OpConstantTrue %bool",
1271 "%uge_1_1 = OpConstantTrue %bool",
1272 "%uge_2_null = OpConstantTrue %bool",
1273 "%uge_minus_1_null = OpConstantTrue %bool",
1274 },
1275 },
1276 // Logical and, or, xor.
1277 {
1278 // original
1279 {
1280 "%logical_or = OpSpecConstantOp %bool LogicalOr %bool_true %bool_false",
1281 "%logical_and = OpSpecConstantOp %bool LogicalAnd %bool_true %bool_false",
1282 "%logical_not = OpSpecConstantOp %bool LogicalNot %bool_true",
1283 "%logical_eq = OpSpecConstantOp %bool LogicalEqual %bool_true %bool_true",
1284 "%logical_neq = OpSpecConstantOp %bool LogicalNotEqual %bool_true %bool_true",
1285 "%logical_and_null = OpSpecConstantOp %bool LogicalAnd %bool_true %bool_null",
1286 },
1287 // expected
1288 {
1289 "%logical_or = OpConstantTrue %bool",
1290 "%logical_and = OpConstantFalse %bool",
1291 "%logical_not = OpConstantFalse %bool",
1292 "%logical_eq = OpConstantTrue %bool",
1293 "%logical_neq = OpConstantFalse %bool",
1294 "%logical_and_null = OpConstantFalse %bool",
1295 },
1296 },
1297 // clang-format on
1298 })));
1299
1300 // Tests about arithmetic operations for scalar int and uint types.
1301 INSTANTIATE_TEST_SUITE_P(
1302 ScalarArithmetic, FoldSpecConstantOpAndCompositePassTest,
1303 ::testing::ValuesIn(std::vector<
1304 FoldSpecConstantOpAndCompositePassTestCase>({
1305 // clang-format off
1306 // scalar integer negate
1307 {
1308 // original
1309 {
1310 "%int_minus_1 = OpSpecConstantOp %int SNegate %signed_one",
1311 "%int_minus_2 = OpSpecConstantOp %int SNegate %signed_two",
1312 "%int_neg_null = OpSpecConstantOp %int SNegate %signed_null",
1313 "%int_max = OpConstant %int 2147483647",
1314 "%int_neg_max = OpSpecConstantOp %int SNegate %int_max",
1315 },
1316 // expected
1317 {
1318 "%int_minus_1 = OpConstant %int -1",
1319 "%int_minus_2 = OpConstant %int -2",
1320 "%int_neg_null = OpConstantNull %int",
1321 "%int_max = OpConstant %int 2147483647",
1322 "%int_neg_max = OpConstant %int -2147483647",
1323 },
1324 },
1325 // scalar integer not
1326 {
1327 // original
1328 {
1329 "%uint_4294967294 = OpSpecConstantOp %uint Not %unsigned_one",
1330 "%uint_4294967293 = OpSpecConstantOp %uint Not %unsigned_two",
1331 "%uint_neg_null = OpSpecConstantOp %uint Not %unsigned_null",
1332 },
1333 // expected
1334 {
1335 "%uint_4294967294 = OpConstant %uint 4294967294",
1336 "%uint_4294967293 = OpConstant %uint 4294967293",
1337 "%uint_neg_null = OpConstant %uint 4294967295",
1338 },
1339 },
1340 // scalar integer add, sub, mul, div
1341 {
1342 // original
1343 {
1344 "%signed_max = OpConstant %int 2147483647",
1345 "%signed_min = OpConstant %int -2147483648",
1346
1347 "%spec_int_iadd = OpSpecConstantOp %int IAdd %signed_three %signed_two",
1348 "%spec_int_isub = OpSpecConstantOp %int ISub %signed_one %spec_int_iadd",
1349 "%spec_int_sdiv = OpSpecConstantOp %int SDiv %spec_int_isub %signed_two",
1350 "%spec_int_imul = OpSpecConstantOp %int IMul %spec_int_sdiv %signed_three",
1351 "%spec_int_iadd_null = OpSpecConstantOp %int IAdd %spec_int_imul %signed_null",
1352 "%spec_int_imul_null = OpSpecConstantOp %int IMul %spec_int_iadd_null %signed_null",
1353 "%spec_int_iadd_overflow = OpSpecConstantOp %int IAdd %signed_max %signed_three",
1354 "%spec_int_isub_overflow = OpSpecConstantOp %int ISub %signed_min %signed_three",
1355
1356 "%spec_uint_iadd = OpSpecConstantOp %uint IAdd %unsigned_three %unsigned_two",
1357 "%spec_uint_isub = OpSpecConstantOp %uint ISub %unsigned_one %spec_uint_iadd",
1358 "%spec_uint_udiv = OpSpecConstantOp %uint UDiv %spec_uint_isub %unsigned_three",
1359 "%spec_uint_imul = OpSpecConstantOp %uint IMul %spec_uint_udiv %unsigned_two",
1360 "%spec_uint_isub_null = OpSpecConstantOp %uint ISub %spec_uint_imul %signed_null",
1361 },
1362 // expected
1363 {
1364 "%signed_max = OpConstant %int 2147483647",
1365 "%signed_min = OpConstant %int -2147483648",
1366
1367 "%spec_int_iadd = OpConstant %int 5",
1368 "%spec_int_isub = OpConstant %int -4",
1369 "%spec_int_sdiv = OpConstant %int -2",
1370 "%spec_int_imul = OpConstant %int -6",
1371 "%spec_int_iadd_null = OpConstant %int -6",
1372 "%spec_int_imul_null = OpConstant %int 0",
1373 "%spec_int_iadd_overflow = OpConstant %int -2147483646",
1374 "%spec_int_isub_overflow = OpConstant %int 2147483645",
1375
1376 "%spec_uint_iadd = OpConstant %uint 5",
1377 "%spec_uint_isub = OpConstant %uint 4294967292",
1378 "%spec_uint_udiv = OpConstant %uint 1431655764",
1379 "%spec_uint_imul = OpConstant %uint 2863311528",
1380 "%spec_uint_isub_null = OpConstant %uint 2863311528",
1381 },
1382 },
1383 // scalar integer rem, mod
1384 {
1385 // original
1386 {
1387 // common constants
1388 "%int_7 = OpConstant %int 7",
1389 "%uint_7 = OpConstant %uint 7",
1390 "%int_minus_7 = OpConstant %int -7",
1391 "%int_minus_3 = OpConstant %int -3",
1392
1393 // srem
1394 "%7_srem_3 = OpSpecConstantOp %int SRem %int_7 %signed_three",
1395 "%minus_7_srem_3 = OpSpecConstantOp %int SRem %int_minus_7 %signed_three",
1396 "%7_srem_minus_3 = OpSpecConstantOp %int SRem %int_7 %int_minus_3",
1397 "%minus_7_srem_minus_3 = OpSpecConstantOp %int SRem %int_minus_7 %int_minus_3",
1398 // smod
1399 "%7_smod_3 = OpSpecConstantOp %int SMod %int_7 %signed_three",
1400 "%minus_7_smod_3 = OpSpecConstantOp %int SMod %int_minus_7 %signed_three",
1401 "%7_smod_minus_3 = OpSpecConstantOp %int SMod %int_7 %int_minus_3",
1402 "%minus_7_smod_minus_3 = OpSpecConstantOp %int SMod %int_minus_7 %int_minus_3",
1403 // umod
1404 "%7_umod_3 = OpSpecConstantOp %uint UMod %uint_7 %unsigned_three",
1405 // null constant
1406 "%null_srem_3 = OpSpecConstantOp %int SRem %signed_null %signed_three",
1407 "%null_smod_3 = OpSpecConstantOp %int SMod %signed_null %signed_three",
1408 "%null_umod_3 = OpSpecConstantOp %uint UMod %unsigned_null %unsigned_three",
1409 },
1410 // expected
1411 {
1412 // common constants
1413 "%int_7 = OpConstant %int 7",
1414 "%uint_7 = OpConstant %uint 7",
1415 "%int_minus_7 = OpConstant %int -7",
1416 "%int_minus_3 = OpConstant %int -3",
1417
1418 // srem
1419 "%7_srem_3 = OpConstant %int 1",
1420 "%minus_7_srem_3 = OpConstant %int -1",
1421 "%7_srem_minus_3 = OpConstant %int 1",
1422 "%minus_7_srem_minus_3 = OpConstant %int -1",
1423 // smod
1424 "%7_smod_3 = OpConstant %int 1",
1425 "%minus_7_smod_3 = OpConstant %int 2",
1426 "%7_smod_minus_3 = OpConstant %int -2",
1427 "%minus_7_smod_minus_3 = OpConstant %int -1",
1428 // umod
1429 "%7_umod_3 = OpConstant %uint 1",
1430 // null constant
1431 "%null_srem_3 = OpConstant %int 0",
1432 "%null_smod_3 = OpConstant %int 0",
1433 "%null_umod_3 = OpConstant %uint 0",
1434 },
1435 },
1436 // scalar integer bitwise and shift
1437 {
1438 // original
1439 {
1440 // bitwise
1441 "%xor_1_3 = OpSpecConstantOp %int BitwiseXor %signed_one %signed_three",
1442 "%and_1_2 = OpSpecConstantOp %int BitwiseAnd %signed_one %xor_1_3",
1443 "%or_1_2 = OpSpecConstantOp %int BitwiseOr %signed_one %xor_1_3",
1444 "%xor_3_null = OpSpecConstantOp %int BitwiseXor %or_1_2 %signed_null",
1445
1446 // shift
1447 "%unsigned_31 = OpConstant %uint 31",
1448 "%unsigned_left_shift_max = OpSpecConstantOp %uint ShiftLeftLogical %unsigned_one %unsigned_31",
1449 "%unsigned_right_shift_logical = OpSpecConstantOp %uint ShiftRightLogical %unsigned_left_shift_max %unsigned_31",
1450 "%signed_right_shift_arithmetic = OpSpecConstantOp %int ShiftRightArithmetic %unsigned_left_shift_max %unsigned_31",
1451 "%left_shift_null_31 = OpSpecConstantOp %uint ShiftLeftLogical %unsigned_null %unsigned_31",
1452 "%right_shift_31_null = OpSpecConstantOp %uint ShiftRightLogical %unsigned_31 %unsigned_null",
1453 },
1454 // expected
1455 {
1456 "%xor_1_3 = OpConstant %int 2",
1457 "%and_1_2 = OpConstant %int 0",
1458 "%or_1_2 = OpConstant %int 3",
1459 "%xor_3_null = OpConstant %int 3",
1460
1461 "%unsigned_31 = OpConstant %uint 31",
1462 "%unsigned_left_shift_max = OpConstant %uint 2147483648",
1463 "%unsigned_right_shift_logical = OpConstant %uint 1",
1464 "%signed_right_shift_arithmetic = OpConstant %int -1",
1465 "%left_shift_null_31 = OpConstant %uint 0",
1466 "%right_shift_31_null = OpConstant %uint 31",
1467 },
1468 },
1469 // Skip folding if any operands have undetermined value.
1470 {
1471 // original
1472 {
1473 "%spec_int = OpSpecConstant %int 1",
1474 "%spec_iadd = OpSpecConstantOp %int IAdd %signed_three %spec_int",
1475 },
1476 // expected
1477 {
1478 "%spec_int = OpSpecConstant %int 1",
1479 "%spec_iadd = OpSpecConstantOp %int IAdd %signed_three %spec_int",
1480 },
1481 },
1482 // clang-format on
1483 })));
1484
1485 // Tests about arithmetic operations for vector int and uint types.
1486 INSTANTIATE_TEST_SUITE_P(
1487 VectorArithmetic, FoldSpecConstantOpAndCompositePassTest,
1488 ::testing::ValuesIn(std::vector<
1489 FoldSpecConstantOpAndCompositePassTestCase>({
1490 // clang-format off
1491 // vector integer negate
1492 {
1493 // original
1494 {
1495 "%v2int_minus_1 = OpSpecConstantOp %v2int SNegate %signed_one_vec",
1496 "%v2int_minus_2 = OpSpecConstantOp %v2int SNegate %signed_two_vec",
1497 "%v2int_neg_null = OpSpecConstantOp %v2int SNegate %signed_null_vec",
1498 },
1499 // expected
1500 {
1501 "%v2int_minus_1 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1502 "%int_n2 = OpConstant %int -2",
1503 "%v2int_minus_2 = OpConstantComposite %v2int %int_n2 %int_n2",
1504 "%v2int_neg_null = OpConstantComposite %v2int %signed_null %signed_null",
1505 },
1506 },
1507 // vector integer (including null vetors) add, sub, div, mul
1508 {
1509 // original
1510 {
1511 "%spec_v2int_iadd = OpSpecConstantOp %v2int IAdd %signed_three_vec %signed_two_vec",
1512 "%spec_v2int_isub = OpSpecConstantOp %v2int ISub %signed_one_vec %spec_v2int_iadd",
1513 "%spec_v2int_sdiv = OpSpecConstantOp %v2int SDiv %spec_v2int_isub %signed_two_vec",
1514 "%spec_v2int_imul = OpSpecConstantOp %v2int IMul %spec_v2int_sdiv %signed_three_vec",
1515 "%spec_v2int_iadd_null = OpSpecConstantOp %v2int IAdd %spec_v2int_imul %signed_null_vec",
1516
1517 "%spec_v2uint_iadd = OpSpecConstantOp %v2uint IAdd %unsigned_three_vec %unsigned_two_vec",
1518 "%spec_v2uint_isub = OpSpecConstantOp %v2uint ISub %unsigned_one_vec %spec_v2uint_iadd",
1519 "%spec_v2uint_udiv = OpSpecConstantOp %v2uint UDiv %spec_v2uint_isub %unsigned_three_vec",
1520 "%spec_v2uint_imul = OpSpecConstantOp %v2uint IMul %spec_v2uint_udiv %unsigned_two_vec",
1521 "%spec_v2uint_isub_null = OpSpecConstantOp %v2uint ISub %spec_v2uint_imul %signed_null_vec",
1522 },
1523 // expected
1524 {
1525 "%int_5 = OpConstant %int 5",
1526 "%spec_v2int_iadd = OpConstantComposite %v2int %int_5 %int_5",
1527 "%int_n4 = OpConstant %int -4",
1528 "%spec_v2int_isub = OpConstantComposite %v2int %int_n4 %int_n4",
1529 "%int_n2 = OpConstant %int -2",
1530 "%spec_v2int_sdiv = OpConstantComposite %v2int %int_n2 %int_n2",
1531 "%int_n6 = OpConstant %int -6",
1532 "%spec_v2int_imul = OpConstantComposite %v2int %int_n6 %int_n6",
1533 "%spec_v2int_iadd_null = OpConstantComposite %v2int %int_n6 %int_n6",
1534
1535 "%uint_5 = OpConstant %uint 5",
1536 "%spec_v2uint_iadd = OpConstantComposite %v2uint %uint_5 %uint_5",
1537 "%uint_4294967292 = OpConstant %uint 4294967292",
1538 "%spec_v2uint_isub = OpConstantComposite %v2uint %uint_4294967292 %uint_4294967292",
1539 "%uint_1431655764 = OpConstant %uint 1431655764",
1540 "%spec_v2uint_udiv = OpConstantComposite %v2uint %uint_1431655764 %uint_1431655764",
1541 "%uint_2863311528 = OpConstant %uint 2863311528",
1542 "%spec_v2uint_imul = OpConstantComposite %v2uint %uint_2863311528 %uint_2863311528",
1543 "%spec_v2uint_isub_null = OpConstantComposite %v2uint %uint_2863311528 %uint_2863311528",
1544 },
1545 },
1546 // vector integer rem, mod
1547 {
1548 // original
1549 {
1550 // common constants
1551 "%int_7 = OpConstant %int 7",
1552 "%v2int_7 = OpConstantComposite %v2int %int_7 %int_7",
1553 "%uint_7 = OpConstant %uint 7",
1554 "%v2uint_7 = OpConstantComposite %v2uint %uint_7 %uint_7",
1555 "%int_minus_7 = OpConstant %int -7",
1556 "%v2int_minus_7 = OpConstantComposite %v2int %int_minus_7 %int_minus_7",
1557 "%int_minus_3 = OpConstant %int -3",
1558 "%v2int_minus_3 = OpConstantComposite %v2int %int_minus_3 %int_minus_3",
1559
1560 // srem
1561 "%7_srem_3 = OpSpecConstantOp %v2int SRem %v2int_7 %signed_three_vec",
1562 "%minus_7_srem_3 = OpSpecConstantOp %v2int SRem %v2int_minus_7 %signed_three_vec",
1563 "%7_srem_minus_3 = OpSpecConstantOp %v2int SRem %v2int_7 %v2int_minus_3",
1564 "%minus_7_srem_minus_3 = OpSpecConstantOp %v2int SRem %v2int_minus_7 %v2int_minus_3",
1565 // smod
1566 "%7_smod_3 = OpSpecConstantOp %v2int SMod %v2int_7 %signed_three_vec",
1567 "%minus_7_smod_3 = OpSpecConstantOp %v2int SMod %v2int_minus_7 %signed_three_vec",
1568 "%7_smod_minus_3 = OpSpecConstantOp %v2int SMod %v2int_7 %v2int_minus_3",
1569 "%minus_7_smod_minus_3 = OpSpecConstantOp %v2int SMod %v2int_minus_7 %v2int_minus_3",
1570 // umod
1571 "%7_umod_3 = OpSpecConstantOp %v2uint UMod %v2uint_7 %unsigned_three_vec",
1572 },
1573 // expected
1574 {
1575 // common constants
1576 "%int_7 = OpConstant %int 7",
1577 "%v2int_7 = OpConstantComposite %v2int %int_7 %int_7",
1578 "%uint_7 = OpConstant %uint 7",
1579 "%v2uint_7 = OpConstantComposite %v2uint %uint_7 %uint_7",
1580 "%int_minus_7 = OpConstant %int -7",
1581 "%v2int_minus_7 = OpConstantComposite %v2int %int_minus_7 %int_minus_7",
1582 "%int_minus_3 = OpConstant %int -3",
1583 "%v2int_minus_3 = OpConstantComposite %v2int %int_minus_3 %int_minus_3",
1584
1585 // srem
1586 "%7_srem_3 = OpConstantComposite %v2int %signed_one %signed_one",
1587 "%minus_7_srem_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1588 "%7_srem_minus_3 = OpConstantComposite %v2int %signed_one %signed_one",
1589 "%minus_7_srem_minus_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1590 // smod
1591 "%7_smod_3 = OpConstantComposite %v2int %signed_one %signed_one",
1592 "%minus_7_smod_3 = OpConstantComposite %v2int %signed_two %signed_two",
1593 "%int_n2 = OpConstant %int -2",
1594 "%7_smod_minus_3 = OpConstantComposite %v2int %int_n2 %int_n2",
1595 "%minus_7_smod_minus_3 = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1596 // umod
1597 "%7_umod_3 = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1598 },
1599 },
1600 // vector integer bitwise, shift
1601 {
1602 // original
1603 {
1604 "%xor_1_3 = OpSpecConstantOp %v2int BitwiseXor %signed_one_vec %signed_three_vec",
1605 "%and_1_2 = OpSpecConstantOp %v2int BitwiseAnd %signed_one_vec %xor_1_3",
1606 "%or_1_2 = OpSpecConstantOp %v2int BitwiseOr %signed_one_vec %xor_1_3",
1607
1608 "%unsigned_31 = OpConstant %uint 31",
1609 "%v2unsigned_31 = OpConstantComposite %v2uint %unsigned_31 %unsigned_31",
1610 "%unsigned_left_shift_max = OpSpecConstantOp %v2uint ShiftLeftLogical %unsigned_one_vec %v2unsigned_31",
1611 "%unsigned_right_shift_logical = OpSpecConstantOp %v2uint ShiftRightLogical %unsigned_left_shift_max %v2unsigned_31",
1612 "%signed_right_shift_arithmetic = OpSpecConstantOp %v2int ShiftRightArithmetic %unsigned_left_shift_max %v2unsigned_31",
1613 },
1614 // expected
1615 {
1616 "%xor_1_3 = OpConstantComposite %v2int %signed_two %signed_two",
1617 "%and_1_2 = OpConstantComposite %v2int %signed_zero %signed_zero",
1618 "%or_1_2 = OpConstantComposite %v2int %signed_three %signed_three",
1619
1620 "%unsigned_31 = OpConstant %uint 31",
1621 "%v2unsigned_31 = OpConstantComposite %v2uint %unsigned_31 %unsigned_31",
1622 "%uint_2147483648 = OpConstant %uint 2147483648",
1623 "%unsigned_left_shift_max = OpConstantComposite %v2uint %uint_2147483648 %uint_2147483648",
1624 "%unsigned_right_shift_logical = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
1625 "%signed_right_shift_arithmetic = OpConstantComposite %v2int %signed_minus_one %signed_minus_one",
1626 },
1627 },
1628 // Skip folding if any vector operands or components of the operands
1629 // have undetermined value.
1630 {
1631 // original
1632 {
1633 "%spec_int = OpSpecConstant %int 1",
1634 "%spec_vec = OpSpecConstantComposite %v2int %signed_zero %spec_int",
1635 "%spec_iadd = OpSpecConstantOp %v2int IAdd %signed_three_vec %spec_vec",
1636 },
1637 // expected
1638 {
1639 "%spec_int = OpSpecConstant %int 1",
1640 "%spec_vec = OpSpecConstantComposite %v2int %signed_zero %spec_int",
1641 "%spec_iadd = OpSpecConstantOp %v2int IAdd %signed_three_vec %spec_vec",
1642 },
1643 },
1644 // Skip folding if any vector operands are defined by OpUndef
1645 {
1646 // original
1647 {
1648 "%undef = OpUndef %int",
1649 "%vec = OpConstantComposite %v2int %undef %signed_one",
1650 "%spec_iadd = OpSpecConstantOp %v2int IAdd %signed_three_vec %vec",
1651 },
1652 // expected
1653 {
1654 "%undef = OpUndef %int",
1655 "%vec = OpConstantComposite %v2int %undef %signed_one",
1656 "%spec_iadd = OpSpecConstantOp %v2int IAdd %signed_three_vec %vec",
1657 },
1658 },
1659 // clang-format on
1660 })));
1661
1662 // Tests for SpecConstantOp CompositeExtract instruction
1663 INSTANTIATE_TEST_SUITE_P(
1664 CompositeExtract, FoldSpecConstantOpAndCompositePassTest,
1665 ::testing::ValuesIn(std::vector<
1666 FoldSpecConstantOpAndCompositePassTestCase>({
1667 // clang-format off
1668 // normal vector
1669 {
1670 // original
1671 {
1672 "%r = OpSpecConstantOp %int CompositeExtract %signed_three_vec 0",
1673 "%x = OpSpecConstantOp %int CompositeExtract %v4int_0_1_2_3 0",
1674 "%y = OpSpecConstantOp %int CompositeExtract %v4int_0_1_2_3 1",
1675 "%z = OpSpecConstantOp %int CompositeExtract %v4int_0_1_2_3 2",
1676 "%w = OpSpecConstantOp %int CompositeExtract %v4int_0_1_2_3 3",
1677 },
1678 // expected
1679 {
1680 "%r = OpConstant %int 3",
1681 "%x = OpConstant %int 0",
1682 "%y = OpConstant %int 1",
1683 "%z = OpConstant %int 2",
1684 "%w = OpConstant %int 3",
1685 },
1686 },
1687 // null vector
1688 {
1689 // original
1690 {
1691 "%x = OpSpecConstantOp %int CompositeExtract %signed_null_vec 0",
1692 "%y = OpSpecConstantOp %int CompositeExtract %signed_null_vec 1",
1693 "%null_v4int = OpConstantNull %v4int",
1694 "%z = OpSpecConstantOp %int CompositeExtract %signed_null_vec 2",
1695 },
1696 // expected
1697 {
1698 "%x = OpConstantNull %int",
1699 "%y = OpConstantNull %int",
1700 "%null_v4int = OpConstantNull %v4int",
1701 "%z = OpConstantNull %int",
1702 }
1703 },
1704 // normal flat struct
1705 {
1706 // original
1707 {
1708 "%float_1 = OpConstant %float 1",
1709 "%flat_1 = OpConstantComposite %flat_struct %bool_true %signed_null %float_1",
1710 "%extract_bool = OpSpecConstantOp %bool CompositeExtract %flat_1 0",
1711 "%extract_int = OpSpecConstantOp %int CompositeExtract %flat_1 1",
1712 "%extract_float_1 = OpSpecConstantOp %float CompositeExtract %flat_1 2",
1713 // foldable composite constants built with OpSpecConstantComposite
1714 // should also be processed.
1715 "%flat_2 = OpSpecConstantComposite %flat_struct %bool_true %signed_null %float_1",
1716 "%extract_float_2 = OpSpecConstantOp %float CompositeExtract %flat_2 2",
1717 },
1718 // expected
1719 {
1720 "%float_1 = OpConstant %float 1",
1721 "%flat_1 = OpConstantComposite %flat_struct %bool_true %signed_null %float_1",
1722 "%extract_bool = OpConstantTrue %bool",
1723 "%extract_int = OpConstantNull %int",
1724 "%extract_float_1 = OpConstant %float 1",
1725 "%flat_2 = OpConstantComposite %flat_struct %bool_true %signed_null %float_1",
1726 "%extract_float_2 = OpConstant %float 1",
1727 },
1728 },
1729 // null flat struct
1730 {
1731 // original
1732 {
1733 "%flat = OpConstantNull %flat_struct",
1734 "%extract_bool = OpSpecConstantOp %bool CompositeExtract %flat 0",
1735 "%extract_int = OpSpecConstantOp %int CompositeExtract %flat 1",
1736 "%extract_float = OpSpecConstantOp %float CompositeExtract %flat 2",
1737 },
1738 // expected
1739 {
1740 "%flat = OpConstantNull %flat_struct",
1741 "%extract_bool = OpConstantNull %bool",
1742 "%extract_int = OpConstantNull %int",
1743 "%extract_float = OpConstantNull %float",
1744 },
1745 },
1746 // normal nested struct
1747 {
1748 // original
1749 {
1750 "%float_1 = OpConstant %float 1",
1751 "%inner = OpConstantComposite %inner_struct %bool_true %signed_null %float_1",
1752 "%outer = OpConstantComposite %outer_struct %inner %signed_one",
1753 "%extract_inner = OpSpecConstantOp %inner_struct CompositeExtract %outer 0",
1754 "%extract_int = OpSpecConstantOp %int CompositeExtract %outer 1",
1755 "%extract_inner_float = OpSpecConstantOp %float CompositeExtract %outer 0 2",
1756 },
1757 // expected
1758 {
1759 "%float_1 = OpConstant %float 1",
1760 "%inner = OpConstantComposite %inner_struct %bool_true %signed_null %float_1",
1761 "%outer = OpConstantComposite %outer_struct %inner %signed_one",
1762 "%extract_inner = OpConstantComposite %inner_struct %bool_true %signed_null %float_1",
1763 "%extract_int = OpConstant %int 1",
1764 "%extract_inner_float = OpConstant %float 1",
1765 },
1766 },
1767 // null nested struct
1768 {
1769 // original
1770 {
1771 "%outer = OpConstantNull %outer_struct",
1772 "%extract_inner = OpSpecConstantOp %inner_struct CompositeExtract %outer 0",
1773 "%extract_int = OpSpecConstantOp %int CompositeExtract %outer 1",
1774 "%extract_inner_float = OpSpecConstantOp %float CompositeExtract %outer 0 2",
1775 },
1776 // expected
1777 {
1778 "%outer = OpConstantNull %outer_struct",
1779 "%extract_inner = OpConstantNull %inner_struct",
1780 "%extract_int = OpConstantNull %int",
1781 "%extract_inner_float = OpConstantNull %float",
1782 },
1783 },
1784 // skip folding if the any composite constant's value are not fully
1785 // determined, even though the extracting target might have
1786 // determined value.
1787 {
1788 // original
1789 {
1790 "%float_1 = OpConstant %float 1",
1791 "%spec_float = OpSpecConstant %float 1",
1792 "%spec_inner = OpSpecConstantComposite %inner_struct %bool_true %signed_null %spec_float",
1793 "%spec_outer = OpSpecConstantComposite %outer_struct %spec_inner %signed_one",
1794 "%spec_vec = OpSpecConstantComposite %v2float %spec_float %float_1",
1795 "%extract_inner = OpSpecConstantOp %int CompositeExtract %spec_inner 1",
1796 "%extract_outer = OpSpecConstantOp %int CompositeExtract %spec_outer 1",
1797 "%extract_vec = OpSpecConstantOp %float CompositeExtract %spec_vec 1",
1798 },
1799 // expected
1800 {
1801 "%float_1 = OpConstant %float 1",
1802 "%spec_float = OpSpecConstant %float 1",
1803 "%spec_inner = OpSpecConstantComposite %inner_struct %bool_true %signed_null %spec_float",
1804 "%spec_outer = OpSpecConstantComposite %outer_struct %spec_inner %signed_one",
1805 "%spec_vec = OpSpecConstantComposite %v2float %spec_float %float_1",
1806 "%extract_inner = OpSpecConstantOp %int CompositeExtract %spec_inner 1",
1807 "%extract_outer = OpSpecConstantOp %int CompositeExtract %spec_outer 1",
1808 "%extract_vec = OpSpecConstantOp %float CompositeExtract %spec_vec 1",
1809 },
1810 },
1811 // skip if the composite constant depends on the result of OpUndef,
1812 // even though the composite extract target element does not depends
1813 // on the OpUndef.
1814 {
1815 // original
1816 {
1817 "%undef = OpUndef %float",
1818 "%inner = OpConstantComposite %inner_struct %bool_true %signed_one %undef",
1819 "%outer = OpConstantComposite %outer_struct %inner %signed_one",
1820 "%extract_inner = OpSpecConstantOp %int CompositeExtract %inner 1",
1821 "%extract_outer = OpSpecConstantOp %int CompositeExtract %outer 1",
1822 },
1823 // expected
1824 {
1825 "%undef = OpUndef %float",
1826 "%inner = OpConstantComposite %inner_struct %bool_true %signed_one %undef",
1827 "%outer = OpConstantComposite %outer_struct %inner %signed_one",
1828 "%extract_inner = OpSpecConstantOp %int CompositeExtract %inner 1",
1829 "%extract_outer = OpSpecConstantOp %int CompositeExtract %outer 1",
1830 },
1831 },
1832 // TODO(qining): Add tests for Array and other composite type constants.
1833 // clang-format on
1834 })));
1835
1836 // Tests the swizzle operations for spec const vectors.
1837 INSTANTIATE_TEST_SUITE_P(
1838 VectorShuffle, FoldSpecConstantOpAndCompositePassTest,
1839 ::testing::ValuesIn(std::vector<
1840 FoldSpecConstantOpAndCompositePassTestCase>({
1841 // clang-format off
1842 // normal vector
1843 {
1844 // original
1845 {
1846 "%xy = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 0 1",
1847 "%yz = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 1 2",
1848 "%zw = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 2 3",
1849 "%wx = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 3 0",
1850 "%xx = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 0 0",
1851 "%yyy = OpSpecConstantOp %v3int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 1 1 1",
1852 "%wwww = OpSpecConstantOp %v4int VectorShuffle %v4int_0_1_2_3 %v4int_0_1_2_3 2 2 2 2",
1853 },
1854 // expected
1855 {
1856 "%xy = OpConstantComposite %v2int %signed_zero %signed_one",
1857 "%yz = OpConstantComposite %v2int %signed_one %signed_two",
1858 "%zw = OpConstantComposite %v2int %signed_two %signed_three",
1859 "%wx = OpConstantComposite %v2int %signed_three %signed_zero",
1860 "%xx = OpConstantComposite %v2int %signed_zero %signed_zero",
1861 "%yyy = OpConstantComposite %v3int %signed_one %signed_one %signed_one",
1862 "%wwww = OpConstantComposite %v4int %signed_two %signed_two %signed_two %signed_two",
1863 },
1864 },
1865 // null vector
1866 {
1867 // original
1868 {
1869 "%a = OpSpecConstantOp %v2int VectorShuffle %signed_null_vec %v4int_0_1_2_3 0 1",
1870 "%b = OpSpecConstantOp %v2int VectorShuffle %signed_null_vec %v4int_0_1_2_3 2 3",
1871 "%c = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %signed_null_vec 3 4",
1872 "%d = OpSpecConstantOp %v2int VectorShuffle %signed_null_vec %signed_null_vec 1 2",
1873 },
1874 // expected
1875 {
1876 "%a = OpConstantComposite %v2int %signed_null %signed_null",
1877 "%b = OpConstantComposite %v2int %signed_zero %signed_one",
1878 "%c = OpConstantComposite %v2int %signed_three %signed_null",
1879 "%d = OpConstantComposite %v2int %signed_null %signed_null",
1880 }
1881 },
1882 // skip if any of the components of the vector operands do not have
1883 // determined value, even though the result vector might not be
1884 // built with those undermined values.
1885 {
1886 // original
1887 {
1888 "%spec_int = OpSpecConstant %int 1",
1889 "%spec_ivec = OpSpecConstantComposite %v2int %signed_null %spec_int",
1890 "%a = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %spec_ivec 0 1",
1891 "%b = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %spec_ivec 3 4",
1892 },
1893 // expected
1894 {
1895 "%spec_int = OpSpecConstant %int 1",
1896 "%spec_ivec = OpSpecConstantComposite %v2int %signed_null %spec_int",
1897 "%a = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %spec_ivec 0 1",
1898 "%b = OpSpecConstantOp %v2int VectorShuffle %v4int_0_1_2_3 %spec_ivec 3 4",
1899 },
1900 },
1901 // Skip if any components of the two vector operands depend on
1902 // the result of OpUndef. Even though the selected components do
1903 // not depend on the OpUndef result.
1904 {
1905 // original
1906 {
1907 "%undef = OpUndef %int",
1908 "%vec_1 = OpConstantComposite %v2int %undef %signed_one",
1909 "%dep = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 0 3",
1910 "%not_dep_element = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 1 3",
1911 "%no_dep_vector = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 2 3",
1912 },
1913 // expected
1914 {
1915 "%undef = OpUndef %int",
1916 "%vec_1 = OpConstantComposite %v2int %undef %signed_one",
1917 "%dep = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 0 3",
1918 "%not_dep_element = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 1 3",
1919 "%no_dep_vector = OpSpecConstantOp %v2int VectorShuffle %vec_1 %signed_three_vec 2 3",
1920 },
1921 },
1922 // clang-format on
1923 })));
1924
1925 // Test with long use-def chain.
1926 INSTANTIATE_TEST_SUITE_P(
1927 LongDefUseChain, FoldSpecConstantOpAndCompositePassTest,
1928 ::testing::ValuesIn(std::vector<
1929 FoldSpecConstantOpAndCompositePassTestCase>({
1930 // clang-format off
1931 // Long Def-Use chain with binary operations.
1932 {
1933 // original
1934 {
1935 "%array_size = OpConstant %int 4",
1936 "%type_arr_int_4 = OpTypeArray %int %array_size",
1937 "%spec_int_0 = OpConstant %int 100",
1938 "%spec_int_1 = OpConstant %int 1",
1939 "%spec_int_2 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_1",
1940 "%spec_int_3 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_2",
1941 "%spec_int_4 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_3",
1942 "%spec_int_5 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_4",
1943 "%spec_int_6 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_5",
1944 "%spec_int_7 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_6",
1945 "%spec_int_8 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_7",
1946 "%spec_int_9 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_8",
1947 "%spec_int_10 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_9",
1948 "%spec_int_11 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_10",
1949 "%spec_int_12 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_11",
1950 "%spec_int_13 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_12",
1951 "%spec_int_14 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_13",
1952 "%spec_int_15 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_14",
1953 "%spec_int_16 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_15",
1954 "%spec_int_17 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_16",
1955 "%spec_int_18 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_17",
1956 "%spec_int_19 = OpSpecConstantOp %int IAdd %spec_int_0 %spec_int_18",
1957 "%spec_int_20 = OpSpecConstantOp %int ISub %spec_int_0 %spec_int_19",
1958 "%used_vec_a = OpSpecConstantComposite %v2int %spec_int_18 %spec_int_19",
1959 "%used_vec_b = OpSpecConstantOp %v2int IMul %used_vec_a %used_vec_a",
1960 "%spec_int_21 = OpSpecConstantOp %int CompositeExtract %used_vec_b 0",
1961 "%array = OpConstantComposite %type_arr_int_4 %spec_int_20 %spec_int_20 %spec_int_21 %spec_int_21",
1962 // Spec constants whose values can not be fully resolved should
1963 // not be processed.
1964 "%spec_int_22 = OpSpecConstant %int 123",
1965 "%spec_int_23 = OpSpecConstantOp %int IAdd %spec_int_22 %signed_one",
1966 },
1967 // expected
1968 {
1969 "%array_size = OpConstant %int 4",
1970 "%type_arr_int_4 = OpTypeArray %int %array_size",
1971 "%spec_int_0 = OpConstant %int 100",
1972 "%spec_int_1 = OpConstant %int 1",
1973 "%spec_int_2 = OpConstant %int 101",
1974 "%spec_int_3 = OpConstant %int -1",
1975 "%spec_int_4 = OpConstant %int 99",
1976 "%spec_int_5 = OpConstant %int 1",
1977 "%spec_int_6 = OpConstant %int 101",
1978 "%spec_int_7 = OpConstant %int -1",
1979 "%spec_int_8 = OpConstant %int 99",
1980 "%spec_int_9 = OpConstant %int 1",
1981 "%spec_int_10 = OpConstant %int 101",
1982 "%spec_int_11 = OpConstant %int -1",
1983 "%spec_int_12 = OpConstant %int 99",
1984 "%spec_int_13 = OpConstant %int 1",
1985 "%spec_int_14 = OpConstant %int 101",
1986 "%spec_int_15 = OpConstant %int -1",
1987 "%spec_int_16 = OpConstant %int 101",
1988 "%spec_int_17 = OpConstant %int 201",
1989 "%spec_int_18 = OpConstant %int -101",
1990 "%spec_int_19 = OpConstant %int -1",
1991 "%spec_int_20 = OpConstant %int 101",
1992 "%used_vec_a = OpConstantComposite %v2int %spec_int_18 %spec_int_19",
1993 "%int_10201 = OpConstant %int 10201",
1994 "%used_vec_b = OpConstantComposite %v2int %int_10201 %signed_one",
1995 "%spec_int_21 = OpConstant %int 10201",
1996 "%array = OpConstantComposite %type_arr_int_4 %spec_int_20 %spec_int_20 %spec_int_21 %spec_int_21",
1997 "%spec_int_22 = OpSpecConstant %int 123",
1998 "%spec_int_23 = OpSpecConstantOp %int IAdd %spec_int_22 %signed_one",
1999 },
2000 },
2001 // Long Def-Use chain with swizzle
2002 })));
2003
2004 } // namespace
2005 } // namespace opt
2006 } // namespace spvtools
2007