Debian Codegraph @

test/opt/fold_spec_const_op_composite_test.cpp

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