Debian Codegraph @

test/opt/aggressive_dead_code_elim_test.cpp

raw
1 // Copyright (c) 2017 Valve Corporation
2 // Copyright (c) 2017 LunarG Inc.
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 <string>
17 #include <vector>
18
19 #include "gmock/gmock.h"
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 AggressiveDCETest = PassTest<::testing::Test>;
29
30 using ::testing::HasSubstr;
31
32 TEST_F(AggressiveDCETest, EliminateExtendedInst) {
33 // #version 140
34 //
35 // in vec4 BaseColor;
36 // in vec4 Dead;
37 //
38 // void main()
39 // {
40 // vec4 v = BaseColor;
41 // vec4 dv = sqrt(Dead);
42 // gl_FragColor = v;
43 // }
44 const std::string spirv = R"(
45 OpCapability Shader
46 %1 = OpExtInstImport "GLSL.std.450"
47 OpMemoryModel Logical GLSL450
48 ; CHECK: OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
49 OpEntryPoint Fragment %main "main" %BaseColor %Dead %gl_FragColor
50 OpExecutionMode %main OriginUpperLeft
51 OpSource GLSL 140
52 OpName %main "main"
53 OpName %v "v"
54 OpName %BaseColor "BaseColor"
55 ; CHECK-NOT: OpName %dv "dv"
56 OpName %dv "dv"
57 ; CHECK-NOT: OpName %Dead "Dead"
58 OpName %Dead "Dead"
59 OpName %gl_FragColor "gl_FragColor"
60 %void = OpTypeVoid
61 %9 = OpTypeFunction %void
62 %float = OpTypeFloat 32
63 %v4float = OpTypeVector %float 4
64 %_ptr_Function_v4float = OpTypePointer Function %v4float
65 %_ptr_Input_v4float = OpTypePointer Input %v4float
66 %BaseColor = OpVariable %_ptr_Input_v4float Input
67 ; CHECK-NOT: %Dead = OpVariable
68 %Dead = OpVariable %_ptr_Input_v4float Input
69 %_ptr_Output_v4float = OpTypePointer Output %v4float
70 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
71 %main = OpFunction %void None %9
72 %15 = OpLabel
73 %v = OpVariable %_ptr_Function_v4float Function
74 ; CHECK-NOT: %dv = OpVariable
75 %dv = OpVariable %_ptr_Function_v4float Function
76 %16 = OpLoad %v4float %BaseColor
77 OpStore %v %16
78 ; CHECK-NOT: OpLoad %v4float %Dead
79 %17 = OpLoad %v4float %Dead
80 ; CHECK-NOT: OpExtInst %v4float %1 Sqrt
81 %18 = OpExtInst %v4float %1 Sqrt %17
82 ; CHECK-NOT: OpStore %dv
83 OpStore %dv %18
84 %19 = OpLoad %v4float %v
85 OpStore %gl_FragColor %19
86 OpReturn
87 OpFunctionEnd
88 )";
89
90 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
91 }
92
93 TEST_F(AggressiveDCETest, NoEliminateFrexp) {
94 // Note: SPIR-V hand-edited to utilize Frexp
95 //
96 // #version 450
97 //
98 // in vec4 BaseColor;
99 // in vec4 Dead;
100 // out vec4 Color;
101 // out ivec4 iv2;
102 //
103 // void main()
104 // {
105 // vec4 v = BaseColor;
106 // vec4 dv = frexp(Dead, iv2);
107 // Color = v;
108 // }
109
110 const std::string predefs1 =
111 R"(OpCapability Shader
112 %1 = OpExtInstImport "GLSL.std.450"
113 OpMemoryModel Logical GLSL450
114 OpEntryPoint Fragment %main "main" %BaseColor %Dead %iv2 %Color
115 OpExecutionMode %main OriginUpperLeft
116 OpSource GLSL 450
117 )";
118
119 const std::string names_before =
120 R"(OpName %main "main"
121 OpName %v "v"
122 OpName %BaseColor "BaseColor"
123 OpName %dv "dv"
124 OpName %Dead "Dead"
125 OpName %iv2 "iv2"
126 OpName %ResType "ResType"
127 OpName %Color "Color"
128 )";
129
130 const std::string names_after =
131 R"(OpName %main "main"
132 OpName %v "v"
133 OpName %BaseColor "BaseColor"
134 OpName %Dead "Dead"
135 OpName %iv2 "iv2"
136 OpName %Color "Color"
137 )";
138
139 const std::string predefs2_before =
140 R"(%void = OpTypeVoid
141 %11 = OpTypeFunction %void
142 %float = OpTypeFloat 32
143 %v4float = OpTypeVector %float 4
144 %_ptr_Function_v4float = OpTypePointer Function %v4float
145 %_ptr_Input_v4float = OpTypePointer Input %v4float
146 %BaseColor = OpVariable %_ptr_Input_v4float Input
147 %Dead = OpVariable %_ptr_Input_v4float Input
148 %int = OpTypeInt 32 1
149 %v4int = OpTypeVector %int 4
150 %_ptr_Output_v4int = OpTypePointer Output %v4int
151 %iv2 = OpVariable %_ptr_Output_v4int Output
152 %ResType = OpTypeStruct %v4float %v4int
153 %_ptr_Output_v4float = OpTypePointer Output %v4float
154 %Color = OpVariable %_ptr_Output_v4float Output
155 )";
156
157 const std::string predefs2_after =
158 R"(%void = OpTypeVoid
159 %11 = OpTypeFunction %void
160 %float = OpTypeFloat 32
161 %v4float = OpTypeVector %float 4
162 %_ptr_Function_v4float = OpTypePointer Function %v4float
163 %_ptr_Input_v4float = OpTypePointer Input %v4float
164 %BaseColor = OpVariable %_ptr_Input_v4float Input
165 %Dead = OpVariable %_ptr_Input_v4float Input
166 %int = OpTypeInt 32 1
167 %v4int = OpTypeVector %int 4
168 %_ptr_Output_v4int = OpTypePointer Output %v4int
169 %iv2 = OpVariable %_ptr_Output_v4int Output
170 %_ptr_Output_v4float = OpTypePointer Output %v4float
171 %Color = OpVariable %_ptr_Output_v4float Output
172 )";
173
174 const std::string func_before =
175 R"(%main = OpFunction %void None %11
176 %20 = OpLabel
177 %v = OpVariable %_ptr_Function_v4float Function
178 %dv = OpVariable %_ptr_Function_v4float Function
179 %21 = OpLoad %v4float %BaseColor
180 OpStore %v %21
181 %22 = OpLoad %v4float %Dead
182 %23 = OpExtInst %v4float %1 Frexp %22 %iv2
183 OpStore %dv %23
184 %24 = OpLoad %v4float %v
185 OpStore %Color %24
186 OpReturn
187 OpFunctionEnd
188 )";
189
190 const std::string func_after =
191 R"(%main = OpFunction %void None %11
192 %20 = OpLabel
193 %v = OpVariable %_ptr_Function_v4float Function
194 %21 = OpLoad %v4float %BaseColor
195 OpStore %v %21
196 %22 = OpLoad %v4float %Dead
197 %23 = OpExtInst %v4float %1 Frexp %22 %iv2
198 %24 = OpLoad %v4float %v
199 OpStore %Color %24
200 OpReturn
201 OpFunctionEnd
202 )";
203
204 SinglePassRunAndCheck<AggressiveDCEPass>(
205 predefs1 + names_before + predefs2_before + func_before,
206 predefs1 + names_after + predefs2_after + func_after, true, true);
207 }
208
209 TEST_F(AggressiveDCETest, EliminateDecorate) {
210 // Note: The SPIR-V was hand-edited to add the OpDecorate
211 //
212 // #version 140
213 //
214 // in vec4 BaseColor;
215 // in vec4 Dead;
216 //
217 // void main()
218 // {
219 // vec4 v = BaseColor;
220 // vec4 dv = Dead * 0.5;
221 // gl_FragColor = v;
222 // }
223
224 const std::string spirv =
225 R"(
226 OpCapability Shader
227 %1 = OpExtInstImport "GLSL.std.450"
228 OpMemoryModel Logical GLSL450
229 OpEntryPoint Fragment %main "main" %BaseColor %Dead %gl_FragColor
230 OpExecutionMode %main OriginUpperLeft
231 OpSource GLSL 140
232 OpName %main "main"
233 OpName %v "v"
234 OpName %BaseColor "BaseColor"
235 OpName %dv "dv"
236 OpName %Dead "Dead"
237 OpName %gl_FragColor "gl_FragColor"
238 ; CHECK-NOT: OpDecorate
239 OpDecorate %8 RelaxedPrecision
240 %void = OpTypeVoid
241 %10 = OpTypeFunction %void
242 %float = OpTypeFloat 32
243 %v4float = OpTypeVector %float 4
244 %_ptr_Function_v4float = OpTypePointer Function %v4float
245 %_ptr_Input_v4float = OpTypePointer Input %v4float
246 %BaseColor = OpVariable %_ptr_Input_v4float Input
247 %Dead = OpVariable %_ptr_Input_v4float Input
248 %float_0_5 = OpConstant %float 0.5
249 %_ptr_Output_v4float = OpTypePointer Output %v4float
250 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
251 %main = OpFunction %void None %10
252 %17 = OpLabel
253 %v = OpVariable %_ptr_Function_v4float Function
254 %dv = OpVariable %_ptr_Function_v4float Function
255 %18 = OpLoad %v4float %BaseColor
256 OpStore %v %18
257 %19 = OpLoad %v4float %Dead
258 ; CHECK-NOT: OpVectorTimesScalar
259 %8 = OpVectorTimesScalar %v4float %19 %float_0_5
260 OpStore %dv %8
261 %20 = OpLoad %v4float %v
262 OpStore %gl_FragColor %20
263 OpReturn
264 OpFunctionEnd
265 )";
266
267 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
268 }
269
270 TEST_F(AggressiveDCETest, Simple) {
271 // #version 140
272 //
273 // in vec4 BaseColor;
274 // in vec4 Dead;
275 //
276 // void main()
277 // {
278 // vec4 v = BaseColor;
279 // vec4 dv = Dead;
280 // gl_FragColor = v;
281 // }
282
283 const std::string spirv =
284 R"(
285 OpCapability Shader
286 %1 = OpExtInstImport "GLSL.std.450"
287 OpMemoryModel Logical GLSL450
288 ; CHECK: OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
289 OpEntryPoint Fragment %main "main" %BaseColor %Dead %gl_FragColor
290 OpExecutionMode %main OriginUpperLeft
291 OpSource GLSL 140
292 OpName %main "main"
293 OpName %v "v"
294 OpName %BaseColor "BaseColor"
295 ; CHECK-NOT: OpName %dv "dv"
296 OpName %dv "dv"
297 ; CHECK-NOT: OpName %Dead "Dead"
298 OpName %Dead "Dead"
299 OpName %gl_FragColor "gl_FragColor"
300 %void = OpTypeVoid
301 %9 = OpTypeFunction %void
302 %float = OpTypeFloat 32
303 %v4float = OpTypeVector %float 4
304 %_ptr_Function_v4float = OpTypePointer Function %v4float
305 %_ptr_Input_v4float = OpTypePointer Input %v4float
306 %BaseColor = OpVariable %_ptr_Input_v4float Input
307 ; CHECK-NOT: %Dead = OpVariable
308 %Dead = OpVariable %_ptr_Input_v4float Input
309 %_ptr_Output_v4float = OpTypePointer Output %v4float
310 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
311 %main = OpFunction %void None %9
312 %15 = OpLabel
313 %v = OpVariable %_ptr_Function_v4float Function
314 ; CHECK-NOT: %dv = OpVariable
315 %dv = OpVariable %_ptr_Function_v4float Function
316 %16 = OpLoad %v4float %BaseColor
317 OpStore %v %16
318 ; CHECK-NOT: OpLoad %v4float %Dead
319 %17 = OpLoad %v4float %Dead
320 ; CHECK-NOT: OpStore %dv
321 OpStore %dv %17
322 %18 = OpLoad %v4float %v
323 OpStore %gl_FragColor %18
324 OpReturn
325 OpFunctionEnd
326 )";
327
328 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
329 }
330
331 TEST_F(AggressiveDCETest, OptAllowListExtension) {
332 // #version 140
333 //
334 // in vec4 BaseColor;
335 // in vec4 Dead;
336 //
337 // void main()
338 // {
339 // vec4 v = BaseColor;
340 // vec4 dv = Dead;
341 // gl_FragColor = v;
342 // }
343
344 const std::string spirv =
345 R"(OpCapability Shader
346 OpExtension "SPV_AMD_gpu_shader_int16"
347 %1 = OpExtInstImport "GLSL.std.450"
348 OpMemoryModel Logical GLSL450
349 ; CHECK: OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
350 OpEntryPoint Fragment %main "main" %BaseColor %Dead %gl_FragColor
351 OpExecutionMode %main OriginUpperLeft
352 OpSource GLSL 140
353 OpName %main "main"
354 OpName %v "v"
355 OpName %BaseColor "BaseColor"
356 OpName %dv "dv"
357 OpName %Dead "Dead"
358 OpName %gl_FragColor "gl_FragColor"
359 %void = OpTypeVoid
360 %9 = OpTypeFunction %void
361 %float = OpTypeFloat 32
362 %v4float = OpTypeVector %float 4
363 %_ptr_Function_v4float = OpTypePointer Function %v4float
364 %_ptr_Input_v4float = OpTypePointer Input %v4float
365 %BaseColor = OpVariable %_ptr_Input_v4float Input
366 %Dead = OpVariable %_ptr_Input_v4float Input
367 %_ptr_Output_v4float = OpTypePointer Output %v4float
368 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
369 %main = OpFunction %void None %9
370 %15 = OpLabel
371 %v = OpVariable %_ptr_Function_v4float Function
372 %dv = OpVariable %_ptr_Function_v4float Function
373 %16 = OpLoad %v4float %BaseColor
374 OpStore %v %16
375 %17 = OpLoad %v4float %Dead
376 OpStore %dv %17
377 %18 = OpLoad %v4float %v
378 OpStore %gl_FragColor %18
379 OpReturn
380 OpFunctionEnd
381 )";
382
383 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
384 }
385
386 TEST_F(AggressiveDCETest, NoOptDenyListExtension) {
387 // #version 140
388 //
389 // in vec4 BaseColor;
390 // in vec4 Dead;
391 //
392 // void main()
393 // {
394 // vec4 v = BaseColor;
395 // vec4 dv = Dead;
396 // gl_FragColor = v;
397 // }
398
399 const std::string assembly =
400 R"(OpCapability Shader
401 OpExtension "SPV_KHR_variable_pointers"
402 %1 = OpExtInstImport "GLSL.std.450"
403 OpMemoryModel Logical GLSL450
404 OpEntryPoint Fragment %main "main" %BaseColor %Dead %gl_FragColor
405 OpExecutionMode %main OriginUpperLeft
406 OpSource GLSL 140
407 OpName %main "main"
408 OpName %v "v"
409 OpName %BaseColor "BaseColor"
410 OpName %dv "dv"
411 OpName %Dead "Dead"
412 OpName %gl_FragColor "gl_FragColor"
413 %void = OpTypeVoid
414 %9 = OpTypeFunction %void
415 %float = OpTypeFloat 32
416 %v4float = OpTypeVector %float 4
417 %_ptr_Function_v4float = OpTypePointer Function %v4float
418 %_ptr_Input_v4float = OpTypePointer Input %v4float
419 %BaseColor = OpVariable %_ptr_Input_v4float Input
420 %Dead = OpVariable %_ptr_Input_v4float Input
421 %_ptr_Output_v4float = OpTypePointer Output %v4float
422 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
423 %main = OpFunction %void None %9
424 %15 = OpLabel
425 %v = OpVariable %_ptr_Function_v4float Function
426 %dv = OpVariable %_ptr_Function_v4float Function
427 %16 = OpLoad %v4float %BaseColor
428 OpStore %v %16
429 %17 = OpLoad %v4float %Dead
430 OpStore %dv %17
431 %18 = OpLoad %v4float %v
432 OpStore %gl_FragColor %18
433 OpReturn
434 OpFunctionEnd
435 )";
436
437 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
438 }
439
440 TEST_F(AggressiveDCETest, ElimWithCall) {
441 // This demonstrates that "dead" function calls are not eliminated.
442 // Also demonstrates that DCE will happen in presence of function call.
443 // #version 140
444 // in vec4 i1;
445 // in vec4 i2;
446 //
447 // void nothing(vec4 v)
448 // {
449 // }
450 //
451 // void main()
452 // {
453 // vec4 v1 = i1;
454 // vec4 v2 = i2;
455 // nothing(v1);
456 // gl_FragColor = vec4(0.0);
457 // }
458
459 const std::string text =
460 R"( OpCapability Shader
461 %1 = OpExtInstImport "GLSL.std.450"
462 OpMemoryModel Logical GLSL450
463 OpEntryPoint Fragment %main "main" %i1 %i2 %gl_FragColor
464 OpExecutionMode %main OriginUpperLeft
465 OpSource GLSL 140
466 OpName %main "main"
467 OpName %nothing_vf4_ "nothing(vf4;"
468 OpName %v "v"
469 OpName %v1 "v1"
470 OpName %i1 "i1"
471 OpName %v2 "v2"
472 OpName %i2 "i2"
473 OpName %param "param"
474 OpName %gl_FragColor "gl_FragColor"
475 %void = OpTypeVoid
476 %12 = OpTypeFunction %void
477 %float = OpTypeFloat 32
478 %v4float = OpTypeVector %float 4
479 %_ptr_Function_v4float = OpTypePointer Function %v4float
480 %16 = OpTypeFunction %void %_ptr_Function_v4float
481 %_ptr_Input_v4float = OpTypePointer Input %v4float
482 %i1 = OpVariable %_ptr_Input_v4float Input
483 %i2 = OpVariable %_ptr_Input_v4float Input
484 %_ptr_Output_v4float = OpTypePointer Output %v4float
485 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
486 %float_0 = OpConstant %float 0
487 %20 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
488 %main = OpFunction %void None %12
489 %21 = OpLabel
490 %v1 = OpVariable %_ptr_Function_v4float Function
491 %v2 = OpVariable %_ptr_Function_v4float Function
492 %param = OpVariable %_ptr_Function_v4float Function
493 %22 = OpLoad %v4float %i1
494 OpStore %v1 %22
495 ; CHECK-NOT: OpLoad %v4float %i2
496 %23 = OpLoad %v4float %i2
497 ; CHECK-NOT: OpStore %v2
498 OpStore %v2 %23
499 %24 = OpLoad %v4float %v1
500 OpStore %param %24
501 ; CHECK: OpFunctionCall %void %nothing_vf4_
502 %25 = OpFunctionCall %void %nothing_vf4_ %param
503 OpStore %gl_FragColor %20
504 OpReturn
505 OpFunctionEnd
506 ; CHECK: %nothing_vf4_ = OpFunction
507 %nothing_vf4_ = OpFunction %void None %16
508 %v = OpFunctionParameter %_ptr_Function_v4float
509 %26 = OpLabel
510 OpReturn
511 OpFunctionEnd
512 )";
513
514 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
515 }
516
517 TEST_F(AggressiveDCETest, NoParamElim) {
518 // This demonstrates that unused parameters are not eliminated, but
519 // dead uses of them are.
520 // #version 140
521 //
522 // in vec4 BaseColor;
523 //
524 // vec4 foo(vec4 v1, vec4 v2)
525 // {
526 // vec4 t = -v1;
527 // return v2;
528 // }
529 //
530 // void main()
531 // {
532 // vec4 dead;
533 // gl_FragColor = foo(dead, BaseColor);
534 // }
535
536 const std::string defs_before =
537 R"(OpCapability Shader
538 %1 = OpExtInstImport "GLSL.std.450"
539 OpMemoryModel Logical GLSL450
540 OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor
541 OpExecutionMode %main OriginUpperLeft
542 OpSource GLSL 140
543 OpName %main "main"
544 OpName %foo_vf4_vf4_ "foo(vf4;vf4;"
545 OpName %v1 "v1"
546 OpName %v2 "v2"
547 OpName %t "t"
548 OpName %gl_FragColor "gl_FragColor"
549 OpName %dead "dead"
550 OpName %BaseColor "BaseColor"
551 OpName %param "param"
552 OpName %param_0 "param"
553 %void = OpTypeVoid
554 %13 = OpTypeFunction %void
555 %float = OpTypeFloat 32
556 %v4float = OpTypeVector %float 4
557 %_ptr_Function_v4float = OpTypePointer Function %v4float
558 %17 = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
559 %_ptr_Output_v4float = OpTypePointer Output %v4float
560 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
561 %_ptr_Input_v4float = OpTypePointer Input %v4float
562 %BaseColor = OpVariable %_ptr_Input_v4float Input
563 %main = OpFunction %void None %13
564 %20 = OpLabel
565 %dead = OpVariable %_ptr_Function_v4float Function
566 %param = OpVariable %_ptr_Function_v4float Function
567 %param_0 = OpVariable %_ptr_Function_v4float Function
568 %21 = OpLoad %v4float %dead
569 OpStore %param %21
570 %22 = OpLoad %v4float %BaseColor
571 OpStore %param_0 %22
572 %23 = OpFunctionCall %v4float %foo_vf4_vf4_ %param %param_0
573 OpStore %gl_FragColor %23
574 OpReturn
575 OpFunctionEnd
576 )";
577
578 const std::string defs_after =
579 R"(OpCapability Shader
580 %1 = OpExtInstImport "GLSL.std.450"
581 OpMemoryModel Logical GLSL450
582 OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor
583 OpExecutionMode %main OriginUpperLeft
584 OpSource GLSL 140
585 OpName %main "main"
586 OpName %foo_vf4_vf4_ "foo(vf4;vf4;"
587 OpName %v1 "v1"
588 OpName %v2 "v2"
589 OpName %gl_FragColor "gl_FragColor"
590 OpName %dead "dead"
591 OpName %BaseColor "BaseColor"
592 OpName %param "param"
593 OpName %param_0 "param"
594 %void = OpTypeVoid
595 %13 = OpTypeFunction %void
596 %float = OpTypeFloat 32
597 %v4float = OpTypeVector %float 4
598 %_ptr_Function_v4float = OpTypePointer Function %v4float
599 %17 = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
600 %_ptr_Output_v4float = OpTypePointer Output %v4float
601 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
602 %_ptr_Input_v4float = OpTypePointer Input %v4float
603 %BaseColor = OpVariable %_ptr_Input_v4float Input
604 %main = OpFunction %void None %13
605 %20 = OpLabel
606 %dead = OpVariable %_ptr_Function_v4float Function
607 %param = OpVariable %_ptr_Function_v4float Function
608 %param_0 = OpVariable %_ptr_Function_v4float Function
609 %21 = OpLoad %v4float %dead
610 OpStore %param %21
611 %22 = OpLoad %v4float %BaseColor
612 OpStore %param_0 %22
613 %23 = OpFunctionCall %v4float %foo_vf4_vf4_ %param %param_0
614 OpStore %gl_FragColor %23
615 OpReturn
616 OpFunctionEnd
617 )";
618
619 const std::string func_before =
620 R"(%foo_vf4_vf4_ = OpFunction %v4float None %17
621 %v1 = OpFunctionParameter %_ptr_Function_v4float
622 %v2 = OpFunctionParameter %_ptr_Function_v4float
623 %24 = OpLabel
624 %t = OpVariable %_ptr_Function_v4float Function
625 %25 = OpLoad %v4float %v1
626 %26 = OpFNegate %v4float %25
627 OpStore %t %26
628 %27 = OpLoad %v4float %v2
629 OpReturnValue %27
630 OpFunctionEnd
631 )";
632
633 const std::string func_after =
634 R"(%foo_vf4_vf4_ = OpFunction %v4float None %17
635 %v1 = OpFunctionParameter %_ptr_Function_v4float
636 %v2 = OpFunctionParameter %_ptr_Function_v4float
637 %24 = OpLabel
638 %27 = OpLoad %v4float %v2
639 OpReturnValue %27
640 OpFunctionEnd
641 )";
642
643 SinglePassRunAndCheck<AggressiveDCEPass>(defs_before + func_before,
644 defs_after + func_after, true, true);
645 }
646
647 TEST_F(AggressiveDCETest, ElimOpaque) {
648 // SPIR-V not representable from GLSL; not generatable from HLSL
649 // for the moment.
650
651 const std::string defs_before =
652 R"(OpCapability Shader
653 %1 = OpExtInstImport "GLSL.std.450"
654 OpMemoryModel Logical GLSL450
655 OpEntryPoint Fragment %main "main" %outColor %texCoords
656 OpExecutionMode %main OriginUpperLeft
657 OpSource GLSL 140
658 OpName %main "main"
659 OpName %S_t "S_t"
660 OpMemberName %S_t 0 "v0"
661 OpMemberName %S_t 1 "v1"
662 OpMemberName %S_t 2 "smp"
663 OpName %outColor "outColor"
664 OpName %sampler15 "sampler15"
665 OpName %s0 "s0"
666 OpName %texCoords "texCoords"
667 OpDecorate %sampler15 DescriptorSet 0
668 %void = OpTypeVoid
669 %9 = OpTypeFunction %void
670 %float = OpTypeFloat 32
671 %v2float = OpTypeVector %float 2
672 %v4float = OpTypeVector %float 4
673 %_ptr_Output_v4float = OpTypePointer Output %v4float
674 %outColor = OpVariable %_ptr_Output_v4float Output
675 %14 = OpTypeImage %float 2D 0 0 0 1 Unknown
676 %15 = OpTypeSampledImage %14
677 %S_t = OpTypeStruct %v2float %v2float %15
678 %_ptr_Function_S_t = OpTypePointer Function %S_t
679 %17 = OpTypeFunction %void %_ptr_Function_S_t
680 %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
681 %_ptr_Function_15 = OpTypePointer Function %15
682 %sampler15 = OpVariable %_ptr_UniformConstant_15 UniformConstant
683 %int = OpTypeInt 32 1
684 %int_0 = OpConstant %int 0
685 %int_2 = OpConstant %int 2
686 %_ptr_Function_v2float = OpTypePointer Function %v2float
687 %_ptr_Input_v2float = OpTypePointer Input %v2float
688 %texCoords = OpVariable %_ptr_Input_v2float Input
689 )";
690
691 const std::string defs_after =
692 R"(OpCapability Shader
693 %1 = OpExtInstImport "GLSL.std.450"
694 OpMemoryModel Logical GLSL450
695 OpEntryPoint Fragment %main "main" %outColor %texCoords
696 OpExecutionMode %main OriginUpperLeft
697 OpSource GLSL 140
698 OpName %main "main"
699 OpName %outColor "outColor"
700 OpName %sampler15 "sampler15"
701 OpName %texCoords "texCoords"
702 OpDecorate %sampler15 DescriptorSet 0
703 %void = OpTypeVoid
704 %9 = OpTypeFunction %void
705 %float = OpTypeFloat 32
706 %v2float = OpTypeVector %float 2
707 %v4float = OpTypeVector %float 4
708 %_ptr_Output_v4float = OpTypePointer Output %v4float
709 %outColor = OpVariable %_ptr_Output_v4float Output
710 %14 = OpTypeImage %float 2D 0 0 0 1 Unknown
711 %15 = OpTypeSampledImage %14
712 %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
713 %sampler15 = OpVariable %_ptr_UniformConstant_15 UniformConstant
714 %_ptr_Input_v2float = OpTypePointer Input %v2float
715 %texCoords = OpVariable %_ptr_Input_v2float Input
716 )";
717
718 const std::string func_before =
719 R"(%main = OpFunction %void None %9
720 %25 = OpLabel
721 %s0 = OpVariable %_ptr_Function_S_t Function
722 %26 = OpLoad %v2float %texCoords
723 %27 = OpLoad %S_t %s0
724 %28 = OpCompositeInsert %S_t %26 %27 0
725 %29 = OpLoad %15 %sampler15
726 %30 = OpCompositeInsert %S_t %29 %28 2
727 OpStore %s0 %30
728 %31 = OpImageSampleImplicitLod %v4float %29 %26
729 OpStore %outColor %31
730 OpReturn
731 OpFunctionEnd
732 )";
733
734 const std::string func_after =
735 R"(%main = OpFunction %void None %9
736 %25 = OpLabel
737 %26 = OpLoad %v2float %texCoords
738 %29 = OpLoad %15 %sampler15
739 %31 = OpImageSampleImplicitLod %v4float %29 %26
740 OpStore %outColor %31
741 OpReturn
742 OpFunctionEnd
743 )";
744
745 SinglePassRunAndCheck<AggressiveDCEPass>(defs_before + func_before,
746 defs_after + func_after, true, true);
747 }
748
749 TEST_F(AggressiveDCETest, NoParamStoreElim) {
750 // Should not eliminate stores to params
751 //
752 // #version 450
753 //
754 // layout(location = 0) in vec4 BaseColor;
755 // layout(location = 0) out vec4 OutColor;
756 //
757 // void foo(in vec4 v1, out vec4 v2)
758 // {
759 // v2 = -v1;
760 // }
761 //
762 // void main()
763 // {
764 // foo(BaseColor, OutColor);
765 // }
766
767 const std::string assembly =
768 R"(OpCapability Shader
769 %1 = OpExtInstImport "GLSL.std.450"
770 OpMemoryModel Logical GLSL450
771 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
772 OpExecutionMode %main OriginUpperLeft
773 OpSource GLSL 450
774 OpName %main "main"
775 OpName %foo_vf4_vf4_ "foo(vf4;vf4;"
776 OpName %v1 "v1"
777 OpName %v2 "v2"
778 OpName %BaseColor "BaseColor"
779 OpName %OutColor "OutColor"
780 OpName %param "param"
781 OpName %param_0 "param"
782 OpDecorate %BaseColor Location 0
783 OpDecorate %OutColor Location 0
784 %void = OpTypeVoid
785 %11 = OpTypeFunction %void
786 %float = OpTypeFloat 32
787 %v4float = OpTypeVector %float 4
788 %_ptr_Function_v4float = OpTypePointer Function %v4float
789 %15 = OpTypeFunction %void %_ptr_Function_v4float %_ptr_Function_v4float
790 %_ptr_Input_v4float = OpTypePointer Input %v4float
791 %BaseColor = OpVariable %_ptr_Input_v4float Input
792 %_ptr_Output_v4float = OpTypePointer Output %v4float
793 %OutColor = OpVariable %_ptr_Output_v4float Output
794 %main = OpFunction %void None %11
795 %18 = OpLabel
796 %param = OpVariable %_ptr_Function_v4float Function
797 %param_0 = OpVariable %_ptr_Function_v4float Function
798 %19 = OpLoad %v4float %BaseColor
799 OpStore %param %19
800 %20 = OpFunctionCall %void %foo_vf4_vf4_ %param %param_0
801 %21 = OpLoad %v4float %param_0
802 OpStore %OutColor %21
803 OpReturn
804 OpFunctionEnd
805 %foo_vf4_vf4_ = OpFunction %void None %15
806 %v1 = OpFunctionParameter %_ptr_Function_v4float
807 %v2 = OpFunctionParameter %_ptr_Function_v4float
808 %22 = OpLabel
809 %23 = OpLoad %v4float %v1
810 %24 = OpFNegate %v4float %23
811 OpStore %v2 %24
812 OpReturn
813 OpFunctionEnd
814 )";
815
816 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
817 }
818
819 TEST_F(AggressiveDCETest, PrivateStoreElimInEntryNoCalls) {
820 // Eliminate stores to private in entry point with no calls
821 // Note: Not legal GLSL
822 //
823 // layout(location = 0) in vec4 BaseColor;
824 // layout(location = 1) in vec4 Dead;
825 // layout(location = 0) out vec4 OutColor;
826 //
827 // private vec4 dv;
828 //
829 // void main()
830 // {
831 // vec4 v = BaseColor;
832 // dv = Dead;
833 // OutColor = v;
834 // }
835
836 const std::string spirv =
837 R"(OpCapability Shader
838 %1 = OpExtInstImport "GLSL.std.450"
839 OpMemoryModel Logical GLSL450
840 OpEntryPoint Fragment %main "main" %BaseColor %Dead %OutColor
841 OpExecutionMode %main OriginUpperLeft
842 OpSource GLSL 450
843 OpName %main "main"
844 OpName %v "v"
845 OpName %BaseColor "BaseColor"
846 ; CHECK-NOT: OpName %dv "dv"
847 OpName %dv "dv"
848 OpName %Dead "Dead"
849 OpName %OutColor "OutColor"
850 OpDecorate %BaseColor Location 0
851 OpDecorate %Dead Location 1
852 OpDecorate %OutColor Location 0
853 %void = OpTypeVoid
854 %9 = OpTypeFunction %void
855 %float = OpTypeFloat 32
856 %v4float = OpTypeVector %float 4
857 %_ptr_Function_v4float = OpTypePointer Function %v4float
858 ; CHECK-NOT: OpTypePointer Private
859 %_ptr_Private_v4float = OpTypePointer Private %v4float
860 %_ptr_Input_v4float = OpTypePointer Input %v4float
861 %BaseColor = OpVariable %_ptr_Input_v4float Input
862 %Dead = OpVariable %_ptr_Input_v4float Input
863 %_ptr_Output_v4float = OpTypePointer Output %v4float
864 ; CHECK-NOT: %dv = OpVariable
865 %dv = OpVariable %_ptr_Private_v4float Private
866 %OutColor = OpVariable %_ptr_Output_v4float Output
867 %main = OpFunction %void None %9
868 %16 = OpLabel
869 %v = OpVariable %_ptr_Function_v4float Function
870 %17 = OpLoad %v4float %BaseColor
871 OpStore %v %17
872 %18 = OpLoad %v4float %Dead
873 ; CHECK-NOT: OpStore %dv
874 OpStore %dv %18
875 %19 = OpLoad %v4float %v
876 %20 = OpFNegate %v4float %19
877 OpStore %OutColor %20
878 OpReturn
879 OpFunctionEnd
880 )";
881
882 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
883 }
884
885 TEST_F(AggressiveDCETest, NoPrivateStoreElimIfLoad) {
886 // Should not eliminate stores to private when there is a load
887 // Note: Not legal GLSL
888 //
889 // #version 450
890 //
891 // layout(location = 0) in vec4 BaseColor;
892 // layout(location = 0) out vec4 OutColor;
893 //
894 // private vec4 pv;
895 //
896 // void main()
897 // {
898 // pv = BaseColor;
899 // OutColor = pv;
900 // }
901
902 const std::string assembly =
903 R"(OpCapability Shader
904 %1 = OpExtInstImport "GLSL.std.450"
905 OpMemoryModel Logical GLSL450
906 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
907 OpExecutionMode %main OriginUpperLeft
908 OpSource GLSL 450
909 OpName %main "main"
910 OpName %pv "pv"
911 OpName %BaseColor "BaseColor"
912 OpName %OutColor "OutColor"
913 OpDecorate %BaseColor Location 0
914 OpDecorate %OutColor Location 0
915 %void = OpTypeVoid
916 %7 = OpTypeFunction %void
917 %float = OpTypeFloat 32
918 %v4float = OpTypeVector %float 4
919 %_ptr_Private_v4float = OpTypePointer Private %v4float
920 %_ptr_Input_v4float = OpTypePointer Input %v4float
921 %BaseColor = OpVariable %_ptr_Input_v4float Input
922 %_ptr_Output_v4float = OpTypePointer Output %v4float
923 %OutColor = OpVariable %_ptr_Output_v4float Output
924 %pv = OpVariable %_ptr_Private_v4float Private
925 %main = OpFunction %void None %7
926 %13 = OpLabel
927 %14 = OpLoad %v4float %BaseColor
928 OpStore %pv %14
929 %15 = OpLoad %v4float %pv
930 %16 = OpFNegate %v4float %15
931 OpStore %OutColor %16
932 OpReturn
933 OpFunctionEnd
934 )";
935
936 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
937 }
938
939 TEST_F(AggressiveDCETest, NoPrivateStoreElimWithCall) {
940 // Should not eliminate stores to private when function contains call
941 // Note: Not legal GLSL
942 //
943 // #version 450
944 //
945 // layout(location = 0) in vec4 BaseColor;
946 // layout(location = 0) out vec4 OutColor;
947 //
948 // private vec4 v1;
949 //
950 // void foo()
951 // {
952 // OutColor = -v1;
953 // }
954 //
955 // void main()
956 // {
957 // v1 = BaseColor;
958 // foo();
959 // }
960
961 const std::string assembly =
962 R"(OpCapability Shader
963 %1 = OpExtInstImport "GLSL.std.450"
964 OpMemoryModel Logical GLSL450
965 OpEntryPoint Fragment %main "main" %OutColor %BaseColor
966 OpExecutionMode %main OriginUpperLeft
967 OpSource GLSL 450
968 OpName %main "main"
969 OpName %foo_ "foo("
970 OpName %OutColor "OutColor"
971 OpName %v1 "v1"
972 OpName %BaseColor "BaseColor"
973 OpDecorate %OutColor Location 0
974 OpDecorate %BaseColor Location 0
975 %void = OpTypeVoid
976 %8 = OpTypeFunction %void
977 %float = OpTypeFloat 32
978 %v4float = OpTypeVector %float 4
979 %_ptr_Output_v4float = OpTypePointer Output %v4float
980 %OutColor = OpVariable %_ptr_Output_v4float Output
981 %_ptr_Private_v4float = OpTypePointer Private %v4float
982 %_ptr_Input_v4float = OpTypePointer Input %v4float
983 %v1 = OpVariable %_ptr_Private_v4float Private
984 %BaseColor = OpVariable %_ptr_Input_v4float Input
985 %main = OpFunction %void None %8
986 %14 = OpLabel
987 %15 = OpLoad %v4float %BaseColor
988 OpStore %v1 %15
989 %16 = OpFunctionCall %void %foo_
990 OpReturn
991 OpFunctionEnd
992 %foo_ = OpFunction %void None %8
993 %17 = OpLabel
994 %18 = OpLoad %v4float %v1
995 %19 = OpFNegate %v4float %18
996 OpStore %OutColor %19
997 OpReturn
998 OpFunctionEnd
999 )";
1000
1001 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1002 }
1003
1004 TEST_F(AggressiveDCETest, NoPrivateStoreElimInNonEntry) {
1005 // Should not eliminate stores to private when function is not entry point
1006 // Note: Not legal GLSL
1007 //
1008 // #version 450
1009 //
1010 // layout(location = 0) in vec4 BaseColor;
1011 // layout(location = 0) out vec4 OutColor;
1012 //
1013 // private vec4 v1;
1014 //
1015 // void foo()
1016 // {
1017 // v1 = BaseColor;
1018 // }
1019 //
1020 // void main()
1021 // {
1022 // foo();
1023 // OutColor = -v1;
1024 // }
1025
1026 const std::string assembly =
1027 R"(OpCapability Shader
1028 %1 = OpExtInstImport "GLSL.std.450"
1029 OpMemoryModel Logical GLSL450
1030 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1031 OpExecutionMode %main OriginUpperLeft
1032 OpSource GLSL 450
1033 OpName %main "main"
1034 OpName %foo_ "foo("
1035 OpName %v1 "v1"
1036 OpName %BaseColor "BaseColor"
1037 OpName %OutColor "OutColor"
1038 OpDecorate %BaseColor Location 0
1039 OpDecorate %OutColor Location 0
1040 %void = OpTypeVoid
1041 %8 = OpTypeFunction %void
1042 %float = OpTypeFloat 32
1043 %v4float = OpTypeVector %float 4
1044 %_ptr_Private_v4float = OpTypePointer Private %v4float
1045 %_ptr_Input_v4float = OpTypePointer Input %v4float
1046 %BaseColor = OpVariable %_ptr_Input_v4float Input
1047 %_ptr_Output_v4float = OpTypePointer Output %v4float
1048 %v1 = OpVariable %_ptr_Private_v4float Private
1049 %OutColor = OpVariable %_ptr_Output_v4float Output
1050 %main = OpFunction %void None %8
1051 %14 = OpLabel
1052 %15 = OpFunctionCall %void %foo_
1053 %16 = OpLoad %v4float %v1
1054 %17 = OpFNegate %v4float %16
1055 OpStore %OutColor %17
1056 OpReturn
1057 OpFunctionEnd
1058 %foo_ = OpFunction %void None %8
1059 %18 = OpLabel
1060 %19 = OpLoad %v4float %BaseColor
1061 OpStore %v1 %19
1062 OpReturn
1063 OpFunctionEnd
1064 )";
1065
1066 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1067 }
1068
1069 TEST_F(AggressiveDCETest, WorkgroupStoreElimInEntryNoCalls) {
1070 // Eliminate stores to private in entry point with no calls
1071 // Note: Not legal GLSL
1072 //
1073 // layout(location = 0) in vec4 BaseColor;
1074 // layout(location = 1) in vec4 Dead;
1075 // layout(location = 0) out vec4 OutColor;
1076 //
1077 // workgroup vec4 dv;
1078 //
1079 // void main()
1080 // {
1081 // vec4 v = BaseColor;
1082 // dv = Dead;
1083 // OutColor = v;
1084 // }
1085
1086 const std::string spirv =
1087 R"(OpCapability Shader
1088 %1 = OpExtInstImport "GLSL.std.450"
1089 OpMemoryModel Logical GLSL450
1090 OpEntryPoint Fragment %main "main" %BaseColor %Dead %OutColor
1091 OpExecutionMode %main OriginUpperLeft
1092 OpSource GLSL 450
1093 OpName %main "main"
1094 OpName %v "v"
1095 OpName %BaseColor "BaseColor"
1096 ; CHECK-NOT: OpName %dv "dv"
1097 OpName %dv "dv"
1098 OpName %Dead "Dead"
1099 OpName %OutColor "OutColor"
1100 OpDecorate %BaseColor Location 0
1101 OpDecorate %Dead Location 1
1102 OpDecorate %OutColor Location 0
1103 %void = OpTypeVoid
1104 %9 = OpTypeFunction %void
1105 %float = OpTypeFloat 32
1106 %v4float = OpTypeVector %float 4
1107 %_ptr_Function_v4float = OpTypePointer Function %v4float
1108 ; CHECK-NOT: OpTypePointer Workgroup
1109 %_ptr_Workgroup_v4float = OpTypePointer Workgroup %v4float
1110 %_ptr_Input_v4float = OpTypePointer Input %v4float
1111 %BaseColor = OpVariable %_ptr_Input_v4float Input
1112 %Dead = OpVariable %_ptr_Input_v4float Input
1113 %_ptr_Output_v4float = OpTypePointer Output %v4float
1114 ; CHECK-NOT: %dv = OpVariable
1115 %dv = OpVariable %_ptr_Workgroup_v4float Workgroup
1116 %OutColor = OpVariable %_ptr_Output_v4float Output
1117 %main = OpFunction %void None %9
1118 %16 = OpLabel
1119 %v = OpVariable %_ptr_Function_v4float Function
1120 %17 = OpLoad %v4float %BaseColor
1121 OpStore %v %17
1122 %18 = OpLoad %v4float %Dead
1123 ; CHECK-NOT: OpStore %dv
1124 OpStore %dv %18
1125 %19 = OpLoad %v4float %v
1126 %20 = OpFNegate %v4float %19
1127 OpStore %OutColor %20
1128 OpReturn
1129 OpFunctionEnd
1130 )";
1131
1132 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
1133 }
1134
1135 TEST_F(AggressiveDCETest, EliminateDeadIfThenElse) {
1136 // #version 450
1137 //
1138 // layout(location = 0) in vec4 BaseColor;
1139 // layout(location = 0) out vec4 OutColor;
1140 //
1141 // void main()
1142 // {
1143 // float d;
1144 // if (BaseColor.x == 0)
1145 // d = BaseColor.y;
1146 // else
1147 // d = BaseColor.z;
1148 // OutColor = vec4(1.0,1.0,1.0,1.0);
1149 // }
1150
1151 const std::string spirv =
1152 R"(OpCapability Shader
1153 %1 = OpExtInstImport "GLSL.std.450"
1154 OpMemoryModel Logical GLSL450
1155 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1156 OpExecutionMode %main OriginUpperLeft
1157 OpSource GLSL 450
1158 OpName %main "main"
1159 OpName %BaseColor "BaseColor"
1160 OpName %d "d"
1161 OpName %OutColor "OutColor"
1162 OpDecorate %BaseColor Location 0
1163 OpDecorate %OutColor Location 0
1164 %void = OpTypeVoid
1165 %7 = OpTypeFunction %void
1166 %float = OpTypeFloat 32
1167 %v4float = OpTypeVector %float 4
1168 %_ptr_Input_v4float = OpTypePointer Input %v4float
1169 %BaseColor = OpVariable %_ptr_Input_v4float Input
1170 %uint = OpTypeInt 32 0
1171 %uint_0 = OpConstant %uint 0
1172 %_ptr_Input_float = OpTypePointer Input %float
1173 %float_0 = OpConstant %float 0
1174 %bool = OpTypeBool
1175 %_ptr_Function_float = OpTypePointer Function %float
1176 %uint_1 = OpConstant %uint 1
1177 %uint_2 = OpConstant %uint 2
1178 %_ptr_Output_v4float = OpTypePointer Output %v4float
1179 %OutColor = OpVariable %_ptr_Output_v4float Output
1180 %float_1 = OpConstant %float 1
1181 %21 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
1182 ; CHECK: = OpFunction %void
1183 ; CHECK-NEXT: %22 = OpLabel
1184 ; CHECK-NEXT: OpBranch %26
1185 ; CHECK-NEXT: %26 = OpLabel
1186 %main = OpFunction %void None %7
1187 %22 = OpLabel
1188 %d = OpVariable %_ptr_Function_float Function
1189 %23 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1190 %24 = OpLoad %float %23
1191 %25 = OpFOrdEqual %bool %24 %float_0
1192 OpSelectionMerge %26 None
1193 OpBranchConditional %25 %27 %28
1194 %27 = OpLabel
1195 %29 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1196 %30 = OpLoad %float %29
1197 OpStore %d %30
1198 OpBranch %26
1199 %28 = OpLabel
1200 %31 = OpAccessChain %_ptr_Input_float %BaseColor %uint_2
1201 %32 = OpLoad %float %31
1202 OpStore %d %32
1203 OpBranch %26
1204 %26 = OpLabel
1205 OpStore %OutColor %21
1206 OpReturn
1207 OpFunctionEnd
1208 )";
1209
1210 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
1211 }
1212
1213 TEST_F(AggressiveDCETest, EliminateDeadIfThen) {
1214 // #version 450
1215 //
1216 // layout(location = 0) in vec4 BaseColor;
1217 // layout(location = 0) out vec4 OutColor;
1218 //
1219 // void main()
1220 // {
1221 // float d;
1222 // if (BaseColor.x == 0)
1223 // d = BaseColor.y;
1224 // OutColor = vec4(1.0,1.0,1.0,1.0);
1225 // }
1226
1227 const std::string spirv =
1228 R"(OpCapability Shader
1229 %1 = OpExtInstImport "GLSL.std.450"
1230 OpMemoryModel Logical GLSL450
1231 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1232 OpExecutionMode %main OriginUpperLeft
1233 OpSource GLSL 450
1234 OpName %main "main"
1235 OpName %BaseColor "BaseColor"
1236 OpName %d "d"
1237 OpName %OutColor "OutColor"
1238 OpDecorate %BaseColor Location 0
1239 OpDecorate %OutColor Location 0
1240 %void = OpTypeVoid
1241 %7 = OpTypeFunction %void
1242 %float = OpTypeFloat 32
1243 %v4float = OpTypeVector %float 4
1244 %_ptr_Input_v4float = OpTypePointer Input %v4float
1245 %BaseColor = OpVariable %_ptr_Input_v4float Input
1246 %uint = OpTypeInt 32 0
1247 %uint_0 = OpConstant %uint 0
1248 %_ptr_Input_float = OpTypePointer Input %float
1249 %float_0 = OpConstant %float 0
1250 %bool = OpTypeBool
1251 %_ptr_Function_float = OpTypePointer Function %float
1252 %uint_1 = OpConstant %uint 1
1253 %_ptr_Output_v4float = OpTypePointer Output %v4float
1254 %OutColor = OpVariable %_ptr_Output_v4float Output
1255 %float_1 = OpConstant %float 1
1256 %20 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
1257 ; CHECK: = OpFunction
1258 ; CHECK-NEXT: %21 = OpLabel
1259 ; CHECK-NEXT: OpBranch [[target:%\w+]]
1260 ; CHECK-NEXT: [[target]] = OpLabel
1261 %main = OpFunction %void None %7
1262 %21 = OpLabel
1263 %d = OpVariable %_ptr_Function_float Function
1264 %22 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1265 %23 = OpLoad %float %22
1266 %24 = OpFOrdEqual %bool %23 %float_0
1267 OpSelectionMerge %25 None
1268 OpBranchConditional %24 %26 %25
1269 %26 = OpLabel
1270 %27 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1271 %28 = OpLoad %float %27
1272 OpStore %d %28
1273 OpBranch %25
1274 %25 = OpLabel
1275 OpStore %OutColor %20
1276 OpReturn
1277 OpFunctionEnd
1278 )";
1279
1280 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
1281 }
1282
1283 TEST_F(AggressiveDCETest, EliminateDeadSwitch) {
1284 // #version 450
1285 //
1286 // layout(location = 0) in vec4 BaseColor;
1287 // layout(location = 1) in flat int x;
1288 // layout(location = 0) out vec4 OutColor;
1289 //
1290 // void main()
1291 // {
1292 // float d;
1293 // switch (x) {
1294 // case 0:
1295 // d = BaseColor.y;
1296 // }
1297 // OutColor = vec4(1.0,1.0,1.0,1.0);
1298 // }
1299 const std::string spirv =
1300 R"(OpCapability Shader
1301 %1 = OpExtInstImport "GLSL.std.450"
1302 OpMemoryModel Logical GLSL450
1303 OpEntryPoint Fragment %main "main" %x %BaseColor %OutColor
1304 OpExecutionMode %main OriginUpperLeft
1305 OpSource GLSL 450
1306 OpName %main "main"
1307 OpName %x "x"
1308 OpName %d "d"
1309 OpName %BaseColor "BaseColor"
1310 OpName %OutColor "OutColor"
1311 OpDecorate %x Flat
1312 OpDecorate %x Location 1
1313 OpDecorate %BaseColor Location 0
1314 OpDecorate %OutColor Location 0
1315 %void = OpTypeVoid
1316 %3 = OpTypeFunction %void
1317 %int = OpTypeInt 32 1
1318 %_ptr_Input_int = OpTypePointer Input %int
1319 %x = OpVariable %_ptr_Input_int Input
1320 %float = OpTypeFloat 32
1321 %_ptr_Function_float = OpTypePointer Function %float
1322 %v4float = OpTypeVector %float 4
1323 %_ptr_Input_v4float = OpTypePointer Input %v4float
1324 %BaseColor = OpVariable %_ptr_Input_v4float Input
1325 %uint = OpTypeInt 32 0
1326 %uint_1 = OpConstant %uint 1
1327 %_ptr_Input_float = OpTypePointer Input %float
1328 %_ptr_Output_v4float = OpTypePointer Output %v4float
1329 %OutColor = OpVariable %_ptr_Output_v4float Output
1330 %float_1 = OpConstant %float 1
1331 %27 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
1332 ; CHECK: = OpFunction
1333 ; CHECK-NEXT: = OpLabel
1334 ; CHECK-NEXT: OpBranch [[target:%\w+]]
1335 ; CHECK-NEXT: [[target]] = OpLabel
1336 %main = OpFunction %void None %3
1337 %5 = OpLabel
1338 %d = OpVariable %_ptr_Function_float Function
1339 %9 = OpLoad %int %x
1340 OpSelectionMerge %11 None
1341 OpSwitch %9 %11 0 %10
1342 %10 = OpLabel
1343 %21 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1344 %22 = OpLoad %float %21
1345 OpStore %d %22
1346 OpBranch %11
1347 %11 = OpLabel
1348 OpStore %OutColor %27
1349 OpReturn
1350 OpFunctionEnd)";
1351
1352 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
1353 }
1354
1355 TEST_F(AggressiveDCETest, EliminateDeadIfThenElseNested) {
1356 // #version 450
1357 //
1358 // layout(location = 0) in vec4 BaseColor;
1359 // layout(location = 0) out vec4 OutColor;
1360 //
1361 // void main()
1362 // {
1363 // float d;
1364 // if (BaseColor.x == 0)
1365 // if (BaseColor.y == 0)
1366 // d = 0.0;
1367 // else
1368 // d = 0.25;
1369 // else
1370 // if (BaseColor.y == 0)
1371 // d = 0.5;
1372 // else
1373 // d = 0.75;
1374 // OutColor = vec4(1.0,1.0,1.0,1.0);
1375 // }
1376
1377 const std::string spirv =
1378 R"(OpCapability Shader
1379 %1 = OpExtInstImport "GLSL.std.450"
1380 OpMemoryModel Logical GLSL450
1381 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1382 OpExecutionMode %main OriginUpperLeft
1383 OpSource GLSL 450
1384 OpName %main "main"
1385 OpName %BaseColor "BaseColor"
1386 OpName %d "d"
1387 OpName %OutColor "OutColor"
1388 OpDecorate %BaseColor Location 0
1389 OpDecorate %OutColor Location 0
1390 %void = OpTypeVoid
1391 %7 = OpTypeFunction %void
1392 %float = OpTypeFloat 32
1393 %v4float = OpTypeVector %float 4
1394 %_ptr_Input_v4float = OpTypePointer Input %v4float
1395 %BaseColor = OpVariable %_ptr_Input_v4float Input
1396 %uint = OpTypeInt 32 0
1397 %uint_0 = OpConstant %uint 0
1398 %_ptr_Input_float = OpTypePointer Input %float
1399 %float_0 = OpConstant %float 0
1400 %bool = OpTypeBool
1401 %uint_1 = OpConstant %uint 1
1402 %_ptr_Function_float = OpTypePointer Function %float
1403 %float_0_25 = OpConstant %float 0.25
1404 %float_0_5 = OpConstant %float 0.5
1405 %float_0_75 = OpConstant %float 0.75
1406 %_ptr_Output_v4float = OpTypePointer Output %v4float
1407 %OutColor = OpVariable %_ptr_Output_v4float Output
1408 %float_1 = OpConstant %float 1
1409 %23 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
1410
1411 ; CHECK: = OpFunction
1412 ; CHECK-NEXT: = OpLabel
1413 ; CHECK-NEXT: OpBranch [[target:%\w+]]
1414 ; CHECK-NEXT: [[target]] = OpLabel
1415 ; CHECK-NOT: OpLabel
1416
1417 %main = OpFunction %void None %7
1418 %24 = OpLabel
1419 %d = OpVariable %_ptr_Function_float Function
1420 %25 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1421 %26 = OpLoad %float %25
1422 %27 = OpFOrdEqual %bool %26 %float_0
1423 OpSelectionMerge %28 None
1424 OpBranchConditional %27 %29 %30
1425 %29 = OpLabel
1426 %31 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1427 %32 = OpLoad %float %31
1428 %33 = OpFOrdEqual %bool %32 %float_0
1429 OpSelectionMerge %34 None
1430 OpBranchConditional %33 %35 %36
1431 %35 = OpLabel
1432 OpStore %d %float_0
1433 OpBranch %34
1434 %36 = OpLabel
1435 OpStore %d %float_0_25
1436 OpBranch %34
1437 %34 = OpLabel
1438 OpBranch %28
1439 %30 = OpLabel
1440 %37 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1441 %38 = OpLoad %float %37
1442 %39 = OpFOrdEqual %bool %38 %float_0
1443 OpSelectionMerge %40 None
1444 OpBranchConditional %39 %41 %42
1445 %41 = OpLabel
1446 OpStore %d %float_0_5
1447 OpBranch %40
1448 %42 = OpLabel
1449 OpStore %d %float_0_75
1450 OpBranch %40
1451 %40 = OpLabel
1452 OpBranch %28
1453 %28 = OpLabel
1454 OpStore %OutColor %23
1455 OpReturn
1456 OpFunctionEnd
1457 )";
1458
1459 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
1460 }
1461
1462 TEST_F(AggressiveDCETest, NoEliminateLiveIfThenElse) {
1463 // #version 450
1464 //
1465 // layout(location = 0) in vec4 BaseColor;
1466 // layout(location = 0) out vec4 OutColor;
1467 //
1468 // void main()
1469 // {
1470 // float t;
1471 // if (BaseColor.x == 0)
1472 // t = BaseColor.y;
1473 // else
1474 // t = BaseColor.z;
1475 // OutColor = vec4(t);
1476 // }
1477
1478 const std::string assembly =
1479 R"(OpCapability Shader
1480 %1 = OpExtInstImport "GLSL.std.450"
1481 OpMemoryModel Logical GLSL450
1482 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1483 OpExecutionMode %main OriginUpperLeft
1484 OpSource GLSL 450
1485 OpName %main "main"
1486 OpName %BaseColor "BaseColor"
1487 OpName %t "t"
1488 OpName %OutColor "OutColor"
1489 OpDecorate %BaseColor Location 0
1490 OpDecorate %OutColor Location 0
1491 %void = OpTypeVoid
1492 %7 = OpTypeFunction %void
1493 %float = OpTypeFloat 32
1494 %v4float = OpTypeVector %float 4
1495 %_ptr_Input_v4float = OpTypePointer Input %v4float
1496 %BaseColor = OpVariable %_ptr_Input_v4float Input
1497 %uint = OpTypeInt 32 0
1498 %uint_0 = OpConstant %uint 0
1499 %_ptr_Input_float = OpTypePointer Input %float
1500 %float_0 = OpConstant %float 0
1501 %bool = OpTypeBool
1502 %_ptr_Function_float = OpTypePointer Function %float
1503 %uint_1 = OpConstant %uint 1
1504 %uint_2 = OpConstant %uint 2
1505 %_ptr_Output_v4float = OpTypePointer Output %v4float
1506 %OutColor = OpVariable %_ptr_Output_v4float Output
1507 %main = OpFunction %void None %7
1508 %20 = OpLabel
1509 %t = OpVariable %_ptr_Function_float Function
1510 %21 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1511 %22 = OpLoad %float %21
1512 %23 = OpFOrdEqual %bool %22 %float_0
1513 OpSelectionMerge %24 None
1514 OpBranchConditional %23 %25 %26
1515 %25 = OpLabel
1516 %27 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1517 %28 = OpLoad %float %27
1518 OpStore %t %28
1519 OpBranch %24
1520 %26 = OpLabel
1521 %29 = OpAccessChain %_ptr_Input_float %BaseColor %uint_2
1522 %30 = OpLoad %float %29
1523 OpStore %t %30
1524 OpBranch %24
1525 %24 = OpLabel
1526 %31 = OpLoad %float %t
1527 %32 = OpCompositeConstruct %v4float %31 %31 %31 %31
1528 OpStore %OutColor %32
1529 OpReturn
1530 OpFunctionEnd
1531 )";
1532
1533 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1534 }
1535
1536 TEST_F(AggressiveDCETest, NoEliminateLiveIfThenElseNested) {
1537 // #version 450
1538 //
1539 // layout(location = 0) in vec4 BaseColor;
1540 // layout(location = 0) out vec4 OutColor;
1541 //
1542 // void main()
1543 // {
1544 // float t;
1545 // if (BaseColor.x == 0)
1546 // if (BaseColor.y == 0)
1547 // t = 0.0;
1548 // else
1549 // t = 0.25;
1550 // else
1551 // if (BaseColor.y == 0)
1552 // t = 0.5;
1553 // else
1554 // t = 0.75;
1555 // OutColor = vec4(t);
1556 // }
1557
1558 const std::string assembly =
1559 R"(OpCapability Shader
1560 %1 = OpExtInstImport "GLSL.std.450"
1561 OpMemoryModel Logical GLSL450
1562 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1563 OpExecutionMode %main OriginUpperLeft
1564 OpSource GLSL 450
1565 OpName %main "main"
1566 OpName %BaseColor "BaseColor"
1567 OpName %t "t"
1568 OpName %OutColor "OutColor"
1569 OpDecorate %BaseColor Location 0
1570 OpDecorate %OutColor Location 0
1571 %void = OpTypeVoid
1572 %7 = OpTypeFunction %void
1573 %float = OpTypeFloat 32
1574 %v4float = OpTypeVector %float 4
1575 %_ptr_Input_v4float = OpTypePointer Input %v4float
1576 %BaseColor = OpVariable %_ptr_Input_v4float Input
1577 %uint = OpTypeInt 32 0
1578 %uint_0 = OpConstant %uint 0
1579 %_ptr_Input_float = OpTypePointer Input %float
1580 %float_0 = OpConstant %float 0
1581 %bool = OpTypeBool
1582 %uint_1 = OpConstant %uint 1
1583 %_ptr_Function_float = OpTypePointer Function %float
1584 %float_0_25 = OpConstant %float 0.25
1585 %float_0_5 = OpConstant %float 0.5
1586 %float_0_75 = OpConstant %float 0.75
1587 %_ptr_Output_v4float = OpTypePointer Output %v4float
1588 %OutColor = OpVariable %_ptr_Output_v4float Output
1589 %main = OpFunction %void None %7
1590 %22 = OpLabel
1591 %t = OpVariable %_ptr_Function_float Function
1592 %23 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1593 %24 = OpLoad %float %23
1594 %25 = OpFOrdEqual %bool %24 %float_0
1595 OpSelectionMerge %26 None
1596 OpBranchConditional %25 %27 %28
1597 %27 = OpLabel
1598 %29 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1599 %30 = OpLoad %float %29
1600 %31 = OpFOrdEqual %bool %30 %float_0
1601 OpSelectionMerge %32 None
1602 OpBranchConditional %31 %33 %34
1603 %33 = OpLabel
1604 OpStore %t %float_0
1605 OpBranch %32
1606 %34 = OpLabel
1607 OpStore %t %float_0_25
1608 OpBranch %32
1609 %32 = OpLabel
1610 OpBranch %26
1611 %28 = OpLabel
1612 %35 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
1613 %36 = OpLoad %float %35
1614 %37 = OpFOrdEqual %bool %36 %float_0
1615 OpSelectionMerge %38 None
1616 OpBranchConditional %37 %39 %40
1617 %39 = OpLabel
1618 OpStore %t %float_0_5
1619 OpBranch %38
1620 %40 = OpLabel
1621 OpStore %t %float_0_75
1622 OpBranch %38
1623 %38 = OpLabel
1624 OpBranch %26
1625 %26 = OpLabel
1626 %41 = OpLoad %float %t
1627 %42 = OpCompositeConstruct %v4float %41 %41 %41 %41
1628 OpStore %OutColor %42
1629 OpReturn
1630 OpFunctionEnd
1631 )";
1632
1633 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1634 }
1635
1636 TEST_F(AggressiveDCETest, NoEliminateIfWithPhi) {
1637 // Note: Assembly hand-optimized from GLSL
1638 //
1639 // #version 450
1640 //
1641 // layout(location = 0) in vec4 BaseColor;
1642 // layout(location = 0) out vec4 OutColor;
1643 //
1644 // void main()
1645 // {
1646 // float t;
1647 // if (BaseColor.x == 0)
1648 // t = 0.0;
1649 // else
1650 // t = 1.0;
1651 // OutColor = vec4(t);
1652 // }
1653
1654 const std::string assembly =
1655 R"(OpCapability Shader
1656 %1 = OpExtInstImport "GLSL.std.450"
1657 OpMemoryModel Logical GLSL450
1658 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
1659 OpExecutionMode %main OriginUpperLeft
1660 OpSource GLSL 450
1661 OpName %main "main"
1662 OpName %BaseColor "BaseColor"
1663 OpName %OutColor "OutColor"
1664 OpDecorate %BaseColor Location 0
1665 OpDecorate %OutColor Location 0
1666 %void = OpTypeVoid
1667 %6 = OpTypeFunction %void
1668 %float = OpTypeFloat 32
1669 %v4float = OpTypeVector %float 4
1670 %_ptr_Input_v4float = OpTypePointer Input %v4float
1671 %BaseColor = OpVariable %_ptr_Input_v4float Input
1672 %uint = OpTypeInt 32 0
1673 %uint_0 = OpConstant %uint 0
1674 %_ptr_Input_float = OpTypePointer Input %float
1675 %float_0 = OpConstant %float 0
1676 %bool = OpTypeBool
1677 %float_1 = OpConstant %float 1
1678 %_ptr_Output_v4float = OpTypePointer Output %v4float
1679 %OutColor = OpVariable %_ptr_Output_v4float Output
1680 %main = OpFunction %void None %6
1681 %17 = OpLabel
1682 %18 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
1683 %19 = OpLoad %float %18
1684 %20 = OpFOrdEqual %bool %19 %float_0
1685 OpSelectionMerge %21 None
1686 OpBranchConditional %20 %22 %23
1687 %22 = OpLabel
1688 OpBranch %21
1689 %23 = OpLabel
1690 OpBranch %21
1691 %21 = OpLabel
1692 %24 = OpPhi %float %float_0 %22 %float_1 %23
1693 %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
1694 OpStore %OutColor %25
1695 OpReturn
1696 OpFunctionEnd
1697 )";
1698
1699 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1700 }
1701
1702 TEST_F(AggressiveDCETest, NoEliminateIfBreak) {
1703 // Note: Assembly optimized from GLSL
1704 //
1705 // #version 450
1706 //
1707 // layout(location=0) in vec4 InColor;
1708 // layout(location=0) out vec4 OutColor;
1709 //
1710 // void main()
1711 // {
1712 // float f = 0.0;
1713 // for (;;) {
1714 // f += 2.0;
1715 // if (f > 20.0)
1716 // break;
1717 // }
1718 //
1719 // OutColor = InColor / f;
1720 // }
1721
1722 const std::string assembly =
1723 R"(OpCapability Shader
1724 %1 = OpExtInstImport "GLSL.std.450"
1725 OpMemoryModel Logical GLSL450
1726 OpEntryPoint Fragment %main "main" %OutColor %InColor
1727 OpExecutionMode %main OriginUpperLeft
1728 OpSource GLSL 450
1729 OpName %main "main"
1730 OpName %f "f"
1731 OpName %OutColor "OutColor"
1732 OpName %InColor "InColor"
1733 OpDecorate %OutColor Location 0
1734 OpDecorate %InColor Location 0
1735 %void = OpTypeVoid
1736 %7 = OpTypeFunction %void
1737 %float = OpTypeFloat 32
1738 %_ptr_Function_float = OpTypePointer Function %float
1739 %float_0 = OpConstant %float 0
1740 %float_2 = OpConstant %float 2
1741 %float_20 = OpConstant %float 20
1742 %bool = OpTypeBool
1743 %v4float = OpTypeVector %float 4
1744 %_ptr_Output_v4float = OpTypePointer Output %v4float
1745 %OutColor = OpVariable %_ptr_Output_v4float Output
1746 %_ptr_Input_v4float = OpTypePointer Input %v4float
1747 %InColor = OpVariable %_ptr_Input_v4float Input
1748 %main = OpFunction %void None %7
1749 %17 = OpLabel
1750 %f = OpVariable %_ptr_Function_float Function
1751 OpStore %f %float_0
1752 OpBranch %18
1753 %18 = OpLabel
1754 OpLoopMerge %19 %20 None
1755 OpBranch %21
1756 %21 = OpLabel
1757 %22 = OpLoad %float %f
1758 %23 = OpFAdd %float %22 %float_2
1759 OpStore %f %23
1760 %24 = OpLoad %float %f
1761 %25 = OpFOrdGreaterThan %bool %24 %float_20
1762 OpSelectionMerge %26 None
1763 OpBranchConditional %25 %27 %26
1764 %27 = OpLabel
1765 OpBranch %19
1766 %26 = OpLabel
1767 OpBranch %20
1768 %20 = OpLabel
1769 OpBranch %18
1770 %19 = OpLabel
1771 %28 = OpLoad %v4float %InColor
1772 %29 = OpLoad %float %f
1773 %30 = OpCompositeConstruct %v4float %29 %29 %29 %29
1774 %31 = OpFDiv %v4float %28 %30
1775 OpStore %OutColor %31
1776 OpReturn
1777 OpFunctionEnd
1778 )";
1779
1780 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1781 }
1782
1783 TEST_F(AggressiveDCETest, NoEliminateIfBreak2) {
1784 // Do not eliminate break as conditional branch with merge instruction
1785 // Note: SPIR-V edited to add merge instruction before break.
1786 //
1787 // #version 430
1788 //
1789 // layout(std430) buffer U_t
1790 // {
1791 // float g_F[10];
1792 // };
1793 //
1794 // layout(location = 0)out float o;
1795 //
1796 // void main(void)
1797 // {
1798 // float s = 0.0;
1799 // for (int i=0; i<10; i++)
1800 // s += g_F[i];
1801 // o = s;
1802 // }
1803
1804 const std::string assembly =
1805 R"(OpCapability Shader
1806 %1 = OpExtInstImport "GLSL.std.450"
1807 OpMemoryModel Logical GLSL450
1808 OpEntryPoint Fragment %main "main" %o
1809 OpExecutionMode %main OriginUpperLeft
1810 OpSource GLSL 430
1811 OpName %main "main"
1812 OpName %s "s"
1813 OpName %i "i"
1814 OpName %U_t "U_t"
1815 OpMemberName %U_t 0 "g_F"
1816 OpName %_ ""
1817 OpName %o "o"
1818 OpDecorate %_arr_float_uint_10 ArrayStride 4
1819 OpMemberDecorate %U_t 0 Offset 0
1820 OpDecorate %U_t BufferBlock
1821 OpDecorate %_ DescriptorSet 0
1822 OpDecorate %o Location 0
1823 %void = OpTypeVoid
1824 %10 = OpTypeFunction %void
1825 %float = OpTypeFloat 32
1826 %_ptr_Function_float = OpTypePointer Function %float
1827 %float_0 = OpConstant %float 0
1828 %int = OpTypeInt 32 1
1829 %_ptr_Function_int = OpTypePointer Function %int
1830 %int_0 = OpConstant %int 0
1831 %int_10 = OpConstant %int 10
1832 %bool = OpTypeBool
1833 %uint = OpTypeInt 32 0
1834 %uint_10 = OpConstant %uint 10
1835 %_arr_float_uint_10 = OpTypeArray %float %uint_10
1836 %U_t = OpTypeStruct %_arr_float_uint_10
1837 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
1838 %_ = OpVariable %_ptr_Uniform_U_t Uniform
1839 %_ptr_Uniform_float = OpTypePointer Uniform %float
1840 %int_1 = OpConstant %int 1
1841 %_ptr_Output_float = OpTypePointer Output %float
1842 %o = OpVariable %_ptr_Output_float Output
1843 %main = OpFunction %void None %10
1844 %25 = OpLabel
1845 %s = OpVariable %_ptr_Function_float Function
1846 %i = OpVariable %_ptr_Function_int Function
1847 OpStore %s %float_0
1848 OpStore %i %int_0
1849 OpBranch %26
1850 %26 = OpLabel
1851 OpLoopMerge %27 %28 None
1852 OpBranch %29
1853 %29 = OpLabel
1854 %30 = OpLoad %int %i
1855 %31 = OpSLessThan %bool %30 %int_10
1856 OpSelectionMerge %32 None
1857 OpBranchConditional %31 %32 %27
1858 %32 = OpLabel
1859 %33 = OpLoad %int %i
1860 %34 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %33
1861 %35 = OpLoad %float %34
1862 %36 = OpLoad %float %s
1863 %37 = OpFAdd %float %36 %35
1864 OpStore %s %37
1865 OpBranch %28
1866 %28 = OpLabel
1867 %38 = OpLoad %int %i
1868 %39 = OpIAdd %int %38 %int_1
1869 OpStore %i %39
1870 OpBranch %26
1871 %27 = OpLabel
1872 %40 = OpLoad %float %s
1873 OpStore %o %40
1874 OpReturn
1875 OpFunctionEnd
1876 )";
1877
1878 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
1879 }
1880
1881 TEST_F(AggressiveDCETest, EliminateEntireUselessLoop) {
1882 // #version 140
1883 // in vec4 BaseColor;
1884 //
1885 // layout(std140) uniform U_t
1886 // {
1887 // int g_I ;
1888 // } ;
1889 //
1890 // void main()
1891 // {
1892 // vec4 v = BaseColor;
1893 // float df = 0.0;
1894 // int i = 0;
1895 // while (i < g_I) {
1896 // df = df * 0.5;
1897 // i = i + 1;
1898 // }
1899 // gl_FragColor = v;
1900 // }
1901
1902 const std::string predefs1 =
1903 R"(OpCapability Shader
1904 %1 = OpExtInstImport "GLSL.std.450"
1905 OpMemoryModel Logical GLSL450
1906 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
1907 OpExecutionMode %main OriginUpperLeft
1908 OpSource GLSL 140
1909 )";
1910
1911 const std::string names_before =
1912 R"(OpName %main "main"
1913 OpName %v "v"
1914 OpName %BaseColor "BaseColor"
1915 OpName %df "df"
1916 OpName %i "i"
1917 OpName %U_t "U_t"
1918 OpMemberName %U_t 0 "g_I"
1919 OpName %_ ""
1920 OpName %gl_FragColor "gl_FragColor"
1921 )";
1922
1923 const std::string names_after =
1924 R"(OpName %main "main"
1925 OpName %v "v"
1926 OpName %BaseColor "BaseColor"
1927 OpName %gl_FragColor "gl_FragColor"
1928 )";
1929
1930 const std::string predefs2_before =
1931 R"(OpMemberDecorate %U_t 0 Offset 0
1932 OpDecorate %U_t Block
1933 OpDecorate %_ DescriptorSet 0
1934 %void = OpTypeVoid
1935 %11 = OpTypeFunction %void
1936 %float = OpTypeFloat 32
1937 %v4float = OpTypeVector %float 4
1938 %_ptr_Function_v4float = OpTypePointer Function %v4float
1939 %_ptr_Input_v4float = OpTypePointer Input %v4float
1940 %BaseColor = OpVariable %_ptr_Input_v4float Input
1941 %_ptr_Function_float = OpTypePointer Function %float
1942 %float_0 = OpConstant %float 0
1943 %int = OpTypeInt 32 1
1944 %_ptr_Function_int = OpTypePointer Function %int
1945 %int_0 = OpConstant %int 0
1946 %U_t = OpTypeStruct %int
1947 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
1948 %_ = OpVariable %_ptr_Uniform_U_t Uniform
1949 %_ptr_Uniform_int = OpTypePointer Uniform %int
1950 %bool = OpTypeBool
1951 %float_0_5 = OpConstant %float 0.5
1952 %int_1 = OpConstant %int 1
1953 %_ptr_Output_v4float = OpTypePointer Output %v4float
1954 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1955 )";
1956
1957 const std::string predefs2_after =
1958 R"(%void = OpTypeVoid
1959 %11 = OpTypeFunction %void
1960 %float = OpTypeFloat 32
1961 %v4float = OpTypeVector %float 4
1962 %_ptr_Function_v4float = OpTypePointer Function %v4float
1963 %_ptr_Input_v4float = OpTypePointer Input %v4float
1964 %BaseColor = OpVariable %_ptr_Input_v4float Input
1965 %_ptr_Output_v4float = OpTypePointer Output %v4float
1966 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1967 )";
1968
1969 const std::string func_before =
1970 R"(%main = OpFunction %void None %11
1971 %27 = OpLabel
1972 %v = OpVariable %_ptr_Function_v4float Function
1973 %df = OpVariable %_ptr_Function_float Function
1974 %i = OpVariable %_ptr_Function_int Function
1975 %28 = OpLoad %v4float %BaseColor
1976 OpStore %v %28
1977 OpStore %df %float_0
1978 OpStore %i %int_0
1979 OpBranch %29
1980 %29 = OpLabel
1981 OpLoopMerge %30 %31 None
1982 OpBranch %32
1983 %32 = OpLabel
1984 %33 = OpLoad %int %i
1985 %34 = OpAccessChain %_ptr_Uniform_int %_ %int_0
1986 %35 = OpLoad %int %34
1987 %36 = OpSLessThan %bool %33 %35
1988 OpBranchConditional %36 %37 %30
1989 %37 = OpLabel
1990 %38 = OpLoad %float %df
1991 %39 = OpFMul %float %38 %float_0_5
1992 OpStore %df %39
1993 %40 = OpLoad %int %i
1994 %41 = OpIAdd %int %40 %int_1
1995 OpStore %i %41
1996 OpBranch %31
1997 %31 = OpLabel
1998 OpBranch %29
1999 %30 = OpLabel
2000 %42 = OpLoad %v4float %v
2001 OpStore %gl_FragColor %42
2002 OpReturn
2003 OpFunctionEnd
2004 )";
2005
2006 const std::string func_after =
2007 R"(%main = OpFunction %void None %11
2008 %27 = OpLabel
2009 %v = OpVariable %_ptr_Function_v4float Function
2010 %28 = OpLoad %v4float %BaseColor
2011 OpStore %v %28
2012 OpBranch %29
2013 %29 = OpLabel
2014 OpBranch %30
2015 %30 = OpLabel
2016 %42 = OpLoad %v4float %v
2017 OpStore %gl_FragColor %42
2018 OpReturn
2019 OpFunctionEnd
2020 )";
2021
2022 SinglePassRunAndCheck<AggressiveDCEPass>(
2023 predefs1 + names_before + predefs2_before + func_before,
2024 predefs1 + names_after + predefs2_after + func_after, true, true);
2025 }
2026
2027 TEST_F(AggressiveDCETest, NoEliminateBusyLoop) {
2028 // Note: SPIR-V edited to replace AtomicAdd(i,0) with AtomicLoad(i)
2029 //
2030 // #version 450
2031 //
2032 // layout(std430) buffer I_t
2033 // {
2034 // int g_I;
2035 // int g_I2;
2036 // };
2037 //
2038 // layout(location = 0) out int o;
2039 //
2040 // void main(void)
2041 // {
2042 // while (atomicAdd(g_I, 0) == 0) {}
2043 // o = g_I2;
2044 // }
2045
2046 const std::string assembly =
2047 R"(OpCapability Shader
2048 %1 = OpExtInstImport "GLSL.std.450"
2049 OpMemoryModel Logical GLSL450
2050 OpEntryPoint Fragment %main "main" %o
2051 OpExecutionMode %main OriginUpperLeft
2052 OpSource GLSL 450
2053 OpName %main "main"
2054 OpName %I_t "I_t"
2055 OpMemberName %I_t 0 "g_I"
2056 OpMemberName %I_t 1 "g_I2"
2057 OpName %_ ""
2058 OpName %o "o"
2059 OpMemberDecorate %I_t 0 Offset 0
2060 OpMemberDecorate %I_t 1 Offset 4
2061 OpDecorate %I_t BufferBlock
2062 OpDecorate %_ DescriptorSet 0
2063 OpDecorate %o Location 0
2064 %void = OpTypeVoid
2065 %7 = OpTypeFunction %void
2066 %int = OpTypeInt 32 1
2067 %I_t = OpTypeStruct %int %int
2068 %_ptr_Uniform_I_t = OpTypePointer Uniform %I_t
2069 %_ = OpVariable %_ptr_Uniform_I_t Uniform
2070 %int_0 = OpConstant %int 0
2071 %int_1 = OpConstant %int 1
2072 %_ptr_Uniform_int = OpTypePointer Uniform %int
2073 %uint = OpTypeInt 32 0
2074 %uint_1 = OpConstant %uint 1
2075 %uint_0 = OpConstant %uint 0
2076 %bool = OpTypeBool
2077 %_ptr_Output_int = OpTypePointer Output %int
2078 %o = OpVariable %_ptr_Output_int Output
2079 %main = OpFunction %void None %7
2080 %18 = OpLabel
2081 OpBranch %19
2082 %19 = OpLabel
2083 OpLoopMerge %20 %21 None
2084 OpBranch %22
2085 %22 = OpLabel
2086 %23 = OpAccessChain %_ptr_Uniform_int %_ %int_0
2087 %24 = OpAtomicLoad %int %23 %uint_1 %uint_0
2088 %25 = OpIEqual %bool %24 %int_0
2089 OpBranchConditional %25 %26 %20
2090 %26 = OpLabel
2091 OpBranch %21
2092 %21 = OpLabel
2093 OpBranch %19
2094 %20 = OpLabel
2095 %27 = OpAccessChain %_ptr_Uniform_int %_ %int_1
2096 %28 = OpLoad %int %27
2097 OpStore %o %28
2098 OpReturn
2099 OpFunctionEnd
2100 )";
2101
2102 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
2103 }
2104
2105 TEST_F(AggressiveDCETest, NoEliminateLiveLoop) {
2106 // Note: SPIR-V optimized
2107 //
2108 // #version 430
2109 //
2110 // layout(std430) buffer U_t
2111 // {
2112 // float g_F[10];
2113 // };
2114 //
2115 // layout(location = 0)out float o;
2116 //
2117 // void main(void)
2118 // {
2119 // float s = 0.0;
2120 // for (int i=0; i<10; i++)
2121 // s += g_F[i];
2122 // o = s;
2123 // }
2124
2125 const std::string assembly =
2126 R"(OpCapability Shader
2127 %1 = OpExtInstImport "GLSL.std.450"
2128 OpMemoryModel Logical GLSL450
2129 OpEntryPoint Fragment %main "main" %o
2130 OpExecutionMode %main OriginUpperLeft
2131 OpSource GLSL 430
2132 OpName %main "main"
2133 OpName %U_t "U_t"
2134 OpMemberName %U_t 0 "g_F"
2135 OpName %_ ""
2136 OpName %o "o"
2137 OpDecorate %_arr_float_uint_10 ArrayStride 4
2138 OpMemberDecorate %U_t 0 Offset 0
2139 OpDecorate %U_t BufferBlock
2140 OpDecorate %_ DescriptorSet 0
2141 OpDecorate %o Location 0
2142 %void = OpTypeVoid
2143 %8 = OpTypeFunction %void
2144 %float = OpTypeFloat 32
2145 %float_0 = OpConstant %float 0
2146 %int = OpTypeInt 32 1
2147 %int_0 = OpConstant %int 0
2148 %int_10 = OpConstant %int 10
2149 %bool = OpTypeBool
2150 %uint = OpTypeInt 32 0
2151 %uint_10 = OpConstant %uint 10
2152 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2153 %U_t = OpTypeStruct %_arr_float_uint_10
2154 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2155 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2156 %_ptr_Uniform_float = OpTypePointer Uniform %float
2157 %int_1 = OpConstant %int 1
2158 %_ptr_Output_float = OpTypePointer Output %float
2159 %o = OpVariable %_ptr_Output_float Output
2160 %main = OpFunction %void None %8
2161 %21 = OpLabel
2162 OpBranch %22
2163 %22 = OpLabel
2164 %23 = OpPhi %float %float_0 %21 %24 %25
2165 %26 = OpPhi %int %int_0 %21 %27 %25
2166 OpLoopMerge %28 %25 None
2167 OpBranch %29
2168 %29 = OpLabel
2169 %30 = OpSLessThan %bool %26 %int_10
2170 OpBranchConditional %30 %31 %28
2171 %31 = OpLabel
2172 %32 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %26
2173 %33 = OpLoad %float %32
2174 %24 = OpFAdd %float %23 %33
2175 OpBranch %25
2176 %25 = OpLabel
2177 %27 = OpIAdd %int %26 %int_1
2178 OpBranch %22
2179 %28 = OpLabel
2180 OpStore %o %23
2181 OpReturn
2182 OpFunctionEnd
2183 )";
2184
2185 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
2186 }
2187
2188 TEST_F(AggressiveDCETest, EliminateEntireFunctionBody) {
2189 // #version 450
2190 //
2191 // layout(location = 0) in vec4 BaseColor;
2192 // layout(location = 0) out vec4 OutColor;
2193 //
2194 // void main()
2195 // {
2196 // float d;
2197 // if (BaseColor.x == 0)
2198 // d = BaseColor.y;
2199 // else
2200 // d = BaseColor.z;
2201 // }
2202
2203 const std::string spirv =
2204 R"(OpCapability Shader
2205 %1 = OpExtInstImport "GLSL.std.450"
2206 OpMemoryModel Logical GLSL450
2207 OpEntryPoint Fragment %main "main" %BaseColor %OutColor
2208 OpExecutionMode %main OriginUpperLeft
2209 OpSource GLSL 450
2210 OpName %main "main"
2211 OpName %BaseColor "BaseColor"
2212 OpName %d "d"
2213 OpName %OutColor "OutColor"
2214 OpDecorate %BaseColor Location 0
2215 OpDecorate %OutColor Location 0
2216 %void = OpTypeVoid
2217 %7 = OpTypeFunction %void
2218 %float = OpTypeFloat 32
2219 %v4float = OpTypeVector %float 4
2220 %_ptr_Input_v4float = OpTypePointer Input %v4float
2221 %BaseColor = OpVariable %_ptr_Input_v4float Input
2222 %uint = OpTypeInt 32 0
2223 %uint_0 = OpConstant %uint 0
2224 %_ptr_Input_float = OpTypePointer Input %float
2225 %float_0 = OpConstant %float 0
2226 %bool = OpTypeBool
2227 %_ptr_Function_float = OpTypePointer Function %float
2228 %uint_1 = OpConstant %uint 1
2229 %uint_2 = OpConstant %uint 2
2230 %_ptr_Output_v4float = OpTypePointer Output %v4float
2231 %OutColor = OpVariable %_ptr_Output_v4float Output
2232
2233 ; CHECK: = OpFunction
2234 ; CHECK-NEXT: = OpLabel
2235 ; CHECK-NEXT: OpBranch [[target:%\w+]]
2236 ; CHECK-NEXT: [[target]] = OpLabel
2237 ; CHECK-NEXT: OpReturn
2238 ; CHECK-NEXT: OpFunctionEnd
2239
2240 %main = OpFunction %void None %7
2241 %20 = OpLabel
2242 %d = OpVariable %_ptr_Function_float Function
2243 %21 = OpAccessChain %_ptr_Input_float %BaseColor %uint_0
2244 %22 = OpLoad %float %21
2245 %23 = OpFOrdEqual %bool %22 %float_0
2246 OpSelectionMerge %24 None
2247 OpBranchConditional %23 %25 %26
2248 %25 = OpLabel
2249 %27 = OpAccessChain %_ptr_Input_float %BaseColor %uint_1
2250 %28 = OpLoad %float %27
2251 OpStore %d %28
2252 OpBranch %24
2253 %26 = OpLabel
2254 %29 = OpAccessChain %_ptr_Input_float %BaseColor %uint_2
2255 %30 = OpLoad %float %29
2256 OpStore %d %30
2257 OpBranch %24
2258 %24 = OpLabel
2259 OpReturn
2260 OpFunctionEnd
2261 )";
2262
2263 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
2264 }
2265
2266 TEST_F(AggressiveDCETest, EliminateUselessInnerLoop) {
2267 // #version 430
2268 //
2269 // layout(std430) buffer U_t
2270 // {
2271 // float g_F[10];
2272 // };
2273 //
2274 // layout(location = 0)out float o;
2275 //
2276 // void main(void)
2277 // {
2278 // float s = 0.0;
2279 // for (int i=0; i<10; i++) {
2280 // for (int j=0; j<10; j++) {
2281 // }
2282 // s += g_F[i];
2283 // }
2284 // o = s;
2285 // }
2286
2287 const std::string predefs_before =
2288 R"(OpCapability Shader
2289 %1 = OpExtInstImport "GLSL.std.450"
2290 OpMemoryModel Logical GLSL450
2291 OpEntryPoint Fragment %main "main" %o
2292 OpExecutionMode %main OriginUpperLeft
2293 OpSource GLSL 430
2294 OpName %main "main"
2295 OpName %s "s"
2296 OpName %i "i"
2297 OpName %j "j"
2298 OpName %U_t "U_t"
2299 OpMemberName %U_t 0 "g_F"
2300 OpName %_ ""
2301 OpName %o "o"
2302 OpDecorate %_arr_float_uint_10 ArrayStride 4
2303 OpMemberDecorate %U_t 0 Offset 0
2304 OpDecorate %U_t BufferBlock
2305 OpDecorate %_ DescriptorSet 0
2306 OpDecorate %o Location 0
2307 %void = OpTypeVoid
2308 %11 = OpTypeFunction %void
2309 %float = OpTypeFloat 32
2310 %_ptr_Function_float = OpTypePointer Function %float
2311 %float_0 = OpConstant %float 0
2312 %int = OpTypeInt 32 1
2313 %_ptr_Function_int = OpTypePointer Function %int
2314 %int_0 = OpConstant %int 0
2315 %int_10 = OpConstant %int 10
2316 %bool = OpTypeBool
2317 %int_1 = OpConstant %int 1
2318 %uint = OpTypeInt 32 0
2319 %uint_10 = OpConstant %uint 10
2320 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2321 %U_t = OpTypeStruct %_arr_float_uint_10
2322 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2323 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2324 %_ptr_Uniform_float = OpTypePointer Uniform %float
2325 %_ptr_Output_float = OpTypePointer Output %float
2326 %o = OpVariable %_ptr_Output_float Output
2327 )";
2328
2329 const std::string predefs_after =
2330 R"(OpCapability Shader
2331 %1 = OpExtInstImport "GLSL.std.450"
2332 OpMemoryModel Logical GLSL450
2333 OpEntryPoint Fragment %main "main" %o
2334 OpExecutionMode %main OriginUpperLeft
2335 OpSource GLSL 430
2336 OpName %main "main"
2337 OpName %s "s"
2338 OpName %i "i"
2339 OpName %U_t "U_t"
2340 OpMemberName %U_t 0 "g_F"
2341 OpName %_ ""
2342 OpName %o "o"
2343 OpDecorate %_arr_float_uint_10 ArrayStride 4
2344 OpMemberDecorate %U_t 0 Offset 0
2345 OpDecorate %U_t BufferBlock
2346 OpDecorate %_ DescriptorSet 0
2347 OpDecorate %o Location 0
2348 %void = OpTypeVoid
2349 %11 = OpTypeFunction %void
2350 %float = OpTypeFloat 32
2351 %_ptr_Function_float = OpTypePointer Function %float
2352 %float_0 = OpConstant %float 0
2353 %int = OpTypeInt 32 1
2354 %_ptr_Function_int = OpTypePointer Function %int
2355 %int_0 = OpConstant %int 0
2356 %int_10 = OpConstant %int 10
2357 %bool = OpTypeBool
2358 %int_1 = OpConstant %int 1
2359 %uint = OpTypeInt 32 0
2360 %uint_10 = OpConstant %uint 10
2361 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2362 %U_t = OpTypeStruct %_arr_float_uint_10
2363 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2364 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2365 %_ptr_Uniform_float = OpTypePointer Uniform %float
2366 %_ptr_Output_float = OpTypePointer Output %float
2367 %o = OpVariable %_ptr_Output_float Output
2368 )";
2369
2370 const std::string func_before =
2371 R"(%main = OpFunction %void None %11
2372 %26 = OpLabel
2373 %s = OpVariable %_ptr_Function_float Function
2374 %i = OpVariable %_ptr_Function_int Function
2375 %j = OpVariable %_ptr_Function_int Function
2376 OpStore %s %float_0
2377 OpStore %i %int_0
2378 OpBranch %27
2379 %27 = OpLabel
2380 OpLoopMerge %28 %29 None
2381 OpBranch %30
2382 %30 = OpLabel
2383 %31 = OpLoad %int %i
2384 %32 = OpSLessThan %bool %31 %int_10
2385 OpBranchConditional %32 %33 %28
2386 %33 = OpLabel
2387 OpStore %j %int_0
2388 OpBranch %34
2389 %34 = OpLabel
2390 OpLoopMerge %35 %36 None
2391 OpBranch %37
2392 %37 = OpLabel
2393 %38 = OpLoad %int %j
2394 %39 = OpSLessThan %bool %38 %int_10
2395 OpBranchConditional %39 %40 %35
2396 %40 = OpLabel
2397 OpBranch %36
2398 %36 = OpLabel
2399 %41 = OpLoad %int %j
2400 %42 = OpIAdd %int %41 %int_1
2401 OpStore %j %42
2402 OpBranch %34
2403 %35 = OpLabel
2404 %43 = OpLoad %int %i
2405 %44 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %43
2406 %45 = OpLoad %float %44
2407 %46 = OpLoad %float %s
2408 %47 = OpFAdd %float %46 %45
2409 OpStore %s %47
2410 OpBranch %29
2411 %29 = OpLabel
2412 %48 = OpLoad %int %i
2413 %49 = OpIAdd %int %48 %int_1
2414 OpStore %i %49
2415 OpBranch %27
2416 %28 = OpLabel
2417 %50 = OpLoad %float %s
2418 OpStore %o %50
2419 OpReturn
2420 OpFunctionEnd
2421 )";
2422
2423 const std::string func_after =
2424 R"(%main = OpFunction %void None %11
2425 %26 = OpLabel
2426 %s = OpVariable %_ptr_Function_float Function
2427 %i = OpVariable %_ptr_Function_int Function
2428 OpStore %s %float_0
2429 OpStore %i %int_0
2430 OpBranch %27
2431 %27 = OpLabel
2432 OpLoopMerge %28 %29 None
2433 OpBranch %30
2434 %30 = OpLabel
2435 %31 = OpLoad %int %i
2436 %32 = OpSLessThan %bool %31 %int_10
2437 OpBranchConditional %32 %33 %28
2438 %33 = OpLabel
2439 OpBranch %34
2440 %34 = OpLabel
2441 OpBranch %35
2442 %35 = OpLabel
2443 %43 = OpLoad %int %i
2444 %44 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %43
2445 %45 = OpLoad %float %44
2446 %46 = OpLoad %float %s
2447 %47 = OpFAdd %float %46 %45
2448 OpStore %s %47
2449 OpBranch %29
2450 %29 = OpLabel
2451 %48 = OpLoad %int %i
2452 %49 = OpIAdd %int %48 %int_1
2453 OpStore %i %49
2454 OpBranch %27
2455 %28 = OpLabel
2456 %50 = OpLoad %float %s
2457 OpStore %o %50
2458 OpReturn
2459 OpFunctionEnd
2460 )";
2461
2462 SinglePassRunAndCheck<AggressiveDCEPass>(
2463 predefs_before + func_before, predefs_after + func_after, true, true);
2464 }
2465
2466 TEST_F(AggressiveDCETest, EliminateUselessNestedLoopWithIf) {
2467 // #version 430
2468 //
2469 // layout(std430) buffer U_t
2470 // {
2471 // float g_F[10][10];
2472 // };
2473 //
2474 // layout(location = 0)out float o;
2475 //
2476 // void main(void)
2477 // {
2478 // float s = 0.0;
2479 // for (int i=0; i<10; i++) {
2480 // for (int j=0; j<10; j++) {
2481 // float t = g_F[i][j];
2482 // if (t > 0.0)
2483 // s += t;
2484 // }
2485 // }
2486 // o = 0.0;
2487 // }
2488
2489 const std::string predefs_before =
2490 R"(OpCapability Shader
2491 %1 = OpExtInstImport "GLSL.std.450"
2492 OpMemoryModel Logical GLSL450
2493 OpEntryPoint Fragment %main "main" %o
2494 OpExecutionMode %main OriginUpperLeft
2495 OpSource GLSL 430
2496 OpName %main "main"
2497 OpName %s "s"
2498 OpName %i "i"
2499 OpName %j "j"
2500 OpName %U_t "U_t"
2501 OpMemberName %U_t 0 "g_F"
2502 OpName %_ ""
2503 OpName %o "o"
2504 OpDecorate %_arr_float_uint_10 ArrayStride 4
2505 OpDecorate %_arr__arr_float_uint_10_uint_10 ArrayStride 40
2506 OpMemberDecorate %U_t 0 Offset 0
2507 OpDecorate %U_t BufferBlock
2508 OpDecorate %_ DescriptorSet 0
2509 OpDecorate %o Location 0
2510 %void = OpTypeVoid
2511 %12 = OpTypeFunction %void
2512 %float = OpTypeFloat 32
2513 %_ptr_Function_float = OpTypePointer Function %float
2514 %float_0 = OpConstant %float 0
2515 %int = OpTypeInt 32 1
2516 %_ptr_Function_int = OpTypePointer Function %int
2517 %int_0 = OpConstant %int 0
2518 %int_10 = OpConstant %int 10
2519 %bool = OpTypeBool
2520 %uint = OpTypeInt 32 0
2521 %uint_10 = OpConstant %uint 10
2522 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2523 %_arr__arr_float_uint_10_uint_10 = OpTypeArray %_arr_float_uint_10 %uint_10
2524 %U_t = OpTypeStruct %_arr__arr_float_uint_10_uint_10
2525 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2526 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2527 %_ptr_Uniform_float = OpTypePointer Uniform %float
2528 %int_1 = OpConstant %int 1
2529 %_ptr_Output_float = OpTypePointer Output %float
2530 %o = OpVariable %_ptr_Output_float Output
2531 )";
2532
2533 const std::string predefs_after =
2534 R"(OpCapability Shader
2535 %1 = OpExtInstImport "GLSL.std.450"
2536 OpMemoryModel Logical GLSL450
2537 OpEntryPoint Fragment %main "main" %o
2538 OpExecutionMode %main OriginUpperLeft
2539 OpSource GLSL 430
2540 OpName %main "main"
2541 OpName %o "o"
2542 OpDecorate %o Location 0
2543 %void = OpTypeVoid
2544 %12 = OpTypeFunction %void
2545 %float = OpTypeFloat 32
2546 %float_0 = OpConstant %float 0
2547 %_ptr_Output_float = OpTypePointer Output %float
2548 %o = OpVariable %_ptr_Output_float Output
2549 )";
2550
2551 const std::string func_before =
2552 R"(%main = OpFunction %void None %12
2553 %27 = OpLabel
2554 %s = OpVariable %_ptr_Function_float Function
2555 %i = OpVariable %_ptr_Function_int Function
2556 %j = OpVariable %_ptr_Function_int Function
2557 OpStore %s %float_0
2558 OpStore %i %int_0
2559 OpBranch %28
2560 %28 = OpLabel
2561 OpLoopMerge %29 %30 None
2562 OpBranch %31
2563 %31 = OpLabel
2564 %32 = OpLoad %int %i
2565 %33 = OpSLessThan %bool %32 %int_10
2566 OpBranchConditional %33 %34 %29
2567 %34 = OpLabel
2568 OpStore %j %int_0
2569 OpBranch %35
2570 %35 = OpLabel
2571 OpLoopMerge %36 %37 None
2572 OpBranch %38
2573 %38 = OpLabel
2574 %39 = OpLoad %int %j
2575 %40 = OpSLessThan %bool %39 %int_10
2576 OpBranchConditional %40 %41 %36
2577 %41 = OpLabel
2578 %42 = OpLoad %int %i
2579 %43 = OpLoad %int %j
2580 %44 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %42 %43
2581 %45 = OpLoad %float %44
2582 %46 = OpFOrdGreaterThan %bool %45 %float_0
2583 OpSelectionMerge %47 None
2584 OpBranchConditional %46 %48 %47
2585 %48 = OpLabel
2586 %49 = OpLoad %float %s
2587 %50 = OpFAdd %float %49 %45
2588 OpStore %s %50
2589 OpBranch %47
2590 %47 = OpLabel
2591 OpBranch %37
2592 %37 = OpLabel
2593 %51 = OpLoad %int %j
2594 %52 = OpIAdd %int %51 %int_1
2595 OpStore %j %52
2596 OpBranch %35
2597 %36 = OpLabel
2598 OpBranch %30
2599 %30 = OpLabel
2600 %53 = OpLoad %int %i
2601 %54 = OpIAdd %int %53 %int_1
2602 OpStore %i %54
2603 OpBranch %28
2604 %29 = OpLabel
2605 OpStore %o %float_0
2606 OpReturn
2607 OpFunctionEnd
2608 )";
2609
2610 const std::string func_after =
2611 R"(%main = OpFunction %void None %12
2612 %27 = OpLabel
2613 OpBranch %28
2614 %28 = OpLabel
2615 OpBranch %29
2616 %29 = OpLabel
2617 OpStore %o %float_0
2618 OpReturn
2619 OpFunctionEnd
2620 )";
2621
2622 SinglePassRunAndCheck<AggressiveDCEPass>(
2623 predefs_before + func_before, predefs_after + func_after, true, true);
2624 }
2625
2626 TEST_F(AggressiveDCETest, EliminateEmptyIfBeforeContinue) {
2627 // #version 430
2628 //
2629 // layout(location = 0)out float o;
2630 //
2631 // void main(void)
2632 // {
2633 // float s = 0.0;
2634 // for (int i=0; i<10; i++) {
2635 // s += 1.0;
2636 // if (i > s) {}
2637 // }
2638 // o = s;
2639 // }
2640
2641 const std::string predefs_before =
2642 R"(OpCapability Shader
2643 %1 = OpExtInstImport "GLSL.std.450"
2644 OpMemoryModel Logical GLSL450
2645 OpEntryPoint Fragment %main "main" %3
2646 OpExecutionMode %main OriginUpperLeft
2647 OpSource GLSL 430
2648 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
2649 OpSourceExtension "GL_GOOGLE_include_directive"
2650 OpName %main "main"
2651 OpDecorate %3 Location 0
2652 %void = OpTypeVoid
2653 %5 = OpTypeFunction %void
2654 %float = OpTypeFloat 32
2655 %float_0 = OpConstant %float 0
2656 %int = OpTypeInt 32 1
2657 %_ptr_Function_int = OpTypePointer Function %int
2658 %int_0 = OpConstant %int 0
2659 %int_10 = OpConstant %int 10
2660 %bool = OpTypeBool
2661 %float_1 = OpConstant %float 1
2662 %int_1 = OpConstant %int 1
2663 %_ptr_Output_float = OpTypePointer Output %float
2664 %3 = OpVariable %_ptr_Output_float Output
2665 )";
2666
2667 const std::string predefs_after =
2668 R"(OpCapability Shader
2669 %1 = OpExtInstImport "GLSL.std.450"
2670 OpMemoryModel Logical GLSL450
2671 OpEntryPoint Fragment %main "main" %3
2672 OpExecutionMode %main OriginUpperLeft
2673 OpSource GLSL 430
2674 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
2675 OpSourceExtension "GL_GOOGLE_include_directive"
2676 OpName %main "main"
2677 OpDecorate %3 Location 0
2678 %void = OpTypeVoid
2679 %5 = OpTypeFunction %void
2680 %float = OpTypeFloat 32
2681 %float_0 = OpConstant %float 0
2682 %int = OpTypeInt 32 1
2683 %int_0 = OpConstant %int 0
2684 %int_10 = OpConstant %int 10
2685 %bool = OpTypeBool
2686 %float_1 = OpConstant %float 1
2687 %int_1 = OpConstant %int 1
2688 %_ptr_Output_float = OpTypePointer Output %float
2689 %3 = OpVariable %_ptr_Output_float Output
2690 )";
2691
2692 const std::string func_before =
2693 R"(%main = OpFunction %void None %5
2694 %16 = OpLabel
2695 OpBranch %17
2696 %17 = OpLabel
2697 %18 = OpPhi %float %float_0 %16 %19 %20
2698 %21 = OpPhi %int %int_0 %16 %22 %20
2699 OpLoopMerge %23 %20 None
2700 OpBranch %24
2701 %24 = OpLabel
2702 %25 = OpSLessThan %bool %21 %int_10
2703 OpBranchConditional %25 %26 %23
2704 %26 = OpLabel
2705 %19 = OpFAdd %float %18 %float_1
2706 %27 = OpConvertFToS %int %19
2707 %28 = OpSGreaterThan %bool %21 %27
2708 OpSelectionMerge %20 None
2709 OpBranchConditional %28 %29 %20
2710 %29 = OpLabel
2711 OpBranch %20
2712 %20 = OpLabel
2713 %22 = OpIAdd %int %21 %int_1
2714 OpBranch %17
2715 %23 = OpLabel
2716 OpStore %3 %18
2717 OpReturn
2718 OpFunctionEnd
2719 )";
2720
2721 const std::string func_after =
2722 R"(%main = OpFunction %void None %5
2723 %16 = OpLabel
2724 OpBranch %17
2725 %17 = OpLabel
2726 %18 = OpPhi %float %float_0 %16 %19 %20
2727 %21 = OpPhi %int %int_0 %16 %22 %20
2728 OpLoopMerge %23 %20 None
2729 OpBranch %24
2730 %24 = OpLabel
2731 %25 = OpSLessThan %bool %21 %int_10
2732 OpBranchConditional %25 %26 %23
2733 %26 = OpLabel
2734 %19 = OpFAdd %float %18 %float_1
2735 OpBranch %20
2736 %20 = OpLabel
2737 %22 = OpIAdd %int %21 %int_1
2738 OpBranch %17
2739 %23 = OpLabel
2740 OpStore %3 %18
2741 OpReturn
2742 OpFunctionEnd
2743 )";
2744
2745 SinglePassRunAndCheck<AggressiveDCEPass>(
2746 predefs_before + func_before, predefs_after + func_after, true, true);
2747 }
2748
2749 TEST_F(AggressiveDCETest, NoEliminateLiveNestedLoopWithIf) {
2750 // Note: SPIR-V optimized
2751 //
2752 // #version 430
2753 //
2754 // layout(std430) buffer U_t
2755 // {
2756 // float g_F[10][10];
2757 // };
2758 //
2759 // layout(location = 0)out float o;
2760 //
2761 // void main(void)
2762 // {
2763 // float s = 0.0;
2764 // for (int i=0; i<10; i++) {
2765 // for (int j=0; j<10; j++) {
2766 // float t = g_F[i][j];
2767 // if (t > 0.0)
2768 // s += t;
2769 // }
2770 // }
2771 // o = s;
2772 // }
2773
2774 const std::string assembly =
2775 R"(OpCapability Shader
2776 %1 = OpExtInstImport "GLSL.std.450"
2777 OpMemoryModel Logical GLSL450
2778 OpEntryPoint Fragment %main "main" %o
2779 OpExecutionMode %main OriginUpperLeft
2780 OpSource GLSL 430
2781 OpName %main "main"
2782 OpName %s "s"
2783 OpName %i "i"
2784 OpName %j "j"
2785 OpName %U_t "U_t"
2786 OpMemberName %U_t 0 "g_F"
2787 OpName %_ ""
2788 OpName %o "o"
2789 OpDecorate %_arr_float_uint_10 ArrayStride 4
2790 OpDecorate %_arr__arr_float_uint_10_uint_10 ArrayStride 40
2791 OpMemberDecorate %U_t 0 Offset 0
2792 OpDecorate %U_t BufferBlock
2793 OpDecorate %_ DescriptorSet 0
2794 OpDecorate %o Location 0
2795 %void = OpTypeVoid
2796 %12 = OpTypeFunction %void
2797 %float = OpTypeFloat 32
2798 %_ptr_Function_float = OpTypePointer Function %float
2799 %float_0 = OpConstant %float 0
2800 %int = OpTypeInt 32 1
2801 %_ptr_Function_int = OpTypePointer Function %int
2802 %int_0 = OpConstant %int 0
2803 %int_10 = OpConstant %int 10
2804 %bool = OpTypeBool
2805 %uint = OpTypeInt 32 0
2806 %uint_10 = OpConstant %uint 10
2807 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2808 %_arr__arr_float_uint_10_uint_10 = OpTypeArray %_arr_float_uint_10 %uint_10
2809 %U_t = OpTypeStruct %_arr__arr_float_uint_10_uint_10
2810 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2811 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2812 %_ptr_Uniform_float = OpTypePointer Uniform %float
2813 %int_1 = OpConstant %int 1
2814 %_ptr_Output_float = OpTypePointer Output %float
2815 %o = OpVariable %_ptr_Output_float Output
2816 %main = OpFunction %void None %12
2817 %27 = OpLabel
2818 %s = OpVariable %_ptr_Function_float Function
2819 %i = OpVariable %_ptr_Function_int Function
2820 %j = OpVariable %_ptr_Function_int Function
2821 OpStore %s %float_0
2822 OpStore %i %int_0
2823 OpBranch %28
2824 %28 = OpLabel
2825 OpLoopMerge %29 %30 None
2826 OpBranch %31
2827 %31 = OpLabel
2828 %32 = OpLoad %int %i
2829 %33 = OpSLessThan %bool %32 %int_10
2830 OpBranchConditional %33 %34 %29
2831 %34 = OpLabel
2832 OpStore %j %int_0
2833 OpBranch %35
2834 %35 = OpLabel
2835 OpLoopMerge %36 %37 None
2836 OpBranch %38
2837 %38 = OpLabel
2838 %39 = OpLoad %int %j
2839 %40 = OpSLessThan %bool %39 %int_10
2840 OpBranchConditional %40 %41 %36
2841 %41 = OpLabel
2842 %42 = OpLoad %int %i
2843 %43 = OpLoad %int %j
2844 %44 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %42 %43
2845 %45 = OpLoad %float %44
2846 %46 = OpFOrdGreaterThan %bool %45 %float_0
2847 OpSelectionMerge %47 None
2848 OpBranchConditional %46 %48 %47
2849 %48 = OpLabel
2850 %49 = OpLoad %float %s
2851 %50 = OpFAdd %float %49 %45
2852 OpStore %s %50
2853 OpBranch %47
2854 %47 = OpLabel
2855 OpBranch %37
2856 %37 = OpLabel
2857 %51 = OpLoad %int %j
2858 %52 = OpIAdd %int %51 %int_1
2859 OpStore %j %52
2860 OpBranch %35
2861 %36 = OpLabel
2862 OpBranch %30
2863 %30 = OpLabel
2864 %53 = OpLoad %int %i
2865 %54 = OpIAdd %int %53 %int_1
2866 OpStore %i %54
2867 OpBranch %28
2868 %29 = OpLabel
2869 %55 = OpLoad %float %s
2870 OpStore %o %55
2871 OpReturn
2872 OpFunctionEnd
2873 )";
2874
2875 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
2876 }
2877
2878 TEST_F(AggressiveDCETest, NoEliminateIfContinue) {
2879 // Do not eliminate continue embedded in if construct
2880 //
2881 // #version 430
2882 //
2883 // layout(std430) buffer U_t
2884 // {
2885 // float g_F[10];
2886 // };
2887 //
2888 // layout(location = 0)out float o;
2889 //
2890 // void main(void)
2891 // {
2892 // float s = 0.0;
2893 // for (int i=0; i<10; i++) {
2894 // if (i % 2 == 0) continue;
2895 // s += g_F[i];
2896 // }
2897 // o = s;
2898 // }
2899
2900 const std::string assembly =
2901 R"(OpCapability Shader
2902 %1 = OpExtInstImport "GLSL.std.450"
2903 OpMemoryModel Logical GLSL450
2904 OpEntryPoint Fragment %main "main" %o
2905 OpExecutionMode %main OriginUpperLeft
2906 OpSource GLSL 430
2907 OpName %main "main"
2908 OpName %s "s"
2909 OpName %i "i"
2910 OpName %U_t "U_t"
2911 OpMemberName %U_t 0 "g_F"
2912 OpName %_ ""
2913 OpName %o "o"
2914 OpDecorate %_arr_float_uint_10 ArrayStride 4
2915 OpMemberDecorate %U_t 0 Offset 0
2916 OpDecorate %U_t BufferBlock
2917 OpDecorate %_ DescriptorSet 0
2918 OpDecorate %o Location 0
2919 %void = OpTypeVoid
2920 %10 = OpTypeFunction %void
2921 %float = OpTypeFloat 32
2922 %_ptr_Function_float = OpTypePointer Function %float
2923 %float_0 = OpConstant %float 0
2924 %int = OpTypeInt 32 1
2925 %_ptr_Function_int = OpTypePointer Function %int
2926 %int_0 = OpConstant %int 0
2927 %int_10 = OpConstant %int 10
2928 %bool = OpTypeBool
2929 %int_2 = OpConstant %int 2
2930 %uint = OpTypeInt 32 0
2931 %uint_10 = OpConstant %uint 10
2932 %_arr_float_uint_10 = OpTypeArray %float %uint_10
2933 %U_t = OpTypeStruct %_arr_float_uint_10
2934 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
2935 %_ = OpVariable %_ptr_Uniform_U_t Uniform
2936 %_ptr_Uniform_float = OpTypePointer Uniform %float
2937 %int_1 = OpConstant %int 1
2938 %_ptr_Output_float = OpTypePointer Output %float
2939 %o = OpVariable %_ptr_Output_float Output
2940 %main = OpFunction %void None %10
2941 %26 = OpLabel
2942 %s = OpVariable %_ptr_Function_float Function
2943 %i = OpVariable %_ptr_Function_int Function
2944 OpStore %s %float_0
2945 OpStore %i %int_0
2946 OpBranch %27
2947 %27 = OpLabel
2948 OpLoopMerge %28 %29 None
2949 OpBranch %30
2950 %30 = OpLabel
2951 %31 = OpLoad %int %i
2952 %32 = OpSLessThan %bool %31 %int_10
2953 OpBranchConditional %32 %33 %28
2954 %33 = OpLabel
2955 %34 = OpLoad %int %i
2956 %35 = OpSMod %int %34 %int_2
2957 %36 = OpIEqual %bool %35 %int_0
2958 OpSelectionMerge %37 None
2959 OpBranchConditional %36 %38 %37
2960 %38 = OpLabel
2961 OpBranch %29
2962 %37 = OpLabel
2963 %39 = OpLoad %int %i
2964 %40 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %39
2965 %41 = OpLoad %float %40
2966 %42 = OpLoad %float %s
2967 %43 = OpFAdd %float %42 %41
2968 OpStore %s %43
2969 OpBranch %29
2970 %29 = OpLabel
2971 %44 = OpLoad %int %i
2972 %45 = OpIAdd %int %44 %int_1
2973 OpStore %i %45
2974 OpBranch %27
2975 %28 = OpLabel
2976 %46 = OpLoad %float %s
2977 OpStore %o %46
2978 OpReturn
2979 OpFunctionEnd
2980 )";
2981
2982 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
2983 }
2984
2985 TEST_F(AggressiveDCETest, NoEliminateIfContinue2) {
2986 // Do not eliminate continue not embedded in if construct
2987 //
2988 // #version 430
2989 //
2990 // layout(std430) buffer U_t
2991 // {
2992 // float g_F[10];
2993 // };
2994 //
2995 // layout(location = 0)out float o;
2996 //
2997 // void main(void)
2998 // {
2999 // float s = 0.0;
3000 // for (int i=0; i<10; i++) {
3001 // if (i % 2 == 0) continue;
3002 // s += g_F[i];
3003 // }
3004 // o = s;
3005 // }
3006
3007 const std::string assembly =
3008 R"(OpCapability Shader
3009 %1 = OpExtInstImport "GLSL.std.450"
3010 OpMemoryModel Logical GLSL450
3011 OpEntryPoint Fragment %main "main" %o
3012 OpExecutionMode %main OriginUpperLeft
3013 OpSource GLSL 430
3014 OpName %main "main"
3015 OpName %s "s"
3016 OpName %i "i"
3017 OpName %U_t "U_t"
3018 OpMemberName %U_t 0 "g_F"
3019 OpName %_ ""
3020 OpName %o "o"
3021 OpDecorate %_arr_float_uint_10 ArrayStride 4
3022 OpMemberDecorate %U_t 0 Offset 0
3023 OpDecorate %U_t BufferBlock
3024 OpDecorate %_ DescriptorSet 0
3025 OpDecorate %o Location 0
3026 %void = OpTypeVoid
3027 %10 = OpTypeFunction %void
3028 %float = OpTypeFloat 32
3029 %_ptr_Function_float = OpTypePointer Function %float
3030 %float_0 = OpConstant %float 0
3031 %int = OpTypeInt 32 1
3032 %_ptr_Function_int = OpTypePointer Function %int
3033 %int_0 = OpConstant %int 0
3034 %int_10 = OpConstant %int 10
3035 %bool = OpTypeBool
3036 %int_2 = OpConstant %int 2
3037 %uint = OpTypeInt 32 0
3038 %uint_10 = OpConstant %uint 10
3039 %_arr_float_uint_10 = OpTypeArray %float %uint_10
3040 %U_t = OpTypeStruct %_arr_float_uint_10
3041 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
3042 %_ = OpVariable %_ptr_Uniform_U_t Uniform
3043 %_ptr_Uniform_float = OpTypePointer Uniform %float
3044 %int_1 = OpConstant %int 1
3045 %_ptr_Output_float = OpTypePointer Output %float
3046 %o = OpVariable %_ptr_Output_float Output
3047 %main = OpFunction %void None %10
3048 %26 = OpLabel
3049 %s = OpVariable %_ptr_Function_float Function
3050 %i = OpVariable %_ptr_Function_int Function
3051 OpStore %s %float_0
3052 OpStore %i %int_0
3053 OpBranch %27
3054 %27 = OpLabel
3055 OpLoopMerge %28 %29 None
3056 OpBranch %30
3057 %30 = OpLabel
3058 %31 = OpLoad %int %i
3059 %32 = OpSLessThan %bool %31 %int_10
3060 OpBranchConditional %32 %33 %28
3061 %33 = OpLabel
3062 %34 = OpLoad %int %i
3063 %35 = OpSMod %int %34 %int_2
3064 %36 = OpIEqual %bool %35 %int_0
3065 OpBranchConditional %36 %29 %37
3066 %37 = OpLabel
3067 %38 = OpLoad %int %i
3068 %39 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %38
3069 %40 = OpLoad %float %39
3070 %41 = OpLoad %float %s
3071 %42 = OpFAdd %float %41 %40
3072 OpStore %s %42
3073 OpBranch %29
3074 %29 = OpLabel
3075 %43 = OpLoad %int %i
3076 %44 = OpIAdd %int %43 %int_1
3077 OpStore %i %44
3078 OpBranch %27
3079 %28 = OpLabel
3080 %45 = OpLoad %float %s
3081 OpStore %o %45
3082 OpReturn
3083 OpFunctionEnd
3084 )";
3085
3086 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
3087 }
3088
3089 TEST_F(AggressiveDCETest, NoEliminateIfContinue3) {
3090 // Do not eliminate continue as conditional branch with merge instruction
3091 // Note: SPIR-V edited to add merge instruction before continue.
3092 //
3093 // #version 430
3094 //
3095 // layout(std430) buffer U_t
3096 // {
3097 // float g_F[10];
3098 // };
3099 //
3100 // layout(location = 0)out float o;
3101 //
3102 // void main(void)
3103 // {
3104 // float s = 0.0;
3105 // for (int i=0; i<10; i++) {
3106 // if (i % 2 == 0) continue;
3107 // s += g_F[i];
3108 // }
3109 // o = s;
3110 // }
3111
3112 const std::string assembly =
3113 R"(OpCapability Shader
3114 %1 = OpExtInstImport "GLSL.std.450"
3115 OpMemoryModel Logical GLSL450
3116 OpEntryPoint Fragment %main "main" %o
3117 OpExecutionMode %main OriginUpperLeft
3118 OpSource GLSL 430
3119 OpName %main "main"
3120 OpName %s "s"
3121 OpName %i "i"
3122 OpName %U_t "U_t"
3123 OpMemberName %U_t 0 "g_F"
3124 OpName %_ ""
3125 OpName %o "o"
3126 OpDecorate %_arr_float_uint_10 ArrayStride 4
3127 OpMemberDecorate %U_t 0 Offset 0
3128 OpDecorate %U_t BufferBlock
3129 OpDecorate %_ DescriptorSet 0
3130 OpDecorate %o Location 0
3131 %void = OpTypeVoid
3132 %10 = OpTypeFunction %void
3133 %float = OpTypeFloat 32
3134 %_ptr_Function_float = OpTypePointer Function %float
3135 %float_0 = OpConstant %float 0
3136 %int = OpTypeInt 32 1
3137 %_ptr_Function_int = OpTypePointer Function %int
3138 %int_0 = OpConstant %int 0
3139 %int_10 = OpConstant %int 10
3140 %bool = OpTypeBool
3141 %int_2 = OpConstant %int 2
3142 %uint = OpTypeInt 32 0
3143 %uint_10 = OpConstant %uint 10
3144 %_arr_float_uint_10 = OpTypeArray %float %uint_10
3145 %U_t = OpTypeStruct %_arr_float_uint_10
3146 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
3147 %_ = OpVariable %_ptr_Uniform_U_t Uniform
3148 %_ptr_Uniform_float = OpTypePointer Uniform %float
3149 %int_1 = OpConstant %int 1
3150 %_ptr_Output_float = OpTypePointer Output %float
3151 %o = OpVariable %_ptr_Output_float Output
3152 %main = OpFunction %void None %10
3153 %26 = OpLabel
3154 %s = OpVariable %_ptr_Function_float Function
3155 %i = OpVariable %_ptr_Function_int Function
3156 OpStore %s %float_0
3157 OpStore %i %int_0
3158 OpBranch %27
3159 %27 = OpLabel
3160 OpLoopMerge %28 %29 None
3161 OpBranch %30
3162 %30 = OpLabel
3163 %31 = OpLoad %int %i
3164 %32 = OpSLessThan %bool %31 %int_10
3165 OpBranchConditional %32 %33 %28
3166 %33 = OpLabel
3167 %34 = OpLoad %int %i
3168 %35 = OpSMod %int %34 %int_2
3169 %36 = OpIEqual %bool %35 %int_0
3170 OpSelectionMerge %37 None
3171 OpBranchConditional %36 %29 %37
3172 %37 = OpLabel
3173 %38 = OpLoad %int %i
3174 %39 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %38
3175 %40 = OpLoad %float %39
3176 %41 = OpLoad %float %s
3177 %42 = OpFAdd %float %41 %40
3178 OpStore %s %42
3179 OpBranch %29
3180 %29 = OpLabel
3181 %43 = OpLoad %int %i
3182 %44 = OpIAdd %int %43 %int_1
3183 OpStore %i %44
3184 OpBranch %27
3185 %28 = OpLabel
3186 %45 = OpLoad %float %s
3187 OpStore %o %45
3188 OpReturn
3189 OpFunctionEnd
3190 )";
3191
3192 SinglePassRunAndCheck<AggressiveDCEPass>(assembly, assembly, true, true);
3193 }
3194
3195 // This is not valid input and ADCE does not support variable pointers and only
3196 // supports shaders.
3197 TEST_F(AggressiveDCETest, PointerVariable) {
3198 // ADCE is able to handle code that contains a load whose base address
3199 // comes from a load and not an OpVariable. I want to see an instruction
3200 // removed to be sure that ADCE is not exiting early.
3201
3202 const std::string before =
3203 R"(OpCapability Shader
3204 OpMemoryModel Logical GLSL450
3205 OpEntryPoint Fragment %1 "main" %2
3206 OpExecutionMode %1 OriginUpperLeft
3207 OpMemberDecorate %_struct_3 0 Offset 0
3208 OpDecorate %_runtimearr__struct_3 ArrayStride 16
3209 OpMemberDecorate %_struct_5 0 Offset 0
3210 OpDecorate %_struct_5 BufferBlock
3211 OpMemberDecorate %_struct_6 0 Offset 0
3212 OpDecorate %_struct_6 BufferBlock
3213 OpDecorate %2 Location 0
3214 OpDecorate %7 DescriptorSet 0
3215 OpDecorate %7 Binding 0
3216 OpDecorate %8 DescriptorSet 0
3217 OpDecorate %8 Binding 1
3218 %void = OpTypeVoid
3219 %10 = OpTypeFunction %void
3220 %int = OpTypeInt 32 1
3221 %uint = OpTypeInt 32 0
3222 %float = OpTypeFloat 32
3223 %v4float = OpTypeVector %float 4
3224 %_ptr_Output_v4float = OpTypePointer Output %v4float
3225 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
3226 %_struct_3 = OpTypeStruct %v4float
3227 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
3228 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
3229 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
3230 %_struct_6 = OpTypeStruct %int
3231 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
3232 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
3233 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
3234 %int_0 = OpConstant %int 0
3235 %uint_0 = OpConstant %uint 0
3236 %2 = OpVariable %_ptr_Output_v4float Output
3237 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
3238 %8 = OpVariable %_ptr_Uniform__struct_6 Uniform
3239 %1 = OpFunction %void None %10
3240 %23 = OpLabel
3241 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
3242 OpStore %24 %7
3243 %26 = OpLoad %_ptr_Uniform__struct_5 %24
3244 %27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0
3245 %28 = OpLoad %v4float %27
3246 %29 = OpCopyObject %v4float %28
3247 OpStore %2 %28
3248 OpReturn
3249 OpFunctionEnd
3250 )";
3251
3252 const std::string after =
3253 R"(OpCapability Shader
3254 OpMemoryModel Logical GLSL450
3255 OpEntryPoint Fragment %1 "main" %2
3256 OpExecutionMode %1 OriginUpperLeft
3257 OpMemberDecorate %_struct_3 0 Offset 0
3258 OpDecorate %_runtimearr__struct_3 ArrayStride 16
3259 OpMemberDecorate %_struct_5 0 Offset 0
3260 OpDecorate %_struct_5 BufferBlock
3261 OpDecorate %2 Location 0
3262 OpDecorate %7 DescriptorSet 0
3263 OpDecorate %7 Binding 0
3264 %void = OpTypeVoid
3265 %10 = OpTypeFunction %void
3266 %int = OpTypeInt 32 1
3267 %uint = OpTypeInt 32 0
3268 %float = OpTypeFloat 32
3269 %v4float = OpTypeVector %float 4
3270 %_ptr_Output_v4float = OpTypePointer Output %v4float
3271 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
3272 %_struct_3 = OpTypeStruct %v4float
3273 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
3274 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
3275 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
3276 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
3277 %int_0 = OpConstant %int 0
3278 %uint_0 = OpConstant %uint 0
3279 %2 = OpVariable %_ptr_Output_v4float Output
3280 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
3281 %1 = OpFunction %void None %10
3282 %23 = OpLabel
3283 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
3284 OpStore %24 %7
3285 %25 = OpLoad %_ptr_Uniform__struct_5 %24
3286 %26 = OpAccessChain %_ptr_Uniform_v4float %25 %int_0 %uint_0 %int_0
3287 %27 = OpLoad %v4float %26
3288 OpStore %2 %27
3289 OpReturn
3290 OpFunctionEnd
3291 )";
3292
3293 // The input is not valid and ADCE only supports shaders, but not variable
3294 // pointers. Workaround this by enabling relaxed logical pointers in the
3295 // validator.
3296 ValidatorOptions()->relax_logical_pointer = true;
3297 SinglePassRunAndCheck<AggressiveDCEPass>(before, after, true, true);
3298 }
3299
3300 // %dead is unused. Make sure we remove it along with its name.
3301 TEST_F(AggressiveDCETest, RemoveUnreferenced) {
3302 const std::string before =
3303 R"(OpCapability Shader
3304 OpCapability Linkage
3305 %1 = OpExtInstImport "GLSL.std.450"
3306 OpMemoryModel Logical GLSL450
3307 OpEntryPoint Fragment %main "main"
3308 OpExecutionMode %main OriginUpperLeft
3309 OpSource GLSL 150
3310 OpName %main "main"
3311 OpName %dead "dead"
3312 %void = OpTypeVoid
3313 %5 = OpTypeFunction %void
3314 %float = OpTypeFloat 32
3315 %_ptr_Private_float = OpTypePointer Private %float
3316 %dead = OpVariable %_ptr_Private_float Private
3317 %main = OpFunction %void None %5
3318 %8 = OpLabel
3319 OpReturn
3320 OpFunctionEnd
3321 )";
3322
3323 const std::string after =
3324 R"(OpCapability Shader
3325 OpCapability Linkage
3326 %1 = OpExtInstImport "GLSL.std.450"
3327 OpMemoryModel Logical GLSL450
3328 OpEntryPoint Fragment %main "main"
3329 OpExecutionMode %main OriginUpperLeft
3330 OpSource GLSL 150
3331 OpName %main "main"
3332 %void = OpTypeVoid
3333 %5 = OpTypeFunction %void
3334 %main = OpFunction %void None %5
3335 %8 = OpLabel
3336 OpReturn
3337 OpFunctionEnd
3338 )";
3339
3340 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3341 SinglePassRunAndCheck<AggressiveDCEPass>(before, after, true, true);
3342 }
3343
3344 // Delete %dead because it is unreferenced. Then %initializer becomes
3345 // unreferenced, so remove it as well.
3346 TEST_F(AggressiveDCETest, RemoveUnreferencedWithInit1) {
3347 const std::string before =
3348 R"(OpCapability Shader
3349 OpCapability Linkage
3350 %1 = OpExtInstImport "GLSL.std.450"
3351 OpMemoryModel Logical GLSL450
3352 OpEntryPoint Fragment %main "main"
3353 OpExecutionMode %main OriginUpperLeft
3354 OpSource GLSL 150
3355 OpName %main "main"
3356 OpName %dead "dead"
3357 OpName %initializer "initializer"
3358 %void = OpTypeVoid
3359 %6 = OpTypeFunction %void
3360 %float = OpTypeFloat 32
3361 %_ptr_Private_float = OpTypePointer Private %float
3362 %initializer = OpVariable %_ptr_Private_float Private
3363 %dead = OpVariable %_ptr_Private_float Private %initializer
3364 %main = OpFunction %void None %6
3365 %9 = OpLabel
3366 OpReturn
3367 OpFunctionEnd
3368 )";
3369
3370 const std::string after =
3371 R"(OpCapability Shader
3372 OpCapability Linkage
3373 %1 = OpExtInstImport "GLSL.std.450"
3374 OpMemoryModel Logical GLSL450
3375 OpEntryPoint Fragment %main "main"
3376 OpExecutionMode %main OriginUpperLeft
3377 OpSource GLSL 150
3378 OpName %main "main"
3379 %void = OpTypeVoid
3380 %6 = OpTypeFunction %void
3381 %main = OpFunction %void None %6
3382 %9 = OpLabel
3383 OpReturn
3384 OpFunctionEnd
3385 )";
3386
3387 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3388 SinglePassRunAndCheck<AggressiveDCEPass>(before, after, true, true);
3389 }
3390
3391 // Keep %live because it is used, and its initializer.
3392 TEST_F(AggressiveDCETest, KeepReferenced) {
3393 const std::string before =
3394 R"(OpCapability Shader
3395 OpCapability Linkage
3396 %1 = OpExtInstImport "GLSL.std.450"
3397 OpMemoryModel Logical GLSL450
3398 OpEntryPoint Fragment %main "main" %output
3399 OpExecutionMode %main OriginUpperLeft
3400 OpSource GLSL 150
3401 OpName %main "main"
3402 OpName %live "live"
3403 OpName %initializer "initializer"
3404 OpName %output "output"
3405 %void = OpTypeVoid
3406 %6 = OpTypeFunction %void
3407 %float = OpTypeFloat 32
3408 %_ptr_Private_float = OpTypePointer Private %float
3409 %initializer = OpConstant %float 0
3410 %live = OpVariable %_ptr_Private_float Private %initializer
3411 %_ptr_Output_float = OpTypePointer Output %float
3412 %output = OpVariable %_ptr_Output_float Output
3413 %main = OpFunction %void None %6
3414 %9 = OpLabel
3415 %10 = OpLoad %float %live
3416 OpStore %output %10
3417 OpReturn
3418 OpFunctionEnd
3419 )";
3420
3421 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3422 SinglePassRunAndCheck<AggressiveDCEPass>(before, before, true, true);
3423 }
3424
3425 // This test that the decoration associated with a variable are removed when the
3426 // variable is removed.
3427 TEST_F(AggressiveDCETest, RemoveVariableAndDecorations) {
3428 const std::string before =
3429 R"(OpCapability Shader
3430 %1 = OpExtInstImport "GLSL.std.450"
3431 OpMemoryModel Logical GLSL450
3432 OpEntryPoint Vertex %main "main"
3433 OpSource GLSL 450
3434 OpName %main "main"
3435 OpName %B "B"
3436 OpMemberName %B 0 "a"
3437 OpName %Bdat "Bdat"
3438 OpMemberDecorate %B 0 Offset 0
3439 OpDecorate %B BufferBlock
3440 OpDecorate %Bdat DescriptorSet 0
3441 OpDecorate %Bdat Binding 0
3442 %void = OpTypeVoid
3443 %6 = OpTypeFunction %void
3444 %uint = OpTypeInt 32 0
3445 %B = OpTypeStruct %uint
3446 %_ptr_Uniform_B = OpTypePointer Uniform %B
3447 %Bdat = OpVariable %_ptr_Uniform_B Uniform
3448 %int = OpTypeInt 32 1
3449 %int_0 = OpConstant %int 0
3450 %uint_1 = OpConstant %uint 1
3451 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
3452 %main = OpFunction %void None %6
3453 %13 = OpLabel
3454 OpReturn
3455 OpFunctionEnd
3456 )";
3457
3458 const std::string after =
3459 R"(OpCapability Shader
3460 %1 = OpExtInstImport "GLSL.std.450"
3461 OpMemoryModel Logical GLSL450
3462 OpEntryPoint Vertex %main "main"
3463 OpSource GLSL 450
3464 OpName %main "main"
3465 %void = OpTypeVoid
3466 %6 = OpTypeFunction %void
3467 %main = OpFunction %void None %6
3468 %13 = OpLabel
3469 OpReturn
3470 OpFunctionEnd
3471 )";
3472
3473 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3474 SinglePassRunAndCheck<AggressiveDCEPass>(before, after, true, true);
3475 }
3476
3477 TEST_F(AggressiveDCETest, DeadNestedSwitch) {
3478 const std::string text = R"(
3479 ; CHECK: OpLabel
3480 ; CHECK: OpBranch [[block:%\w+]]
3481 ; CHECK-NOT: OpSwitch
3482 ; CHECK-NEXT: [[block]] = OpLabel
3483 ; CHECK: OpBranch [[block:%\w+]]
3484 ; CHECK-NOT: OpSwitch
3485 ; CHECK-NEXT: [[block]] = OpLabel
3486 ; CHECK-NEXT: OpStore
3487 OpCapability Shader
3488 OpMemoryModel Logical GLSL450
3489 OpEntryPoint Fragment %func "func" %x
3490 OpExecutionMode %func OriginUpperLeft
3491 OpName %func "func"
3492 %void = OpTypeVoid
3493 %1 = OpTypeFunction %void
3494 %uint = OpTypeInt 32 0
3495 %uint_0 = OpConstant %uint 0
3496 %uint_ptr_Output = OpTypePointer Output %uint
3497 %uint_ptr_Input = OpTypePointer Input %uint
3498 %x = OpVariable %uint_ptr_Output Output
3499 %a = OpVariable %uint_ptr_Input Input
3500 %func = OpFunction %void None %1
3501 %entry = OpLabel
3502 OpBranch %header
3503 %header = OpLabel
3504 %ld = OpLoad %uint %a
3505 OpLoopMerge %merge %continue None
3506 OpBranch %postheader
3507 %postheader = OpLabel
3508 ; This switch doesn't require an OpSelectionMerge and is nested in the dead loop.
3509 OpSwitch %ld %merge 0 %extra 1 %continue
3510 %extra = OpLabel
3511 OpBranch %continue
3512 %continue = OpLabel
3513 OpBranch %header
3514 %merge = OpLabel
3515 OpStore %x %uint_0
3516 OpReturn
3517 OpFunctionEnd
3518 )";
3519
3520 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
3521 }
3522
3523 TEST_F(AggressiveDCETest, LiveNestedSwitch) {
3524 const std::string text = R"(OpCapability Shader
3525 OpMemoryModel Logical GLSL450
3526 OpEntryPoint Fragment %func "func" %3 %10
3527 OpExecutionMode %func OriginUpperLeft
3528 OpName %func "func"
3529 %void = OpTypeVoid
3530 %1 = OpTypeFunction %void
3531 %uint = OpTypeInt 32 0
3532 %uint_0 = OpConstant %uint 0
3533 %uint_1 = OpConstant %uint 1
3534 %_ptr_Output_uint = OpTypePointer Output %uint
3535 %_ptr_Input_uint = OpTypePointer Input %uint
3536 %3 = OpVariable %_ptr_Output_uint Output
3537 %10 = OpVariable %_ptr_Input_uint Input
3538 %func = OpFunction %void None %1
3539 %11 = OpLabel
3540 OpBranch %12
3541 %12 = OpLabel
3542 %13 = OpLoad %uint %10
3543 OpLoopMerge %14 %15 None
3544 OpBranch %16
3545 %16 = OpLabel
3546 OpSelectionMerge %18 None
3547 OpSwitch %13 %18 0 %17 1 %19
3548 %17 = OpLabel
3549 OpStore %3 %uint_1
3550 OpBranch %19
3551 %19 = OpLabel
3552 OpBranch %15
3553 %15 = OpLabel
3554 OpBranch %12
3555 %18 = OpLabel
3556 OpBranch %14
3557 %14 = OpLabel
3558 OpStore %3 %uint_0
3559 OpReturn
3560 OpFunctionEnd
3561 )";
3562
3563 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3564 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, false, true);
3565 }
3566
3567 TEST_F(AggressiveDCETest, BasicDeleteDeadFunction) {
3568 // The function Dead should be removed because it is never called.
3569 const std::vector<const char*> common_code = {
3570 // clang-format off
3571 "OpCapability Shader",
3572 "OpMemoryModel Logical GLSL450",
3573 "OpEntryPoint Fragment %main \"main\"",
3574 "OpName %main \"main\"",
3575 "OpName %Live \"Live\"",
3576 "%void = OpTypeVoid",
3577 "%7 = OpTypeFunction %void",
3578 "%main = OpFunction %void None %7",
3579 "%15 = OpLabel",
3580 "%16 = OpFunctionCall %void %Live",
3581 "%17 = OpFunctionCall %void %Live",
3582 "OpReturn",
3583 "OpFunctionEnd",
3584 "%Live = OpFunction %void None %7",
3585 "%20 = OpLabel",
3586 "OpReturn",
3587 "OpFunctionEnd"
3588 // clang-format on
3589 };
3590
3591 const std::vector<const char*> dead_function = {
3592 // clang-format off
3593 "%Dead = OpFunction %void None %7",
3594 "%19 = OpLabel",
3595 "OpReturn",
3596 "OpFunctionEnd",
3597 // clang-format on
3598 };
3599
3600 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3601 SinglePassRunAndCheck<AggressiveDCEPass>(
3602 JoinAllInsts(Concat(common_code, dead_function)),
3603 JoinAllInsts(common_code), /* skip_nop = */ true);
3604 }
3605
3606 TEST_F(AggressiveDCETest, BasicKeepLiveFunction) {
3607 // Everything is reachable from an entry point, so no functions should be
3608 // deleted.
3609 const std::vector<const char*> text = {
3610 // clang-format off
3611 "OpCapability Shader",
3612 "OpMemoryModel Logical GLSL450",
3613 "OpEntryPoint Fragment %main \"main\"",
3614 "OpName %main \"main\"",
3615 "OpName %Live1 \"Live1\"",
3616 "OpName %Live2 \"Live2\"",
3617 "%void = OpTypeVoid",
3618 "%7 = OpTypeFunction %void",
3619 "%main = OpFunction %void None %7",
3620 "%15 = OpLabel",
3621 "%16 = OpFunctionCall %void %Live2",
3622 "%17 = OpFunctionCall %void %Live1",
3623 "OpReturn",
3624 "OpFunctionEnd",
3625 "%Live1 = OpFunction %void None %7",
3626 "%19 = OpLabel",
3627 "OpReturn",
3628 "OpFunctionEnd",
3629 "%Live2 = OpFunction %void None %7",
3630 "%20 = OpLabel",
3631 "OpReturn",
3632 "OpFunctionEnd"
3633 // clang-format on
3634 };
3635
3636 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3637 std::string assembly = JoinAllInsts(text);
3638 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
3639 assembly, /* skip_nop = */ true, /* do_validation = */ false);
3640 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
3641 EXPECT_EQ(assembly, std::get<0>(result));
3642 }
3643
3644 TEST_F(AggressiveDCETest, BasicRemoveDecorationsAndNames) {
3645 // We want to remove the names and decorations associated with results that
3646 // are removed. This test will check for that.
3647 const std::string text = R"(
3648 OpCapability Shader
3649 OpMemoryModel Logical GLSL450
3650 OpEntryPoint Vertex %main "main"
3651 OpName %main "main"
3652 OpName %Dead "Dead"
3653 OpName %x "x"
3654 OpName %y "y"
3655 OpName %z "z"
3656 OpDecorate %x RelaxedPrecision
3657 OpDecorate %y RelaxedPrecision
3658 OpDecorate %z RelaxedPrecision
3659 OpDecorate %6 RelaxedPrecision
3660 OpDecorate %7 RelaxedPrecision
3661 OpDecorate %8 RelaxedPrecision
3662 %void = OpTypeVoid
3663 %10 = OpTypeFunction %void
3664 %float = OpTypeFloat 32
3665 %_ptr_Function_float = OpTypePointer Function %float
3666 %float_1 = OpConstant %float 1
3667 %main = OpFunction %void None %10
3668 %14 = OpLabel
3669 OpReturn
3670 OpFunctionEnd
3671 %Dead = OpFunction %void None %10
3672 %15 = OpLabel
3673 %x = OpVariable %_ptr_Function_float Function
3674 %y = OpVariable %_ptr_Function_float Function
3675 %z = OpVariable %_ptr_Function_float Function
3676 OpStore %x %float_1
3677 OpStore %y %float_1
3678 %6 = OpLoad %float %x
3679 %7 = OpLoad %float %y
3680 %8 = OpFAdd %float %6 %7
3681 OpStore %z %8
3682 OpReturn
3683 OpFunctionEnd)";
3684
3685 const std::string expected_output = R"(OpCapability Shader
3686 OpMemoryModel Logical GLSL450
3687 OpEntryPoint Vertex %main "main"
3688 OpName %main "main"
3689 %void = OpTypeVoid
3690 %10 = OpTypeFunction %void
3691 %main = OpFunction %void None %10
3692 %14 = OpLabel
3693 OpReturn
3694 OpFunctionEnd
3695 )";
3696
3697 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3698 SinglePassRunAndCheck<AggressiveDCEPass>(text, expected_output,
3699 /* skip_nop = */ true);
3700 }
3701
3702 TEST_F(AggressiveDCETest, BasicAllDeadConstants) {
3703 const std::string text = R"(
3704 ; CHECK-NOT: OpConstant
3705 OpCapability Shader
3706 OpCapability Float64
3707 %1 = OpExtInstImport "GLSL.std.450"
3708 OpMemoryModel Logical GLSL450
3709 OpEntryPoint Vertex %main "main"
3710 OpName %main "main"
3711 %void = OpTypeVoid
3712 %4 = OpTypeFunction %void
3713 %bool = OpTypeBool
3714 %true = OpConstantTrue %bool
3715 %false = OpConstantFalse %bool
3716 %int = OpTypeInt 32 1
3717 %9 = OpConstant %int 1
3718 %uint = OpTypeInt 32 0
3719 %11 = OpConstant %uint 2
3720 %float = OpTypeFloat 32
3721 %13 = OpConstant %float 3.1415
3722 %double = OpTypeFloat 64
3723 %15 = OpConstant %double 3.14159265358979
3724 %main = OpFunction %void None %4
3725 %16 = OpLabel
3726 OpReturn
3727 OpFunctionEnd
3728 )";
3729
3730 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
3731 }
3732
3733 TEST_F(AggressiveDCETest, BasicNoneDeadConstants) {
3734 const std::vector<const char*> text = {
3735 // clang-format off
3736 "OpCapability Shader",
3737 "OpCapability Float64",
3738 "%1 = OpExtInstImport \"GLSL.std.450\"",
3739 "OpMemoryModel Logical GLSL450",
3740 "OpEntryPoint Vertex %main \"main\" %btv %bfv %iv %uv %fv %dv",
3741 "OpName %main \"main\"",
3742 "OpName %btv \"btv\"",
3743 "OpName %bfv \"bfv\"",
3744 "OpName %iv \"iv\"",
3745 "OpName %uv \"uv\"",
3746 "OpName %fv \"fv\"",
3747 "OpName %dv \"dv\"",
3748 "%void = OpTypeVoid",
3749 "%10 = OpTypeFunction %void",
3750 "%bool = OpTypeBool",
3751 "%_ptr_Output_bool = OpTypePointer Output %bool",
3752 "%true = OpConstantTrue %bool",
3753 "%false = OpConstantFalse %bool",
3754 "%int = OpTypeInt 32 1",
3755 "%_ptr_Output_int = OpTypePointer Output %int",
3756 "%int_1 = OpConstant %int 1",
3757 "%uint = OpTypeInt 32 0",
3758 "%_ptr_Output_uint = OpTypePointer Output %uint",
3759 "%uint_2 = OpConstant %uint 2",
3760 "%float = OpTypeFloat 32",
3761 "%_ptr_Output_float = OpTypePointer Output %float",
3762 "%float_3_1415 = OpConstant %float 3.1415",
3763 "%double = OpTypeFloat 64",
3764 "%_ptr_Output_double = OpTypePointer Output %double",
3765 "%double_3_14159265358979 = OpConstant %double 3.14159265358979",
3766 "%btv = OpVariable %_ptr_Output_bool Output",
3767 "%bfv = OpVariable %_ptr_Output_bool Output",
3768 "%iv = OpVariable %_ptr_Output_int Output",
3769 "%uv = OpVariable %_ptr_Output_uint Output",
3770 "%fv = OpVariable %_ptr_Output_float Output",
3771 "%dv = OpVariable %_ptr_Output_double Output",
3772 "%main = OpFunction %void None %10",
3773 "%27 = OpLabel",
3774 "OpStore %btv %true",
3775 "OpStore %bfv %false",
3776 "OpStore %iv %int_1",
3777 "OpStore %uv %uint_2",
3778 "OpStore %fv %float_3_1415",
3779 "OpStore %dv %double_3_14159265358979",
3780 "OpReturn",
3781 "OpFunctionEnd",
3782 // clang-format on
3783 };
3784 // All constants are used, so none of them should be eliminated.
3785 SinglePassRunAndCheck<AggressiveDCEPass>(
3786 JoinAllInsts(text), JoinAllInsts(text), /* skip_nop = */ true);
3787 }
3788
3789 struct AggressiveEliminateDeadConstantTestCase {
3790 // Type declarations and constants that should be kept.
3791 std::vector<std::string> used_consts;
3792 // Instructions that refer to constants, this is added to create uses for
3793 // some constants so they won't be treated as dead constants.
3794 std::vector<std::string> main_insts;
3795 // Dead constants that should be removed.
3796 std::vector<std::string> dead_consts;
3797 // Expectations
3798 std::vector<std::string> checks;
3799 };
3800
3801 // All types that are potentially required in
3802 // AggressiveEliminateDeadConstantTest.
3803 const std::vector<std::string> CommonTypes = {
3804 // clang-format off
3805 // scalar types
3806 "%bool = OpTypeBool",
3807 "%uint = OpTypeInt 32 0",
3808 "%int = OpTypeInt 32 1",
3809 "%float = OpTypeFloat 32",
3810 "%double = OpTypeFloat 64",
3811 // vector types
3812 "%v2bool = OpTypeVector %bool 2",
3813 "%v2uint = OpTypeVector %uint 2",
3814 "%v2int = OpTypeVector %int 2",
3815 "%v3int = OpTypeVector %int 3",
3816 "%v4int = OpTypeVector %int 4",
3817 "%v2float = OpTypeVector %float 2",
3818 "%v3float = OpTypeVector %float 3",
3819 "%v2double = OpTypeVector %double 2",
3820 // variable pointer types
3821 "%_pf_bool = OpTypePointer Output %bool",
3822 "%_pf_uint = OpTypePointer Output %uint",
3823 "%_pf_int = OpTypePointer Output %int",
3824 "%_pf_float = OpTypePointer Output %float",
3825 "%_pf_double = OpTypePointer Output %double",
3826 "%_pf_v2int = OpTypePointer Output %v2int",
3827 "%_pf_v3int = OpTypePointer Output %v3int",
3828 "%_pf_v2float = OpTypePointer Output %v2float",
3829 "%_pf_v3float = OpTypePointer Output %v3float",
3830 "%_pf_v2double = OpTypePointer Output %v2double",
3831 // struct types
3832 "%inner_struct = OpTypeStruct %bool %int %float %double",
3833 "%outer_struct = OpTypeStruct %inner_struct %int %double",
3834 "%flat_struct = OpTypeStruct %bool %int %float %double",
3835 // clang-format on
3836 };
3837
3838 using AggressiveEliminateDeadConstantTest =
3839 PassTest<::testing::TestWithParam<AggressiveEliminateDeadConstantTestCase>>;
3840
3841 TEST_P(AggressiveEliminateDeadConstantTest, Custom) {
3842 auto& tc = GetParam();
3843 AssemblyBuilder builder;
3844 builder.AppendTypesConstantsGlobals(CommonTypes)
3845 .AppendTypesConstantsGlobals(tc.used_consts)
3846 .AppendInMain(tc.main_insts);
3847 const std::string expected = builder.GetCode();
3848 builder.AppendTypesConstantsGlobals(tc.dead_consts);
3849 builder.PrependPreamble(tc.checks);
3850 const std::string assembly_with_dead_const = builder.GetCode();
3851
3852 // Do not enable validation. As the input code is invalid from the base
3853 // tests (ported from other passes).
3854 SinglePassRunAndMatch<AggressiveDCEPass>(assembly_with_dead_const, false);
3855 }
3856
3857 INSTANTIATE_TEST_SUITE_P(
3858 ScalarTypeConstants, AggressiveEliminateDeadConstantTest,
3859 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
3860 // clang-format off
3861 // Scalar type constants, one dead constant and one used constant.
3862 {
3863 /* .used_consts = */
3864 {
3865 "%used_const_int = OpConstant %int 1",
3866 },
3867 /* .main_insts = */
3868 {
3869 "%int_var = OpVariable %_pf_int Output",
3870 "OpStore %int_var %used_const_int",
3871 },
3872 /* .dead_consts = */
3873 {
3874 "%dead_const_int = OpConstant %int 1",
3875 },
3876 /* .checks = */
3877 {
3878 "; CHECK: [[const:%\\w+]] = OpConstant %int 1",
3879 "; CHECK-NOT: OpConstant",
3880 "; CHECK: OpStore {{%\\w+}} [[const]]",
3881 },
3882 },
3883 {
3884 /* .used_consts = */
3885 {
3886 "%used_const_uint = OpConstant %uint 1",
3887 },
3888 /* .main_insts = */
3889 {
3890 "%uint_var = OpVariable %_pf_uint Output",
3891 "OpStore %uint_var %used_const_uint",
3892 },
3893 /* .dead_consts = */
3894 {
3895 "%dead_const_uint = OpConstant %uint 1",
3896 },
3897 /* .checks = */
3898 {
3899 "; CHECK: [[const:%\\w+]] = OpConstant %uint 1",
3900 "; CHECK-NOT: OpConstant",
3901 "; CHECK: OpStore {{%\\w+}} [[const]]",
3902 },
3903 },
3904 {
3905 /* .used_consts = */
3906 {
3907 "%used_const_float = OpConstant %float 3.1415",
3908 },
3909 /* .main_insts = */
3910 {
3911 "%float_var = OpVariable %_pf_float Output",
3912 "OpStore %float_var %used_const_float",
3913 },
3914 /* .dead_consts = */
3915 {
3916 "%dead_const_float = OpConstant %float 3.1415",
3917 },
3918 /* .checks = */
3919 {
3920 "; CHECK: [[const:%\\w+]] = OpConstant %float 3.1415",
3921 "; CHECK-NOT: OpConstant",
3922 "; CHECK: OpStore {{%\\w+}} [[const]]",
3923 },
3924 },
3925 {
3926 /* .used_consts = */
3927 {
3928 "%used_const_double = OpConstant %double 3.14",
3929 },
3930 /* .main_insts = */
3931 {
3932 "%double_var = OpVariable %_pf_double Output",
3933 "OpStore %double_var %used_const_double",
3934 },
3935 /* .dead_consts = */
3936 {
3937 "%dead_const_double = OpConstant %double 3.14",
3938 },
3939 /* .checks = */
3940 {
3941 "; CHECK: [[const:%\\w+]] = OpConstant %double 3.14",
3942 "; CHECK-NOT: OpConstant",
3943 "; CHECK: OpStore {{%\\w+}} [[const]]",
3944 },
3945 },
3946 // clang-format on
3947 })));
3948
3949 INSTANTIATE_TEST_SUITE_P(
3950 VectorTypeConstants, AggressiveEliminateDeadConstantTest,
3951 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
3952 // clang-format off
3953 // Tests eliminating dead constant type ivec2. One dead constant vector
3954 // and one used constant vector, each built from its own group of
3955 // scalar constants.
3956 {
3957 /* .used_consts = */
3958 {
3959 "%used_int_x = OpConstant %int 1",
3960 "%used_int_y = OpConstant %int 2",
3961 "%used_v2int = OpConstantComposite %v2int %used_int_x %used_int_y",
3962 },
3963 /* .main_insts = */
3964 {
3965 "%v2int_var = OpVariable %_pf_v2int Output",
3966 "OpStore %v2int_var %used_v2int",
3967 },
3968 /* .dead_consts = */
3969 {
3970 "%dead_int_x = OpConstant %int 1",
3971 "%dead_int_y = OpConstant %int 2",
3972 "%dead_v2int = OpConstantComposite %v2int %dead_int_x %dead_int_y",
3973 },
3974 /* .checks = */
3975 {
3976 "; CHECK: [[constx:%\\w+]] = OpConstant %int 1",
3977 "; CHECK: [[consty:%\\w+]] = OpConstant %int 2",
3978 "; CHECK: [[const:%\\w+]] = OpConstantComposite %v2int [[constx]] [[consty]]",
3979 "; CHECK-NOT: OpConstant",
3980 "; CHECK: OpStore {{%\\w+}} [[const]]",
3981 },
3982 },
3983 // Tests eliminating dead constant ivec3. One dead constant vector and
3984 // one used constant vector. But both built from a same group of
3985 // scalar constants.
3986 {
3987 /* .used_consts = */
3988 {
3989 "%used_int_x = OpConstant %int 1",
3990 "%used_int_y = OpConstant %int 2",
3991 "%used_int_z = OpConstant %int 3",
3992 "%used_v3int = OpConstantComposite %v3int %used_int_x %used_int_y %used_int_z",
3993 },
3994 /* .main_insts = */
3995 {
3996 "%v3int_var = OpVariable %_pf_v3int Output",
3997 "OpStore %v3int_var %used_v3int",
3998 },
3999 /* .dead_consts = */
4000 {
4001 "%dead_v3int = OpConstantComposite %v3int %used_int_x %used_int_y %used_int_z",
4002 },
4003 /* .checks = */
4004 {
4005 "; CHECK: [[constx:%\\w+]] = OpConstant %int 1",
4006 "; CHECK: [[consty:%\\w+]] = OpConstant %int 2",
4007 "; CHECK: [[constz:%\\w+]] = OpConstant %int 3",
4008 "; CHECK: [[const:%\\w+]] = OpConstantComposite %v3int [[constx]] [[consty]] [[constz]]",
4009 "; CHECK-NOT: OpConstant",
4010 "; CHECK: OpStore {{%\\w+}} [[const]]",
4011 },
4012 },
4013 // Tests eliminating dead constant vec2. One dead constant vector and
4014 // one used constant vector. Each built from its own group of scalar
4015 // constants.
4016 {
4017 /* .used_consts = */
4018 {
4019 "%used_float_x = OpConstant %float 3.1415",
4020 "%used_float_y = OpConstant %float 4.13",
4021 "%used_v2float = OpConstantComposite %v2float %used_float_x %used_float_y",
4022 },
4023 /* .main_insts = */
4024 {
4025 "%v2float_var = OpVariable %_pf_v2float Output",
4026 "OpStore %v2float_var %used_v2float",
4027 },
4028 /* .dead_consts = */
4029 {
4030 "%dead_float_x = OpConstant %float 3.1415",
4031 "%dead_float_y = OpConstant %float 4.13",
4032 "%dead_v2float = OpConstantComposite %v2float %dead_float_x %dead_float_y",
4033 },
4034 /* .checks = */
4035 {
4036 "; CHECK: [[constx:%\\w+]] = OpConstant %float 3.1415",
4037 "; CHECK: [[consty:%\\w+]] = OpConstant %float 4.13",
4038 "; CHECK: [[const:%\\w+]] = OpConstantComposite %v2float [[constx]] [[consty]]",
4039 "; CHECK-NOT: OpConstant",
4040 "; CHECK: OpStore {{%\\w+}} [[const]]",
4041 },
4042 },
4043 // Tests eliminating dead constant vec3. One dead constant vector and
4044 // one used constant vector. Both built from a same group of scalar
4045 // constants.
4046 {
4047 /* .used_consts = */
4048 {
4049 "%used_float_x = OpConstant %float 3.1415",
4050 "%used_float_y = OpConstant %float 4.25",
4051 "%used_float_z = OpConstant %float 4.75",
4052 "%used_v3float = OpConstantComposite %v3float %used_float_x %used_float_y %used_float_z",
4053 },
4054 /* .main_insts = */
4055 {
4056 "%v3float_var = OpVariable %_pf_v3float Output",
4057 "OpStore %v3float_var %used_v3float",
4058 },
4059 /* .dead_consts = */
4060 {
4061 "%dead_v3float = OpConstantComposite %v3float %used_float_x %used_float_y %used_float_z",
4062 },
4063 /* .checks = */
4064 {
4065 "; CHECK: [[constx:%\\w+]] = OpConstant %float 3.1415",
4066 "; CHECK: [[consty:%\\w+]] = OpConstant %float 4.25",
4067 "; CHECK: [[constz:%\\w+]] = OpConstant %float 4.75",
4068 "; CHECK: [[const:%\\w+]] = OpConstantComposite %v3float [[constx]] [[consty]]",
4069 "; CHECK-NOT: OpConstant",
4070 "; CHECK: OpStore {{%\\w+}} [[const]]",
4071 },
4072 },
4073 // clang-format on
4074 })));
4075
4076 INSTANTIATE_TEST_SUITE_P(
4077 StructTypeConstants, AggressiveEliminateDeadConstantTest,
4078 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
4079 // clang-format off
4080 // A plain struct type dead constants. All of its components are dead
4081 // constants too.
4082 {
4083 /* .used_consts = */ {},
4084 /* .main_insts = */ {},
4085 /* .dead_consts = */
4086 {
4087 "%dead_bool = OpConstantTrue %bool",
4088 "%dead_int = OpConstant %int 1",
4089 "%dead_float = OpConstant %float 2.5",
4090 "%dead_double = OpConstant %double 3.14159265358979",
4091 "%dead_struct = OpConstantComposite %flat_struct %dead_bool %dead_int %dead_float %dead_double",
4092 },
4093 /* .checks = */
4094 {
4095 "; CHECK-NOT: OpConstant",
4096 },
4097 },
4098 // A plain struct type dead constants. Some of its components are dead
4099 // constants while others are not.
4100 {
4101 /* .used_consts = */
4102 {
4103 "%used_int = OpConstant %int 1",
4104 "%used_double = OpConstant %double 3.14159265358979",
4105 },
4106 /* .main_insts = */
4107 {
4108 "%int_var = OpVariable %_pf_int Output",
4109 "OpStore %int_var %used_int",
4110 "%double_var = OpVariable %_pf_double Output",
4111 "OpStore %double_var %used_double",
4112 },
4113 /* .dead_consts = */
4114 {
4115 "%dead_bool = OpConstantTrue %bool",
4116 "%dead_float = OpConstant %float 2.5",
4117 "%dead_struct = OpConstantComposite %flat_struct %dead_bool %used_int %dead_float %used_double",
4118 },
4119 /* .checks = */
4120 {
4121 "; CHECK: [[int:%\\w+]] = OpConstant %int 1",
4122 "; CHECK: [[double:%\\w+]] = OpConstant %double 3.14159265358979",
4123 "; CHECK-NOT: OpConstant",
4124 "; CHECK: OpStore {{%\\w+}} [[int]]",
4125 "; CHECK: OpStore {{%\\w+}} [[double]]",
4126 },
4127 },
4128 // A nesting struct type dead constants. All components of both outer
4129 // and inner structs are dead and should be removed after dead constant
4130 // elimination.
4131 {
4132 /* .used_consts = */ {},
4133 /* .main_insts = */ {},
4134 /* .dead_consts = */
4135 {
4136 "%dead_bool = OpConstantTrue %bool",
4137 "%dead_int = OpConstant %int 1",
4138 "%dead_float = OpConstant %float 2.5",
4139 "%dead_double = OpConstant %double 3.1415926535",
4140 "%dead_inner_struct = OpConstantComposite %inner_struct %dead_bool %dead_int %dead_float %dead_double",
4141 "%dead_int2 = OpConstant %int 2",
4142 "%dead_double2 = OpConstant %double 1.428571428514",
4143 "%dead_outer_struct = OpConstantComposite %outer_struct %dead_inner_struct %dead_int2 %dead_double2",
4144 },
4145 /* .checks = */
4146 {
4147 "; CHECK-NOT: OpConstant",
4148 },
4149 },
4150 // A nesting struct type dead constants. Some of its components are
4151 // dead constants while others are not.
4152 {
4153 /* .used_consts = */
4154 {
4155 "%used_int = OpConstant %int 1",
4156 "%used_double = OpConstant %double 3.14159265358979",
4157 },
4158 /* .main_insts = */
4159 {
4160 "%int_var = OpVariable %_pf_int Output",
4161 "OpStore %int_var %used_int",
4162 "%double_var = OpVariable %_pf_double Output",
4163 "OpStore %double_var %used_double",
4164 },
4165 /* .dead_consts = */
4166 {
4167 "%dead_bool = OpConstantTrue %bool",
4168 "%dead_float = OpConstant %float 2.5",
4169 "%dead_inner_struct = OpConstantComposite %inner_struct %dead_bool %used_int %dead_float %used_double",
4170 "%dead_int = OpConstant %int 2",
4171 "%dead_outer_struct = OpConstantComposite %outer_struct %dead_inner_struct %dead_int %used_double",
4172 },
4173 /* .checks = */
4174 {
4175 "; CHECK: [[int:%\\w+]] = OpConstant %int 1",
4176 "; CHECK: [[double:%\\w+]] = OpConstant %double 3.14159265358979",
4177 "; CHECK-NOT: OpConstant",
4178 "; CHECK: OpStore {{%\\w+}} [[int]]",
4179 "; CHECK: OpStore {{%\\w+}} [[double]]",
4180 },
4181 },
4182 // A nesting struct case. The inner struct is used while the outer struct is not
4183 {
4184 /* .used_const = */
4185 {
4186 "%used_bool = OpConstantTrue %bool",
4187 "%used_int = OpConstant %int 1",
4188 "%used_float = OpConstant %float 1.23",
4189 "%used_double = OpConstant %double 1.2345678901234",
4190 "%used_inner_struct = OpConstantComposite %inner_struct %used_bool %used_int %used_float %used_double",
4191 },
4192 /* .main_insts = */
4193 {
4194 "%bool_var = OpVariable %_pf_bool Output",
4195 "%bool_from_inner_struct = OpCompositeExtract %bool %used_inner_struct 0",
4196 "OpStore %bool_var %bool_from_inner_struct",
4197 },
4198 /* .dead_consts = */
4199 {
4200 "%dead_int = OpConstant %int 2",
4201 "%dead_outer_struct = OpConstantComposite %outer_struct %used_inner_struct %dead_int %used_double"
4202 },
4203 /* .checks = */
4204 {
4205 "; CHECK: [[bool:%\\w+]] = OpConstantTrue",
4206 "; CHECK: [[int:%\\w+]] = OpConstant %int 1",
4207 "; CHECK: [[float:%\\w+]] = OpConstant %float 1.23",
4208 "; CHECK: [[double:%\\w+]] = OpConstant %double 1.2345678901234",
4209 "; CHECK: [[struct:%\\w+]] = OpConstantComposite %inner_struct [[bool]] [[int]] [[float]] [[double]]",
4210 "; CHECK-NOT: OpConstant",
4211 "; CHECK: OpCompositeExtract %bool [[struct]]",
4212 }
4213 },
4214 // A nesting struct case. The outer struct is used, so the inner struct should not
4215 // be removed even though it is not used anywhere.
4216 {
4217 /* .used_const = */
4218 {
4219 "%used_bool = OpConstantTrue %bool",
4220 "%used_int = OpConstant %int 1",
4221 "%used_float = OpConstant %float 1.23",
4222 "%used_double = OpConstant %double 1.2345678901234",
4223 "%used_inner_struct = OpConstantComposite %inner_struct %used_bool %used_int %used_float %used_double",
4224 "%used_outer_struct = OpConstantComposite %outer_struct %used_inner_struct %used_int %used_double"
4225 },
4226 /* .main_insts = */
4227 {
4228 "%int_var = OpVariable %_pf_int Output",
4229 "%int_from_outer_struct = OpCompositeExtract %int %used_outer_struct 1",
4230 "OpStore %int_var %int_from_outer_struct",
4231 },
4232 /* .dead_consts = */ {},
4233 /* .checks = */
4234 {
4235 "; CHECK: [[bool:%\\w+]] = OpConstantTrue %bool",
4236 "; CHECK: [[int:%\\w+]] = OpConstant %int 1",
4237 "; CHECK: [[float:%\\w+]] = OpConstant %float 1.23",
4238 "; CHECK: [[double:%\\w+]] = OpConstant %double 1.2345678901234",
4239 "; CHECK: [[inner_struct:%\\w+]] = OpConstantComposite %inner_struct %used_bool %used_int %used_float %used_double",
4240 "; CHECK: [[outer_struct:%\\w+]] = OpConstantComposite %outer_struct %used_inner_struct %used_int %used_double",
4241 "; CHECK: OpCompositeExtract %int [[outer_struct]]",
4242 },
4243 },
4244 // clang-format on
4245 })));
4246
4247 INSTANTIATE_TEST_SUITE_P(
4248 ScalarTypeSpecConstants, AggressiveEliminateDeadConstantTest,
4249 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
4250 // clang-format off
4251 // All scalar type spec constants.
4252 {
4253 /* .used_consts = */
4254 {
4255 "%used_bool = OpSpecConstantTrue %bool",
4256 "%used_uint = OpSpecConstant %uint 2",
4257 "%used_int = OpSpecConstant %int 2",
4258 "%used_float = OpSpecConstant %float 2.5",
4259 "%used_double = OpSpecConstant %double 1.428571428514",
4260 },
4261 /* .main_insts = */
4262 {
4263 "%bool_var = OpVariable %_pf_bool Output",
4264 "%uint_var = OpVariable %_pf_uint Output",
4265 "%int_var = OpVariable %_pf_int Output",
4266 "%float_var = OpVariable %_pf_float Output",
4267 "%double_var = OpVariable %_pf_double Output",
4268 "OpStore %bool_var %used_bool",
4269 "OpStore %uint_var %used_uint",
4270 "OpStore %int_var %used_int",
4271 "OpStore %float_var %used_float",
4272 "OpStore %double_var %used_double",
4273 },
4274 /* .dead_consts = */
4275 {
4276 "%dead_bool = OpSpecConstantTrue %bool",
4277 "%dead_uint = OpSpecConstant %uint 2",
4278 "%dead_int = OpSpecConstant %int 2",
4279 "%dead_float = OpSpecConstant %float 2.5",
4280 "%dead_double = OpSpecConstant %double 1.428571428514",
4281 },
4282 /* .checks = */
4283 {
4284 "; CHECK: [[bool:%\\w+]] = OpSpecConstantTrue %bool",
4285 "; CHECK: [[uint:%\\w+]] = OpSpecConstant %uint 2",
4286 "; CHECK: [[int:%\\w+]] = OpSpecConstant %int 2",
4287 "; CHECK: [[float:%\\w+]] = OpSpecConstant %float 2.5",
4288 "; CHECK: [[double:%\\w+]] = OpSpecConstant %double 1.428571428514",
4289 "; CHECK-NOT: OpSpecConstant",
4290 "; CHECK: OpStore {{%\\w+}} [[bool]]",
4291 "; CHECK: OpStore {{%\\w+}} [[uint]]",
4292 "; CHECK: OpStore {{%\\w+}} [[int]]",
4293 "; CHECK: OpStore {{%\\w+}} [[float]]",
4294 "; CHECK: OpStore {{%\\w+}} [[double]]",
4295 },
4296 },
4297 // clang-format on
4298 })));
4299
4300 INSTANTIATE_TEST_SUITE_P(
4301 VectorTypeSpecConstants, AggressiveEliminateDeadConstantTest,
4302 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
4303 // clang-format off
4304 // Bool vector type spec constants. One vector has all component dead,
4305 // another vector has one dead boolean and one used boolean.
4306 {
4307 /* .used_consts = */
4308 {
4309 "%used_bool = OpSpecConstantTrue %bool",
4310 },
4311 /* .main_insts = */
4312 {
4313 "%bool_var = OpVariable %_pf_bool Output",
4314 "OpStore %bool_var %used_bool",
4315 },
4316 /* .dead_consts = */
4317 {
4318 "%dead_bool = OpSpecConstantFalse %bool",
4319 "%dead_bool_vec1 = OpSpecConstantComposite %v2bool %dead_bool %dead_bool",
4320 "%dead_bool_vec2 = OpSpecConstantComposite %v2bool %dead_bool %used_bool",
4321 },
4322 /* .checks = */
4323 {
4324 "; CHECK: [[bool:%\\w+]] = OpSpecConstantTrue %bool",
4325 "; CHECK-NOT: OpSpecConstant",
4326 "; CHECK: OpStore {{%\\w+}} [[bool]]",
4327 },
4328 },
4329
4330 // Uint vector type spec constants. One vector has all component dead,
4331 // another vector has one dead unsigned integer and one used unsigned
4332 // integer.
4333 {
4334 /* .used_consts = */
4335 {
4336 "%used_uint = OpSpecConstant %uint 3",
4337 },
4338 /* .main_insts = */
4339 {
4340 "%uint_var = OpVariable %_pf_uint Output",
4341 "OpStore %uint_var %used_uint",
4342 },
4343 /* .dead_consts = */
4344 {
4345 "%dead_uint = OpSpecConstant %uint 1",
4346 "%dead_uint_vec1 = OpSpecConstantComposite %v2uint %dead_uint %dead_uint",
4347 "%dead_uint_vec2 = OpSpecConstantComposite %v2uint %dead_uint %used_uint",
4348 },
4349 /* .checks = */
4350 {
4351 "; CHECK: [[uint:%\\w+]] = OpSpecConstant %uint 3",
4352 "; CHECK-NOT: OpSpecConstant",
4353 "; CHECK: OpStore {{%\\w+}} [[uint]]",
4354 },
4355 },
4356
4357 // Int vector type spec constants. One vector has all component dead,
4358 // another vector has one dead integer and one used integer.
4359 {
4360 /* .used_consts = */
4361 {
4362 "%used_int = OpSpecConstant %int 3",
4363 },
4364 /* .main_insts = */
4365 {
4366 "%int_var = OpVariable %_pf_int Output",
4367 "OpStore %int_var %used_int",
4368 },
4369 /* .dead_consts = */
4370 {
4371 "%dead_int = OpSpecConstant %int 1",
4372 "%dead_int_vec1 = OpSpecConstantComposite %v2int %dead_int %dead_int",
4373 "%dead_int_vec2 = OpSpecConstantComposite %v2int %dead_int %used_int",
4374 },
4375 /* .checks = */
4376 {
4377 "; CHECK: [[int:%\\w+]] = OpSpecConstant %int 3",
4378 "; CHECK-NOT: OpSpecConstant",
4379 "; CHECK: OpStore {{%\\w+}} [[int]]",
4380 },
4381 },
4382
4383 // Int vector type spec constants built with both spec constants and
4384 // front-end constants.
4385 {
4386 /* .used_consts = */
4387 {
4388 "%used_spec_int = OpSpecConstant %int 3",
4389 "%used_front_end_int = OpConstant %int 3",
4390 },
4391 /* .main_insts = */
4392 {
4393 "%int_var1 = OpVariable %_pf_int Output",
4394 "OpStore %int_var1 %used_spec_int",
4395 "%int_var2 = OpVariable %_pf_int Output",
4396 "OpStore %int_var2 %used_front_end_int",
4397 },
4398 /* .dead_consts = */
4399 {
4400 "%dead_spec_int = OpSpecConstant %int 1",
4401 "%dead_front_end_int = OpConstant %int 1",
4402 // Dead front-end and dead spec constants
4403 "%dead_int_vec1 = OpSpecConstantComposite %v2int %dead_spec_int %dead_front_end_int",
4404 // Used front-end and dead spec constants
4405 "%dead_int_vec2 = OpSpecConstantComposite %v2int %dead_spec_int %used_front_end_int",
4406 // Dead front-end and used spec constants
4407 "%dead_int_vec3 = OpSpecConstantComposite %v2int %dead_front_end_int %used_spec_int",
4408 },
4409 /* .checks = */
4410 {
4411 "; CHECK: [[int1:%\\w+]] = OpSpecConstant %int 3",
4412 "; CHECK: [[int2:%\\w+]] = OpConstant %int 3",
4413 "; CHECK-NOT: OpSpecConstant",
4414 "; CHECK-NOT: OpConstant",
4415 "; CHECK: OpStore {{%\\w+}} [[int1]]",
4416 "; CHECK: OpStore {{%\\w+}} [[int2]]",
4417 },
4418 },
4419 // clang-format on
4420 })));
4421
4422 INSTANTIATE_TEST_SUITE_P(
4423 SpecConstantOp, AggressiveEliminateDeadConstantTest,
4424 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
4425 // clang-format off
4426 // Cast operations: uint <-> int <-> bool
4427 {
4428 /* .used_consts = */ {},
4429 /* .main_insts = */ {},
4430 /* .dead_consts = */
4431 {
4432 // Assistant constants, only used in dead spec constant
4433 // operations.
4434 "%signed_zero = OpConstant %int 0",
4435 "%signed_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
4436 "%unsigned_zero = OpConstant %uint 0",
4437 "%unsigned_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
4438 "%signed_one = OpConstant %int 1",
4439 "%signed_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
4440 "%unsigned_one = OpConstant %uint 1",
4441 "%unsigned_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
4442
4443 // Spec constants that support casting to each other.
4444 "%dead_bool = OpSpecConstantTrue %bool",
4445 "%dead_uint = OpSpecConstant %uint 1",
4446 "%dead_int = OpSpecConstant %int 2",
4447 "%dead_bool_vec = OpSpecConstantComposite %v2bool %dead_bool %dead_bool",
4448 "%dead_uint_vec = OpSpecConstantComposite %v2uint %dead_uint %dead_uint",
4449 "%dead_int_vec = OpSpecConstantComposite %v2int %dead_int %dead_int",
4450
4451 // Scalar cast to boolean spec constant.
4452 "%int_to_bool = OpSpecConstantOp %bool INotEqual %dead_int %signed_zero",
4453 "%uint_to_bool = OpSpecConstantOp %bool INotEqual %dead_uint %unsigned_zero",
4454
4455 // Vector cast to boolean spec constant.
4456 "%int_to_bool_vec = OpSpecConstantOp %v2bool INotEqual %dead_int_vec %signed_zero_vec",
4457 "%uint_to_bool_vec = OpSpecConstantOp %v2bool INotEqual %dead_uint_vec %unsigned_zero_vec",
4458
4459 // Scalar cast to int spec constant.
4460 "%bool_to_int = OpSpecConstantOp %int Select %dead_bool %signed_one %signed_zero",
4461 "%uint_to_int = OpSpecConstantOp %uint IAdd %dead_uint %unsigned_zero",
4462
4463 // Vector cast to int spec constant.
4464 "%bool_to_int_vec = OpSpecConstantOp %v2int Select %dead_bool_vec %signed_one_vec %signed_zero_vec",
4465 "%uint_to_int_vec = OpSpecConstantOp %v2uint IAdd %dead_uint_vec %unsigned_zero_vec",
4466
4467 // Scalar cast to uint spec constant.
4468 "%bool_to_uint = OpSpecConstantOp %uint Select %dead_bool %unsigned_one %unsigned_zero",
4469 "%int_to_uint_vec = OpSpecConstantOp %uint IAdd %dead_int %signed_zero",
4470
4471 // Vector cast to uint spec constant.
4472 "%bool_to_uint_vec = OpSpecConstantOp %v2uint Select %dead_bool_vec %unsigned_one_vec %unsigned_zero_vec",
4473 "%int_to_uint = OpSpecConstantOp %v2uint IAdd %dead_int_vec %signed_zero_vec",
4474 },
4475 /* .checks = */
4476 {
4477 "; CHECK-NOT: OpConstant",
4478 "; CHECK-NOT: OpSpecConstant",
4479 },
4480 },
4481
4482 // Add, sub, mul, div, rem.
4483 {
4484 /* .used_consts = */ {},
4485 /* .main_insts = */ {},
4486 /* .dead_consts = */
4487 {
4488 "%dead_spec_int_a = OpSpecConstant %int 1",
4489 "%dead_spec_int_a_vec = OpSpecConstantComposite %v2int %dead_spec_int_a %dead_spec_int_a",
4490
4491 "%dead_spec_int_b = OpSpecConstant %int 2",
4492 "%dead_spec_int_b_vec = OpSpecConstantComposite %v2int %dead_spec_int_b %dead_spec_int_b",
4493
4494 "%dead_const_int_c = OpConstant %int 3",
4495 "%dead_const_int_c_vec = OpConstantComposite %v2int %dead_const_int_c %dead_const_int_c",
4496
4497 // Add
4498 "%add_a_b = OpSpecConstantOp %int IAdd %dead_spec_int_a %dead_spec_int_b",
4499 "%add_a_b_vec = OpSpecConstantOp %v2int IAdd %dead_spec_int_a_vec %dead_spec_int_b_vec",
4500
4501 // Sub
4502 "%sub_a_b = OpSpecConstantOp %int ISub %dead_spec_int_a %dead_spec_int_b",
4503 "%sub_a_b_vec = OpSpecConstantOp %v2int ISub %dead_spec_int_a_vec %dead_spec_int_b_vec",
4504
4505 // Mul
4506 "%mul_a_b = OpSpecConstantOp %int IMul %dead_spec_int_a %dead_spec_int_b",
4507 "%mul_a_b_vec = OpSpecConstantOp %v2int IMul %dead_spec_int_a_vec %dead_spec_int_b_vec",
4508
4509 // Div
4510 "%div_a_b = OpSpecConstantOp %int SDiv %dead_spec_int_a %dead_spec_int_b",
4511 "%div_a_b_vec = OpSpecConstantOp %v2int SDiv %dead_spec_int_a_vec %dead_spec_int_b_vec",
4512
4513 // Bitwise Xor
4514 "%xor_a_b = OpSpecConstantOp %int BitwiseXor %dead_spec_int_a %dead_spec_int_b",
4515 "%xor_a_b_vec = OpSpecConstantOp %v2int BitwiseXor %dead_spec_int_a_vec %dead_spec_int_b_vec",
4516
4517 // Scalar Comparison
4518 "%less_a_b = OpSpecConstantOp %bool SLessThan %dead_spec_int_a %dead_spec_int_b",
4519 },
4520 /* .checks = */
4521 {
4522 "; CHECK-NOT: OpConstant",
4523 "; CHECK-NOT: OpSpecConstant",
4524 },
4525 },
4526
4527 // Vectors without used swizzles should be removed.
4528 {
4529 /* .used_consts = */
4530 {
4531 "%used_int = OpConstant %int 3",
4532 },
4533 /* .main_insts = */
4534 {
4535 "%int_var = OpVariable %_pf_int Output",
4536 "OpStore %int_var %used_int",
4537 },
4538 /* .dead_consts = */
4539 {
4540 "%dead_int = OpConstant %int 3",
4541
4542 "%dead_spec_int_a = OpSpecConstant %int 1",
4543 "%vec_a = OpSpecConstantComposite %v4int %dead_spec_int_a %dead_spec_int_a %dead_int %dead_int",
4544
4545 "%dead_spec_int_b = OpSpecConstant %int 2",
4546 "%vec_b = OpSpecConstantComposite %v4int %dead_spec_int_b %dead_spec_int_b %used_int %used_int",
4547
4548 // Extract scalar
4549 "%a_x = OpSpecConstantOp %int CompositeExtract %vec_a 0",
4550 "%b_x = OpSpecConstantOp %int CompositeExtract %vec_b 0",
4551
4552 // Extract vector
4553 "%a_xy = OpSpecConstantOp %v2int VectorShuffle %vec_a %vec_a 0 1",
4554 "%b_xy = OpSpecConstantOp %v2int VectorShuffle %vec_b %vec_b 0 1",
4555 },
4556 /* .checks = */
4557 {
4558 "; CHECK: [[int:%\\w+]] = OpConstant %int 3",
4559 "; CHECK-NOT: OpConstant",
4560 "; CHECK-NOT: OpSpecConstant",
4561 "; CHECK: OpStore {{%\\w+}} [[int]]",
4562 },
4563 },
4564 // Vectors with used swizzles should not be removed.
4565 {
4566 /* .used_consts = */
4567 {
4568 "%used_int = OpConstant %int 3",
4569 "%used_spec_int_a = OpSpecConstant %int 1",
4570 "%used_spec_int_b = OpSpecConstant %int 2",
4571 // Create vectors
4572 "%vec_a = OpSpecConstantComposite %v4int %used_spec_int_a %used_spec_int_a %used_int %used_int",
4573 "%vec_b = OpSpecConstantComposite %v4int %used_spec_int_b %used_spec_int_b %used_int %used_int",
4574 // Extract vector
4575 "%a_xy = OpSpecConstantOp %v2int VectorShuffle %vec_a %vec_a 0 1",
4576 "%b_xy = OpSpecConstantOp %v2int VectorShuffle %vec_b %vec_b 0 1",
4577 },
4578 /* .main_insts = */
4579 {
4580 "%v2int_var_a = OpVariable %_pf_v2int Output",
4581 "%v2int_var_b = OpVariable %_pf_v2int Output",
4582 "OpStore %v2int_var_a %a_xy",
4583 "OpStore %v2int_var_b %b_xy",
4584 },
4585 /* .dead_consts = */ {},
4586 /* .checks = */
4587 {
4588 "; CHECK: [[int:%\\w+]] = OpConstant %int 3",
4589 "; CHECK: [[a:%\\w+]] = OpSpecConstant %int 1",
4590 "; CHECK: [[b:%\\w+]] = OpSpecConstant %int 2",
4591 "; CHECK: [[veca:%\\w+]] = OpSpecConstantComposite %v4int [[a]] [[a]] [[int]] [[int]]",
4592 "; CHECK: [[vecb:%\\w+]] = OpSpecConstantComposite %v4int [[b]] [[b]] [[int]] [[int]]",
4593 "; CHECK: [[exa:%\\w+]] = OpSpecConstantOp %v2int VectorShuffle [[veca]] [[veca]] 0 1",
4594 "; CHECK: [[exb:%\\w+]] = OpSpecConstantOp %v2int VectorShuffle [[vecb]] [[vecb]] 0 1",
4595 "; CHECK-NOT: OpConstant",
4596 "; CHECK-NOT: OpSpecConstant",
4597 "; CHECK: OpStore {{%\\w+}} [[exa]]",
4598 "; CHECK: OpStore {{%\\w+}} [[exb]]",
4599 },
4600 },
4601 // clang-format on
4602 })));
4603
4604 INSTANTIATE_TEST_SUITE_P(
4605 LongDefUseChain, AggressiveEliminateDeadConstantTest,
4606 ::testing::ValuesIn(std::vector<AggressiveEliminateDeadConstantTestCase>({
4607 // clang-format off
4608 // Long Def-Use chain with binary operations.
4609 {
4610 /* .used_consts = */
4611 {
4612 "%array_size = OpConstant %int 4",
4613 "%type_arr_int_4 = OpTypeArray %int %array_size",
4614 "%used_int_0 = OpConstant %int 100",
4615 "%used_int_1 = OpConstant %int 1",
4616 "%used_int_2 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_1",
4617 "%used_int_3 = OpSpecConstantOp %int ISub %used_int_0 %used_int_2",
4618 "%used_int_4 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_3",
4619 "%used_int_5 = OpSpecConstantOp %int ISub %used_int_0 %used_int_4",
4620 "%used_int_6 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_5",
4621 "%used_int_7 = OpSpecConstantOp %int ISub %used_int_0 %used_int_6",
4622 "%used_int_8 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_7",
4623 "%used_int_9 = OpSpecConstantOp %int ISub %used_int_0 %used_int_8",
4624 "%used_int_10 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_9",
4625 "%used_int_11 = OpSpecConstantOp %int ISub %used_int_0 %used_int_10",
4626 "%used_int_12 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_11",
4627 "%used_int_13 = OpSpecConstantOp %int ISub %used_int_0 %used_int_12",
4628 "%used_int_14 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_13",
4629 "%used_int_15 = OpSpecConstantOp %int ISub %used_int_0 %used_int_14",
4630 "%used_int_16 = OpSpecConstantOp %int ISub %used_int_0 %used_int_15",
4631 "%used_int_17 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_16",
4632 "%used_int_18 = OpSpecConstantOp %int ISub %used_int_0 %used_int_17",
4633 "%used_int_19 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_18",
4634 "%used_int_20 = OpSpecConstantOp %int ISub %used_int_0 %used_int_19",
4635 "%used_vec_a = OpSpecConstantComposite %v2int %used_int_18 %used_int_19",
4636 "%used_vec_b = OpSpecConstantOp %v2int IMul %used_vec_a %used_vec_a",
4637 "%used_int_21 = OpSpecConstantOp %int CompositeExtract %used_vec_b 0",
4638 "%used_array = OpConstantComposite %type_arr_int_4 %used_int_20 %used_int_20 %used_int_21 %used_int_21",
4639 },
4640 /* .main_insts = */
4641 {
4642 "%int_var = OpVariable %_pf_int Output",
4643 "%used_array_2 = OpCompositeExtract %int %used_array 2",
4644 "OpStore %int_var %used_array_2",
4645 },
4646 /* .dead_consts = */
4647 {
4648 "%dead_int_1 = OpConstant %int 2",
4649 "%dead_int_2 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_1",
4650 "%dead_int_3 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_2",
4651 "%dead_int_4 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_3",
4652 "%dead_int_5 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_4",
4653 "%dead_int_6 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_5",
4654 "%dead_int_7 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_6",
4655 "%dead_int_8 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_7",
4656 "%dead_int_9 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_8",
4657 "%dead_int_10 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_9",
4658 "%dead_int_11 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_10",
4659 "%dead_int_12 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_11",
4660 "%dead_int_13 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_12",
4661 "%dead_int_14 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_13",
4662 "%dead_int_15 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_14",
4663 "%dead_int_16 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_15",
4664 "%dead_int_17 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_16",
4665 "%dead_int_18 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_17",
4666 "%dead_int_19 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_18",
4667 "%dead_int_20 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_19",
4668 "%dead_vec_a = OpSpecConstantComposite %v2int %dead_int_18 %dead_int_19",
4669 "%dead_vec_b = OpSpecConstantOp %v2int IMul %dead_vec_a %dead_vec_a",
4670 "%dead_int_21 = OpSpecConstantOp %int CompositeExtract %dead_vec_b 0",
4671 "%dead_array = OpConstantComposite %type_arr_int_4 %dead_int_20 %used_int_20 %dead_int_19 %used_int_19",
4672 },
4673 /* .checks = */
4674 {
4675 "; CHECK: OpConstant %int 4",
4676 "; CHECK: [[array:%\\w+]] = OpConstantComposite %type_arr_int_4 %used_int_20 %used_int_20 %used_int_21 %used_int_21",
4677 "; CHECK-NOT: OpConstant",
4678 "; CHECK-NOT: OpSpecConstant",
4679 "; CHECK: OpStore {{%\\w+}} [[array]]",
4680 },
4681 },
4682 // Long Def-Use chain with swizzle
4683 // clang-format on
4684 })));
4685
4686 TEST_F(AggressiveDCETest, DeadDecorationGroup) {
4687 // The decoration group should be eliminated because the target of group
4688 // decorate is dead.
4689 const std::string text = R"(
4690 ; CHECK-NOT: OpDecorat
4691 ; CHECK-NOT: OpGroupDecorate
4692 OpCapability Shader
4693 OpMemoryModel Logical GLSL450
4694 OpEntryPoint Fragment %main "main"
4695 OpExecutionMode %main OriginUpperLeft
4696 OpDecorate %1 Restrict
4697 OpDecorate %1 Aliased
4698 %1 = OpDecorationGroup
4699 OpGroupDecorate %1 %var
4700 %void = OpTypeVoid
4701 %func = OpTypeFunction %void
4702 %uint = OpTypeInt 32 0
4703 %uint_ptr = OpTypePointer Function %uint
4704 %main = OpFunction %void None %func
4705 %2 = OpLabel
4706 %var = OpVariable %uint_ptr Function
4707 OpReturn
4708 OpFunctionEnd
4709 )";
4710
4711 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4712 }
4713
4714 TEST_F(AggressiveDCETest, DeadDecorationGroupAndValidDecorationMgr) {
4715 // The decoration group should be eliminated because the target of group
4716 // decorate is dead.
4717 const std::string text = R"(
4718 OpCapability Shader
4719 OpMemoryModel Logical GLSL450
4720 OpEntryPoint Fragment %main "main"
4721 OpExecutionMode %main OriginUpperLeft
4722 OpDecorate %1 Restrict
4723 OpDecorate %1 Aliased
4724 %1 = OpDecorationGroup
4725 OpGroupDecorate %1 %var
4726 %void = OpTypeVoid
4727 %func = OpTypeFunction %void
4728 %uint = OpTypeInt 32 0
4729 %uint_ptr = OpTypePointer Function %uint
4730 %main = OpFunction %void None %func
4731 %2 = OpLabel
4732 %var = OpVariable %uint_ptr Function
4733 OpReturn
4734 OpFunctionEnd
4735 )";
4736
4737 auto pass = MakeUnique<AggressiveDCEPass>();
4738 auto consumer = [](spv_message_level_t, const char*, const spv_position_t&,
4739 const char* message) {
4740 std::cerr << message << std::endl;
4741 };
4742 auto context = BuildModule(SPV_ENV_UNIVERSAL_1_1, consumer, text);
4743
4744 // Build the decoration manager before the pass.
4745 context->get_decoration_mgr();
4746
4747 const auto status = pass->Run(context.get());
4748 EXPECT_EQ(status, Pass::Status::SuccessWithChange);
4749 }
4750
4751 TEST_F(AggressiveDCETest, ParitallyDeadDecorationGroup) {
4752 const std::string text = R"(
4753 ; CHECK: OpDecorate [[grp:%\w+]] Restrict
4754 ; CHECK: [[grp]] = OpDecorationGroup
4755 ; CHECK: OpGroupDecorate [[grp]] [[output:%\w+]]
4756 ; CHECK: [[output]] = OpVariable {{%\w+}} Output
4757 ; CHECK-NOT: OpVariable {{%\w+}} Function
4758 OpCapability Shader
4759 OpMemoryModel Logical GLSL450
4760 OpEntryPoint Fragment %main "main" %output
4761 OpExecutionMode %main OriginUpperLeft
4762 OpDecorate %1 Restrict
4763 %1 = OpDecorationGroup
4764 OpGroupDecorate %1 %var %output
4765 %void = OpTypeVoid
4766 %func = OpTypeFunction %void
4767 %uint = OpTypeInt 32 0
4768 %uint_ptr_Function = OpTypePointer Function %uint
4769 %uint_ptr_Output = OpTypePointer Output %uint
4770 %uint_0 = OpConstant %uint 0
4771 %output = OpVariable %uint_ptr_Output Output
4772 %main = OpFunction %void None %func
4773 %2 = OpLabel
4774 %var = OpVariable %uint_ptr_Function Function
4775 OpStore %output %uint_0
4776 OpReturn
4777 OpFunctionEnd
4778 )";
4779
4780 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4781 }
4782
4783 TEST_F(AggressiveDCETest, ParitallyDeadDecorationGroupDifferentGroupDecorate) {
4784 const std::string text = R"(
4785 ; CHECK: OpDecorate [[grp:%\w+]] Restrict
4786 ; CHECK: [[grp]] = OpDecorationGroup
4787 ; CHECK: OpGroupDecorate [[grp]] [[output:%\w+]]
4788 ; CHECK-NOT: OpGroupDecorate
4789 ; CHECK: [[output]] = OpVariable {{%\w+}} Output
4790 ; CHECK-NOT: OpVariable {{%\w+}} Function
4791 OpCapability Shader
4792 OpMemoryModel Logical GLSL450
4793 OpEntryPoint Fragment %main "main" %output
4794 OpExecutionMode %main OriginUpperLeft
4795 OpDecorate %1 Restrict
4796 %1 = OpDecorationGroup
4797 OpGroupDecorate %1 %output
4798 OpGroupDecorate %1 %var
4799 %void = OpTypeVoid
4800 %func = OpTypeFunction %void
4801 %uint = OpTypeInt 32 0
4802 %uint_ptr_Function = OpTypePointer Function %uint
4803 %uint_ptr_Output = OpTypePointer Output %uint
4804 %uint_0 = OpConstant %uint 0
4805 %output = OpVariable %uint_ptr_Output Output
4806 %main = OpFunction %void None %func
4807 %2 = OpLabel
4808 %var = OpVariable %uint_ptr_Function Function
4809 OpStore %output %uint_0
4810 OpReturn
4811 OpFunctionEnd
4812 )";
4813
4814 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4815 }
4816
4817 TEST_F(AggressiveDCETest, DeadGroupMemberDecorate) {
4818 const std::string text = R"(
4819 ; CHECK-NOT: OpDec
4820 ; CHECK-NOT: OpGroup
4821 OpCapability Shader
4822 OpMemoryModel Logical GLSL450
4823 OpEntryPoint Fragment %main "main"
4824 OpExecutionMode %main OriginUpperLeft
4825 OpDecorate %1 Offset 0
4826 OpDecorate %1 Uniform
4827 %1 = OpDecorationGroup
4828 OpGroupMemberDecorate %1 %var 0
4829 %void = OpTypeVoid
4830 %func = OpTypeFunction %void
4831 %uint = OpTypeInt 32 0
4832 %struct = OpTypeStruct %uint %uint
4833 %struct_ptr = OpTypePointer Function %struct
4834 %main = OpFunction %void None %func
4835 %2 = OpLabel
4836 %var = OpVariable %struct_ptr Function
4837 OpReturn
4838 OpFunctionEnd
4839 )";
4840
4841 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4842 }
4843
4844 TEST_F(AggressiveDCETest, PartiallyDeadGroupMemberDecorate) {
4845 const std::string text = R"(
4846 ; CHECK: OpDecorate [[grp:%\w+]] Offset 0
4847 ; CHECK: OpDecorate [[grp]] RelaxedPrecision
4848 ; CHECK: [[grp]] = OpDecorationGroup
4849 ; CHECK: OpGroupMemberDecorate [[grp]] [[output:%\w+]] 1
4850 ; CHECK: [[output]] = OpTypeStruct
4851 ; CHECK-NOT: OpTypeStruct
4852 OpCapability Shader
4853 OpMemoryModel Logical GLSL450
4854 OpEntryPoint Fragment %main "main" %output
4855 OpExecutionMode %main OriginUpperLeft
4856 OpDecorate %1 Offset 0
4857 OpDecorate %1 RelaxedPrecision
4858 %1 = OpDecorationGroup
4859 OpGroupMemberDecorate %1 %var_struct 0 %output_struct 1
4860 %void = OpTypeVoid
4861 %func = OpTypeFunction %void
4862 %uint = OpTypeInt 32 0
4863 %var_struct = OpTypeStruct %uint %uint
4864 %output_struct = OpTypeStruct %uint %uint
4865 %struct_ptr_Function = OpTypePointer Function %var_struct
4866 %struct_ptr_Output = OpTypePointer Output %output_struct
4867 %uint_ptr_Output = OpTypePointer Output %uint
4868 %output = OpVariable %struct_ptr_Output Output
4869 %uint_0 = OpConstant %uint 0
4870 %main = OpFunction %void None %func
4871 %2 = OpLabel
4872 %var = OpVariable %struct_ptr_Function Function
4873 %3 = OpAccessChain %uint_ptr_Output %output %uint_0
4874 OpStore %3 %uint_0
4875 OpReturn
4876 OpFunctionEnd
4877 )";
4878
4879 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4880 }
4881
4882 TEST_F(AggressiveDCETest,
4883 PartiallyDeadGroupMemberDecorateDifferentGroupDecorate) {
4884 const std::string text = R"(
4885 ; CHECK: OpDecorate [[grp:%\w+]] Offset 0
4886 ; CHECK: OpDecorate [[grp]] RelaxedPrecision
4887 ; CHECK: [[grp]] = OpDecorationGroup
4888 ; CHECK: OpGroupMemberDecorate [[grp]] [[output:%\w+]] 1
4889 ; CHECK-NOT: OpGroupMemberDecorate
4890 ; CHECK: [[output]] = OpTypeStruct
4891 ; CHECK-NOT: OpTypeStruct
4892 OpCapability Shader
4893 OpMemoryModel Logical GLSL450
4894 OpEntryPoint Fragment %main "main" %output
4895 OpExecutionMode %main OriginUpperLeft
4896 OpDecorate %1 Offset 0
4897 OpDecorate %1 RelaxedPrecision
4898 %1 = OpDecorationGroup
4899 OpGroupMemberDecorate %1 %var_struct 0
4900 OpGroupMemberDecorate %1 %output_struct 1
4901 %void = OpTypeVoid
4902 %func = OpTypeFunction %void
4903 %uint = OpTypeInt 32 0
4904 %var_struct = OpTypeStruct %uint %uint
4905 %output_struct = OpTypeStruct %uint %uint
4906 %struct_ptr_Function = OpTypePointer Function %var_struct
4907 %struct_ptr_Output = OpTypePointer Output %output_struct
4908 %uint_ptr_Output = OpTypePointer Output %uint
4909 %output = OpVariable %struct_ptr_Output Output
4910 %uint_0 = OpConstant %uint 0
4911 %main = OpFunction %void None %func
4912 %2 = OpLabel
4913 %var = OpVariable %struct_ptr_Function Function
4914 %3 = OpAccessChain %uint_ptr_Output %output %uint_0
4915 OpStore %3 %uint_0
4916 OpReturn
4917 OpFunctionEnd
4918 )";
4919
4920 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4921 }
4922
4923 // Test for #1404
4924 TEST_F(AggressiveDCETest, DontRemoveWorkgroupSize) {
4925 const std::string text = R"(
4926 ; CHECK: OpDecorate [[wgs:%\w+]] BuiltIn WorkgroupSize
4927 ; CHECK: [[wgs]] = OpSpecConstantComposite
4928 OpCapability Shader
4929 OpMemoryModel Logical GLSL450
4930 OpEntryPoint GLCompute %func "func"
4931 OpExecutionMode %func LocalSize 1 1 1
4932 OpDecorate %1 BuiltIn WorkgroupSize
4933 %void = OpTypeVoid
4934 %int = OpTypeInt 32 0
4935 %functy = OpTypeFunction %void
4936 %v3int = OpTypeVector %int 3
4937 %2 = OpSpecConstant %int 1
4938 %1 = OpSpecConstantComposite %v3int %2 %2 %2
4939 %func = OpFunction %void None %functy
4940 %3 = OpLabel
4941 OpReturn
4942 OpFunctionEnd
4943 )";
4944
4945 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
4946 }
4947
4948 // Test for #1214
4949 TEST_F(AggressiveDCETest, LoopHeaderIsAlsoAnotherLoopMerge) {
4950 const std::string text = R"(OpCapability Shader
4951 OpMemoryModel Logical GLSL450
4952 OpEntryPoint Fragment %1 "func" %2
4953 OpExecutionMode %1 OriginUpperLeft
4954 %void = OpTypeVoid
4955 %bool = OpTypeBool
4956 %true = OpConstantTrue %bool
4957 %uint = OpTypeInt 32 0
4958 %_ptr_Output_uint = OpTypePointer Output %uint
4959 %2 = OpVariable %_ptr_Output_uint Output
4960 %uint_0 = OpConstant %uint 0
4961 %9 = OpTypeFunction %void
4962 %1 = OpFunction %void None %9
4963 %10 = OpLabel
4964 OpBranch %11
4965 %11 = OpLabel
4966 OpLoopMerge %12 %13 None
4967 OpBranchConditional %true %14 %13
4968 %14 = OpLabel
4969 OpStore %2 %uint_0
4970 OpLoopMerge %15 %16 None
4971 OpBranchConditional %true %15 %16
4972 %16 = OpLabel
4973 OpBranch %14
4974 %15 = OpLabel
4975 OpBranchConditional %true %12 %13
4976 %13 = OpLabel
4977 OpBranch %11
4978 %12 = OpLabel
4979 %17 = OpPhi %uint %uint_0 %15 %uint_0 %18
4980 OpStore %2 %17
4981 OpLoopMerge %19 %18 None
4982 OpBranchConditional %true %19 %18
4983 %18 = OpLabel
4984 OpBranch %12
4985 %19 = OpLabel
4986 OpReturn
4987 OpFunctionEnd
4988 )";
4989
4990 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
4991 }
4992
4993 TEST_F(AggressiveDCETest, BreaksDontVisitPhis) {
4994 const std::string text = R"(
4995 OpCapability Shader
4996 OpMemoryModel Logical GLSL450
4997 OpEntryPoint Fragment %func "func" %var
4998 OpExecutionMode %func OriginUpperLeft
4999 %void = OpTypeVoid
5000 %bool = OpTypeBool
5001 %true = OpConstantTrue %bool
5002 %int = OpTypeInt 32 0
5003 %int_ptr_Output = OpTypePointer Output %int
5004 %var = OpVariable %int_ptr_Output Output
5005 %int0 = OpConstant %int 0
5006 %functy = OpTypeFunction %void
5007 %func = OpFunction %void None %functy
5008 %entry = OpLabel
5009 OpBranch %outer_header
5010 %outer_header = OpLabel
5011 OpLoopMerge %outer_merge %outer_continue None
5012 OpBranchConditional %true %inner_header %outer_continue
5013 %inner_header = OpLabel
5014 %phi = OpPhi %int %int0 %outer_header %int0 %inner_continue
5015 OpStore %var %phi
5016 OpLoopMerge %inner_merge %inner_continue None
5017 OpBranchConditional %true %inner_merge %inner_continue
5018 %inner_continue = OpLabel
5019 OpBranch %inner_header
5020 %inner_merge = OpLabel
5021 OpBranch %outer_continue
5022 %outer_continue = OpLabel
5023 %p = OpPhi %int %int0 %outer_header %int0 %inner_merge
5024 OpStore %var %p
5025 OpBranch %outer_header
5026 %outer_merge = OpLabel
5027 OpReturn
5028 OpFunctionEnd
5029 )";
5030
5031 EXPECT_EQ(Pass::Status::SuccessWithoutChange,
5032 std::get<1>(SinglePassRunAndDisassemble<AggressiveDCEPass>(
5033 text, false, true)));
5034 }
5035
5036 // Test for #1212
5037 TEST_F(AggressiveDCETest, ConstStoreInnerLoop) {
5038 const std::string text = R"(OpCapability Shader
5039 OpMemoryModel Logical GLSL450
5040 OpEntryPoint Vertex %1 "main" %2
5041 %void = OpTypeVoid
5042 %4 = OpTypeFunction %void
5043 %float = OpTypeFloat 32
5044 %bool = OpTypeBool
5045 %true = OpConstantTrue %bool
5046 %_ptr_Output_float = OpTypePointer Output %float
5047 %2 = OpVariable %_ptr_Output_float Output
5048 %float_3 = OpConstant %float 3
5049 %1 = OpFunction %void None %4
5050 %13 = OpLabel
5051 OpBranch %14
5052 %14 = OpLabel
5053 OpLoopMerge %15 %16 None
5054 OpBranchConditional %true %17 %15
5055 %17 = OpLabel
5056 OpStore %2 %float_3
5057 OpLoopMerge %18 %17 None
5058 OpBranchConditional %true %18 %17
5059 %18 = OpLabel
5060 OpBranch %15
5061 %16 = OpLabel
5062 OpBranch %14
5063 %15 = OpLabel
5064 OpBranch %20
5065 %20 = OpLabel
5066 OpReturn
5067 OpFunctionEnd
5068 )";
5069
5070 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5071 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
5072 }
5073
5074 // Test for #1212
5075 TEST_F(AggressiveDCETest, InnerLoopCopy) {
5076 const std::string text = R"(OpCapability Shader
5077 OpMemoryModel Logical GLSL450
5078 OpEntryPoint Vertex %1 "main" %2 %3
5079 %void = OpTypeVoid
5080 %5 = OpTypeFunction %void
5081 %float = OpTypeFloat 32
5082 %bool = OpTypeBool
5083 %true = OpConstantTrue %bool
5084 %_ptr_Output_float = OpTypePointer Output %float
5085 %_ptr_Input_float = OpTypePointer Input %float
5086 %2 = OpVariable %_ptr_Output_float Output
5087 %3 = OpVariable %_ptr_Input_float Input
5088 %1 = OpFunction %void None %5
5089 %14 = OpLabel
5090 OpBranch %15
5091 %15 = OpLabel
5092 OpLoopMerge %16 %17 None
5093 OpBranchConditional %true %18 %16
5094 %18 = OpLabel
5095 %19 = OpLoad %float %3
5096 OpStore %2 %19
5097 OpLoopMerge %20 %18 None
5098 OpBranchConditional %true %20 %18
5099 %20 = OpLabel
5100 OpBranch %16
5101 %17 = OpLabel
5102 OpBranch %15
5103 %16 = OpLabel
5104 OpBranch %22
5105 %22 = OpLabel
5106 OpReturn
5107 OpFunctionEnd
5108 )";
5109
5110 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5111 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
5112 }
5113
5114 TEST_F(AggressiveDCETest, AtomicAdd) {
5115 const std::string text = R"(OpCapability SampledBuffer
5116 OpCapability StorageImageExtendedFormats
5117 OpCapability ImageBuffer
5118 OpCapability Shader
5119 %1 = OpExtInstImport "GLSL.std.450"
5120 OpMemoryModel Logical GLSL450
5121 OpEntryPoint GLCompute %2 "min"
5122 OpExecutionMode %2 LocalSize 64 1 1
5123 OpSource HLSL 600
5124 OpDecorate %4 DescriptorSet 4
5125 OpDecorate %4 Binding 70
5126 %uint = OpTypeInt 32 0
5127 %6 = OpTypeImage %uint Buffer 0 0 0 2 R32ui
5128 %_ptr_UniformConstant_6 = OpTypePointer UniformConstant %6
5129 %_ptr_Private_6 = OpTypePointer Private %6
5130 %void = OpTypeVoid
5131 %10 = OpTypeFunction %void
5132 %uint_0 = OpConstant %uint 0
5133 %uint_1 = OpConstant %uint 1
5134 %_ptr_Image_uint = OpTypePointer Image %uint
5135 %4 = OpVariable %_ptr_UniformConstant_6 UniformConstant
5136 %16 = OpVariable %_ptr_Private_6 Private
5137 %2 = OpFunction %void None %10
5138 %17 = OpLabel
5139 %18 = OpLoad %6 %4
5140 OpStore %16 %18
5141 %19 = OpImageTexelPointer %_ptr_Image_uint %16 %uint_0 %uint_0
5142 %20 = OpAtomicIAdd %uint %19 %uint_1 %uint_0 %uint_1
5143 OpReturn
5144 OpFunctionEnd
5145 )";
5146
5147 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5148 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
5149 }
5150
5151 TEST_F(AggressiveDCETest, SafelyRemoveDecorateString) {
5152 const std::string preamble = R"(OpCapability Shader
5153 OpExtension "SPV_GOOGLE_hlsl_functionality1"
5154 OpMemoryModel Logical GLSL450
5155 OpEntryPoint Fragment %1 "main"
5156 OpExecutionMode %1 OriginUpperLeft
5157 )";
5158
5159 const std::string body_before =
5160 R"(OpDecorateStringGOOGLE %2 HlslSemanticGOOGLE "FOOBAR"
5161 %void = OpTypeVoid
5162 %4 = OpTypeFunction %void
5163 %uint = OpTypeInt 32 0
5164 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
5165 %2 = OpVariable %_ptr_StorageBuffer_uint StorageBuffer
5166 %1 = OpFunction %void None %4
5167 %7 = OpLabel
5168 OpReturn
5169 OpFunctionEnd
5170 )";
5171
5172 const std::string body_after = R"(%void = OpTypeVoid
5173 %4 = OpTypeFunction %void
5174 %1 = OpFunction %void None %4
5175 %7 = OpLabel
5176 OpReturn
5177 OpFunctionEnd
5178 )";
5179
5180 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5181 SinglePassRunAndCheck<AggressiveDCEPass>(preamble + body_before,
5182 preamble + body_after, true, true);
5183 }
5184
5185 TEST_F(AggressiveDCETest, CopyMemoryToGlobal) {
5186 // |local| is loaded in an OpCopyMemory instruction. So the store must be
5187 // kept alive.
5188 const std::string test =
5189 R"(OpCapability Geometry
5190 %1 = OpExtInstImport "GLSL.std.450"
5191 OpMemoryModel Logical GLSL450
5192 OpEntryPoint Geometry %main "main" %global
5193 OpExecutionMode %main Triangles
5194 OpExecutionMode %main Invocations 1
5195 OpExecutionMode %main OutputTriangleStrip
5196 OpExecutionMode %main OutputVertices 5
5197 OpSource GLSL 440
5198 OpName %main "main"
5199 OpName %local "local"
5200 OpName %global "global"
5201 %void = OpTypeVoid
5202 %7 = OpTypeFunction %void
5203 %float = OpTypeFloat 32
5204 %v4float = OpTypeVector %float 4
5205 %12 = OpConstantNull %v4float
5206 %_ptr_Function_v4float = OpTypePointer Function %v4float
5207 %_ptr_Output_v4float = OpTypePointer Output %v4float
5208 %global = OpVariable %_ptr_Output_v4float Output
5209 %main = OpFunction %void None %7
5210 %19 = OpLabel
5211 %local = OpVariable %_ptr_Function_v4float Function
5212 OpStore %local %12
5213 OpCopyMemory %global %local
5214 OpEndPrimitive
5215 OpReturn
5216 OpFunctionEnd
5217 )";
5218
5219 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5220 SinglePassRunAndCheck<AggressiveDCEPass>(test, test, true, true);
5221 }
5222
5223 TEST_F(AggressiveDCETest, CopyMemoryToLocal) {
5224 // Make sure the store to |local2| using OpCopyMemory is kept and keeps
5225 // |local1| alive.
5226 const std::string test =
5227 R"(OpCapability Geometry
5228 %1 = OpExtInstImport "GLSL.std.450"
5229 OpMemoryModel Logical GLSL450
5230 OpEntryPoint Geometry %main "main" %global
5231 OpExecutionMode %main Triangles
5232 OpExecutionMode %main Invocations 1
5233 OpExecutionMode %main OutputTriangleStrip
5234 OpExecutionMode %main OutputVertices 5
5235 OpSource GLSL 440
5236 OpName %main "main"
5237 OpName %local1 "local1"
5238 OpName %local2 "local2"
5239 OpName %global "global"
5240 %void = OpTypeVoid
5241 %7 = OpTypeFunction %void
5242 %float = OpTypeFloat 32
5243 %v4float = OpTypeVector %float 4
5244 %12 = OpConstantNull %v4float
5245 %_ptr_Function_v4float = OpTypePointer Function %v4float
5246 %_ptr_Output_v4float = OpTypePointer Output %v4float
5247 %global = OpVariable %_ptr_Output_v4float Output
5248 %main = OpFunction %void None %7
5249 %19 = OpLabel
5250 %local1 = OpVariable %_ptr_Function_v4float Function
5251 %local2 = OpVariable %_ptr_Function_v4float Function
5252 OpStore %local1 %12
5253 OpCopyMemory %local2 %local1
5254 OpCopyMemory %global %local2
5255 OpEndPrimitive
5256 OpReturn
5257 OpFunctionEnd
5258 )";
5259
5260 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5261 SinglePassRunAndCheck<AggressiveDCEPass>(test, test, true, true);
5262 }
5263
5264 TEST_F(AggressiveDCETest, RemoveCopyMemoryToLocal) {
5265 // Test that we remove function scope variables that are stored to using
5266 // OpCopyMemory, but are never loaded. We can remove both |local1| and
5267 // |local2|.
5268 const std::string test =
5269 R"(OpCapability Geometry
5270 %1 = OpExtInstImport "GLSL.std.450"
5271 OpMemoryModel Logical GLSL450
5272 OpEntryPoint Geometry %main "main" %global
5273 OpExecutionMode %main Triangles
5274 OpExecutionMode %main Invocations 1
5275 OpExecutionMode %main OutputTriangleStrip
5276 OpExecutionMode %main OutputVertices 5
5277 OpSource GLSL 440
5278 OpName %main "main"
5279 OpName %local1 "local1"
5280 OpName %local2 "local2"
5281 OpName %global "global"
5282 %void = OpTypeVoid
5283 %7 = OpTypeFunction %void
5284 %float = OpTypeFloat 32
5285 %v4float = OpTypeVector %float 4
5286 %12 = OpConstantNull %v4float
5287 %_ptr_Function_v4float = OpTypePointer Function %v4float
5288 %_ptr_Output_v4float = OpTypePointer Output %v4float
5289 %global = OpVariable %_ptr_Output_v4float Output
5290 %main = OpFunction %void None %7
5291 %19 = OpLabel
5292 %local1 = OpVariable %_ptr_Function_v4float Function
5293 %local2 = OpVariable %_ptr_Function_v4float Function
5294 OpStore %local1 %12
5295 OpCopyMemory %local2 %local1
5296 OpEndPrimitive
5297 OpReturn
5298 OpFunctionEnd
5299 )";
5300
5301 const std::string result =
5302 R"(OpCapability Geometry
5303 %1 = OpExtInstImport "GLSL.std.450"
5304 OpMemoryModel Logical GLSL450
5305 OpEntryPoint Geometry %main "main" %global
5306 OpExecutionMode %main Triangles
5307 OpExecutionMode %main Invocations 1
5308 OpExecutionMode %main OutputTriangleStrip
5309 OpExecutionMode %main OutputVertices 5
5310 OpSource GLSL 440
5311 OpName %main "main"
5312 OpName %global "global"
5313 %void = OpTypeVoid
5314 %7 = OpTypeFunction %void
5315 %float = OpTypeFloat 32
5316 %v4float = OpTypeVector %float 4
5317 %_ptr_Output_v4float = OpTypePointer Output %v4float
5318 %global = OpVariable %_ptr_Output_v4float Output
5319 %main = OpFunction %void None %7
5320 %19 = OpLabel
5321 OpEndPrimitive
5322 OpReturn
5323 OpFunctionEnd
5324 )";
5325
5326 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5327 SinglePassRunAndCheck<AggressiveDCEPass>(test, result, true, true);
5328 }
5329
5330 TEST_F(AggressiveDCETest, RemoveCopyMemoryToLocal2) {
5331 // We are able to remove "local2" because it is not loaded, but have to keep
5332 // the stores to "local1".
5333 const std::string test =
5334 R"(OpCapability Geometry
5335 %1 = OpExtInstImport "GLSL.std.450"
5336 OpMemoryModel Logical GLSL450
5337 OpEntryPoint Geometry %main "main" %global
5338 OpExecutionMode %main Triangles
5339 OpExecutionMode %main Invocations 1
5340 OpExecutionMode %main OutputTriangleStrip
5341 OpExecutionMode %main OutputVertices 5
5342 OpSource GLSL 440
5343 OpName %main "main"
5344 OpName %local1 "local1"
5345 OpName %local2 "local2"
5346 OpName %global "global"
5347 %void = OpTypeVoid
5348 %7 = OpTypeFunction %void
5349 %float = OpTypeFloat 32
5350 %v4float = OpTypeVector %float 4
5351 %12 = OpConstantNull %v4float
5352 %_ptr_Function_v4float = OpTypePointer Function %v4float
5353 %_ptr_Output_v4float = OpTypePointer Output %v4float
5354 %global = OpVariable %_ptr_Output_v4float Output
5355 %main = OpFunction %void None %7
5356 %19 = OpLabel
5357 %local1 = OpVariable %_ptr_Function_v4float Function
5358 %local2 = OpVariable %_ptr_Function_v4float Function
5359 OpStore %local1 %12
5360 OpCopyMemory %local2 %local1
5361 OpCopyMemory %global %local1
5362 OpEndPrimitive
5363 OpReturn
5364 OpFunctionEnd
5365 )";
5366
5367 const std::string result =
5368 R"(OpCapability Geometry
5369 %1 = OpExtInstImport "GLSL.std.450"
5370 OpMemoryModel Logical GLSL450
5371 OpEntryPoint Geometry %main "main" %global
5372 OpExecutionMode %main Triangles
5373 OpExecutionMode %main Invocations 1
5374 OpExecutionMode %main OutputTriangleStrip
5375 OpExecutionMode %main OutputVertices 5
5376 OpSource GLSL 440
5377 OpName %main "main"
5378 OpName %local1 "local1"
5379 OpName %global "global"
5380 %void = OpTypeVoid
5381 %7 = OpTypeFunction %void
5382 %float = OpTypeFloat 32
5383 %v4float = OpTypeVector %float 4
5384 %12 = OpConstantNull %v4float
5385 %_ptr_Function_v4float = OpTypePointer Function %v4float
5386 %_ptr_Output_v4float = OpTypePointer Output %v4float
5387 %global = OpVariable %_ptr_Output_v4float Output
5388 %main = OpFunction %void None %7
5389 %19 = OpLabel
5390 %local1 = OpVariable %_ptr_Function_v4float Function
5391 OpStore %local1 %12
5392 OpCopyMemory %global %local1
5393 OpEndPrimitive
5394 OpReturn
5395 OpFunctionEnd
5396 )";
5397
5398 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5399 SinglePassRunAndCheck<AggressiveDCEPass>(test, result, true, true);
5400 }
5401
5402 TEST_F(AggressiveDCETest, StructuredIfWithConditionalExit) {
5403 // We are able to remove "local2" because it is not loaded, but have to keep
5404 // the stores to "local1".
5405 const std::string test =
5406 R"(OpCapability Shader
5407 %1 = OpExtInstImport "GLSL.std.450"
5408 OpMemoryModel Logical GLSL450
5409 OpEntryPoint Fragment %main "main"
5410 OpExecutionMode %main OriginUpperLeft
5411 OpSource GLSL 140
5412 OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
5413 OpSourceExtension "GL_GOOGLE_include_directive"
5414 OpName %main "main"
5415 OpName %a "a"
5416 %void = OpTypeVoid
5417 %5 = OpTypeFunction %void
5418 %int = OpTypeInt 32 1
5419 %_ptr_Uniform_int = OpTypePointer Uniform %int
5420 %int_0 = OpConstant %int 0
5421 %bool = OpTypeBool
5422 %int_100 = OpConstant %int 100
5423 %int_1 = OpConstant %int 1
5424 %a = OpVariable %_ptr_Uniform_int Uniform
5425 %main = OpFunction %void None %5
5426 %12 = OpLabel
5427 %13 = OpLoad %int %a
5428 %14 = OpSGreaterThan %bool %13 %int_0
5429 OpSelectionMerge %15 None
5430 OpBranchConditional %14 %16 %15
5431 %16 = OpLabel
5432 %17 = OpLoad %int %a
5433 %18 = OpSLessThan %bool %17 %int_100
5434 OpBranchConditional %18 %19 %15
5435 %19 = OpLabel
5436 OpStore %a %int_1
5437 OpBranch %15
5438 %15 = OpLabel
5439 OpReturn
5440 OpFunctionEnd
5441 )";
5442
5443 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5444 SinglePassRunAndCheck<AggressiveDCEPass>(test, test, true, true);
5445 }
5446
5447 TEST_F(AggressiveDCETest, CountingLoopNotEliminated) {
5448 // #version 310 es
5449 //
5450 // precision highp float;
5451 // precision highp int;
5452 //
5453 // layout(location = 0) out vec4 _GLF_color;
5454 //
5455 // void main()
5456 // {
5457 // float data[1];
5458 // for (int c = 0; c < 1; c++) {
5459 // if (true) {
5460 // do {
5461 // for (int i = 0; i < 1; i++) {
5462 // data[i] = 1.0;
5463 // }
5464 // } while (false);
5465 // }
5466 // }
5467 // _GLF_color = vec4(data[0], 0.0, 0.0, 1.0);
5468 // }
5469 const std::string test =
5470 R"(OpCapability Shader
5471 %1 = OpExtInstImport "GLSL.std.450"
5472 OpMemoryModel Logical GLSL450
5473 OpEntryPoint Fragment %main "main" %_GLF_color
5474 OpExecutionMode %main OriginUpperLeft
5475 OpSource ESSL 310
5476 OpName %main "main"
5477 OpName %c "c"
5478 OpName %i "i"
5479 OpName %data "data"
5480 OpName %_GLF_color "_GLF_color"
5481 OpDecorate %_GLF_color Location 0
5482 %void = OpTypeVoid
5483 %8 = OpTypeFunction %void
5484 %int = OpTypeInt 32 1
5485 %_ptr_Function_int = OpTypePointer Function %int
5486 %int_0 = OpConstant %int 0
5487 %int_1 = OpConstant %int 1
5488 %bool = OpTypeBool
5489 %float = OpTypeFloat 32
5490 %uint = OpTypeInt 32 0
5491 %uint_1 = OpConstant %uint 1
5492 %_arr_float_uint_1 = OpTypeArray %float %uint_1
5493 %_ptr_Function__arr_float_uint_1 = OpTypePointer Function %_arr_float_uint_1
5494 %float_1 = OpConstant %float 1
5495 %_ptr_Function_float = OpTypePointer Function %float
5496 %false = OpConstantFalse %bool
5497 %v4float = OpTypeVector %float 4
5498 %_ptr_Output_v4float = OpTypePointer Output %v4float
5499 %_GLF_color = OpVariable %_ptr_Output_v4float Output
5500 %float_0 = OpConstant %float 0
5501 %main = OpFunction %void None %8
5502 %26 = OpLabel
5503 %c = OpVariable %_ptr_Function_int Function
5504 %i = OpVariable %_ptr_Function_int Function
5505 %data = OpVariable %_ptr_Function__arr_float_uint_1 Function
5506 OpStore %c %int_0
5507 OpBranch %27
5508 %27 = OpLabel
5509 OpLoopMerge %28 %29 None
5510 OpBranch %30
5511 %30 = OpLabel
5512 %31 = OpLoad %int %c
5513 %32 = OpSLessThan %bool %31 %int_1
5514 OpBranchConditional %32 %33 %28
5515 %33 = OpLabel
5516 OpBranch %34
5517 %34 = OpLabel
5518 OpBranch %35
5519 %35 = OpLabel
5520 OpLoopMerge %36 %37 None
5521 OpBranch %38
5522 %38 = OpLabel
5523 OpStore %i %int_0
5524 OpBranch %39
5525 %39 = OpLabel
5526 OpLoopMerge %40 %41 None
5527 OpBranch %42
5528 %42 = OpLabel
5529 %43 = OpLoad %int %i
5530 %44 = OpSLessThan %bool %43 %int_1
5531 OpBranchConditional %44 %46 %40
5532 %46 = OpLabel
5533 %47 = OpLoad %int %i
5534 %48 = OpAccessChain %_ptr_Function_float %data %47
5535 OpStore %48 %float_1
5536 OpBranch %41
5537 %41 = OpLabel
5538 %49 = OpLoad %int %i
5539 %50 = OpIAdd %int %49 %int_1
5540 OpStore %i %50
5541 OpBranch %39
5542 %40 = OpLabel
5543 OpBranch %37
5544 %37 = OpLabel
5545 OpBranchConditional %false %35 %36
5546 %36 = OpLabel
5547 OpBranch %45
5548 %45 = OpLabel
5549 OpBranch %29
5550 %29 = OpLabel
5551 %51 = OpLoad %int %c
5552 %52 = OpIAdd %int %51 %int_1
5553 OpStore %c %52
5554 OpBranch %27
5555 %28 = OpLabel
5556 %53 = OpAccessChain %_ptr_Function_float %data %int_0
5557 %54 = OpLoad %float %53
5558 %55 = OpCompositeConstruct %v4float %54 %float_0 %float_0 %float_1
5559 OpStore %_GLF_color %55
5560 OpReturn
5561 OpFunctionEnd
5562 )";
5563
5564 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5565 SinglePassRunAndCheck<AggressiveDCEPass>(test, test, true, true);
5566 }
5567
5568 TEST_F(AggressiveDCETest, EliminateLoopWithUnreachable) {
5569 // #version 430
5570 //
5571 // layout(std430) buffer U_t
5572 // {
5573 // float g_F[10];
5574 // float g_S;
5575 // };
5576 //
5577 // layout(location = 0)out float o;
5578 //
5579 // void main(void)
5580 // {
5581 // // Useless loop
5582 // for (int i = 0; i<10; i++) {
5583 // if (g_F[i] == 0.0)
5584 // break;
5585 // else
5586 // break;
5587 // // Unreachable merge block created here.
5588 // // Need to edit SPIR-V to change to OpUnreachable
5589 // }
5590 // o = g_S;
5591 // }
5592
5593 const std::string before =
5594 R"(OpCapability Shader
5595 %1 = OpExtInstImport "GLSL.std.450"
5596 OpMemoryModel Logical GLSL450
5597 OpEntryPoint Fragment %main "main" %o
5598 OpExecutionMode %main OriginUpperLeft
5599 OpSource GLSL 430
5600 OpName %main "main"
5601 OpName %i "i"
5602 OpName %U_t "U_t"
5603 OpMemberName %U_t 0 "g_F"
5604 OpMemberName %U_t 1 "g_S"
5605 OpName %_ ""
5606 OpName %o "o"
5607 OpDecorate %_arr_float_uint_10 ArrayStride 4
5608 OpMemberDecorate %U_t 0 Offset 0
5609 OpMemberDecorate %U_t 1 Offset 40
5610 OpDecorate %U_t BufferBlock
5611 OpDecorate %_ DescriptorSet 0
5612 OpDecorate %o Location 0
5613 %void = OpTypeVoid
5614 %9 = OpTypeFunction %void
5615 %int = OpTypeInt 32 1
5616 %_ptr_Function_int = OpTypePointer Function %int
5617 %int_0 = OpConstant %int 0
5618 %int_10 = OpConstant %int 10
5619 %bool = OpTypeBool
5620 %float = OpTypeFloat 32
5621 %uint = OpTypeInt 32 0
5622 %uint_10 = OpConstant %uint 10
5623 %_arr_float_uint_10 = OpTypeArray %float %uint_10
5624 %U_t = OpTypeStruct %_arr_float_uint_10 %float
5625 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
5626 %_ = OpVariable %_ptr_Uniform_U_t Uniform
5627 %_ptr_Uniform_float = OpTypePointer Uniform %float
5628 %float_0 = OpConstant %float 0
5629 %int_1 = OpConstant %int 1
5630 %_ptr_Output_float = OpTypePointer Output %float
5631 %o = OpVariable %_ptr_Output_float Output
5632 %main = OpFunction %void None %9
5633 %23 = OpLabel
5634 %i = OpVariable %_ptr_Function_int Function
5635 OpStore %i %int_0
5636 OpBranch %24
5637 %24 = OpLabel
5638 OpLoopMerge %25 %26 None
5639 OpBranch %27
5640 %27 = OpLabel
5641 %28 = OpLoad %int %i
5642 %29 = OpSLessThan %bool %28 %int_10
5643 OpBranchConditional %29 %30 %25
5644 %30 = OpLabel
5645 %31 = OpLoad %int %i
5646 %32 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %31
5647 %33 = OpLoad %float %32
5648 %34 = OpFOrdEqual %bool %33 %float_0
5649 OpSelectionMerge %35 None
5650 OpBranchConditional %34 %36 %37
5651 %36 = OpLabel
5652 OpBranch %25
5653 %37 = OpLabel
5654 OpBranch %25
5655 %35 = OpLabel
5656 OpUnreachable
5657 %26 = OpLabel
5658 %38 = OpLoad %int %i
5659 %39 = OpIAdd %int %38 %int_1
5660 OpStore %i %39
5661 OpBranch %24
5662 %25 = OpLabel
5663 %40 = OpAccessChain %_ptr_Uniform_float %_ %int_1
5664 %41 = OpLoad %float %40
5665 OpStore %o %41
5666 OpReturn
5667 OpFunctionEnd
5668 )";
5669
5670 const std::string after =
5671 R"(OpCapability Shader
5672 %1 = OpExtInstImport "GLSL.std.450"
5673 OpMemoryModel Logical GLSL450
5674 OpEntryPoint Fragment %main "main" %o
5675 OpExecutionMode %main OriginUpperLeft
5676 OpSource GLSL 430
5677 OpName %main "main"
5678 OpName %U_t "U_t"
5679 OpMemberName %U_t 0 "g_F"
5680 OpMemberName %U_t 1 "g_S"
5681 OpName %_ ""
5682 OpName %o "o"
5683 OpDecorate %_arr_float_uint_10 ArrayStride 4
5684 OpMemberDecorate %U_t 0 Offset 0
5685 OpMemberDecorate %U_t 1 Offset 40
5686 OpDecorate %U_t BufferBlock
5687 OpDecorate %_ DescriptorSet 0
5688 OpDecorate %o Location 0
5689 %void = OpTypeVoid
5690 %9 = OpTypeFunction %void
5691 %int = OpTypeInt 32 1
5692 %float = OpTypeFloat 32
5693 %uint = OpTypeInt 32 0
5694 %uint_10 = OpConstant %uint 10
5695 %_arr_float_uint_10 = OpTypeArray %float %uint_10
5696 %U_t = OpTypeStruct %_arr_float_uint_10 %float
5697 %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
5698 %_ = OpVariable %_ptr_Uniform_U_t Uniform
5699 %_ptr_Uniform_float = OpTypePointer Uniform %float
5700 %int_1 = OpConstant %int 1
5701 %_ptr_Output_float = OpTypePointer Output %float
5702 %o = OpVariable %_ptr_Output_float Output
5703 %main = OpFunction %void None %9
5704 %23 = OpLabel
5705 OpBranch %24
5706 %24 = OpLabel
5707 OpBranch %25
5708 %25 = OpLabel
5709 %40 = OpAccessChain %_ptr_Uniform_float %_ %int_1
5710 %41 = OpLoad %float %40
5711 OpStore %o %41
5712 OpReturn
5713 OpFunctionEnd
5714 )";
5715
5716 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5717 SinglePassRunAndCheck<AggressiveDCEPass>(before, after, true, true);
5718 }
5719
5720 TEST_F(AggressiveDCETest, DeadHlslCounterBufferGOOGLE) {
5721 // We are able to remove "local2" because it is not loaded, but have to keep
5722 // the stores to "local1".
5723 const std::string test =
5724 R"(
5725 ; CHECK-NOT: OpDecorateId
5726 ; CHECK: [[var:%\w+]] = OpVariable
5727 ; CHECK-NOT: OpVariable
5728 ; CHECK: [[ac:%\w+]] = OpAccessChain {{%\w+}} [[var]]
5729 ; CHECK: OpStore [[ac]]
5730 OpCapability Shader
5731 OpExtension "SPV_GOOGLE_hlsl_functionality1"
5732 OpMemoryModel Logical GLSL450
5733 OpEntryPoint GLCompute %1 "main"
5734 OpExecutionMode %1 LocalSize 32 1 1
5735 OpSource HLSL 600
5736 OpDecorate %_runtimearr_v2float ArrayStride 8
5737 OpMemberDecorate %_struct_3 0 Offset 0
5738 OpDecorate %_struct_3 BufferBlock
5739 OpMemberDecorate %_struct_4 0 Offset 0
5740 OpDecorate %_struct_4 BufferBlock
5741 OpDecorateId %5 HlslCounterBufferGOOGLE %6
5742 OpDecorate %5 DescriptorSet 0
5743 OpDecorate %5 Binding 0
5744 OpDecorate %6 DescriptorSet 0
5745 OpDecorate %6 Binding 1
5746 %float = OpTypeFloat 32
5747 %v2float = OpTypeVector %float 2
5748 %_runtimearr_v2float = OpTypeRuntimeArray %v2float
5749 %_struct_3 = OpTypeStruct %_runtimearr_v2float
5750 %_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
5751 %int = OpTypeInt 32 1
5752 %_struct_4 = OpTypeStruct %int
5753 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
5754 %void = OpTypeVoid
5755 %13 = OpTypeFunction %void
5756 %19 = OpConstantNull %v2float
5757 %int_0 = OpConstant %int 0
5758 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
5759 %5 = OpVariable %_ptr_Uniform__struct_3 Uniform
5760 %6 = OpVariable %_ptr_Uniform__struct_4 Uniform
5761 %1 = OpFunction %void None %13
5762 %22 = OpLabel
5763 %23 = OpAccessChain %_ptr_Uniform_v2float %5 %int_0 %int_0
5764 OpStore %23 %19
5765 OpReturn
5766 OpFunctionEnd
5767 )";
5768
5769 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5770 SinglePassRunAndMatch<AggressiveDCEPass>(test, true);
5771 }
5772
5773 TEST_F(AggressiveDCETest, Dead) {
5774 // We are able to remove "local2" because it is not loaded, but have to keep
5775 // the stores to "local1".
5776 const std::string test =
5777 R"(
5778 ; CHECK: OpCapability
5779 ; CHECK-NOT: OpMemberDecorateStringGOOGLE
5780 ; CHECK: OpFunctionEnd
5781 OpCapability Shader
5782 OpExtension "SPV_GOOGLE_hlsl_functionality1"
5783 %1 = OpExtInstImport "GLSL.std.450"
5784 OpMemoryModel Logical GLSL450
5785 OpEntryPoint Vertex %VSMain "VSMain"
5786 OpSource HLSL 500
5787 OpName %VSMain "VSMain"
5788 OpName %PSInput "PSInput"
5789 OpMemberName %PSInput 0 "Pos"
5790 OpMemberName %PSInput 1 "uv"
5791 OpMemberDecorateStringGOOGLE %PSInput 0 HlslSemanticGOOGLE "SV_POSITION"
5792 OpMemberDecorateStringGOOGLE %PSInput 1 HlslSemanticGOOGLE "TEX_COORD"
5793 %void = OpTypeVoid
5794 %5 = OpTypeFunction %void
5795 %float = OpTypeFloat 32
5796 %v2float = OpTypeVector %float 2
5797 %v4float = OpTypeVector %float 4
5798 %PSInput = OpTypeStruct %v4float %v2float
5799 %VSMain = OpFunction %void None %5
5800 %9 = OpLabel
5801 OpReturn
5802 OpFunctionEnd
5803 )";
5804
5805 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5806 SinglePassRunAndMatch<AggressiveDCEPass>(test, true);
5807 }
5808
5809 TEST_F(AggressiveDCETest, DeadInfiniteLoop) {
5810 const std::string test = R"(
5811 ; CHECK: OpSwitch {{%\w+}} {{%\w+}} {{\w+}} {{%\w+}} {{\w+}} [[block:%\w+]]
5812 ; CHECK: [[block]] = OpLabel
5813 ; CHECK-NEXT: OpBranch [[block:%\w+]]
5814 ; CHECK: [[block]] = OpLabel
5815 ; CHECK-NEXT: OpBranch [[block:%\w+]]
5816 ; CHECK: [[block]] = OpLabel
5817 ; CHECK-NEXT: OpReturn
5818 OpCapability Shader
5819 OpMemoryModel Logical GLSL450
5820 OpEntryPoint Fragment %2 "main"
5821 OpExecutionMode %2 OriginUpperLeft
5822 %6 = OpTypeVoid
5823 %7 = OpTypeFunction %6
5824 %8 = OpTypeFloat 32
5825 %9 = OpTypeVector %8 3
5826 %10 = OpTypeFunction %9
5827 %11 = OpConstant %8 1
5828 %12 = OpConstantComposite %9 %11 %11 %11
5829 %13 = OpTypeInt 32 1
5830 %32 = OpUndef %13
5831 %2 = OpFunction %6 None %7
5832 %33 = OpLabel
5833 OpBranch %34
5834 %34 = OpLabel
5835 OpLoopMerge %35 %36 None
5836 OpBranch %37
5837 %37 = OpLabel
5838 %38 = OpFunctionCall %9 %39
5839 OpSelectionMerge %40 None
5840 OpSwitch %32 %40 14 %41 58 %42
5841 %42 = OpLabel
5842 OpBranch %43
5843 %43 = OpLabel
5844 OpLoopMerge %44 %45 None
5845 OpBranch %45
5846 %45 = OpLabel
5847 OpBranch %43
5848 %44 = OpLabel
5849 OpUnreachable
5850 %41 = OpLabel
5851 OpBranch %36
5852 %40 = OpLabel
5853 OpBranch %36
5854 %36 = OpLabel
5855 OpBranch %34
5856 %35 = OpLabel
5857 OpReturn
5858 OpFunctionEnd
5859 %39 = OpFunction %9 None %10
5860 %46 = OpLabel
5861 OpReturnValue %12
5862 OpFunctionEnd
5863 )";
5864
5865 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5866 SinglePassRunAndMatch<AggressiveDCEPass>(test, true);
5867 }
5868
5869 TEST_F(AggressiveDCETest, DeadInfiniteLoopReturnValue) {
5870 const std::string test = R"(
5871 ; CHECK: [[vec3:%\w+]] = OpTypeVector
5872 ; CHECK: [[undef:%\w+]] = OpUndef [[vec3]]
5873 ; CHECK: OpSwitch {{%\w+}} {{%\w+}} {{\w+}} {{%\w+}} {{\w+}} [[block:%\w+]]
5874 ; CHECK: [[block]] = OpLabel
5875 ; CHECK-NEXT: OpBranch [[block:%\w+]]
5876 ; CHECK: [[block]] = OpLabel
5877 ; CHECK-NEXT: OpBranch [[block:%\w+]]
5878 ; CHECK: [[block]] = OpLabel
5879 ; CHECK-NEXT: OpReturnValue [[undef]]
5880 OpCapability Shader
5881 OpMemoryModel Logical GLSL450
5882 OpEntryPoint Fragment %2 "main"
5883 OpExecutionMode %2 OriginUpperLeft
5884 %6 = OpTypeVoid
5885 %7 = OpTypeFunction %6
5886 %8 = OpTypeFloat 32
5887 %9 = OpTypeVector %8 3
5888 %10 = OpTypeFunction %9
5889 %11 = OpConstant %8 1
5890 %12 = OpConstantComposite %9 %11 %11 %11
5891 %13 = OpTypeInt 32 1
5892 %32 = OpUndef %13
5893 %2 = OpFunction %6 None %7
5894 %entry = OpLabel
5895 %call = OpFunctionCall %9 %func
5896 OpReturn
5897 OpFunctionEnd
5898 %func = OpFunction %9 None %10
5899 %33 = OpLabel
5900 OpBranch %34
5901 %34 = OpLabel
5902 OpLoopMerge %35 %36 None
5903 OpBranch %37
5904 %37 = OpLabel
5905 %38 = OpFunctionCall %9 %39
5906 OpSelectionMerge %40 None
5907 OpSwitch %32 %40 14 %41 58 %42
5908 %42 = OpLabel
5909 OpBranch %43
5910 %43 = OpLabel
5911 OpLoopMerge %44 %45 None
5912 OpBranch %45
5913 %45 = OpLabel
5914 OpBranch %43
5915 %44 = OpLabel
5916 OpUnreachable
5917 %41 = OpLabel
5918 OpBranch %36
5919 %40 = OpLabel
5920 OpBranch %36
5921 %36 = OpLabel
5922 OpBranch %34
5923 %35 = OpLabel
5924 OpReturnValue %12
5925 OpFunctionEnd
5926 %39 = OpFunction %9 None %10
5927 %46 = OpLabel
5928 OpReturnValue %12
5929 OpFunctionEnd
5930 )";
5931
5932 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5933 SinglePassRunAndMatch<AggressiveDCEPass>(test, true);
5934 }
5935
5936 TEST_F(AggressiveDCETest, TestVariablePointer) {
5937 const std::string before =
5938 R"(OpCapability Shader
5939 OpCapability VariablePointers
5940 %1 = OpExtInstImport "GLSL.std.450"
5941 OpMemoryModel Logical GLSL450
5942 OpEntryPoint GLCompute %2 "main"
5943 OpExecutionMode %2 LocalSize 1 1 1
5944 OpSource GLSL 450
5945 OpMemberDecorate %_struct_3 0 Offset 0
5946 OpDecorate %_struct_3 Block
5947 OpDecorate %4 DescriptorSet 0
5948 OpDecorate %4 Binding 0
5949 OpDecorate %_ptr_StorageBuffer_int ArrayStride 4
5950 OpDecorate %_arr_int_int_128 ArrayStride 4
5951 %void = OpTypeVoid
5952 %8 = OpTypeFunction %void
5953 %int = OpTypeInt 32 1
5954 %int_128 = OpConstant %int 128
5955 %_arr_int_int_128 = OpTypeArray %int %int_128
5956 %_struct_3 = OpTypeStruct %_arr_int_int_128
5957 %_ptr_StorageBuffer__struct_3 = OpTypePointer StorageBuffer %_struct_3
5958 %4 = OpVariable %_ptr_StorageBuffer__struct_3 StorageBuffer
5959 %bool = OpTypeBool
5960 %true = OpConstantTrue %bool
5961 %int_0 = OpConstant %int 0
5962 %int_1 = OpConstant %int 1
5963 %_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
5964 %2 = OpFunction %void None %8
5965 %16 = OpLabel
5966 %17 = OpAccessChain %_ptr_StorageBuffer_int %4 %int_0 %int_0
5967 OpBranch %18
5968 %18 = OpLabel
5969 %19 = OpPhi %_ptr_StorageBuffer_int %17 %16 %20 %21
5970 OpLoopMerge %22 %21 None
5971 OpBranchConditional %true %23 %22
5972 %23 = OpLabel
5973 OpStore %19 %int_0
5974 OpBranch %21
5975 %21 = OpLabel
5976 %20 = OpPtrAccessChain %_ptr_StorageBuffer_int %19 %int_1
5977 OpBranch %18
5978 %22 = OpLabel
5979 OpReturn
5980 OpFunctionEnd
5981 )";
5982
5983 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5984 SinglePassRunAndCheck<AggressiveDCEPass>(before, before, true, true);
5985 }
5986
5987 TEST_F(AggressiveDCETest, DeadInputInterfaceV13) {
5988 const std::string spirv = R"(
5989 ; CHECK: OpEntryPoint GLCompute %main "main"
5990 ; CHECK-NOT: OpVariable
5991 OpCapability Shader
5992 OpMemoryModel Logical GLSL450
5993 OpEntryPoint GLCompute %main "main" %dead
5994 OpExecutionMode %main LocalSize 1 1 1
5995 OpName %main "main"
5996 %void = OpTypeVoid
5997 %int = OpTypeInt 32 0
5998 %ptr_input_int = OpTypePointer Input %int
5999 %dead = OpVariable %ptr_input_int Input
6000 %void_fn = OpTypeFunction %void
6001 %main = OpFunction %void None %void_fn
6002 %entry = OpLabel
6003 OpReturn
6004 OpFunctionEnd
6005 )";
6006
6007 SetTargetEnv(SPV_ENV_UNIVERSAL_1_3);
6008 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6009 }
6010
6011 TEST_F(AggressiveDCETest, DeadInputInterfaceV14) {
6012 const std::string spirv = R"(
6013 ; CHECK: OpEntryPoint GLCompute %main "main"
6014 ; CHECK-NOT: OpVariable
6015 OpCapability Shader
6016 OpMemoryModel Logical GLSL450
6017 OpEntryPoint GLCompute %main "main" %dead
6018 OpExecutionMode %main LocalSize 1 1 1
6019 OpName %main "main"
6020 %void = OpTypeVoid
6021 %int = OpTypeInt 32 0
6022 %ptr_input_int = OpTypePointer Input %int
6023 %dead = OpVariable %ptr_input_int Input
6024 %void_fn = OpTypeFunction %void
6025 %main = OpFunction %void None %void_fn
6026 %entry = OpLabel
6027 OpReturn
6028 OpFunctionEnd
6029 )";
6030
6031 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6032 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6033 }
6034
6035 TEST_F(AggressiveDCETest, DeadInterfaceV14) {
6036 const std::string spirv = R"(
6037 ; CHECK-NOT: OpEntryPoint GLCompute %main "main" %
6038 ; CHECK: OpEntryPoint GLCompute %main "main"
6039 ; CHECK-NOT: OpVariable
6040 OpCapability Shader
6041 OpMemoryModel Logical GLSL450
6042 OpEntryPoint GLCompute %main "main" %dead
6043 OpExecutionMode %main LocalSize 1 1 1
6044 OpName %main "main"
6045 %void = OpTypeVoid
6046 %int = OpTypeInt 32 0
6047 %ptr_private_int = OpTypePointer Private %int
6048 %dead = OpVariable %ptr_private_int Private
6049 %void_fn = OpTypeFunction %void
6050 %main = OpFunction %void None %void_fn
6051 %entry = OpLabel
6052 OpReturn
6053 OpFunctionEnd
6054 )";
6055
6056 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6057 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6058 }
6059
6060 TEST_F(AggressiveDCETest, DeadInterfacesV14) {
6061 const std::string spirv = R"(
6062 ; CHECK: OpEntryPoint GLCompute %main "main" %live1 %live2
6063 ; CHECK-NOT: %dead
6064 OpCapability Shader
6065 OpMemoryModel Logical GLSL450
6066 OpEntryPoint GLCompute %main "main" %live1 %dead1 %dead2 %live2
6067 OpExecutionMode %main LocalSize 1 1 1
6068 OpName %main "main"
6069 OpName %live1 "live1"
6070 OpName %live2 "live2"
6071 OpName %dead1 "dead1"
6072 OpName %dead2 "dead2"
6073 %void = OpTypeVoid
6074 %int = OpTypeInt 32 0
6075 %int0 = OpConstant %int 0
6076 %ptr_ssbo_int = OpTypePointer StorageBuffer %int
6077 %live1 = OpVariable %ptr_ssbo_int StorageBuffer
6078 %live2 = OpVariable %ptr_ssbo_int StorageBuffer
6079 %dead1 = OpVariable %ptr_ssbo_int StorageBuffer
6080 %dead2 = OpVariable %ptr_ssbo_int StorageBuffer
6081 %void_fn = OpTypeFunction %void
6082 %main = OpFunction %void None %void_fn
6083 %entry = OpLabel
6084 OpStore %live1 %int0
6085 OpStore %live2 %int0
6086 OpReturn
6087 OpFunctionEnd
6088 )";
6089
6090 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6091 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6092 }
6093
6094 TEST_F(AggressiveDCETest, PreserveBindings) {
6095 const std::string spirv = R"(
6096 ; CHECK: OpDecorate %unusedSampler DescriptorSet 0
6097 ; CHECK: OpDecorate %unusedSampler Binding 0
6098 OpCapability Shader
6099 %1 = OpExtInstImport "GLSL.std.450"
6100 OpMemoryModel Logical GLSL450
6101 OpEntryPoint Fragment %main "main"
6102 OpExecutionMode %main OriginUpperLeft
6103 OpSource GLSL 430
6104 OpName %main "main"
6105 OpName %unusedSampler "unusedSampler"
6106 OpDecorate %unusedSampler DescriptorSet 0
6107 OpDecorate %unusedSampler Binding 0
6108 %void = OpTypeVoid
6109 %5 = OpTypeFunction %void
6110 %float = OpTypeFloat 32
6111 %7 = OpTypeImage %float 2D 0 0 0 1 Unknown
6112 %8 = OpTypeSampledImage %7
6113 %_ptr_UniformConstant_8 = OpTypePointer UniformConstant %8
6114 %unusedSampler = OpVariable %_ptr_UniformConstant_8 UniformConstant
6115 %main = OpFunction %void None %5
6116 %10 = OpLabel
6117 OpReturn
6118 OpFunctionEnd
6119 )";
6120
6121 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6122
6123 OptimizerOptions()->preserve_bindings_ = true;
6124
6125 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6126 }
6127
6128 TEST_F(AggressiveDCETest, PreserveSpecConstants) {
6129 const std::string spirv = R"(
6130 ; CHECK: OpName %specConstant "specConstant"
6131 ; CHECK: %specConstant = OpSpecConstant %int 0
6132 OpCapability Shader
6133 %1 = OpExtInstImport "GLSL.std.450"
6134 OpMemoryModel Logical GLSL450
6135 OpEntryPoint Fragment %main "main"
6136 OpExecutionMode %main OriginUpperLeft
6137 OpSource GLSL 430
6138 OpName %main "main"
6139 OpName %specConstant "specConstant"
6140 OpDecorate %specConstant SpecId 0
6141 %void = OpTypeVoid
6142 %3 = OpTypeFunction %void
6143 %int = OpTypeInt 32 1
6144 %specConstant = OpSpecConstant %int 0
6145 %main = OpFunction %void None %3
6146 %5 = OpLabel
6147 OpReturn
6148 OpFunctionEnd
6149 )";
6150
6151 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6152
6153 OptimizerOptions()->preserve_spec_constants_ = true;
6154
6155 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6156 }
6157
6158 TEST_F(AggressiveDCETest, LiveDecorateId) {
6159 const std::string spirv = R"(OpCapability Shader
6160 OpMemoryModel Logical GLSL450
6161 OpEntryPoint GLCompute %1 "main" %2
6162 OpExecutionMode %1 LocalSize 8 1 1
6163 OpDecorate %2 DescriptorSet 0
6164 OpDecorate %2 Binding 0
6165 OpDecorateId %3 UniformId %uint_2
6166 %void = OpTypeVoid
6167 %uint = OpTypeInt 32 0
6168 %uint_2 = OpConstant %uint 2
6169 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6170 %2 = OpVariable %_ptr_StorageBuffer_uint StorageBuffer
6171 %8 = OpTypeFunction %void
6172 %1 = OpFunction %void None %8
6173 %9 = OpLabel
6174 %3 = OpLoad %uint %2
6175 OpStore %2 %3
6176 OpReturn
6177 OpFunctionEnd
6178 )";
6179
6180 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6181 OptimizerOptions()->preserve_spec_constants_ = true;
6182 SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true);
6183 }
6184
6185 TEST_F(AggressiveDCETest, LiveDecorateIdOnGroup) {
6186 const std::string spirv = R"(OpCapability Shader
6187 OpMemoryModel Logical GLSL450
6188 OpEntryPoint GLCompute %1 "main" %2
6189 OpExecutionMode %1 LocalSize 8 1 1
6190 OpDecorate %2 DescriptorSet 0
6191 OpDecorate %2 Binding 0
6192 OpDecorateId %3 UniformId %uint_2
6193 %3 = OpDecorationGroup
6194 OpGroupDecorate %3 %5
6195 %void = OpTypeVoid
6196 %uint = OpTypeInt 32 0
6197 %uint_2 = OpConstant %uint 2
6198 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6199 %2 = OpVariable %_ptr_StorageBuffer_uint StorageBuffer
6200 %9 = OpTypeFunction %void
6201 %1 = OpFunction %void None %9
6202 %10 = OpLabel
6203 %5 = OpLoad %uint %2
6204 OpStore %2 %5
6205 OpReturn
6206 OpFunctionEnd
6207 )";
6208
6209 SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
6210 OptimizerOptions()->preserve_spec_constants_ = true;
6211 SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true);
6212 }
6213
6214 TEST_F(AggressiveDCETest, NoEliminateForwardPointer) {
6215 // clang-format off
6216 //
6217 // #version 450
6218 // #extension GL_EXT_buffer_reference : enable
6219 //
6220 // // forward reference
6221 // layout(buffer_reference) buffer blockType;
6222 //
6223 // layout(buffer_reference, std430, buffer_reference_align = 16) buffer blockType {
6224 // int x;
6225 // blockType next;
6226 // };
6227 //
6228 // layout(std430) buffer rootBlock {
6229 // blockType root;
6230 // } r;
6231 //
6232 // void main()
6233 // {
6234 // blockType b = r.root;
6235 // b = b.next;
6236 // b.x = 531;
6237 // }
6238 //
6239 // clang-format on
6240
6241 const std::string predefs1 =
6242 R"(OpCapability Shader
6243 OpCapability PhysicalStorageBufferAddresses
6244 OpExtension "SPV_EXT_physical_storage_buffer"
6245 OpExtension "SPV_KHR_storage_buffer_storage_class"
6246 %1 = OpExtInstImport "GLSL.std.450"
6247 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6248 OpEntryPoint GLCompute %main "main"
6249 OpExecutionMode %main LocalSize 1 1 1
6250 OpSource GLSL 450
6251 OpSourceExtension "GL_EXT_buffer_reference"
6252 )";
6253
6254 const std::string names_before =
6255 R"(OpName %main "main"
6256 OpName %blockType "blockType"
6257 OpMemberName %blockType 0 "x"
6258 OpMemberName %blockType 1 "next"
6259 OpName %b "b"
6260 OpName %rootBlock "rootBlock"
6261 OpMemberName %rootBlock 0 "root"
6262 OpName %r "r"
6263 OpMemberDecorate %blockType 0 Offset 0
6264 OpMemberDecorate %blockType 1 Offset 8
6265 OpDecorate %blockType Block
6266 OpDecorate %b AliasedPointer
6267 OpMemberDecorate %rootBlock 0 Offset 0
6268 OpDecorate %rootBlock Block
6269 OpDecorate %r DescriptorSet 0
6270 OpDecorate %r Binding 0
6271 )";
6272
6273 const std::string names_after =
6274 R"(OpName %main "main"
6275 OpName %blockType "blockType"
6276 OpMemberName %blockType 0 "x"
6277 OpMemberName %blockType 1 "next"
6278 OpName %rootBlock "rootBlock"
6279 OpMemberName %rootBlock 0 "root"
6280 OpName %r "r"
6281 OpMemberDecorate %blockType 0 Offset 0
6282 OpMemberDecorate %blockType 1 Offset 8
6283 OpDecorate %blockType Block
6284 OpMemberDecorate %rootBlock 0 Offset 0
6285 OpDecorate %rootBlock Block
6286 OpDecorate %r DescriptorSet 0
6287 OpDecorate %r Binding 0
6288 )";
6289
6290 const std::string predefs2_before =
6291 R"(%void = OpTypeVoid
6292 %3 = OpTypeFunction %void
6293 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_blockType PhysicalStorageBuffer
6294 %int = OpTypeInt 32 1
6295 %blockType = OpTypeStruct %int %_ptr_PhysicalStorageBuffer_blockType
6296 %_ptr_PhysicalStorageBuffer_blockType = OpTypePointer PhysicalStorageBuffer %blockType
6297 %_ptr_Function__ptr_PhysicalStorageBuffer_blockType = OpTypePointer Function %_ptr_PhysicalStorageBuffer_blockType
6298 %rootBlock = OpTypeStruct %_ptr_PhysicalStorageBuffer_blockType
6299 %_ptr_StorageBuffer_rootBlock = OpTypePointer StorageBuffer %rootBlock
6300 %r = OpVariable %_ptr_StorageBuffer_rootBlock StorageBuffer
6301 %int_0 = OpConstant %int 0
6302 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_blockType = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_blockType
6303 %int_1 = OpConstant %int 1
6304 %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_blockType = OpTypePointer PhysicalStorageBuffer %_ptr_PhysicalStorageBuffer_blockType
6305 %int_531 = OpConstant %int 531
6306 %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
6307 )";
6308
6309 const std::string predefs2_after =
6310 R"(%void = OpTypeVoid
6311 %8 = OpTypeFunction %void
6312 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_blockType PhysicalStorageBuffer
6313 %int = OpTypeInt 32 1
6314 %blockType = OpTypeStruct %int %_ptr_PhysicalStorageBuffer_blockType
6315 %_ptr_PhysicalStorageBuffer_blockType = OpTypePointer PhysicalStorageBuffer %blockType
6316 %rootBlock = OpTypeStruct %_ptr_PhysicalStorageBuffer_blockType
6317 %_ptr_StorageBuffer_rootBlock = OpTypePointer StorageBuffer %rootBlock
6318 %r = OpVariable %_ptr_StorageBuffer_rootBlock StorageBuffer
6319 %int_0 = OpConstant %int 0
6320 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_blockType = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_blockType
6321 %int_1 = OpConstant %int 1
6322 %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_blockType = OpTypePointer PhysicalStorageBuffer %_ptr_PhysicalStorageBuffer_blockType
6323 %int_531 = OpConstant %int 531
6324 %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
6325 )";
6326
6327 const std::string func_before =
6328 R"(%main = OpFunction %void None %3
6329 %5 = OpLabel
6330 %b = OpVariable %_ptr_Function__ptr_PhysicalStorageBuffer_blockType Function
6331 %16 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_blockType %r %int_0
6332 %17 = OpLoad %_ptr_PhysicalStorageBuffer_blockType %16
6333 %21 = OpAccessChain %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_blockType %17 %int_1
6334 %22 = OpLoad %_ptr_PhysicalStorageBuffer_blockType %21 Aligned 8
6335 OpStore %b %22
6336 %26 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %22 %int_0
6337 OpStore %26 %int_531 Aligned 16
6338 OpReturn
6339 OpFunctionEnd
6340 )";
6341
6342 const std::string func_after =
6343 R"(%main = OpFunction %void None %8
6344 %19 = OpLabel
6345 %20 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_blockType %r %int_0
6346 %21 = OpLoad %_ptr_PhysicalStorageBuffer_blockType %20
6347 %22 = OpAccessChain %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_blockType %21 %int_1
6348 %23 = OpLoad %_ptr_PhysicalStorageBuffer_blockType %22 Aligned 8
6349 %24 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %23 %int_0
6350 OpStore %24 %int_531 Aligned 16
6351 OpReturn
6352 OpFunctionEnd
6353 )";
6354
6355 SinglePassRunAndCheck<AggressiveDCEPass>(
6356 predefs1 + names_before + predefs2_before + func_before,
6357 predefs1 + names_after + predefs2_after + func_after, true, true);
6358 }
6359
6360 TEST_F(AggressiveDCETest, MultipleFunctionProcessIndependently) {
6361 const std::string spirv = R"(
6362 OpCapability Shader
6363 OpMemoryModel Logical GLSL450
6364 OpEntryPoint GLCompute %entryHistogram "entryHistogram" %gl_GlobalInvocationID %gl_LocalInvocationIndex
6365 OpEntryPoint GLCompute %entryAverage "entryAverage" %gl_GlobalInvocationID %gl_LocalInvocationIndex
6366 OpExecutionMode %entryHistogram LocalSize 16 16 1
6367 OpExecutionMode %entryAverage LocalSize 256 1 1
6368 OpSource HLSL 640
6369 OpName %type_RWStructuredBuffer_uint "type.RWStructuredBuffer.uint"
6370 OpName %uHistogram "uHistogram"
6371 OpName %type_ACSBuffer_counter "type.ACSBuffer.counter"
6372 OpMemberName %type_ACSBuffer_counter 0 "counter"
6373 OpName %counter_var_uHistogram "counter.var.uHistogram"
6374 OpName %sharedHistogram "sharedHistogram"
6375 OpName %entryHistogram "entryHistogram"
6376 OpName %param_var_id "param.var.id"
6377 OpName %param_var_idx "param.var.idx"
6378 OpName %entryAverage "entryAverage"
6379 OpName %param_var_id_0 "param.var.id"
6380 OpName %param_var_idx_0 "param.var.idx"
6381 OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
6382 OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex
6383 OpDecorate %uHistogram DescriptorSet 0
6384 OpDecorate %uHistogram Binding 0
6385 OpDecorate %counter_var_uHistogram DescriptorSet 0
6386 OpDecorate %counter_var_uHistogram Binding 1
6387 OpDecorate %_runtimearr_uint ArrayStride 4
6388 OpMemberDecorate %type_RWStructuredBuffer_uint 0 Offset 0
6389 OpDecorate %type_RWStructuredBuffer_uint BufferBlock
6390 OpMemberDecorate %type_ACSBuffer_counter 0 Offset 0
6391 OpDecorate %type_ACSBuffer_counter BufferBlock
6392 %uint = OpTypeInt 32 0
6393 %uint_0 = OpConstant %uint 0
6394 %uint_1 = OpConstant %uint 1
6395 %uint_2 = OpConstant %uint 2
6396 %uint_4 = OpConstant %uint 4
6397 %uint_8 = OpConstant %uint 8
6398 %uint_16 = OpConstant %uint 16
6399 %uint_32 = OpConstant %uint 32
6400 %uint_64 = OpConstant %uint 64
6401 %uint_128 = OpConstant %uint 128
6402 %uint_256 = OpConstant %uint 256
6403 %uint_512 = OpConstant %uint 512
6404 %uint_254 = OpConstant %uint 254
6405 %uint_255 = OpConstant %uint 255
6406 %int = OpTypeInt 32 1
6407 %int_0 = OpConstant %int 0
6408 %_runtimearr_uint = OpTypeRuntimeArray %uint
6409 %type_RWStructuredBuffer_uint = OpTypeStruct %_runtimearr_uint
6410 %_ptr_Uniform_type_RWStructuredBuffer_uint = OpTypePointer Uniform %type_RWStructuredBuffer_uint
6411 %type_ACSBuffer_counter = OpTypeStruct %int
6412 %_ptr_Uniform_type_ACSBuffer_counter = OpTypePointer Uniform %type_ACSBuffer_counter
6413 %_arr_uint_uint_256 = OpTypeArray %uint %uint_256
6414 %_ptr_Workgroup__arr_uint_uint_256 = OpTypePointer Workgroup %_arr_uint_uint_256
6415 %v3uint = OpTypeVector %uint 3
6416 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
6417 %_ptr_Input_uint = OpTypePointer Input %uint
6418 %void = OpTypeVoid
6419 %49 = OpTypeFunction %void
6420 %_ptr_Function_v3uint = OpTypePointer Function %v3uint
6421 %_ptr_Function_uint = OpTypePointer Function %uint
6422 %52 = OpTypeFunction %void %_ptr_Function_v3uint %_ptr_Function_uint
6423 %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
6424 %uint_264 = OpConstant %uint 264
6425 %bool = OpTypeBool
6426 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
6427 %uHistogram = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_uint Uniform
6428 %counter_var_uHistogram = OpVariable %_ptr_Uniform_type_ACSBuffer_counter Uniform
6429 %sharedHistogram = OpVariable %_ptr_Workgroup__arr_uint_uint_256 Workgroup
6430 %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
6431 %gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input
6432 %entryHistogram = OpFunction %void None %49
6433 %57 = OpLabel
6434 %param_var_id = OpVariable %_ptr_Function_v3uint Function
6435 %param_var_idx = OpVariable %_ptr_Function_uint Function
6436 %58 = OpLoad %v3uint %gl_GlobalInvocationID
6437 %59 = OpLoad %uint %gl_LocalInvocationIndex
6438 %79 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %int_0
6439 %80 = OpAtomicIAdd %uint %79 %uint_1 %uint_0 %uint_1
6440 OpReturn
6441 OpFunctionEnd
6442 %entryAverage = OpFunction %void None %49
6443 %63 = OpLabel
6444 %param_var_id_0 = OpVariable %_ptr_Function_v3uint Function
6445 %param_var_idx_0 = OpVariable %_ptr_Function_uint Function
6446 %64 = OpLoad %v3uint %gl_GlobalInvocationID
6447 %65 = OpLoad %uint %gl_LocalInvocationIndex
6448 OpStore %param_var_idx_0 %65
6449 %83 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %65
6450 OpStore %83 %uint_0
6451
6452 ; CHECK: [[ieq:%\w+]] = OpIEqual
6453 ; CHECK-NEXT: OpSelectionMerge [[merge:%\w+]]
6454 ; CHECK-NEXT: OpBranchConditional [[ieq]] [[not_elim:%\w+]] [[merge]]
6455 ; CHECK-NEXT: [[not_elim]] = OpLabel
6456 ; CHECK: [[merge]] = OpLabel
6457
6458 OpControlBarrier %uint_2 %uint_2 %uint_264
6459 %85 = OpIEqual %bool %65 %uint_0
6460 OpSelectionMerge %89 None
6461 OpBranchConditional %85 %86 %89
6462 %86 = OpLabel
6463 %88 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %65
6464 OpStore %88 %uint_1
6465 OpBranch %89
6466 %89 = OpLabel
6467 OpControlBarrier %uint_2 %uint_2 %uint_264
6468 %91 = OpAccessChain %_ptr_Workgroup_uint %sharedHistogram %65
6469 %92 = OpLoad %uint %91
6470 %94 = OpAccessChain %_ptr_Uniform_uint %uHistogram %int_0 %65
6471 OpStore %94 %92
6472 OpReturn
6473 OpFunctionEnd
6474 )";
6475
6476 SetTargetEnv(SPV_ENV_UNIVERSAL_1_3);
6477
6478 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
6479 }
6480
6481 TEST_F(AggressiveDCETest, DebugInfoKeepInFunctionElimStoreVar) {
6482 // Verify that dead local variable tc and store eliminated but all
6483 // in-function debuginfo kept.
6484 //
6485 // The SPIR-V has been inlined and local single store eliminated
6486 //
6487 // Texture2D g_tColor;
6488 // SamplerState g_sAniso;
6489 //
6490 // struct PS_INPUT {
6491 // float2 vTextureCoords : TEXCOORD2;
6492 // };
6493 //
6494 // struct PS_OUTPUT {
6495 // float4 vColor : SV_Target0;
6496 // };
6497 //
6498 // PS_OUTPUT MainPs(PS_INPUT i) {
6499 // PS_OUTPUT ps_output;
6500 // float2 tc = i.vTextureCoords.xy;
6501 // ps_output.vColor = g_tColor.Sample(g_sAniso, tc);
6502 // return ps_output;
6503 // }
6504
6505 const std::string text = R"(
6506 OpCapability Shader
6507 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
6508 OpMemoryModel Logical GLSL450
6509 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
6510 OpExecutionMode %MainPs OriginUpperLeft
6511 %7 = OpString "foo.frag"
6512 %8 = OpString "PS_OUTPUT"
6513 %9 = OpString "float"
6514 %10 = OpString "vColor"
6515 %11 = OpString "PS_INPUT"
6516 %12 = OpString "vTextureCoords"
6517 %13 = OpString "@type.2d.image"
6518 %14 = OpString "type.2d.image"
6519 %15 = OpString "Texture2D.TemplateParam"
6520 %16 = OpString "src.MainPs"
6521 %17 = OpString "tc"
6522 %18 = OpString "ps_output"
6523 %19 = OpString "i"
6524 %20 = OpString "@type.sampler"
6525 %21 = OpString "type.sampler"
6526 %22 = OpString "g_sAniso"
6527 %23 = OpString "g_tColor"
6528 OpName %type_2d_image "type.2d.image"
6529 OpName %g_tColor "g_tColor"
6530 OpName %type_sampler "type.sampler"
6531 OpName %g_sAniso "g_sAniso"
6532 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
6533 OpName %out_var_SV_Target0 "out.var.SV_Target0"
6534 OpName %MainPs "MainPs"
6535 OpName %PS_INPUT "PS_INPUT"
6536 OpMemberName %PS_INPUT 0 "vTextureCoords"
6537 OpName %param_var_i "param.var.i"
6538 OpName %PS_OUTPUT "PS_OUTPUT"
6539 OpMemberName %PS_OUTPUT 0 "vColor"
6540 OpName %type_sampled_image "type.sampled.image"
6541 OpDecorate %in_var_TEXCOORD2 Location 0
6542 OpDecorate %out_var_SV_Target0 Location 0
6543 OpDecorate %g_tColor DescriptorSet 0
6544 OpDecorate %g_tColor Binding 0
6545 OpDecorate %g_sAniso DescriptorSet 0
6546 OpDecorate %g_sAniso Binding 1
6547 %int = OpTypeInt 32 1
6548 %int_0 = OpConstant %int 0
6549 %uint = OpTypeInt 32 0
6550 %uint_32 = OpConstant %uint 32
6551 %float = OpTypeFloat 32
6552 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
6553 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
6554 %type_sampler = OpTypeSampler
6555 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
6556 %v2float = OpTypeVector %float 2
6557 %_ptr_Input_v2float = OpTypePointer Input %v2float
6558 %v4float = OpTypeVector %float 4
6559 %_ptr_Output_v4float = OpTypePointer Output %v4float
6560 %void = OpTypeVoid
6561 %uint_128 = OpConstant %uint 128
6562 %uint_0 = OpConstant %uint 0
6563 %uint_64 = OpConstant %uint 64
6564 %45 = OpTypeFunction %void
6565 %PS_INPUT = OpTypeStruct %v2float
6566 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
6567 %PS_OUTPUT = OpTypeStruct %v4float
6568 %47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
6569 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
6570 %_ptr_Function_v2float = OpTypePointer Function %v2float
6571 %type_sampled_image = OpTypeSampledImage %type_2d_image
6572 %_ptr_Function_v4float = OpTypePointer Function %v4float
6573 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
6574 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
6575 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
6576 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
6577 %51 = OpExtInst %void %1 DebugInfoNone
6578 %52 = OpExtInst %void %1 DebugExpression
6579 %53 = OpExtInst %void %1 DebugOperation Deref
6580 %54 = OpExtInst %void %1 DebugExpression %53
6581 %55 = OpExtInst %void %1 DebugSource %7
6582 %56 = OpExtInst %void %1 DebugCompilationUnit 1 4 %55 HLSL
6583 %57 = OpExtInst %void %1 DebugTypeComposite %8 Structure %55 10 1 %56 %8 %uint_128 FlagIsProtected|FlagIsPrivate %58
6584 %59 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 Float
6585 %60 = OpExtInst %void %1 DebugTypeVector %59 4
6586 %58 = OpExtInst %void %1 DebugTypeMember %10 %60 %55 12 5 %57 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
6587 %61 = OpExtInst %void %1 DebugTypeComposite %11 Structure %55 5 1 %56 %11 %uint_64 FlagIsProtected|FlagIsPrivate %62
6588 %63 = OpExtInst %void %1 DebugTypeVector %59 2
6589 %62 = OpExtInst %void %1 DebugTypeMember %12 %63 %55 7 5 %61 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
6590 %64 = OpExtInst %void %1 DebugTypeComposite %13 Class %55 0 0 %56 %14 %51 FlagIsProtected|FlagIsPrivate
6591 %65 = OpExtInst %void %1 DebugTypeTemplateParameter %15 %59 %51 %55 0 0
6592 %66 = OpExtInst %void %1 DebugTypeTemplate %64 %65
6593 %67 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %57 %61
6594 %68 = OpExtInst %void %1 DebugFunction %16 %67 %55 15 1 %56 %16 FlagIsProtected|FlagIsPrivate 16 %51
6595 %69 = OpExtInst %void %1 DebugLexicalBlock %55 16 1 %68
6596 %70 = OpExtInst %void %1 DebugLocalVariable %17 %63 %55 19 12 %69 FlagIsLocal
6597 %71 = OpExtInst %void %1 DebugLocalVariable %18 %57 %55 17 15 %69 FlagIsLocal
6598 %72 = OpExtInst %void %1 DebugLocalVariable %19 %61 %55 15 29 %68 FlagIsLocal 1
6599 %73 = OpExtInst %void %1 DebugTypeComposite %20 Structure %55 0 0 %56 %21 %51 FlagIsProtected|FlagIsPrivate
6600 %74 = OpExtInst %void %1 DebugGlobalVariable %22 %73 %55 3 14 %56 %22 %g_sAniso FlagIsDefinition
6601 %75 = OpExtInst %void %1 DebugGlobalVariable %23 %64 %55 1 11 %56 %23 %g_tColor FlagIsDefinition
6602 %MainPs = OpFunction %void None %45
6603 %76 = OpLabel
6604 %107 = OpExtInst %void %1 DebugScope %69
6605 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugScope %69
6606 %78 = OpVariable %_ptr_Function_PS_OUTPUT Function
6607 %79 = OpVariable %_ptr_Function_v2float Function
6608 %108 = OpExtInst %void %1 DebugNoScope
6609 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugNoScope
6610 %81 = OpVariable %_ptr_Function_PS_OUTPUT Function
6611 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
6612 %82 = OpLoad %v2float %in_var_TEXCOORD2
6613 %83 = OpCompositeConstruct %PS_INPUT %82
6614 OpStore %param_var_i %83
6615 %109 = OpExtInst %void %1 DebugScope %68
6616 %85 = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52
6617 %110 = OpExtInst %void %1 DebugScope %69
6618 %87 = OpExtInst %void %1 DebugDeclare %71 %78 %52
6619 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugScope %68
6620 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52
6621 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugScope %69
6622 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugDeclare %71 %78 %52
6623 OpLine %7 19 17
6624 %88 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
6625 %89 = OpLoad %v2float %88
6626 OpLine %7 19 12
6627 OpStore %79 %89
6628 ;CHECK-NOT: OpStore %79 %89
6629 OpLine %7 19 12
6630 %106 = OpExtInst %void %1 DebugValue %70 %89 %52
6631 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugValue %70 %89 %52
6632 OpLine %7 20 26
6633 %91 = OpLoad %type_2d_image %g_tColor
6634 OpLine %7 20 46
6635 %92 = OpLoad %type_sampler %g_sAniso
6636 OpLine %7 20 26
6637 %94 = OpSampledImage %type_sampled_image %91 %92
6638 %95 = OpImageSampleImplicitLod %v4float %94 %89 None
6639 OpLine %7 20 5
6640 %96 = OpAccessChain %_ptr_Function_v4float %78 %int_0
6641 OpStore %96 %95
6642 OpLine %7 21 12
6643 %97 = OpLoad %PS_OUTPUT %78
6644 OpLine %7 21 5
6645 OpStore %81 %97
6646 %111 = OpExtInst %void %1 DebugNoScope
6647 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugNoScope
6648 %100 = OpCompositeExtract %v4float %97 0
6649 OpStore %out_var_SV_Target0 %100
6650 OpReturn
6651 OpFunctionEnd
6652 )";
6653
6654 SetTargetEnv(SPV_ENV_VULKAN_1_2);
6655 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6656 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
6657 }
6658
6659 TEST_F(AggressiveDCETest, ShaderDebugInfoKeepInFunctionElimStoreVar) {
6660 // Verify that dead local variable tc and store eliminated but all
6661 // in-function NonSemantic Shader debuginfo kept.
6662
6663 const std::string text = R"(
6664 OpCapability Shader
6665 OpExtension "SPV_KHR_non_semantic_info"
6666 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
6667 OpMemoryModel Logical GLSL450
6668 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
6669 OpExecutionMode %MainPs OriginUpperLeft
6670 %7 = OpString "foo.frag"
6671 %8 = OpString "PS_OUTPUT"
6672 %9 = OpString "float"
6673 %10 = OpString "vColor"
6674 %11 = OpString "PS_INPUT"
6675 %12 = OpString "vTextureCoords"
6676 %13 = OpString "@type.2d.image"
6677 %14 = OpString "type.2d.image"
6678 %15 = OpString "Texture2D.TemplateParam"
6679 %16 = OpString "src.MainPs"
6680 %17 = OpString "tc"
6681 %18 = OpString "ps_output"
6682 %19 = OpString "i"
6683 %20 = OpString "@type.sampler"
6684 %21 = OpString "type.sampler"
6685 %22 = OpString "g_sAniso"
6686 %23 = OpString "g_tColor"
6687 OpName %type_2d_image "type.2d.image"
6688 OpName %g_tColor "g_tColor"
6689 OpName %type_sampler "type.sampler"
6690 OpName %g_sAniso "g_sAniso"
6691 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
6692 OpName %out_var_SV_Target0 "out.var.SV_Target0"
6693 OpName %MainPs "MainPs"
6694 OpName %PS_INPUT "PS_INPUT"
6695 OpMemberName %PS_INPUT 0 "vTextureCoords"
6696 OpName %param_var_i "param.var.i"
6697 OpName %PS_OUTPUT "PS_OUTPUT"
6698 OpMemberName %PS_OUTPUT 0 "vColor"
6699 OpName %type_sampled_image "type.sampled.image"
6700 OpDecorate %in_var_TEXCOORD2 Location 0
6701 OpDecorate %out_var_SV_Target0 Location 0
6702 OpDecorate %g_tColor DescriptorSet 0
6703 OpDecorate %g_tColor Binding 0
6704 OpDecorate %g_sAniso DescriptorSet 0
6705 OpDecorate %g_sAniso Binding 1
6706 %int = OpTypeInt 32 1
6707 %int_0 = OpConstant %int 0
6708 %uint = OpTypeInt 32 0
6709 %uint_32 = OpConstant %uint 32
6710 %float = OpTypeFloat 32
6711 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
6712 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
6713 %type_sampler = OpTypeSampler
6714 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
6715 %v2float = OpTypeVector %float 2
6716 %_ptr_Input_v2float = OpTypePointer Input %v2float
6717 %v4float = OpTypeVector %float 4
6718 %_ptr_Output_v4float = OpTypePointer Output %v4float
6719 %void = OpTypeVoid
6720 %uint_128 = OpConstant %uint 128
6721 %uint_0 = OpConstant %uint 0
6722 %uint_1 = OpConstant %uint 1
6723 %uint_2 = OpConstant %uint 2
6724 %uint_3 = OpConstant %uint 3
6725 %uint_4 = OpConstant %uint 4
6726 %uint_5 = OpConstant %uint 5
6727 %uint_7 = OpConstant %uint 7
6728 %uint_8 = OpConstant %uint 8
6729 %uint_10 = OpConstant %uint 10
6730 %uint_11 = OpConstant %uint 11
6731 %uint_12 = OpConstant %uint 12
6732 %uint_14 = OpConstant %uint 14
6733 %uint_15 = OpConstant %uint 15
6734 %uint_16 = OpConstant %uint 16
6735 %uint_17 = OpConstant %uint 17
6736 %uint_19 = OpConstant %uint 19
6737 %uint_20 = OpConstant %uint 20
6738 %uint_21 = OpConstant %uint 21
6739 %uint_25 = OpConstant %uint 25
6740 %uint_29 = OpConstant %uint 29
6741 %uint_30 = OpConstant %uint 30
6742 %uint_35 = OpConstant %uint 35
6743 %uint_41 = OpConstant %uint 41
6744 %uint_48 = OpConstant %uint 48
6745 %uint_53 = OpConstant %uint 53
6746 %uint_64 = OpConstant %uint 64
6747 %45 = OpTypeFunction %void
6748 %PS_INPUT = OpTypeStruct %v2float
6749 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
6750 %PS_OUTPUT = OpTypeStruct %v4float
6751 %47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
6752 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
6753 %_ptr_Function_v2float = OpTypePointer Function %v2float
6754 %type_sampled_image = OpTypeSampledImage %type_2d_image
6755 %_ptr_Function_v4float = OpTypePointer Function %v4float
6756 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
6757 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
6758 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
6759 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
6760 %75 = OpExtInst %void %1 DebugInfoNone
6761 %76 = OpExtInst %void %1 DebugExpression
6762 %77 = OpExtInst %void %1 DebugOperation %uint_0
6763 %78 = OpExtInst %void %1 DebugExpression %77
6764 %79 = OpExtInst %void %1 DebugSource %7
6765 %80 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %79 %uint_5
6766 %81 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 %uint_3 %uint_0
6767 %82 = OpExtInst %void %1 DebugTypeVector %81 %uint_4
6768 %83 = OpExtInst %void %1 DebugTypeMember %10 %82 %79 %uint_12 %uint_5 %uint_0 %uint_128 %uint_3
6769 %84 = OpExtInst %void %1 DebugTypeComposite %8 %uint_1 %79 %uint_10 %uint_1 %80 %8 %uint_128 %uint_3 %83
6770 %85 = OpExtInst %void %1 DebugTypeVector %81 %uint_2
6771 %86 = OpExtInst %void %1 DebugTypeMember %12 %85 %79 %uint_7 %uint_5 %uint_0 %uint_64 %uint_3
6772 %87 = OpExtInst %void %1 DebugTypeComposite %11 %uint_1 %79 %uint_5 %uint_1 %80 %11 %uint_64 %uint_3 %86
6773 %88 = OpExtInst %void %1 DebugTypeComposite %13 %uint_0 %79 %uint_0 %uint_0 %80 %14 %75 %uint_3
6774 %89 = OpExtInst %void %1 DebugTypeFunction %uint_3 %84 %87
6775 %90 = OpExtInst %void %1 DebugFunction %16 %89 %79 %uint_15 %uint_1 %80 %16 %uint_3 %uint_16
6776 %91 = OpExtInst %void %1 DebugLexicalBlock %79 %uint_16 %uint_1 %90
6777 %92 = OpExtInst %void %1 DebugLocalVariable %17 %85 %79 %uint_19 %uint_12 %91 %uint_4
6778 %93 = OpExtInst %void %1 DebugLocalVariable %18 %84 %79 %uint_17 %uint_15 %91 %uint_4
6779 %94 = OpExtInst %void %1 DebugLocalVariable %19 %87 %79 %uint_15 %uint_29 %90 %uint_4 %uint_1
6780 %95 = OpExtInst %void %1 DebugTypeComposite %20 %uint_1 %79 %uint_0 %uint_0 %80 %21 %75 %uint_3
6781 %96 = OpExtInst %void %1 DebugGlobalVariable %22 %95 %79 %uint_3 %uint_14 %80 %22 %g_sAniso %uint_8
6782 %97 = OpExtInst %void %1 DebugGlobalVariable %23 %88 %79 %uint_1 %uint_11 %80 %23 %g_tColor %uint_8
6783 %MainPs = OpFunction %void None %45
6784 %98 = OpLabel
6785 %99 = OpVariable %_ptr_Function_PS_OUTPUT Function
6786 %100 = OpVariable %_ptr_Function_v2float Function
6787 %101 = OpVariable %_ptr_Function_PS_OUTPUT Function
6788 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
6789 %102 = OpLoad %v2float %in_var_TEXCOORD2
6790 %103 = OpCompositeConstruct %PS_INPUT %102
6791 OpStore %param_var_i %103
6792 %104 = OpExtInst %void %1 DebugFunctionDefinition %90 %MainPs
6793 %142 = OpExtInst %void %1 DebugScope %90
6794 %106 = OpExtInst %void %1 DebugDeclare %94 %param_var_i %76
6795 %143 = OpExtInst %void %1 DebugScope %91
6796 %108 = OpExtInst %void %1 DebugDeclare %93 %99 %76
6797 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugFunctionDefinition %90 %MainPs
6798 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugScope %90
6799 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugDeclare %94 %param_var_i %76
6800 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugScope %91
6801 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugDeclare %93 %99 %76
6802 %109 = OpExtInst %void %1 DebugLine %79 %uint_19 %uint_19 %uint_17 %uint_30
6803 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugLine %79 %uint_19 %uint_19 %uint_17 %uint_30
6804 %110 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
6805 %111 = OpLoad %v2float %110
6806 %112 = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_25 %uint_32
6807 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_25 %uint_32
6808 OpStore %100 %111
6809 ;CHECK-NOT: OpStore %100 %111
6810 %114 = OpExtInst %void %1 DebugValue %92 %111 %76
6811 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugValue %92 %111 %76
6812 %115 = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_25 %uint_32
6813 %116 = OpLoad %type_2d_image %g_tColor
6814 %117 = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_41 %uint_48
6815 %118 = OpLoad %type_sampler %g_sAniso
6816 %119 = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_25 %uint_53
6817 %120 = OpSampledImage %type_sampled_image %116 %118
6818 %121 = OpImageSampleImplicitLod %v4float %120 %111 None
6819 %122 = OpExtInst %void %1 DebugLine %79 %uint_20 %uint_20 %uint_5 %uint_53
6820 %123 = OpAccessChain %_ptr_Function_v4float %99 %int_0
6821 OpStore %123 %121
6822 %124 = OpExtInst %void %1 DebugLine %79 %uint_21 %uint_21 %uint_12 %uint_20
6823 %125 = OpLoad %PS_OUTPUT %99
6824 %126 = OpExtInst %void %1 DebugLine %79 %uint_21 %uint_21 %uint_5 %uint_20
6825 OpStore %101 %125
6826 %127 = OpExtInst %void %1 DebugNoLine
6827 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugNoLine
6828 %144 = OpExtInst %void %1 DebugNoScope
6829 ;CHECK: {{%\w+}} = OpExtInst %void %1 DebugNoScope
6830 %129 = OpCompositeExtract %v4float %125 0
6831 OpStore %out_var_SV_Target0 %129
6832 OpReturn
6833 OpFunctionEnd
6834 )";
6835
6836 SetTargetEnv(SPV_ENV_VULKAN_1_2);
6837 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6838 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
6839 }
6840
6841 TEST_F(AggressiveDCETest, ShaderDebugInfoGlobalDCE) {
6842 // Verify that DebugGlobalVariable for eliminated private variable has
6843 // variable operand replaced with DebugInfoNone.
6844
6845 const std::string text = R"(OpCapability Shader
6846 OpExtension "SPV_KHR_non_semantic_info"
6847 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
6848 OpMemoryModel Logical GLSL450
6849 OpEntryPoint Fragment %MainPs "MainPs" %out_var_SV_Target0 %a
6850 OpExecutionMode %MainPs OriginUpperLeft
6851 %5 = OpString "source2.hlsl"
6852 %24 = OpString "float"
6853 %29 = OpString "vColor"
6854 %33 = OpString "PS_OUTPUT"
6855 %37 = OpString "MainPs"
6856 %38 = OpString ""
6857 %42 = OpString "ps_output"
6858 %46 = OpString "a"
6859 OpName %a "a"
6860 OpName %out_var_SV_Target0 "out.var.SV_Target0"
6861 OpName %MainPs "MainPs"
6862 OpName %PS_OUTPUT "PS_OUTPUT"
6863 OpMemberName %PS_OUTPUT 0 "vColor"
6864 OpDecorate %out_var_SV_Target0 Location 0
6865 %float = OpTypeFloat 32
6866 %v4float = OpTypeVector %float 4
6867 %8 = OpConstantNull %v4float
6868 %float_0 = OpConstant %float 0
6869 %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
6870 %int = OpTypeInt 32 1
6871 %int_0 = OpConstant %int 0
6872 %uint = OpTypeInt 32 0
6873 %uint_32 = OpConstant %uint 32
6874 %_ptr_Private_v4float = OpTypePointer Private %v4float
6875 %_ptr_Output_v4float = OpTypePointer Output %v4float
6876 %void = OpTypeVoid
6877 %uint_1 = OpConstant %uint 1
6878 %uint_4 = OpConstant %uint 4
6879 %uint_5 = OpConstant %uint 5
6880 %uint_3 = OpConstant %uint 3
6881 %uint_0 = OpConstant %uint 0
6882 %uint_128 = OpConstant %uint 128
6883 %uint_12 = OpConstant %uint 12
6884 %uint_8 = OpConstant %uint 8
6885 %uint_9 = OpConstant %uint 9
6886 %uint_10 = OpConstant %uint 10
6887 %uint_15 = OpConstant %uint 15
6888 %48 = OpTypeFunction %void
6889 %PS_OUTPUT = OpTypeStruct %v4float
6890 %54 = OpTypeFunction %PS_OUTPUT
6891 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
6892 %_ptr_Function_v4float = OpTypePointer Function %v4float
6893 %a = OpVariable %_ptr_Private_v4float Private
6894 ;CHECK-NOT: %a = OpVariable %_ptr_Private_v4float Private
6895 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
6896 ;CHECK: [[dbg_none:%\w+]] = OpExtInst %void %1 DebugInfoNone
6897 %18 = OpExtInst %void %1 DebugExpression
6898 %19 = OpExtInst %void %1 DebugSource %5
6899 %20 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %19 %uint_5
6900 %25 = OpExtInst %void %1 DebugTypeBasic %24 %uint_32 %uint_3 %uint_0
6901 %28 = OpExtInst %void %1 DebugTypeVector %25 %uint_4
6902 %31 = OpExtInst %void %1 DebugTypeMember %29 %28 %19 %uint_5 %uint_12 %uint_0 %uint_128 %uint_3
6903 %34 = OpExtInst %void %1 DebugTypeComposite %33 %uint_1 %19 %uint_3 %uint_8 %20 %33 %uint_128 %uint_3 %31
6904 %36 = OpExtInst %void %1 DebugTypeFunction %uint_3 %34
6905 %39 = OpExtInst %void %1 DebugFunction %37 %36 %19 %uint_8 %uint_1 %20 %38 %uint_3 %uint_9
6906 %41 = OpExtInst %void %1 DebugLexicalBlock %19 %uint_9 %uint_1 %39
6907 %43 = OpExtInst %void %1 DebugLocalVariable %42 %34 %19 %uint_10 %uint_15 %41 %uint_4
6908 %47 = OpExtInst %void %1 DebugGlobalVariable %46 %28 %19 %uint_1 %uint_15 %20 %46 %a %uint_8
6909 ;CHECK: %47 = OpExtInst %void %1 DebugGlobalVariable %46 %28 %19 %uint_1 %uint_15 %20 %46 [[dbg_none]] %uint_8
6910 %MainPs = OpFunction %void None %48
6911 %49 = OpLabel
6912 %65 = OpVariable %_ptr_Function_PS_OUTPUT Function
6913 %66 = OpVariable %_ptr_Function_PS_OUTPUT Function
6914 OpStore %a %8
6915 %72 = OpExtInst %void %1 DebugScope %41
6916 %69 = OpExtInst %void %1 DebugDeclare %43 %65 %18
6917 OpLine %5 11 5
6918 %70 = OpAccessChain %_ptr_Function_v4float %65 %int_0
6919 OpStore %70 %10
6920 OpLine %5 12 12
6921 %71 = OpLoad %PS_OUTPUT %65
6922 OpLine %5 12 5
6923 OpStore %66 %71
6924 %73 = OpExtInst %void %1 DebugNoLine
6925 %74 = OpExtInst %void %1 DebugNoScope
6926 %51 = OpLoad %PS_OUTPUT %66
6927 %53 = OpCompositeExtract %v4float %51 0
6928 OpStore %out_var_SV_Target0 %53
6929 OpLine %5 13 1
6930 OpReturn
6931 OpFunctionEnd
6932 )";
6933
6934 SetTargetEnv(SPV_ENV_VULKAN_1_2);
6935 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6936 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
6937 }
6938
6939 TEST_F(AggressiveDCETest, DebugInfoDeclareKeepsStore) {
6940 // Verify that local variable tc and its store are kept by DebugDeclare.
6941 //
6942 // Same shader source as DebugInfoInFunctionKeepStoreVarElim. The SPIR-V
6943 // has just been inlined.
6944
6945 const std::string text = R"(
6946 OpCapability Shader
6947 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
6948 OpMemoryModel Logical GLSL450
6949 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
6950 OpExecutionMode %MainPs OriginUpperLeft
6951 %20 = OpString "foo.frag"
6952 %24 = OpString "PS_OUTPUT"
6953 %28 = OpString "float"
6954 %31 = OpString "vColor"
6955 %33 = OpString "PS_INPUT"
6956 %38 = OpString "vTextureCoords"
6957 %40 = OpString "@type.2d.image"
6958 %41 = OpString "type.2d.image"
6959 %43 = OpString "Texture2D.TemplateParam"
6960 %47 = OpString "src.MainPs"
6961 %51 = OpString "tc"
6962 %53 = OpString "ps_output"
6963 %56 = OpString "i"
6964 %58 = OpString "@type.sampler"
6965 %59 = OpString "type.sampler"
6966 %61 = OpString "g_sAniso"
6967 %63 = OpString "g_tColor"
6968 OpName %type_2d_image "type.2d.image"
6969 OpName %g_tColor "g_tColor"
6970 OpName %type_sampler "type.sampler"
6971 OpName %g_sAniso "g_sAniso"
6972 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
6973 OpName %out_var_SV_Target0 "out.var.SV_Target0"
6974 OpName %MainPs "MainPs"
6975 OpName %PS_INPUT "PS_INPUT"
6976 OpMemberName %PS_INPUT 0 "vTextureCoords"
6977 OpName %param_var_i "param.var.i"
6978 OpName %PS_OUTPUT "PS_OUTPUT"
6979 OpMemberName %PS_OUTPUT 0 "vColor"
6980 OpName %type_sampled_image "type.sampled.image"
6981 OpDecorate %in_var_TEXCOORD2 Location 0
6982 OpDecorate %out_var_SV_Target0 Location 0
6983 OpDecorate %g_tColor DescriptorSet 0
6984 OpDecorate %g_tColor Binding 0
6985 OpDecorate %g_sAniso DescriptorSet 0
6986 OpDecorate %g_sAniso Binding 1
6987 %int = OpTypeInt 32 1
6988 %int_0 = OpConstant %int 0
6989 %uint = OpTypeInt 32 0
6990 %uint_32 = OpConstant %uint 32
6991 %float = OpTypeFloat 32
6992 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
6993 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
6994 %type_sampler = OpTypeSampler
6995 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
6996 %v2float = OpTypeVector %float 2
6997 %_ptr_Input_v2float = OpTypePointer Input %v2float
6998 %v4float = OpTypeVector %float 4
6999 %_ptr_Output_v4float = OpTypePointer Output %v4float
7000 %void = OpTypeVoid
7001 %uint_128 = OpConstant %uint 128
7002 %uint_0 = OpConstant %uint 0
7003 %uint_64 = OpConstant %uint 64
7004 %65 = OpTypeFunction %void
7005 %PS_INPUT = OpTypeStruct %v2float
7006 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
7007 %PS_OUTPUT = OpTypeStruct %v4float
7008 %75 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
7009 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
7010 %_ptr_Function_v2float = OpTypePointer Function %v2float
7011 %type_sampled_image = OpTypeSampledImage %type_2d_image
7012 %_ptr_Function_v4float = OpTypePointer Function %v4float
7013 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
7014 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
7015 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
7016 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
7017 %39 = OpExtInst %void %1 DebugInfoNone
7018 %55 = OpExtInst %void %1 DebugExpression
7019 %22 = OpExtInst %void %1 DebugSource %20
7020 %23 = OpExtInst %void %1 DebugCompilationUnit 1 4 %22 HLSL
7021 %26 = OpExtInst %void %1 DebugTypeComposite %24 Structure %22 10 1 %23 %24 %uint_128 FlagIsProtected|FlagIsPrivate %27
7022 %29 = OpExtInst %void %1 DebugTypeBasic %28 %uint_32 Float
7023 %30 = OpExtInst %void %1 DebugTypeVector %29 4
7024 %27 = OpExtInst %void %1 DebugTypeMember %31 %30 %22 12 5 %26 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
7025 %35 = OpExtInst %void %1 DebugTypeComposite %33 Structure %22 5 1 %23 %33 %uint_64 FlagIsProtected|FlagIsPrivate %36
7026 %37 = OpExtInst %void %1 DebugTypeVector %29 2
7027 %36 = OpExtInst %void %1 DebugTypeMember %38 %37 %22 7 5 %35 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
7028 %42 = OpExtInst %void %1 DebugTypeComposite %40 Class %22 0 0 %23 %41 %39 FlagIsProtected|FlagIsPrivate
7029 %44 = OpExtInst %void %1 DebugTypeTemplateParameter %43 %29 %39 %22 0 0
7030 %45 = OpExtInst %void %1 DebugTypeTemplate %42 %44
7031 %46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %26 %35
7032 %48 = OpExtInst %void %1 DebugFunction %47 %46 %22 15 1 %23 %47 FlagIsProtected|FlagIsPrivate 16 %39
7033 %50 = OpExtInst %void %1 DebugLexicalBlock %22 16 1 %48
7034 %52 = OpExtInst %void %1 DebugLocalVariable %51 %37 %22 19 12 %50 FlagIsLocal
7035 %54 = OpExtInst %void %1 DebugLocalVariable %53 %26 %22 17 15 %50 FlagIsLocal
7036 %57 = OpExtInst %void %1 DebugLocalVariable %56 %35 %22 15 29 %48 FlagIsLocal 1
7037 %60 = OpExtInst %void %1 DebugTypeComposite %58 Structure %22 0 0 %23 %59 %39 FlagIsProtected|FlagIsPrivate
7038 %62 = OpExtInst %void %1 DebugGlobalVariable %61 %60 %22 3 14 %23 %61 %g_sAniso FlagIsDefinition
7039 %64 = OpExtInst %void %1 DebugGlobalVariable %63 %42 %22 1 11 %23 %63 %g_tColor FlagIsDefinition
7040 %MainPs = OpFunction %void None %65
7041 %66 = OpLabel
7042 %114 = OpExtInst %void %1 DebugScope %50
7043 %98 = OpVariable %_ptr_Function_PS_OUTPUT Function
7044 %99 = OpVariable %_ptr_Function_v2float Function
7045 %115 = OpExtInst %void %1 DebugNoScope
7046 %100 = OpVariable %_ptr_Function_PS_OUTPUT Function
7047 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
7048 %70 = OpLoad %v2float %in_var_TEXCOORD2
7049 %71 = OpCompositeConstruct %PS_INPUT %70
7050 OpStore %param_var_i %71
7051 %116 = OpExtInst %void %1 DebugScope %48
7052 %102 = OpExtInst %void %1 DebugDeclare %57 %param_var_i %55
7053 %117 = OpExtInst %void %1 DebugScope %50
7054 %103 = OpExtInst %void %1 DebugDeclare %54 %98 %55
7055 OpLine %20 19 17
7056 %104 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
7057 %105 = OpLoad %v2float %104
7058 OpLine %20 19 12
7059 OpStore %99 %105
7060 ;CHECK: OpStore %99 %105
7061 %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55
7062 OpLine %20 20 26
7063 %107 = OpLoad %type_2d_image %g_tColor
7064 OpLine %20 20 46
7065 %108 = OpLoad %type_sampler %g_sAniso
7066 OpLine %20 20 26
7067 %110 = OpSampledImage %type_sampled_image %107 %108
7068 %111 = OpImageSampleImplicitLod %v4float %110 %105 None
7069 OpLine %20 20 5
7070 %112 = OpAccessChain %_ptr_Function_v4float %98 %int_0
7071 OpStore %112 %111
7072 OpLine %20 21 12
7073 %113 = OpLoad %PS_OUTPUT %98
7074 OpLine %20 21 5
7075 OpStore %100 %113
7076 %118 = OpExtInst %void %1 DebugNoScope
7077 %73 = OpLoad %PS_OUTPUT %100
7078 %74 = OpCompositeExtract %v4float %73 0
7079 OpStore %out_var_SV_Target0 %74
7080 OpReturn
7081 OpFunctionEnd
7082 )";
7083
7084 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7085 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7086 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7087 }
7088
7089 TEST_F(AggressiveDCETest, DebugInfoValueDerefKeepsStore) {
7090 // Verify that local variable tc and its store are kept by DebugValue with
7091 // Deref.
7092 //
7093 // Same shader source as DebugInfoInFunctionKeepStoreVarElim. The SPIR-V
7094 // has just been inlined and edited to replace the DebugDeclare with the
7095 // DebugValue/Deref.
7096
7097 const std::string text = R"(
7098 OpCapability Shader
7099 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
7100 OpMemoryModel Logical GLSL450
7101 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
7102 OpExecutionMode %MainPs OriginUpperLeft
7103 %7 = OpString "foo.frag"
7104 %8 = OpString "PS_OUTPUT"
7105 %9 = OpString "float"
7106 %10 = OpString "vColor"
7107 %11 = OpString "PS_INPUT"
7108 %12 = OpString "vTextureCoords"
7109 %13 = OpString "@type.2d.image"
7110 %14 = OpString "type.2d.image"
7111 %15 = OpString "Texture2D.TemplateParam"
7112 %16 = OpString "src.MainPs"
7113 %17 = OpString "tc"
7114 %18 = OpString "ps_output"
7115 %19 = OpString "i"
7116 %20 = OpString "@type.sampler"
7117 %21 = OpString "type.sampler"
7118 %22 = OpString "g_sAniso"
7119 %23 = OpString "g_tColor"
7120 OpName %type_2d_image "type.2d.image"
7121 OpName %g_tColor "g_tColor"
7122 OpName %type_sampler "type.sampler"
7123 OpName %g_sAniso "g_sAniso"
7124 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
7125 OpName %out_var_SV_Target0 "out.var.SV_Target0"
7126 OpName %MainPs "MainPs"
7127 OpName %PS_INPUT "PS_INPUT"
7128 OpMemberName %PS_INPUT 0 "vTextureCoords"
7129 OpName %param_var_i "param.var.i"
7130 OpName %PS_OUTPUT "PS_OUTPUT"
7131 OpMemberName %PS_OUTPUT 0 "vColor"
7132 OpName %type_sampled_image "type.sampled.image"
7133 OpDecorate %in_var_TEXCOORD2 Location 0
7134 OpDecorate %out_var_SV_Target0 Location 0
7135 OpDecorate %g_tColor DescriptorSet 0
7136 OpDecorate %g_tColor Binding 0
7137 OpDecorate %g_sAniso DescriptorSet 0
7138 OpDecorate %g_sAniso Binding 1
7139 %int = OpTypeInt 32 1
7140 %int_0 = OpConstant %int 0
7141 %uint = OpTypeInt 32 0
7142 %uint_32 = OpConstant %uint 32
7143 %float = OpTypeFloat 32
7144 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
7145 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
7146 %type_sampler = OpTypeSampler
7147 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
7148 %v2float = OpTypeVector %float 2
7149 %_ptr_Input_v2float = OpTypePointer Input %v2float
7150 %v4float = OpTypeVector %float 4
7151 %_ptr_Output_v4float = OpTypePointer Output %v4float
7152 %void = OpTypeVoid
7153 %uint_128 = OpConstant %uint 128
7154 %uint_0 = OpConstant %uint 0
7155 %uint_64 = OpConstant %uint 64
7156 %45 = OpTypeFunction %void
7157 %PS_INPUT = OpTypeStruct %v2float
7158 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
7159 %PS_OUTPUT = OpTypeStruct %v4float
7160 %47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
7161 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
7162 %_ptr_Function_v2float = OpTypePointer Function %v2float
7163 %type_sampled_image = OpTypeSampledImage %type_2d_image
7164 %_ptr_Function_v4float = OpTypePointer Function %v4float
7165 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
7166 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
7167 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
7168 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
7169 %51 = OpExtInst %void %1 DebugInfoNone
7170 %52 = OpExtInst %void %1 DebugExpression
7171 %53 = OpExtInst %void %1 DebugOperation Deref
7172 %54 = OpExtInst %void %1 DebugExpression %53
7173 %55 = OpExtInst %void %1 DebugSource %7
7174 %56 = OpExtInst %void %1 DebugCompilationUnit 1 4 %55 HLSL
7175 %57 = OpExtInst %void %1 DebugTypeComposite %8 Structure %55 10 1 %56 %8 %uint_128 FlagIsProtected|FlagIsPrivate %58
7176 %59 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 Float
7177 %60 = OpExtInst %void %1 DebugTypeVector %59 4
7178 %58 = OpExtInst %void %1 DebugTypeMember %10 %60 %55 12 5 %57 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
7179 %61 = OpExtInst %void %1 DebugTypeComposite %11 Structure %55 5 1 %56 %11 %uint_64 FlagIsProtected|FlagIsPrivate %62
7180 %63 = OpExtInst %void %1 DebugTypeVector %59 2
7181 %62 = OpExtInst %void %1 DebugTypeMember %12 %63 %55 7 5 %61 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
7182 %64 = OpExtInst %void %1 DebugTypeComposite %13 Class %55 0 0 %56 %14 %51 FlagIsProtected|FlagIsPrivate
7183 %65 = OpExtInst %void %1 DebugTypeTemplateParameter %15 %59 %51 %55 0 0
7184 %66 = OpExtInst %void %1 DebugTypeTemplate %64 %65
7185 %67 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %57 %61
7186 %68 = OpExtInst %void %1 DebugFunction %16 %67 %55 15 1 %56 %16 FlagIsProtected|FlagIsPrivate 16 %51
7187 %69 = OpExtInst %void %1 DebugLexicalBlock %55 16 1 %68
7188 %70 = OpExtInst %void %1 DebugLocalVariable %17 %63 %55 19 12 %69 FlagIsLocal
7189 %71 = OpExtInst %void %1 DebugLocalVariable %18 %57 %55 17 15 %69 FlagIsLocal
7190 %72 = OpExtInst %void %1 DebugLocalVariable %19 %61 %55 15 29 %68 FlagIsLocal 1
7191 %73 = OpExtInst %void %1 DebugTypeComposite %20 Structure %55 0 0 %56 %21 %51 FlagIsProtected|FlagIsPrivate
7192 %74 = OpExtInst %void %1 DebugGlobalVariable %22 %73 %55 3 14 %56 %22 %g_sAniso FlagIsDefinition
7193 %75 = OpExtInst %void %1 DebugGlobalVariable %23 %64 %55 1 11 %56 %23 %g_tColor FlagIsDefinition
7194 %MainPs = OpFunction %void None %45
7195 %76 = OpLabel
7196 %101 = OpExtInst %void %1 DebugScope %69
7197 %78 = OpVariable %_ptr_Function_PS_OUTPUT Function
7198 %79 = OpVariable %_ptr_Function_v2float Function
7199 %102 = OpExtInst %void %1 DebugNoScope
7200 %81 = OpVariable %_ptr_Function_PS_OUTPUT Function
7201 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
7202 %82 = OpLoad %v2float %in_var_TEXCOORD2
7203 %83 = OpCompositeConstruct %PS_INPUT %82
7204 OpStore %param_var_i %83
7205 %103 = OpExtInst %void %1 DebugScope %68
7206 %85 = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52
7207 %104 = OpExtInst %void %1 DebugScope %69
7208 %87 = OpExtInst %void %1 DebugDeclare %71 %78 %52
7209 OpLine %7 19 17
7210 %88 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
7211 %89 = OpLoad %v2float %88
7212 OpLine %7 19 12
7213 OpStore %79 %89
7214 ;CHECK: OpStore %79 %89
7215 %90 = OpExtInst %void %1 DebugValue %70 %79 %54
7216 OpLine %7 20 26
7217 %91 = OpLoad %type_2d_image %g_tColor
7218 OpLine %7 20 46
7219 %92 = OpLoad %type_sampler %g_sAniso
7220 OpLine %7 20 26
7221 %94 = OpSampledImage %type_sampled_image %91 %92
7222 %95 = OpImageSampleImplicitLod %v4float %94 %89 None
7223 OpLine %7 20 5
7224 %96 = OpAccessChain %_ptr_Function_v4float %78 %int_0
7225 OpStore %96 %95
7226 OpLine %7 21 12
7227 %97 = OpLoad %PS_OUTPUT %78
7228 OpLine %7 21 5
7229 OpStore %81 %97
7230 %105 = OpExtInst %void %1 DebugNoScope
7231 %99 = OpLoad %PS_OUTPUT %81
7232 %100 = OpCompositeExtract %v4float %99 0
7233 OpStore %out_var_SV_Target0 %100
7234 OpReturn
7235 OpFunctionEnd
7236 )";
7237
7238 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7239 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7240 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7241 }
7242
7243 TEST_F(AggressiveDCETest, DebugInfoElimUnusedTextureKeepGlobalVariable) {
7244 // Verify that unused texture g_tColor2 is eliminated but its
7245 // DebugGlobalVariable is retained but with DebugInfoNone for its Variable.
7246 //
7247 // Same shader source as DebugInfoInFunctionKeepStoreVarElim but with unused
7248 // g_tColor2 added.
7249
7250 const std::string text = R"(
7251 OpCapability Shader
7252 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
7253 OpMemoryModel Logical GLSL450
7254 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_tColor2 %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
7255 OpExecutionMode %MainPs OriginUpperLeft
7256 %21 = OpString "foo6.frag"
7257 %25 = OpString "PS_OUTPUT"
7258 %29 = OpString "float"
7259 %32 = OpString "vColor"
7260 %34 = OpString "PS_INPUT"
7261 %39 = OpString "vTextureCoords"
7262 %41 = OpString "@type.2d.image"
7263 %42 = OpString "type.2d.image"
7264 %44 = OpString "Texture2D.TemplateParam"
7265 %48 = OpString "src.MainPs"
7266 %52 = OpString "tc"
7267 %54 = OpString "ps_output"
7268 %57 = OpString "i"
7269 %59 = OpString "@type.sampler"
7270 %60 = OpString "type.sampler"
7271 %62 = OpString "g_sAniso"
7272 %64 = OpString "g_tColor2"
7273 %66 = OpString "g_tColor"
7274 OpName %type_2d_image "type.2d.image"
7275 OpName %g_tColor "g_tColor"
7276 OpName %g_tColor2 "g_tColor2"
7277 OpName %type_sampler "type.sampler"
7278 OpName %g_sAniso "g_sAniso"
7279 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
7280 OpName %out_var_SV_Target0 "out.var.SV_Target0"
7281 OpName %MainPs "MainPs"
7282 OpName %PS_INPUT "PS_INPUT"
7283 OpMemberName %PS_INPUT 0 "vTextureCoords"
7284 OpName %param_var_i "param.var.i"
7285 OpName %PS_OUTPUT "PS_OUTPUT"
7286 OpMemberName %PS_OUTPUT 0 "vColor"
7287 OpName %type_sampled_image "type.sampled.image"
7288 OpDecorate %in_var_TEXCOORD2 Location 0
7289 OpDecorate %out_var_SV_Target0 Location 0
7290 OpDecorate %g_tColor DescriptorSet 0
7291 OpDecorate %g_tColor Binding 0
7292 OpDecorate %g_tColor2 DescriptorSet 0
7293 OpDecorate %g_tColor2 Binding 1
7294 OpDecorate %g_sAniso DescriptorSet 0
7295 OpDecorate %g_sAniso Binding 2
7296 %int = OpTypeInt 32 1
7297 %int_0 = OpConstant %int 0
7298 %uint = OpTypeInt 32 0
7299 %uint_32 = OpConstant %uint 32
7300 %float = OpTypeFloat 32
7301 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
7302 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
7303 %type_sampler = OpTypeSampler
7304 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
7305 %v2float = OpTypeVector %float 2
7306 %_ptr_Input_v2float = OpTypePointer Input %v2float
7307 %v4float = OpTypeVector %float 4
7308 %_ptr_Output_v4float = OpTypePointer Output %v4float
7309 %void = OpTypeVoid
7310 %uint_128 = OpConstant %uint 128
7311 %uint_0 = OpConstant %uint 0
7312 %uint_64 = OpConstant %uint 64
7313 %68 = OpTypeFunction %void
7314 %PS_INPUT = OpTypeStruct %v2float
7315 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
7316 %PS_OUTPUT = OpTypeStruct %v4float
7317 %78 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
7318 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
7319 %_ptr_Function_v2float = OpTypePointer Function %v2float
7320 %type_sampled_image = OpTypeSampledImage %type_2d_image
7321 %_ptr_Function_v4float = OpTypePointer Function %v4float
7322 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
7323 %g_tColor2 = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
7324 ;CHECK-NOT: %g_tColor2 = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
7325 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
7326 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
7327 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
7328 %40 = OpExtInst %void %1 DebugInfoNone
7329 %56 = OpExtInst %void %1 DebugExpression
7330 %23 = OpExtInst %void %1 DebugSource %21
7331 %24 = OpExtInst %void %1 DebugCompilationUnit 1 4 %23 HLSL
7332 %27 = OpExtInst %void %1 DebugTypeComposite %25 Structure %23 11 1 %24 %25 %uint_128 FlagIsProtected|FlagIsPrivate %28
7333 %30 = OpExtInst %void %1 DebugTypeBasic %29 %uint_32 Float
7334 %31 = OpExtInst %void %1 DebugTypeVector %30 4
7335 %28 = OpExtInst %void %1 DebugTypeMember %32 %31 %23 13 5 %27 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
7336 %36 = OpExtInst %void %1 DebugTypeComposite %34 Structure %23 6 1 %24 %34 %uint_64 FlagIsProtected|FlagIsPrivate %37
7337 %38 = OpExtInst %void %1 DebugTypeVector %30 2
7338 %37 = OpExtInst %void %1 DebugTypeMember %39 %38 %23 8 5 %36 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
7339 %43 = OpExtInst %void %1 DebugTypeComposite %41 Class %23 0 0 %24 %42 %40 FlagIsProtected|FlagIsPrivate
7340 %45 = OpExtInst %void %1 DebugTypeTemplateParameter %44 %30 %40 %23 0 0
7341 %46 = OpExtInst %void %1 DebugTypeTemplate %43 %45
7342 %47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %27 %36
7343 %49 = OpExtInst %void %1 DebugFunction %48 %47 %23 16 1 %24 %48 FlagIsProtected|FlagIsPrivate 17 %40
7344 %51 = OpExtInst %void %1 DebugLexicalBlock %23 17 1 %49
7345 %53 = OpExtInst %void %1 DebugLocalVariable %52 %38 %23 20 12 %51 FlagIsLocal
7346 %55 = OpExtInst %void %1 DebugLocalVariable %54 %27 %23 18 15 %51 FlagIsLocal
7347 %58 = OpExtInst %void %1 DebugLocalVariable %57 %36 %23 16 29 %49 FlagIsLocal 1
7348 %61 = OpExtInst %void %1 DebugTypeComposite %59 Structure %23 0 0 %24 %60 %40 FlagIsProtected|FlagIsPrivate
7349 %63 = OpExtInst %void %1 DebugGlobalVariable %62 %61 %23 4 14 %24 %62 %g_sAniso FlagIsDefinition
7350 %65 = OpExtInst %void %1 DebugGlobalVariable %64 %43 %23 2 11 %24 %64 %g_tColor2 FlagIsDefinition
7351 ;CHECK-NOT: %65 = OpExtInst %void %1 DebugGlobalVariable %64 %43 %23 2 11 %24 %64 %g_tColor2 FlagIsDefinition
7352 ;CHECK: %65 = OpExtInst %void %1 DebugGlobalVariable %64 %43 %23 2 11 %24 %64 %40 FlagIsDefinition
7353 %67 = OpExtInst %void %1 DebugGlobalVariable %66 %43 %23 1 11 %24 %66 %g_tColor FlagIsDefinition
7354 %MainPs = OpFunction %void None %68
7355 %69 = OpLabel
7356 %117 = OpExtInst %void %1 DebugScope %51
7357 %101 = OpVariable %_ptr_Function_PS_OUTPUT Function
7358 %102 = OpVariable %_ptr_Function_v2float Function
7359 %118 = OpExtInst %void %1 DebugNoScope
7360 %103 = OpVariable %_ptr_Function_PS_OUTPUT Function
7361 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
7362 %73 = OpLoad %v2float %in_var_TEXCOORD2
7363 %74 = OpCompositeConstruct %PS_INPUT %73
7364 OpStore %param_var_i %74
7365 %119 = OpExtInst %void %1 DebugScope %49
7366 %105 = OpExtInst %void %1 DebugDeclare %58 %param_var_i %56
7367 %120 = OpExtInst %void %1 DebugScope %51
7368 %106 = OpExtInst %void %1 DebugDeclare %55 %101 %56
7369 OpLine %21 20 17
7370 %107 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
7371 %108 = OpLoad %v2float %107
7372 OpLine %21 20 12
7373 OpStore %102 %108
7374 %109 = OpExtInst %void %1 DebugDeclare %53 %102 %56
7375 OpLine %21 21 26
7376 %110 = OpLoad %type_2d_image %g_tColor
7377 OpLine %21 21 46
7378 %111 = OpLoad %type_sampler %g_sAniso
7379 OpLine %21 21 57
7380 %112 = OpLoad %v2float %102
7381 OpLine %21 21 26
7382 %113 = OpSampledImage %type_sampled_image %110 %111
7383 %114 = OpImageSampleImplicitLod %v4float %113 %112 None
7384 OpLine %21 21 5
7385 %115 = OpAccessChain %_ptr_Function_v4float %101 %int_0
7386 OpStore %115 %114
7387 OpLine %21 22 12
7388 %116 = OpLoad %PS_OUTPUT %101
7389 OpLine %21 22 5
7390 OpStore %103 %116
7391 %121 = OpExtInst %void %1 DebugNoScope
7392 %76 = OpLoad %PS_OUTPUT %103
7393 %77 = OpCompositeExtract %v4float %76 0
7394 OpStore %out_var_SV_Target0 %77
7395 OpReturn
7396 OpFunctionEnd
7397 )";
7398
7399 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7400 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7401 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7402 }
7403
7404 TEST_F(AggressiveDCETest, KeepDebugScopeParent) {
7405 // Verify that local variable tc and its store are kept by DebugDeclare.
7406 //
7407 // Same shader source as DebugInfoInFunctionKeepStoreVarElim. The SPIR-V
7408 // has just been inlined.
7409
7410 const std::string text = R"(
7411 OpCapability Shader
7412 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
7413 OpMemoryModel Logical GLSL450
7414 OpEntryPoint Fragment %main "main" %out_var_SV_TARGET0
7415 OpExecutionMode %main OriginUpperLeft
7416 %11 = OpString "float"
7417 %16 = OpString "t.hlsl"
7418 %19 = OpString "src.main"
7419 OpName %out_var_SV_TARGET0 "out.var.SV_TARGET0"
7420 OpName %main "main"
7421 OpDecorate %out_var_SV_TARGET0 Location 0
7422 %float = OpTypeFloat 32
7423 %float_0 = OpConstant %float 0
7424 %v4float = OpTypeVector %float 4
7425 %7 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
7426 %uint = OpTypeInt 32 0
7427 %uint_32 = OpConstant %uint 32
7428 %_ptr_Output_v4float = OpTypePointer Output %v4float
7429 %void = OpTypeVoid
7430 %23 = OpTypeFunction %void
7431 %26 = OpTypeFunction %v4float
7432 %out_var_SV_TARGET0 = OpVariable %_ptr_Output_v4float Output
7433 %_ptr_Function_v4float = OpTypePointer Function %v4float
7434 %33 = OpExtInst %void %1 DebugInfoNone
7435 %13 = OpExtInst %void %1 DebugTypeBasic %11 %uint_32 Float
7436 %14 = OpExtInst %void %1 DebugTypeVector %13 4
7437 %15 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %14
7438 %17 = OpExtInst %void %1 DebugSource %16
7439 %18 = OpExtInst %void %1 DebugCompilationUnit 1 4 %17 HLSL
7440 %20 = OpExtInst %void %1 DebugFunction %19 %15 %17 1 1 %18 %19 FlagIsProtected|FlagIsPrivate 2 %33
7441 %22 = OpExtInst %void %1 DebugLexicalBlock %17 2 1 %20
7442 %main = OpFunction %void None %23
7443 %24 = OpLabel
7444 %31 = OpVariable %_ptr_Function_v4float Function
7445 ; CHECK: [[block:%\w+]] = OpExtInst %void %1 DebugLexicalBlock
7446 ; CHECK: DebugScope [[block]]
7447 %34 = OpExtInst %void %1 DebugScope %22
7448 OpLine %16 3 5
7449 OpStore %31 %7
7450 OpStore %out_var_SV_TARGET0 %7
7451 %35 = OpExtInst %void %1 DebugNoScope
7452 OpReturn
7453 OpFunctionEnd
7454 )";
7455
7456 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7457 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7458 }
7459
7460 TEST_F(AggressiveDCETest, KeepExportFunctions) {
7461 // All functions are reachable. In particular, ExportedFunc and Constant are
7462 // reachable because ExportedFunc is exported. Nothing should be removed.
7463 const std::vector<const char*> text = {
7464 // clang-format off
7465 "OpCapability Shader",
7466 "OpCapability Linkage",
7467 "OpMemoryModel Logical GLSL450",
7468 "OpEntryPoint Fragment %main \"main\"",
7469 "OpName %main \"main\"",
7470 "OpName %ExportedFunc \"ExportedFunc\"",
7471 "OpName %Live \"Live\"",
7472 "OpDecorate %ExportedFunc LinkageAttributes \"ExportedFunc\" Export",
7473 "%void = OpTypeVoid",
7474 "%7 = OpTypeFunction %void",
7475 "%main = OpFunction %void None %7",
7476 "%15 = OpLabel",
7477 "OpReturn",
7478 "OpFunctionEnd",
7479 "%ExportedFunc = OpFunction %void None %7",
7480 "%19 = OpLabel",
7481 "%16 = OpFunctionCall %void %Live",
7482 "OpReturn",
7483 "OpFunctionEnd",
7484 "%Live = OpFunction %void None %7",
7485 "%20 = OpLabel",
7486 "OpReturn",
7487 "OpFunctionEnd"
7488 // clang-format on
7489 };
7490
7491 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7492 std::string assembly = JoinAllInsts(text);
7493 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
7494 assembly, /* skip_nop = */ true, /* do_validation = */ false);
7495 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
7496 EXPECT_EQ(assembly, std::get<0>(result));
7497 }
7498
7499 TEST_F(AggressiveDCETest, KeepPrivateVarInExportFunctions) {
7500 // The loads and stores from the private variable should not be removed
7501 // because the functions are exported and could be called.
7502 const std::string text = R"(OpCapability Shader
7503 OpCapability Linkage
7504 OpMemoryModel Logical GLSL450
7505 OpSource HLSL 630
7506 OpName %privateVar "privateVar"
7507 OpName %ReadPrivate "ReadPrivate"
7508 OpName %WritePrivate "WritePrivate"
7509 OpName %value "value"
7510 OpDecorate %ReadPrivate LinkageAttributes "ReadPrivate" Export
7511 OpDecorate %WritePrivate LinkageAttributes "WritePrivate" Export
7512 %int = OpTypeInt 32 1
7513 %_ptr_Private_int = OpTypePointer Private %int
7514 %6 = OpTypeFunction %int
7515 %void = OpTypeVoid
7516 %_ptr_Function_int = OpTypePointer Function %int
7517 %10 = OpTypeFunction %void %_ptr_Function_int
7518 %privateVar = OpVariable %_ptr_Private_int Private
7519 %ReadPrivate = OpFunction %int None %6
7520 %12 = OpLabel
7521 %8 = OpLoad %int %privateVar
7522 OpReturnValue %8
7523 OpFunctionEnd
7524 %WritePrivate = OpFunction %void None %10
7525 %value = OpFunctionParameter %_ptr_Function_int
7526 %13 = OpLabel
7527 %14 = OpLoad %int %value
7528 OpStore %privateVar %14
7529 OpReturn
7530 OpFunctionEnd
7531 )";
7532
7533 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7534 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
7535 text, /* skip_nop = */ true, /* do_validation = */ false);
7536 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
7537 EXPECT_EQ(text, std::get<0>(result));
7538 }
7539
7540 TEST_F(AggressiveDCETest, KeepLableNames) {
7541 const std::string text = R"(OpCapability Shader
7542 OpCapability Linkage
7543 OpMemoryModel Logical GLSL450
7544 OpSource HLSL 630
7545 OpName %WritePrivate "WritePrivate"
7546 OpName %entry "entry"
7547 OpName %target "target"
7548 OpDecorate %WritePrivate LinkageAttributes "WritePrivate" Export
7549 %void = OpTypeVoid
7550 %3 = OpTypeFunction %void
7551 %WritePrivate = OpFunction %void None %3
7552 %entry = OpLabel
7553 OpBranch %target
7554 %target = OpLabel
7555 OpReturn
7556 OpFunctionEnd
7557 )";
7558
7559 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7560 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
7561 text, /* skip_nop = */ true, /* do_validation = */ false);
7562 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
7563 EXPECT_EQ(text, std::get<0>(result));
7564 }
7565
7566 TEST_F(AggressiveDCETest, PreserveInterface) {
7567 // Set preserve_interface to true. Verify that unused uniform
7568 // constant in entry point interface is not eliminated.
7569 const std::string text = R"(OpCapability RayTracingKHR
7570 OpExtension "SPV_KHR_ray_tracing"
7571 %1 = OpExtInstImport "GLSL.std.450"
7572 OpMemoryModel Logical GLSL450
7573 OpEntryPoint RayGenerationKHR %2 "main" %3 %4
7574 OpDecorate %3 Location 0
7575 OpDecorate %4 DescriptorSet 2
7576 OpDecorate %4 Binding 0
7577 %void = OpTypeVoid
7578 %6 = OpTypeFunction %void
7579 %uint = OpTypeInt 32 0
7580 %uint_0 = OpConstant %uint 0
7581 %float = OpTypeFloat 32
7582 %_ptr_CallableDataKHR_float = OpTypePointer CallableDataKHR %float
7583 %3 = OpVariable %_ptr_CallableDataKHR_float CallableDataKHR
7584 %13 = OpTypeAccelerationStructureKHR
7585 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
7586 %4 = OpVariable %_ptr_UniformConstant_13 UniformConstant
7587 %2 = OpFunction %void None %6
7588 %15 = OpLabel
7589 OpExecuteCallableKHR %uint_0 %3
7590 OpReturn
7591 OpFunctionEnd
7592 )";
7593
7594 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7595 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7596 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
7597 text, /* skip_nop = */ true, /* do_validation = */ false,
7598 /* preserve_interface */ true);
7599 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
7600 EXPECT_EQ(text, std::get<0>(result));
7601 }
7602
7603 TEST_F(AggressiveDCETest, EmptyContinueWithConditionalBranch) {
7604 const std::string text = R"(OpCapability Shader
7605 %1 = OpExtInstImport "GLSL.std.450"
7606 OpMemoryModel Logical GLSL450
7607 OpEntryPoint Fragment %2 "main"
7608 OpExecutionMode %2 OriginUpperLeft
7609 %void = OpTypeVoid
7610 %4 = OpTypeFunction %void
7611 %bool = OpTypeBool
7612 %false = OpConstantFalse %bool
7613 %2 = OpFunction %void None %4
7614 %9 = OpLabel
7615 OpBranch %10
7616 %10 = OpLabel
7617 OpLoopMerge %11 %12 None
7618 OpBranch %13
7619 %13 = OpLabel
7620 OpKill
7621 %12 = OpLabel
7622 OpBranchConditional %false %10 %10
7623 %11 = OpLabel
7624 OpUnreachable
7625 OpFunctionEnd
7626 )";
7627
7628 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7629 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7630 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, false);
7631 }
7632
7633 TEST_F(AggressiveDCETest, FunctionBecomesUnreachableAfterDCE) {
7634 const std::string text = R"(
7635 OpCapability Shader
7636 %1 = OpExtInstImport "GLSL.std.450"
7637 OpMemoryModel Logical GLSL450
7638 OpEntryPoint Fragment %2 "main"
7639 OpExecutionMode %2 OriginUpperLeft
7640 OpSource ESSL 320
7641 %void = OpTypeVoid
7642 %4 = OpTypeFunction %void
7643 %int = OpTypeInt 32 1
7644 %_ptr_Function_int = OpTypePointer Function %int
7645 %7 = OpTypeFunction %int
7646 %int_n1 = OpConstant %int -1
7647 %2 = OpFunction %void None %4
7648 %9 = OpLabel
7649 OpKill
7650 %10 = OpLabel
7651 %11 = OpFunctionCall %int %12
7652 OpReturn
7653 OpFunctionEnd
7654 ; CHECK: {{%\w+}} = OpFunction %int DontInline|Pure
7655 %12 = OpFunction %int DontInline|Pure %7
7656 ; CHECK-NEXT: {{%\w+}} = OpLabel
7657 %13 = OpLabel
7658 %14 = OpVariable %_ptr_Function_int Function
7659 ; CHECK-NEXT: OpBranch [[header:%\w+]]
7660 OpBranch %15
7661 ; CHECK-NEXT: [[header]] = OpLabel
7662 ; CHECK-NEXT: OpBranch [[merge:%\w+]]
7663 %15 = OpLabel
7664 OpLoopMerge %16 %17 None
7665 OpBranch %18
7666 %18 = OpLabel
7667 %19 = OpLoad %int %14
7668 OpBranch %17
7669 %17 = OpLabel
7670 OpBranch %15
7671 ; CHECK-NEXT: [[merge]] = OpLabel
7672 %16 = OpLabel
7673 ; CHECK-NEXT: OpReturnValue %int_n1
7674 OpReturnValue %int_n1
7675 ; CHECK-NEXT: OpFunctionEnd
7676 OpFunctionEnd
7677 )";
7678
7679 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7680 }
7681
7682 TEST_F(AggressiveDCETest, KeepTopLevelDebugInfo) {
7683 // Don't eliminate DebugCompilationUnit, DebugSourceContinued, and
7684 // DebugEntryPoint
7685 const std::string text = R"(
7686 OpCapability Shader
7687 OpExtension "SPV_KHR_non_semantic_info"
7688 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
7689 OpMemoryModel Logical GLSL450
7690 OpEntryPoint Fragment %MainPs "MainPs" %out_var_SV_Target0
7691 OpExecutionMode %MainPs OriginUpperLeft
7692 %4 = OpString "foo2.frag"
7693 %5 = OpString "
7694 struct PS_OUTPUT
7695 {
7696 float4 vColor : SV_Target0 ;
7697 } ;
7698
7699 "
7700 %6 = OpString "
7701 PS_OUTPUT MainPs ( )
7702 {
7703 PS_OUTPUT ps_output ;
7704 ps_output . vColor = float4( 1.0, 0.0, 0.0, 0.0 );
7705 return ps_output ;
7706 }
7707
7708 "
7709 %7 = OpString "float"
7710 %8 = OpString "vColor"
7711 %9 = OpString "PS_OUTPUT"
7712 %10 = OpString "MainPs"
7713 %11 = OpString ""
7714 %12 = OpString "ps_output"
7715 %13 = OpString "97a939fb"
7716 %14 = OpString " foo2.frag -E MainPs -T ps_6_1 -spirv -fspv-target-env=vulkan1.2 -fspv-debug=vulkan-with-source -fcgl -Fo foo2.frag.nopt.spv -Qembed_debug"
7717 OpName %out_var_SV_Target0 "out.var.SV_Target0"
7718 OpName %MainPs "MainPs"
7719 OpDecorate %out_var_SV_Target0 Location 0
7720 %float = OpTypeFloat 32
7721 %float_1 = OpConstant %float 1
7722 %float_0 = OpConstant %float 0
7723 %v4float = OpTypeVector %float 4
7724 %23 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_0
7725 %int = OpTypeInt 32 1
7726 %int_0 = OpConstant %int 0
7727 %uint = OpTypeInt 32 0
7728 %uint_32 = OpConstant %uint 32
7729 %_ptr_Output_v4float = OpTypePointer Output %v4float
7730 %void = OpTypeVoid
7731 %uint_1 = OpConstant %uint 1
7732 %uint_4 = OpConstant %uint 4
7733 %uint_5 = OpConstant %uint 5
7734 %uint_3 = OpConstant %uint 3
7735 %uint_0 = OpConstant %uint 0
7736 %uint_128 = OpConstant %uint 128
7737 %uint_12 = OpConstant %uint 12
7738 %uint_2 = OpConstant %uint 2
7739 %uint_8 = OpConstant %uint 8
7740 %uint_7 = OpConstant %uint 7
7741 %uint_9 = OpConstant %uint 9
7742 %uint_15 = OpConstant %uint 15
7743 %42 = OpTypeFunction %void
7744 %uint_10 = OpConstant %uint 10
7745 %uint_53 = OpConstant %uint 53
7746 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
7747 %49 = OpExtInst %void %1 DebugExpression
7748 %50 = OpExtInst %void %1 DebugSource %4 %5
7749 %51 = OpExtInst %void %1 DebugSourceContinued %6
7750 ; CHECK: %51 = OpExtInst %void %1 DebugSourceContinued %6
7751 %52 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %50 %uint_5
7752 ; CHECK: %52 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %50 %uint_5
7753 %53 = OpExtInst %void %1 DebugTypeBasic %7 %uint_32 %uint_3 %uint_0
7754 %54 = OpExtInst %void %1 DebugTypeVector %53 %uint_4
7755 %55 = OpExtInst %void %1 DebugTypeMember %8 %54 %50 %uint_4 %uint_12 %uint_0 %uint_128 %uint_3
7756 %56 = OpExtInst %void %1 DebugTypeComposite %9 %uint_1 %50 %uint_2 %uint_8 %52 %9 %uint_128 %uint_3 %55
7757 %57 = OpExtInst %void %1 DebugTypeFunction %uint_3 %56
7758 %58 = OpExtInst %void %1 DebugFunction %10 %57 %50 %uint_7 %uint_1 %52 %11 %uint_3 %uint_8
7759 %59 = OpExtInst %void %1 DebugLexicalBlock %50 %uint_8 %uint_1 %58
7760 %60 = OpExtInst %void %1 DebugLocalVariable %12 %56 %50 %uint_9 %uint_15 %59 %uint_4
7761 %61 = OpExtInst %void %1 DebugEntryPoint %58 %52 %13 %14
7762 ; CHECK: %61 = OpExtInst %void %1 DebugEntryPoint %58 %52 %13 %14
7763 %MainPs = OpFunction %void None %42
7764 %62 = OpLabel
7765 %63 = OpExtInst %void %1 DebugFunctionDefinition %58 %MainPs
7766 %112 = OpExtInst %void %1 DebugScope %59
7767 %111 = OpExtInst %void %1 DebugLine %50 %uint_10 %uint_10 %uint_5 %uint_53
7768 %110 = OpExtInst %void %1 DebugValue %60 %23 %49 %int_0
7769 %113 = OpExtInst %void %1 DebugNoLine
7770 %114 = OpExtInst %void %1 DebugNoScope
7771 OpStore %out_var_SV_Target0 %23
7772 %66 = OpExtInst %void %1 DebugLine %50 %uint_12 %uint_12 %uint_1 %uint_1
7773 OpReturn
7774 OpFunctionEnd
7775 )";
7776
7777 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7778 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7779 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7780 }
7781
7782 TEST_F(AggressiveDCETest, RemoveOutputTrue) {
7783 // Remove dead n_out output variable from module
7784 const std::string text = R"(
7785 OpCapability Shader
7786 %1 = OpExtInstImport "GLSL.std.450"
7787 OpMemoryModel Logical GLSL450
7788 OpEntryPoint Vertex %main "main" %c_out %c_in %n_out
7789 ;CHECK: OpEntryPoint Vertex %main "main" %c_out %c_in
7790 OpSource GLSL 450
7791 OpName %main "main"
7792 OpName %c_out "c_out"
7793 OpName %c_in "c_in"
7794 OpName %n_out "n_out"
7795 OpDecorate %c_out Location 0
7796 OpDecorate %c_in Location 0
7797 OpDecorate %n_out Location 1
7798 %void = OpTypeVoid
7799 %3 = OpTypeFunction %void
7800 %float = OpTypeFloat 32
7801 %v4float = OpTypeVector %float 4
7802 %_ptr_Output_v4float = OpTypePointer Output %v4float
7803 %c_out = OpVariable %_ptr_Output_v4float Output
7804 %_ptr_Input_v4float = OpTypePointer Input %v4float
7805 %c_in = OpVariable %_ptr_Input_v4float Input
7806 %v3float = OpTypeVector %float 3
7807 %_ptr_Output_v3float = OpTypePointer Output %v3float
7808 %n_out = OpVariable %_ptr_Output_v3float Output
7809 %main = OpFunction %void None %3
7810 %5 = OpLabel
7811 %12 = OpLoad %v4float %c_in
7812 OpStore %c_out %12
7813 OpReturn
7814 OpFunctionEnd
7815 )";
7816
7817 SetTargetEnv(SPV_ENV_VULKAN_1_3);
7818 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7819 SinglePassRunAndMatch<AggressiveDCEPass>(text, true, false, true);
7820 }
7821
7822 TEST_F(AggressiveDCETest, RemoveOutputFalse) {
7823 // Remove dead n_out output variable from module
7824 const std::string text = R"(
7825 OpCapability Shader
7826 %1 = OpExtInstImport "GLSL.std.450"
7827 OpMemoryModel Logical GLSL450
7828 OpEntryPoint Vertex %main "main" %c_out %c_in %n_out
7829 ;CHECK: OpEntryPoint Vertex %main "main" %c_out %c_in %n_out
7830 OpSource GLSL 450
7831 OpName %main "main"
7832 OpName %c_out "c_out"
7833 OpName %c_in "c_in"
7834 OpName %n_out "n_out"
7835 OpDecorate %c_out Location 0
7836 OpDecorate %c_in Location 0
7837 OpDecorate %n_out Location 1
7838 %void = OpTypeVoid
7839 %3 = OpTypeFunction %void
7840 %float = OpTypeFloat 32
7841 %v4float = OpTypeVector %float 4
7842 %_ptr_Output_v4float = OpTypePointer Output %v4float
7843 %c_out = OpVariable %_ptr_Output_v4float Output
7844 %_ptr_Input_v4float = OpTypePointer Input %v4float
7845 %c_in = OpVariable %_ptr_Input_v4float Input
7846 %v3float = OpTypeVector %float 3
7847 %_ptr_Output_v3float = OpTypePointer Output %v3float
7848 %n_out = OpVariable %_ptr_Output_v3float Output
7849 %main = OpFunction %void None %3
7850 %5 = OpLabel
7851 %12 = OpLoad %v4float %c_in
7852 OpStore %c_out %12
7853 OpReturn
7854 OpFunctionEnd
7855 )";
7856
7857 SetTargetEnv(SPV_ENV_VULKAN_1_3);
7858 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7859 SinglePassRunAndMatch<AggressiveDCEPass>(text, true, false, false);
7860 }
7861
7862 TEST_F(AggressiveDCETest, RemoveWhenUsingPrintfExtension) {
7863 // Remove dead n_out output variable from module
7864 const std::string text = R"(
7865 ; CHECK: OpExtInstImport "NonSemantic.DebugPrintf"
7866 ; CHECK-NOT: OpVariable
7867 OpCapability Shader
7868 %1 = OpExtInstImport "NonSemantic.DebugPrintf"
7869 OpMemoryModel Logical GLSL450
7870 OpEntryPoint GLCompute %main "main"
7871 OpExecutionMode %main LocalSize 8 8 1
7872 OpSource HLSL 660
7873 OpName %main "main"
7874 %uint = OpTypeInt 32 0
7875 %void = OpTypeVoid
7876 %5 = OpTypeFunction %void
7877 %_ptr_Function_uint = OpTypePointer Function %uint
7878 %main = OpFunction %void None %5
7879 %7 = OpLabel
7880 %8 = OpVariable %_ptr_Function_uint Function
7881 OpReturn
7882 OpFunctionEnd
7883 )";
7884
7885 SetTargetEnv(SPV_ENV_VULKAN_1_3);
7886 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7887 }
7888
7889 TEST_F(AggressiveDCETest, FunctionReturnPointer) {
7890 // Run DCE when a function returning a pointer to a reference is present
7891
7892 const std::string text = R"(
7893 OpCapability Shader
7894 OpCapability PhysicalStorageBufferAddresses
7895 %1 = OpExtInstImport "GLSL.std.450"
7896 OpMemoryModel PhysicalStorageBuffer64 GLSL450
7897 OpEntryPoint Vertex %2 "main" %3 %4
7898 OpSource GLSL 450
7899 OpSourceExtension "GL_EXT_buffer_reference"
7900 OpSourceExtension "GL_EXT_scalar_block_layout"
7901 OpName %4 "color"
7902 OpMemberDecorate %5 0 Offset 0
7903 OpDecorate %5 Block
7904 OpMemberDecorate %7 0 Offset 0
7905 OpDecorate %7 Block
7906 OpDecorate %8 AliasedPointer
7907 OpDecorate %4 Location 0
7908 %9 = OpTypeVoid
7909 %10 = OpTypeFunction %9
7910 OpTypeForwardPointer %11 PhysicalStorageBuffer
7911 %12 = OpTypeInt 32 0
7912 %5 = OpTypeStruct %12
7913 %11 = OpTypePointer PhysicalStorageBuffer %5
7914 ;CHECK: [[pt:%\w+]] = OpTypePointer PhysicalStorageBuffer {{%\w+}}
7915 %13 = OpTypeFunction %11
7916 ;CHECK: [[pt_fn:%\w+]] = OpTypeFunction [[pt]]
7917 %7 = OpTypeStruct %11
7918 %14 = OpTypePointer PushConstant %7
7919 %3 = OpVariable %14 PushConstant
7920 %15 = OpTypeInt 32 1
7921 %16 = OpConstant %15 0
7922 %17 = OpTypePointer PushConstant %11
7923 %18 = OpTypePointer Function %11
7924 %19 = OpTypeFloat 32
7925 %20 = OpTypeVector %19 4
7926 %21 = OpTypePointer Output %20
7927 %4 = OpVariable %21 Output
7928 %22 = OpConstant %19 1
7929 %23 = OpConstant %19 0
7930 %24 = OpConstantComposite %20 %22 %23 %22 %22
7931 %6 = OpFunction %11 None %13
7932 ;CHECK: [[fn:%\w+]] = OpFunction [[pt]] None [[pt_fn]]
7933 %27 = OpLabel
7934 %28 = OpAccessChain %17 %3 %16
7935 %29 = OpLoad %11 %28
7936 OpReturnValue %29
7937 OpFunctionEnd
7938 %2 = OpFunction %9 None %10
7939 %25 = OpLabel
7940 %8 = OpVariable %18 Function
7941 %26 = OpFunctionCall %11 %6
7942 ;CHECK: {{%\w+}} = OpFunctionCall [[pt]] [[fn]]
7943 OpStore %8 %26
7944 OpStore %4 %24
7945 OpReturn
7946 OpFunctionEnd
7947 )";
7948
7949 // For physical storage buffer support
7950 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7951 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
7952 }
7953
7954 TEST_F(AggressiveDCETest, KeepBeginEndInvocationInterlock) {
7955 // OpBeginInvocationInterlockEXT and OpEndInvocationInterlockEXT delimit a
7956 // critical section. As such, they should be treated as if they have side
7957 // effects and should not be removed.
7958 const std::string test =
7959 R"(OpCapability Shader
7960 OpCapability FragmentShaderSampleInterlockEXT
7961 OpExtension "SPV_EXT_fragment_shader_interlock"
7962 OpMemoryModel Logical GLSL450
7963 OpEntryPoint Fragment %1 "main" %gl_FragCoord
7964 OpExecutionMode %1 OriginUpperLeft
7965 OpExecutionMode %1 SampleInterlockOrderedEXT
7966 OpDecorate %gl_FragCoord BuiltIn FragCoord
7967 %float = OpTypeFloat 32
7968 %float_0 = OpConstant %float 0
7969 %v4float = OpTypeVector %float 4
7970 %_ptr_Input_v4float = OpTypePointer Input %v4float
7971 %void = OpTypeVoid
7972 %8 = OpTypeFunction %void
7973 %bool = OpTypeBool
7974 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
7975 %1 = OpFunction %void None %8
7976 %10 = OpLabel
7977 %11 = OpLoad %v4float %gl_FragCoord
7978 %12 = OpCompositeExtract %float %11 0
7979 %13 = OpFOrdGreaterThan %bool %12 %float_0
7980 OpSelectionMerge %14 None
7981 OpBranchConditional %13 %15 %16
7982 %15 = OpLabel
7983 OpBeginInvocationInterlockEXT
7984 OpBranch %14
7985 %16 = OpLabel
7986 OpBeginInvocationInterlockEXT
7987 OpBranch %14
7988 %14 = OpLabel
7989 OpEndInvocationInterlockEXT
7990 OpReturn
7991 OpFunctionEnd
7992 )";
7993
7994 SinglePassRunAndCheck<AggressiveDCEPass>(test, test, true, true);
7995 }
7996
7997 TEST_F(AggressiveDCETest, StoringAPointer) {
7998 // A store that stores a pointer should not be kept live because the value
7999 // being stored is eventually loaded from.
8000
8001 const std::string text = R"(
8002 OpCapability CooperativeMatrixKHR
8003 OpCapability Shader
8004 OpCapability VulkanMemoryModelKHR
8005 OpExtension "SPV_KHR_cooperative_matrix"
8006 OpExtension "SPV_KHR_vulkan_memory_model"
8007 OpMemoryModel Logical VulkanKHR
8008 OpEntryPoint GLCompute %1 "main" %2
8009 OpExecutionMode %1 LocalSize 64 1 1
8010 OpSource HLSL 600
8011 OpDecorate %2 DescriptorSet 0
8012 OpDecorate %2 Binding 0
8013 OpDecorate %_runtimearr_int ArrayStride 4
8014 OpMemberDecorate %_struct_4 0 Offset 0
8015 OpDecorate %_struct_4 Block
8016 %int = OpTypeInt 32 1
8017 %int_0 = OpConstant %int 0
8018 %int_1 = OpConstant %int 1
8019 %uint = OpTypeInt 32 0
8020 %uint_0 = OpConstant %uint 0
8021 %uint_64 = OpConstant %uint 64
8022 %uint_3 = OpConstant %uint 3
8023 %uint_16 = OpConstant %uint 16
8024 %uint_4 = OpConstant %uint 4
8025 %coop_stride = OpConstant %int 42
8026 %_runtimearr_int = OpTypeRuntimeArray %int
8027 %_struct_4 = OpTypeStruct %_runtimearr_int
8028 %_ptr_StorageBuffer__struct_4 = OpTypePointer StorageBuffer %_struct_4
8029 %void = OpTypeVoid
8030 %16 = OpTypeFunction %void
8031 ; CHECK: [[mat:%\w+]] = OpTypeCooperativeMatrixKHR %int %uint_3 %uint_16 %uint_4 %uint_0
8032 %17 = OpTypeCooperativeMatrixKHR %int %uint_3 %uint_16 %uint_4 %uint_0
8033 ; CHECK: [[struct:%\w+]] = OpTypeStruct [[mat]]
8034 %_struct_18 = OpTypeStruct %17
8035 ; CHECK: [[ptr:%\w+]] = OpTypePointer Function [[struct]]
8036 %_ptr_Function__struct_18 = OpTypePointer Function %_struct_18
8037 %_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
8038 %_ptr_Function_17 = OpTypePointer Function %17
8039 %_ptr_Function_int = OpTypePointer Function %int
8040 %_ptr_Function__ptr_Function_int = OpTypePointer Function %_ptr_Function_int
8041 %2 = OpVariable %_ptr_StorageBuffer__struct_4 StorageBuffer
8042
8043 ; The stored to the fist two variables should be removed and the variables
8044 ; as well. The only function scope variable should be the cooperative matrix.
8045 ; CHECK: OpFunction
8046 ; CHECK-NOT: OpVariable %_ptr_Function__ptr_Function_int Function
8047 ; CHECK: OpVariable [[ptr]] Function
8048 ; CHECK-NOT: OpVariable
8049 %1 = OpFunction %void None %16
8050 %24 = OpLabel
8051 %25 = OpVariable %_ptr_Function__ptr_Function_int Function
8052 %26 = OpVariable %_ptr_Function__ptr_Function_int Function
8053 %27 = OpVariable %_ptr_Function__struct_18 Function
8054 %28 = OpAccessChain %_ptr_StorageBuffer_int %2 %int_0 %uint_0
8055 %29 = OpCooperativeMatrixLoadKHR %17 %28 %int_1 %coop_stride
8056 %30 = OpCompositeConstruct %_struct_18 %29
8057 OpStore %27 %30
8058 %31 = OpAccessChain %_ptr_Function_17 %27 %int_0
8059 %32 = OpAccessChain %_ptr_Function_int %27 %int_0 %uint_0
8060 OpStore %26 %32
8061 %33 = OpLoad %int %32
8062 %34 = OpIAdd %int %33 %int_1
8063 OpStore %25 %32
8064 OpStore %32 %34
8065 %35 = OpAccessChain %_ptr_StorageBuffer_int %2 %int_0 %uint_64
8066 %36 = OpLoad %17 %31
8067 OpCooperativeMatrixStoreKHR %35 %36 %int_0 %coop_stride
8068 OpReturn
8069 OpFunctionEnd
8070 )";
8071
8072 // For physical storage buffer support
8073 SetTargetEnv(SPV_ENV_VULKAN_1_2);
8074 SinglePassRunAndMatch<AggressiveDCEPass>(text, true);
8075 }
8076
8077 TEST_F(AggressiveDCETest, FunctionDeclaration) {
8078 // Ensure the optimizer can handle traversing over a function declaration
8079 // 'myfunc' which has no blocks
8080
8081 const std::string text = R"(OpCapability Linkage
8082 OpCapability Shader
8083 OpMemoryModel Logical GLSL450
8084 OpEntryPoint Fragment %PSMain "main" %entryPointParam_PSMain
8085 OpExecutionMode %PSMain OriginUpperLeft
8086 OpSource Slang 1
8087 OpName %myfunc "myfunc"
8088 OpName %entryPointParam_PSMain "entryPointParam_PSMain"
8089 OpName %PSMain "PSMain"
8090 OpDecorate %myfunc LinkageAttributes "_S6myfuncp0pv4f" Import
8091 OpDecorate %entryPointParam_PSMain Location 0
8092 %void = OpTypeVoid
8093 %5 = OpTypeFunction %void
8094 %float = OpTypeFloat 32
8095 %v4float = OpTypeVector %float 4
8096 %8 = OpTypeFunction %v4float
8097 %_ptr_Output_v4float = OpTypePointer Output %v4float
8098 %entryPointParam_PSMain = OpVariable %_ptr_Output_v4float Output
8099 %myfunc = OpFunction %v4float None %8
8100 OpFunctionEnd
8101 %PSMain = OpFunction %void None %5
8102 %10 = OpLabel
8103 %11 = OpFunctionCall %v4float %myfunc
8104 OpStore %entryPointParam_PSMain %11
8105 OpReturn
8106 OpFunctionEnd
8107 )";
8108
8109 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8110 SinglePassRunAndCheck<AggressiveDCEPass>(text, text, true, true);
8111 }
8112
8113 TEST_F(AggressiveDCETest, MarkCentroidInterpolantLive) {
8114 const std::string spirv =
8115 R"(OpCapability InterpolationFunction
8116 OpCapability Shader
8117 %1 = OpExtInstImport "GLSL.std.450"
8118 OpMemoryModel Logical GLSL450
8119 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8120 OpExecutionMode %main OriginUpperLeft
8121 OpSource HLSL 680
8122 OpName %in_var_COLOR "in.var.COLOR"
8123 OpName %out_var_SV_Target "out.var.SV_Target"
8124 OpName %main "main"
8125 OpName %param_var_p1 "param.var.p1"
8126 OpDecorate %in_var_COLOR Location 0
8127 OpDecorate %out_var_SV_Target Location 0
8128 %float = OpTypeFloat 32
8129 %v4float = OpTypeVector %float 4
8130 %_ptr_Input_v4float = OpTypePointer Input %v4float
8131 %_ptr_Output_v4float = OpTypePointer Output %v4float
8132 %void = OpTypeVoid
8133 %11 = OpTypeFunction %void
8134 %_ptr_Function_v4float = OpTypePointer Function %v4float
8135 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8136 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8137 %main = OpFunction %void None %11
8138 %13 = OpLabel
8139 %14 = OpVariable %_ptr_Function_v4float Function
8140 %param_var_p1 = OpVariable %_ptr_Function_v4float Function
8141 %15 = OpLoad %v4float %in_var_COLOR
8142 OpStore %param_var_p1 %15
8143 %16 = OpExtInst %v4float %1 InterpolateAtCentroid %param_var_p1
8144 OpStore %14 %16
8145 %17 = OpLoad %v4float %14
8146 OpStore %out_var_SV_Target %17
8147 OpReturn
8148 OpFunctionEnd
8149 )";
8150
8151 SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8152 }
8153
8154 TEST_F(AggressiveDCETest, MarkSampleInterpolantLive) {
8155 const std::string spirv =
8156 R"(OpCapability InterpolationFunction
8157 OpCapability Shader
8158 %1 = OpExtInstImport "GLSL.std.450"
8159 OpMemoryModel Logical GLSL450
8160 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8161 OpExecutionMode %main OriginUpperLeft
8162 OpSource HLSL 680
8163 OpName %in_var_COLOR "in.var.COLOR"
8164 OpName %out_var_SV_Target "out.var.SV_Target"
8165 OpName %main "main"
8166 OpName %param_var_p1 "param.var.p1"
8167 OpDecorate %in_var_COLOR Location 0
8168 OpDecorate %out_var_SV_Target Location 0
8169 %float = OpTypeFloat 32
8170 %int = OpTypeInt 32 1
8171 %v4float = OpTypeVector %float 4
8172 %_ptr_Input_v4float = OpTypePointer Input %v4float
8173 %_ptr_Output_v4float = OpTypePointer Output %v4float
8174 %void = OpTypeVoid
8175 %12 = OpTypeFunction %void
8176 %_ptr_Function_v4float = OpTypePointer Function %v4float
8177 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8178 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8179 %int_123 = OpConstant %int 123
8180 %main = OpFunction %void None %12
8181 %15 = OpLabel
8182 %16 = OpVariable %_ptr_Function_v4float Function
8183 %param_var_p1 = OpVariable %_ptr_Function_v4float Function
8184 %17 = OpLoad %v4float %in_var_COLOR
8185 OpStore %param_var_p1 %17
8186 %18 = OpExtInst %v4float %1 InterpolateAtSample %param_var_p1 %int_123
8187 OpStore %16 %18
8188 %19 = OpLoad %v4float %16
8189 OpStore %out_var_SV_Target %19
8190 OpReturn
8191 OpFunctionEnd
8192 )";
8193
8194 SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8195 }
8196
8197 TEST_F(AggressiveDCETest, MarkOffsetInterpolantLive) {
8198 const std::string spirv =
8199 R"(OpCapability InterpolationFunction
8200 OpCapability Shader
8201 %1 = OpExtInstImport "GLSL.std.450"
8202 OpMemoryModel Logical GLSL450
8203 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_Target
8204 OpExecutionMode %main OriginUpperLeft
8205 OpSource HLSL 680
8206 OpName %in_var_COLOR "in.var.COLOR"
8207 OpName %out_var_SV_Target "out.var.SV_Target"
8208 OpName %main "main"
8209 OpName %param_var_p1 "param.var.p1"
8210 OpDecorate %in_var_COLOR Location 0
8211 OpDecorate %out_var_SV_Target Location 0
8212 %float = OpTypeFloat 32
8213 %int = OpTypeInt 32 1
8214 %v4float = OpTypeVector %float 4
8215 %_ptr_Input_v4float = OpTypePointer Input %v4float
8216 %_ptr_Output_v4float = OpTypePointer Output %v4float
8217 %void = OpTypeVoid
8218 %12 = OpTypeFunction %void
8219 %_ptr_Function_v4float = OpTypePointer Function %v4float
8220 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8221 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
8222 %int_123 = OpConstant %int 123
8223 %main = OpFunction %void None %12
8224 %15 = OpLabel
8225 %16 = OpVariable %_ptr_Function_v4float Function
8226 %param_var_p1 = OpVariable %_ptr_Function_v4float Function
8227 %17 = OpLoad %v4float %in_var_COLOR
8228 OpStore %param_var_p1 %17
8229 %18 = OpExtInst %v4float %1 InterpolateAtOffset %param_var_p1 %int_123
8230 OpStore %16 %18
8231 %19 = OpLoad %v4float %16
8232 OpStore %out_var_SV_Target %19
8233 OpReturn
8234 OpFunctionEnd
8235 )";
8236
8237 SinglePassRunAndCheck<AggressiveDCEPass>(spirv, spirv, true, false);
8238 }
8239
8240 TEST_F(AggressiveDCETest, NoEliminateOpSource) {
8241 // Should not eliminate OpSource
8242
8243 const std::string text =
8244 R"(OpCapability Shader
8245 OpMemoryModel Logical GLSL450
8246 OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
8247 OpExecutionMode %main OriginUpperLeft
8248 %4 = OpString "D:\\directxshadercompiler\\tools\\clang\\test\\CodeGenSPIRV\\spirv.debug.opsource.include.hlsl"
8249 %5 = OpString "D:\\directxshadercompiler\\tools\\clang\\test\\CodeGenSPIRV/spirv.debug.opsource.include-file.hlsli"
8250 OpSource HLSL 600 %4 "// RUN: %dxc -T ps_6_0 -E main -Zi %s -spirv | FileCheck %s
8251 #include \"spirv.debug.opsource.include-file.hlsli\"
8252
8253 struct ColorType
8254 {
8255 float4 position : SV_POSITION;
8256 float4 color : COLOR;
8257 };
8258
8259 float4 main(UBER_TYPE(Color) input) : SV_TARGET
8260 {
8261 return input.color;
8262 }
8263 "
8264 OpSource HLSL 600 %5 "#define UBER_TYPE(x) x ## Type
8265 "
8266 OpName %in_var_COLOR "in.var.COLOR"
8267 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
8268 OpName %main "main"
8269 OpDecorate %in_var_COLOR Location 0
8270 OpDecorate %out_var_SV_TARGET Location 0
8271 %float = OpTypeFloat 32
8272 %v4float = OpTypeVector %float 4
8273 %_ptr_Input_v4float = OpTypePointer Input %v4float
8274 %_ptr_Output_v4float = OpTypePointer Output %v4float
8275 %void = OpTypeVoid
8276 %11 = OpTypeFunction %void
8277 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
8278 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
8279 OpLine %4 22 1
8280 %main = OpFunction %void None %11
8281 OpNoLine
8282 %12 = OpLabel
8283 OpLine %4 22 1
8284 %13 = OpLoad %v4float %in_var_COLOR
8285 OpStore %out_var_SV_TARGET %13
8286 OpLine %4 25 1
8287 OpReturn
8288 OpFunctionEnd
8289 )";
8290
8291 auto result = SinglePassRunAndDisassemble<AggressiveDCEPass>(
8292 text, /* skip_nop = */ true, /* skip_validation = */ false);
8293
8294 EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
8295 const std::string& output = std::get<0>(result);
8296 EXPECT_THAT(
8297 output,
8298 HasSubstr("OpSource HLSL 600 %5 \"#define UBER_TYPE(x) x ## Type"));
8299 }
8300
8301 TEST_F(AggressiveDCETest, EliminateCopyLogical) {
8302 const std::string before = R"(
8303 ; CHECK: [[float32:%\w+]] = OpTypeFloat 32
8304 ; CHECK: [[v4float:%\w+]] = OpTypeVector [[float32]] 4
8305 ; CHECK-NOT: %10 = OpTypeArray [[v4float]] %9
8306 ; CHECK-NOT: %11 = OpTypeStruct %10 %10
8307 ; CHECK-NOT: %22 = OpTypePointer Uniform %16
8308 ; CHECK-NOT: %38 = OpTypePointer Function [[v4float]]
8309 ; CHECK-NOT: %43 = OpTypePointer Function %10
8310 ; CHECK-NOT: %44 = OpVariable %42 Function
8311 ; CHECK-NOT: %23 = OpAccessChain %22 %19 %21
8312 ; CHECK-NOT: %24 = OpLoad %16 %23
8313 ; CHECK-NOT: %25 = OpCopyLogical %11 %24
8314 ; CHECK-NOT: %46 = OpCompositeExtract %10 %25 0
8315 ; CHECK-NOT: OpStore %44 %46
8316 OpCapability Shader
8317 %1 = OpExtInstImport "GLSL.std.450"
8318 OpMemoryModel Logical GLSL450
8319 OpEntryPoint Vertex %4 "main" %19 %30 %32
8320 OpSource GLSL 430
8321 OpName %4 "main"
8322 OpDecorate %14 ArrayStride 16
8323 OpDecorate %15 ArrayStride 16
8324 OpMemberDecorate %16 0 Offset 0
8325 OpMemberDecorate %16 1 Offset 32
8326 OpDecorate %17 Block
8327 OpMemberDecorate %17 0 Offset 0
8328 OpDecorate %19 Binding 0
8329 OpDecorate %19 DescriptorSet 0
8330 OpDecorate %28 Block
8331 OpMemberDecorate %28 0 BuiltIn Position
8332 OpMemberDecorate %28 1 BuiltIn PointSize
8333 OpMemberDecorate %28 2 BuiltIn ClipDistance
8334 OpDecorate %32 Location 0
8335 %2 = OpTypeVoid
8336 %3 = OpTypeFunction %2
8337 %6 = OpTypeFloat 32
8338 %7 = OpTypeVector %6 4
8339 %8 = OpTypeInt 32 0
8340 %9 = OpConstant %8 2
8341 %10 = OpTypeArray %7 %9
8342 %11 = OpTypeStruct %10 %10
8343 %14 = OpTypeArray %7 %9
8344 %15 = OpTypeArray %7 %9
8345 %16 = OpTypeStruct %14 %15
8346 %17 = OpTypeStruct %16
8347 %18 = OpTypePointer Uniform %17
8348 %19 = OpVariable %18 Uniform
8349 %20 = OpTypeInt 32 1
8350 %21 = OpConstant %20 0
8351 %22 = OpTypePointer Uniform %16
8352 %26 = OpConstant %8 1
8353 %27 = OpTypeArray %6 %26
8354 %28 = OpTypeStruct %7 %6 %27
8355 %29 = OpTypePointer Output %28
8356 %30 = OpVariable %29 Output
8357 %31 = OpTypePointer Input %7
8358 %32 = OpVariable %31 Input
8359 %33 = OpConstant %8 0
8360 %34 = OpTypePointer Input %6
8361 %38 = OpTypePointer Function %7
8362 %41 = OpTypePointer Output %7
8363 %43 = OpTypePointer Function %10
8364 %48 = OpTypePointer Uniform %14
8365 %49 = OpTypePointer Uniform %7
8366 %4 = OpFunction %2 None %3
8367 %5 = OpLabel
8368 %44 = OpVariable %43 Function
8369 %23 = OpAccessChain %22 %19 %21
8370 %24 = OpLoad %16 %23
8371 %25 = OpCopyLogical %11 %24
8372 %46 = OpCompositeExtract %10 %25 0
8373 %50 = OpAccessChain %48 %19 %21 %33
8374 OpStore %44 %46
8375 %35 = OpAccessChain %34 %32 %33
8376 %36 = OpLoad %6 %35
8377 %37 = OpConvertFToS %20 %36
8378 %47 = OpAccessChain %49 %50 %37
8379 %40 = OpLoad %7 %47
8380 %42 = OpAccessChain %41 %30 %21
8381 OpStore %42 %40
8382 OpReturn
8383 OpFunctionEnd
8384 )";
8385
8386 SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8387 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8388 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
8389 SinglePassRunAndMatch<AggressiveDCEPass>(before, true);
8390 }
8391
8392 TEST_F(AggressiveDCETest, KeepCopyLogical) {
8393 const std::string before = R"(
8394 ; CHECK: OpCopyLogical
8395 OpCapability Shader
8396 %1 = OpExtInstImport "GLSL.std.450"
8397 OpMemoryModel Logical GLSL450
8398 OpEntryPoint GLCompute %4 "main" %15 %23 %38
8399 OpExecutionMode %4 LocalSize 32 32 1
8400 OpSource GLSL 430
8401 OpName %4 "main"
8402 OpDecorate %10 ArrayStride 16
8403 OpDecorate %11 ArrayStride 16
8404 OpMemberDecorate %12 0 Offset 0
8405 OpMemberDecorate %12 1 Offset 2048
8406 OpDecorate %13 Block
8407 OpMemberDecorate %13 0 NonReadable
8408 OpMemberDecorate %13 0 Offset 0
8409 OpDecorate %15 NonReadable
8410 OpDecorate %15 Binding 1
8411 OpDecorate %15 DescriptorSet 0
8412 OpDecorate %18 ArrayStride 16
8413 OpDecorate %19 ArrayStride 16
8414 OpMemberDecorate %20 0 Offset 0
8415 OpMemberDecorate %20 1 Offset 2048
8416 OpDecorate %21 Block
8417 OpMemberDecorate %21 0 NonWritable
8418 OpMemberDecorate %21 0 Offset 0
8419 OpDecorate %23 NonWritable
8420 OpDecorate %23 Binding 0
8421 OpDecorate %23 DescriptorSet 0
8422 OpDecorate %30 ArrayStride 16
8423 OpDecorate %31 ArrayStride 16
8424 OpMemberDecorate %32 0 Offset 0
8425 OpMemberDecorate %32 1 Offset 2048
8426 OpDecorate %34 ArrayStride 4096
8427 OpMemberDecorate %35 0 Offset 0
8428 OpDecorate %36 Block
8429 OpMemberDecorate %36 0 Offset 0
8430 OpDecorate %38 Binding 0
8431 OpDecorate %38 DescriptorSet 0
8432 %2 = OpTypeVoid
8433 %3 = OpTypeFunction %2
8434 %6 = OpTypeFloat 32
8435 %7 = OpTypeVector %6 4
8436 %8 = OpTypeInt 32 0
8437 %9 = OpConstant %8 128
8438 %10 = OpTypeArray %7 %9
8439 %11 = OpTypeArray %7 %9
8440 %12 = OpTypeStruct %10 %11
8441 %13 = OpTypeStruct %12
8442 %14 = OpTypePointer StorageBuffer %13
8443 %15 = OpVariable %14 StorageBuffer
8444 %16 = OpTypeInt 32 1
8445 %17 = OpConstant %16 0
8446 %18 = OpTypeArray %7 %9
8447 %19 = OpTypeArray %7 %9
8448 %20 = OpTypeStruct %18 %19
8449 %21 = OpTypeStruct %20
8450 %22 = OpTypePointer StorageBuffer %21
8451 %23 = OpVariable %22 StorageBuffer
8452 %24 = OpTypePointer StorageBuffer %20
8453 %27 = OpTypePointer StorageBuffer %12
8454 %30 = OpTypeArray %7 %9
8455 %31 = OpTypeArray %7 %9
8456 %32 = OpTypeStruct %30 %31
8457 %33 = OpConstant %8 8
8458 %34 = OpTypeArray %32 %33
8459 %35 = OpTypeStruct %34
8460 %36 = OpTypeStruct %35
8461 %37 = OpTypePointer Uniform %36
8462 %38 = OpVariable %37 Uniform
8463 %4 = OpFunction %2 None %3
8464 %5 = OpLabel
8465 %25 = OpAccessChain %24 %23 %17
8466 %26 = OpLoad %20 %25
8467 %28 = OpAccessChain %27 %15 %17
8468 %29 = OpCopyLogical %12 %26
8469 OpStore %28 %29
8470 OpReturn
8471 OpFunctionEnd
8472 )";
8473
8474 SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8475 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8476 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
8477 SinglePassRunAndMatch<AggressiveDCEPass>(before, true);
8478 }
8479 TEST_F(AggressiveDCETest, KeepOnlyLiveDebugValues) {
8480 // DebugValue should replace dead Value with Undef.
8481 const std::string before =
8482 R"(OpCapability MinLod
8483 OpCapability StorageImageWriteWithoutFormat
8484 OpCapability StorageImageReadWithoutFormat
8485 OpCapability FragmentShaderSampleInterlockEXT
8486 OpCapability FragmentShaderPixelInterlockEXT
8487 OpCapability FragmentShaderShadingRateInterlockEXT
8488 OpCapability ComputeDerivativeGroupQuadsKHR
8489 OpCapability ComputeDerivativeGroupLinearKHR
8490 OpCapability RayQueryKHR
8491 OpCapability GroupNonUniformPartitionedNV
8492 OpCapability InterpolationFunction
8493 OpCapability QuadControlKHR
8494 OpCapability Shader
8495 OpCapability SampledBuffer
8496 OpCapability ImageBuffer
8497 OpExtension "SPV_EXT_fragment_shader_interlock"
8498 OpExtension "SPV_KHR_compute_shader_derivatives"
8499 OpExtension "SPV_KHR_ray_query"
8500 OpExtension "SPV_NV_shader_subgroup_partitioned"
8501 OpExtension "SPV_KHR_quad_control"
8502 OpExtension "SPV_KHR_non_semantic_info"
8503 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8504 OpMemoryModel Logical GLSL450
8505 OpEntryPoint GLCompute %main "main"
8506 OpExecutionMode %main LocalSize 1 1 1
8507 %3 = OpString "partially_unused_var.cs.hlsl"
8508 %17 = OpString "int"
8509 %25 = OpString "main"
8510 %26 = OpString ""
8511 %30 = OpString "a"
8512 %33 = OpString "__dxc_setup"
8513 %35 = OpString "cb000c74"
8514 %36 = OpString " -E main -T cs_6_6 -spirv -fspv-print-all -fspv-debug=vulkan -Qembed_debug"
8515 %39 = OpString "@type.buffer.image"
8516 %40 = OpString "type.buffer.image"
8517 %42 = OpString "TemplateParam"
8518 %45 = OpString "b"
8519 OpName %type_buffer_image "type.buffer.image"
8520 OpName %b "b"
8521 OpName %main "main"
8522 OpDecorate %b DescriptorSet 0
8523 OpDecorate %b Binding 0
8524 %uint = OpTypeInt 32 0
8525 %uint_1 = OpConstant %uint 1
8526 %uint_2 = OpConstant %uint 2
8527 %uint_3 = OpConstant %uint 3
8528 %int = OpTypeInt 32 1
8529 %int_0 = OpConstant %int 0
8530 %uint_0 = OpConstant %uint 0
8531 %uint_32 = OpConstant %uint 32
8532 %type_buffer_image = OpTypeImage %int Buffer 2 0 0 2 R32i
8533 %_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
8534 %void = OpTypeVoid
8535 %uint_4 = OpConstant %uint 4
8536 %uint_5 = OpConstant %uint 5
8537 %uint_13 = OpConstant %uint 13
8538 %uint_7 = OpConstant %uint 7
8539 %uint_15 = OpConstant %uint 15
8540 %uint_8 = OpConstant %uint 8
8541 %49 = OpTypeFunction %void
8542 %_arr_int_uint_3 = OpTypeArray %int %uint_3
8543 %_ptr_Function__arr_int_uint_3 = OpTypePointer Function %_arr_int_uint_3
8544 %v4int = OpTypeVector %int 4
8545 %uint_21 = OpConstant %uint 21
8546 %uint_27 = OpConstant %uint 27
8547 %uint_14 = OpConstant %uint 14
8548 %uint_31 = OpConstant %uint 31
8549 %_ptr_Function_int = OpTypePointer Function %int
8550 %uint_6 = OpConstant %uint 6
8551 %uint_10 = OpConstant %uint 10
8552 %b = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
8553 %int_1 = OpConstant %int 1
8554 %int_2 = OpConstant %int 2
8555 ; CHECK: %281 = OpUndef %int
8556 %38 = OpExtInst %void %1 DebugInfoNone
8557 %16 = OpExtInst %void %1 DebugExpression
8558 ; CHECK: %216 = OpExtInst %void %1 DebugOperation %uint_0
8559 %216 = OpExtInst %void %1 DebugOperation %uint_0
8560 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 %uint_4 %uint_0
8561 %20 = OpExtInst %void %1 DebugTypeArray %18 %uint_3
8562 %21 = OpExtInst %void %1 DebugTypeFunction %uint_3 %void
8563 %22 = OpExtInst %void %1 DebugSource %3
8564 %23 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %22 %uint_5
8565 %27 = OpExtInst %void %1 DebugFunction %25 %21 %22 %uint_4 %uint_1 %23 %26 %uint_3 %uint_4
8566 %28 = OpExtInst %void %1 DebugLexicalBlock %22 %uint_4 %uint_13 %27
8567 %31 = OpExtInst %void %1 DebugLocalVariable %30 %20 %22 %uint_5 %uint_7 %28 %uint_4
8568 %34 = OpExtInst %void %1 DebugFunction %33 %21 %22 %uint_4 %uint_1 %23 %26 %uint_3 %uint_4
8569 %41 = OpExtInst %void %1 DebugTypeComposite %39 %uint_0 %22 %uint_0 %uint_0 %23 %40 %38 %uint_3
8570 %43 = OpExtInst %void %1 DebugTypeTemplateParameter %42 %18 %38 %22 %uint_0 %uint_0
8571 %44 = OpExtInst %void %1 DebugTypeTemplate %41 %43
8572 %46 = OpExtInst %void %1 DebugGlobalVariable %45 %44 %22 %uint_1 %uint_15 %23 %45 %b %uint_8
8573 %37 = OpExtInst %void %1 DebugEntryPoint %34 %23 %35 %36
8574 %138 = OpExtInst %void %1 DebugInlinedAt %uint_4 %34
8575 ; CHECK: %215 = OpExtInst %void %1 DebugExpression %216
8576 %215 = OpExtInst %void %1 DebugExpression %216
8577 %main = OpFunction %void None %49
8578 %50 = OpLabel
8579 %205 = OpVariable %_ptr_Function_int Function
8580 %204 = OpVariable %_ptr_Function_int Function
8581 %203 = OpVariable %_ptr_Function_int Function
8582 %255 = OpExtInst %void %1 DebugScope %34
8583 %52 = OpExtInst %void %1 DebugFunctionDefinition %34 %main
8584 %256 = OpExtInst %void %1 DebugScope %28 %138
8585 %155 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_15 %uint_15
8586 %141 = OpLoad %type_buffer_image %b
8587 %142 = OpImageRead %v4int %141 %uint_1 None
8588 %143 = OpCompositeExtract %int %142 0
8589 %158 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_21 %uint_21
8590 %144 = OpLoad %type_buffer_image %b
8591 %145 = OpImageRead %v4int %144 %uint_2 None
8592 %146 = OpCompositeExtract %int %145 0
8593 %161 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_27 %uint_27
8594 %147 = OpLoad %type_buffer_image %b
8595 %148 = OpImageRead %v4int %147 %uint_3 None
8596 %149 = OpCompositeExtract %int %148 0
8597 %164 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_14 %uint_31
8598 %150 = OpCompositeConstruct %_arr_int_uint_3 %143 %146 %149
8599 %207 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_3 %uint_31
8600 %206 = OpCompositeExtract %int %150 0
8601 ; CHECK-NOT: OpStore %203 %206
8602 OpStore %203 %206
8603 ; CHECK: %253 = OpExtInst %void %1 DebugValue %31 %206 %16 %int_0
8604 %253 = OpExtInst %void %1 DebugValue %31 %206 %16 %int_0
8605 ; CHECK-NOT: %209 = OpCompositeExtract %int %150 1
8606 %209 = OpCompositeExtract %int %150 1
8607 ; CHECK-NOT: OpStore %204 %209
8608 OpStore %204 %209
8609 ; CHECK: %250 = OpExtInst %void %1 DebugValue %31 %281 %16 %int_1
8610 %250 = OpExtInst %void %1 DebugValue %31 %209 %16 %int_1
8611 ; CHECK-NOT: %212 = OpCompositeExtract %int %150 2
8612 %212 = OpCompositeExtract %int %150 2
8613 ; CHECK-NOT: OpStore %205 %212
8614 OpStore %205 %212
8615 ; CHECK: %247 = OpExtInst %void %1 DebugValue %31 %281 %16 %int_2
8616 %247 = OpExtInst %void %1 DebugValue %31 %212 %16 %int_2
8617 %169 = OpExtInst %void %1 DebugLine %22 %uint_6 %uint_6 %uint_3 %uint_13
8618 %154 = OpLoad %type_buffer_image %b
8619 OpImageWrite %154 %uint_0 %206 None
8620 %257 = OpExtInst %void %1 DebugScope %34
8621 %55 = OpExtInst %void %1 DebugLine %22 %uint_7 %uint_7 %uint_1 %uint_1
8622 OpReturn
8623 %258 = OpExtInst %void %1 DebugNoScope
8624 OpFunctionEnd
8625 )";
8626
8627 SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8628 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8629 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
8630 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
8631 SinglePassRunAndMatch<AggressiveDCEPass>(before, false);
8632 }
8633 TEST_F(AggressiveDCETest, KeepLocalDebugValue) {
8634 // DebugValue should only be live when Value is live.
8635 const std::string before =
8636 R"(
8637 OpCapability MinLod
8638 OpCapability StorageImageWriteWithoutFormat
8639 OpCapability StorageImageReadWithoutFormat
8640 OpCapability FragmentShaderSampleInterlockEXT
8641 OpCapability FragmentShaderPixelInterlockEXT
8642 OpCapability FragmentShaderShadingRateInterlockEXT
8643 OpCapability ComputeDerivativeGroupQuadsKHR
8644 OpCapability ComputeDerivativeGroupLinearKHR
8645 OpCapability RayQueryKHR
8646 OpCapability GroupNonUniformPartitionedNV
8647 OpCapability InterpolationFunction
8648 OpCapability QuadControlKHR
8649 OpCapability Shader
8650 OpCapability SampledBuffer
8651 OpCapability ImageBuffer
8652 OpExtension "SPV_EXT_fragment_shader_interlock"
8653 OpExtension "SPV_KHR_compute_shader_derivatives"
8654 OpExtension "SPV_KHR_ray_query"
8655 OpExtension "SPV_NV_shader_subgroup_partitioned"
8656 OpExtension "SPV_KHR_quad_control"
8657 OpExtension "SPV_KHR_non_semantic_info"
8658 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8659 OpMemoryModel Logical GLSL450
8660 OpEntryPoint GLCompute %main "main"
8661 OpExecutionMode %main LocalSize 1 1 1
8662 %3 = OpString "partially_unused_var2.cs.hlsl"
8663 %17 = OpString "int"
8664 %25 = OpString "main"
8665 %26 = OpString ""
8666 ; CHECK: [[VarName:%\w+]] = OpString "some_real_obvious_name"
8667 %30 = OpString "some_real_obvious_name"
8668 %33 = OpString "__dxc_setup"
8669 %35 = OpString "cb000c74"
8670 %36 = OpString " -E main -T cs_6_6 -spirv -fspv-print-all -fspv-debug=vulkan -Qembed_debug"
8671 %39 = OpString "@type.buffer.image"
8672 %40 = OpString "type.buffer.image"
8673 %42 = OpString "TemplateParam"
8674 %45 = OpString "b"
8675 OpName %type_buffer_image "type.buffer.image"
8676 OpName %b "b"
8677 OpName %main "main"
8678 OpDecorate %b DescriptorSet 0
8679 OpDecorate %b Binding 0
8680 %uint = OpTypeInt 32 0
8681 %uint_1 = OpConstant %uint 1
8682 %uint_3 = OpConstant %uint 3
8683 %int = OpTypeInt 32 1
8684 %int_0 = OpConstant %int 0
8685 %uint_0 = OpConstant %uint 0
8686 %uint_32 = OpConstant %uint 32
8687 %type_buffer_image = OpTypeImage %int Buffer 2 0 0 2 R32i
8688 %_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
8689 %void = OpTypeVoid
8690 %uint_4 = OpConstant %uint 4
8691 %uint_5 = OpConstant %uint 5
8692 %uint_13 = OpConstant %uint 13
8693 %uint_7 = OpConstant %uint 7
8694 %uint_15 = OpConstant %uint 15
8695 %uint_8 = OpConstant %uint 8
8696 %49 = OpTypeFunction %void
8697 %uint_52 = OpConstant %uint 52
8698 %uint_6 = OpConstant %uint 6
8699 %uint_10 = OpConstant %uint 10
8700 %b = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
8701 %int_1 = OpConstant %int 1
8702 %int_2 = OpConstant %int 2
8703 ; CHECK: [[undef:%\w+]] = OpUndef %int
8704 %198 = OpUndef %int
8705 %38 = OpExtInst %void %1 DebugInfoNone
8706 %16 = OpExtInst %void %1 DebugExpression
8707 %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 %uint_4 %uint_0
8708 %20 = OpExtInst %void %1 DebugTypeArray %18 %uint_3
8709 %21 = OpExtInst %void %1 DebugTypeFunction %uint_3 %void
8710 %22 = OpExtInst %void %1 DebugSource %3
8711 %23 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %22 %uint_5
8712 %27 = OpExtInst %void %1 DebugFunction %25 %21 %22 %uint_4 %uint_1 %23 %26 %uint_3 %uint_4
8713 %28 = OpExtInst %void %1 DebugLexicalBlock %22 %uint_4 %uint_13 %27
8714 %31 = OpExtInst %void %1 DebugLocalVariable %30 %20 %22 %uint_5 %uint_7 %28 %uint_4
8715 ; CHECK: [[var:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable [[VarName]]
8716 %34 = OpExtInst %void %1 DebugFunction %33 %21 %22 %uint_4 %uint_1 %23 %26 %uint_3 %uint_4
8717 %41 = OpExtInst %void %1 DebugTypeComposite %39 %uint_0 %22 %uint_0 %uint_0 %23 %40 %38 %uint_3
8718 %43 = OpExtInst %void %1 DebugTypeTemplateParameter %42 %18 %38 %22 %uint_0 %uint_0
8719 %44 = OpExtInst %void %1 DebugTypeTemplate %41 %43
8720 %46 = OpExtInst %void %1 DebugGlobalVariable %45 %44 %22 %uint_1 %uint_15 %23 %45 %b %uint_8
8721 %37 = OpExtInst %void %1 DebugEntryPoint %34 %23 %35 %36
8722 %133 = OpExtInst %void %1 DebugInlinedAt %uint_4 %34
8723 %main = OpFunction %void None %49
8724 %50 = OpLabel
8725 %303 = OpExtInst %void %1 DebugScope %34
8726 %52 = OpExtInst %void %1 DebugFunctionDefinition %34 %main
8727 %304 = OpExtInst %void %1 DebugScope %28 %133
8728 %199 = OpExtInst %void %1 DebugLine %22 %uint_5 %uint_5 %uint_3 %uint_52
8729 %245 = OpExtInst %void %1 DebugValue %31 %198 %16 %int_0
8730 %242 = OpExtInst %void %1 DebugValue %31 %198 %16 %int_1
8731 %239 = OpExtInst %void %1 DebugValue %31 %198 %16 %int_2
8732 ; CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[var]] [[undef]] {{%\w+}} %int_0
8733 ; CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[var]] [[undef]] {{%\w+}} %int_1
8734 ; CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[var]] [[undef]] {{%\w+}} %int_2
8735 %160 = OpExtInst %void %1 DebugLine %22 %uint_6 %uint_6 %uint_3 %uint_10
8736 %147 = OpLoad %type_buffer_image %b
8737 OpImageWrite %147 %uint_0 %int_0 None
8738 %305 = OpExtInst %void %1 DebugScope %34
8739 %55 = OpExtInst %void %1 DebugLine %22 %uint_7 %uint_7 %uint_1 %uint_1
8740 OpReturn
8741 %306 = OpExtInst %void %1 DebugNoScope
8742 OpFunctionEnd
8743 )";
8744
8745 SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8746 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8747 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
8748 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
8749 SinglePassRunAndMatch<AggressiveDCEPass>(before, false);
8750 }
8751
8752 TEST_F(AggressiveDCETest, UndefIsOutsideFunction) {
8753 const std::string spirv = R"(
8754 ; CHECK: OpUndef
8755 ; CHECK: OpFunction %void
8756 OpCapability Shader
8757 OpExtension "SPV_KHR_non_semantic_info"
8758 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8759 OpMemoryModel Logical GLSL450
8760 OpEntryPoint GLCompute %2 "main"
8761 OpExecutionMode %2 LocalSize 1 1 1
8762 %3 = OpString ""
8763 %4 = OpString "int"
8764 %5 = OpString "x"
8765 %6 = OpString "Val.set"
8766 %7 = OpString "Val"
8767 %8 = OpString "this"
8768 %void = OpTypeVoid
8769 %uint = OpTypeInt 32 0
8770 %uint_11 = OpConstant %uint 11
8771 %uint_5 = OpConstant %uint 5
8772 %uint_100 = OpConstant %uint 100
8773 %14 = OpTypeFunction %void
8774 %int = OpTypeInt 32 1
8775 %_ptr_Function_int = OpTypePointer Function %int
8776 %uint_0 = OpConstant %uint 0
8777 %uint_15 = OpConstant %uint 15
8778 %uint_6 = OpConstant %uint 6
8779 %uint_32 = OpConstant %uint 32
8780 %uint_4 = OpConstant %uint 4
8781 %uint_131072 = OpConstant %uint 131072
8782 %uint_1 = OpConstant %uint 1
8783 %uint_10 = OpConstant %uint 10
8784 %uint_8 = OpConstant %uint 8
8785 %26 = OpExtInst %void %1 DebugOperation %uint_0
8786 %27 = OpExtInst %void %1 DebugSource %3 %3
8787 %28 = OpExtInst %void %1 DebugCompilationUnit %uint_100 %uint_5 %27 %uint_11
8788 %29 = OpExtInst %void %1 DebugTypeBasic %4 %uint_32 %uint_4 %uint_131072
8789 %30 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void
8790 %31 = OpExtInst %void %1 DebugFunction %6 %30 %27 %uint_6 %uint_10 %28 %6 %uint_0 %uint_6
8791 %32 = OpExtInst %void %1 DebugTypeMember %5 %29 %27 %uint_1 %uint_8 %uint_0 %uint_32 %uint_0
8792 %33 = OpExtInst %void %1 DebugTypeComposite %7 %uint_1 %27 %uint_1 %uint_8 %28 %7 %uint_32 %uint_131072 %32
8793 %34 = OpExtInst %void %1 DebugLocalVariable %8 %33 %27 %uint_6 %uint_10 %31 %uint_0 %uint_1
8794 %35 = OpExtInst %void %1 DebugExpression %26
8795 %2 = OpFunction %void None %14
8796 %36 = OpLabel
8797 %37 = OpVariable %_ptr_Function_int Function
8798 %38 = OpExtInst %void %1 DebugValue %34 %37 %35 %uint_0
8799 OpReturn
8800 OpFunctionEnd
8801
8802 )";
8803
8804 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
8805 }
8806 TEST_F(AggressiveDCETest, ConvertDebugDeclareToDebugValue) {
8807 const std::string spirv =
8808 R"(OpCapability Shader
8809 OpExtension "SPV_KHR_non_semantic_info"
8810 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8811 OpMemoryModel Logical GLSL450
8812 OpEntryPoint Fragment %main "main" %input1 %input2 %output
8813 OpExecutionMode %main OriginUpperLeft
8814 %5 = OpString "test.hlsl"
8815 OpSource HLSL 600
8816 OpName %main "main"
8817 OpDecorate %input1 Location 0
8818 OpDecorate %input2 Location 1
8819 OpDecorate %output Location 0
8820 %void = OpTypeVoid
8821 %float = OpTypeFloat 32
8822 %v3float = OpTypeVector %float 3
8823 %v4float = OpTypeVector %float 4
8824 %uint = OpTypeInt 32 0
8825 %uint_1 = OpConstant %uint 1
8826 %uint_2 = OpConstant %uint 2
8827 %uint_3 = OpConstant %uint 3
8828 %uint_4 = OpConstant %uint 4
8829 %uint_32 = OpConstant %uint 32
8830 %uint_0 = OpConstant %uint 0
8831 %float_0 = OpConstant %float 0
8832 %float_1 = OpConstant %float 1
8833 %_ptr_Input_v3float = OpTypePointer Input %v3float
8834 %_ptr_Output_v3float = OpTypePointer Output %v3float
8835 %_ptr_Function_v4float = OpTypePointer Function %v4float
8836 %input1 = OpVariable %_ptr_Input_v3float Input
8837 %input2 = OpVariable %_ptr_Input_v3float Input
8838 %output = OpVariable %_ptr_Output_v3float Output
8839 %29 = OpTypeFunction %void
8840 ; CHECK: [[initial:%\w+]] = OpConstantComposite
8841 ; CHECK: [[expr:%\w+]] = OpExtInst %void {{%\w+}} DebugExpression
8842 ; CHECK: [[source:%\w+]] = OpExtInst %void {{%\w+}} DebugSource %6
8843 %30 = OpExtInst %void %1 DebugSource %5
8844 %31 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_3 %30 %uint_32
8845 ; CHECK: [[basic:%\w+]] = OpExtInst %void {{%\w+}} DebugTypeBasic
8846 %32 = OpExtInst %void %1 DebugTypeBasic %5 %uint_32 %uint_3 %uint_0
8847 ; CHECK: [[vec_type:%\w+]] = OpExtInst %void {{%\w+}} DebugTypeVector [[basic]] %uint_4
8848 %33 = OpExtInst %void %1 DebugTypeVector %32 %uint_4
8849 %34 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void
8850 %35 = OpExtInst %void %1 DebugFunction %5 %34 %30 %uint_1 %uint_0 %31 %5 %uint_0 %uint_1
8851 ; CHECK: [[local:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable %6 [[vec_type]] [[source]] %uint_1
8852 %36 = OpExtInst %void %1 DebugLocalVariable %5 %33 %30 %uint_1 %uint_0 %35 %uint_0
8853 %38 = OpExtInst %void %1 DebugExpression
8854 %initial_value = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_1
8855 %main = OpFunction %void None %29
8856 %39 = OpLabel
8857 %dead_pos_w = OpVariable %_ptr_Function_v4float Function
8858 %live_var1 = OpVariable %_ptr_Function_v4float Function
8859 %live_var2 = OpVariable %_ptr_Function_v4float Function
8860 %input1_value = OpLoad %v3float %input1
8861 %input2_value = OpLoad %v3float %input2
8862 ; CHECK-NOT: DebugDeclare
8863 %40 = OpExtInst %void %1 DebugDeclare %36 %dead_pos_w %38
8864 OpStore %dead_pos_w %initial_value
8865 ; CHECK: DebugValue %30 [[initial]] %31
8866 OpStore %live_var1 %initial_value
8867 %computed1 = OpVectorTimesScalar %v3float %input1_value %float_1
8868 %computed2 = OpFAdd %v3float %computed1 %input2_value
8869 ; CHECK: [[new:%\w+]] = OpCompositeConstruct
8870 %new_pos_w = OpCompositeConstruct %v4float %computed2 %float_1
8871 OpStore %dead_pos_w %new_pos_w
8872 ; CHECK: DebugValue %30 [[new]] %31
8873 OpStore %live_var2 %new_pos_w
8874 %loaded1 = OpLoad %v4float %live_var1
8875 %loaded2 = OpLoad %v4float %live_var2
8876 %pos_xyz1 = OpVectorShuffle %v3float %loaded1 %loaded1 0 1 2
8877 %pos_xyz2 = OpVectorShuffle %v3float %loaded2 %loaded2 0 1 2
8878 %mixed_result = OpFAdd %v3float %pos_xyz1 %pos_xyz2
8879 OpStore %output %mixed_result
8880 OpReturn
8881 OpFunctionEnd
8882 )";
8883
8884 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
8885 }
8886
8887 TEST_F(AggressiveDCETest, DebugDeclareConvertedToDebugValueKeepsDebugScope) {
8888 const std::string spirv = R"(
8889 ; CHECK: [[ext:%\w+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8890 ; CHECK-DAG: [[inlined:%\w+]] = OpExtInst %void [[ext]] DebugInlinedAt
8891 ; CHECK-DAG: [[UniformVar:%\w+]] = OpVariable %_ptr_Uniform_float Uniform
8892 ; CHECK: [[scope:%\w+]] = OpExtInst %void [[ext]] DebugScope {{%\w+}} [[inlined]]
8893 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue {{%\w+}} {{%\w+}} {{%\w+}}
8894 ; CHECK-NEXT: DebugScope
8895 ; CHECK-NEXT: DebugLine
8896 ; CHECK-NEXT: OpStore [[UniformVar]] %float_0
8897 ; CHECK-NEXT: OpReturn
8898 OpCapability Shader
8899 OpExtension "SPV_KHR_non_semantic_info"
8900 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8901 OpMemoryModel Logical GLSL450
8902 OpEntryPoint GLCompute %2 "main"
8903 OpExecutionMode %2 LocalSize 1 1 1
8904 %3 = OpString ""
8905 %void = OpTypeVoid
8906 %uint = OpTypeInt 32 0
8907 %6 = OpTypeFunction %void
8908 %float = OpTypeFloat 32
8909 %uint_3 = OpConstant %uint 3
8910 %uint_0 = OpConstant %uint 0
8911 %_ptr_Function_float = OpTypePointer Function %float
8912 %_ptr_Uniform_float = OpTypePointer Uniform %float
8913 %float_0 = OpConstant %float 0
8914 %13 = OpVariable %_ptr_Uniform_float Uniform
8915 %uint_57 = OpConstant %uint 57
8916 %15 = OpExtInst %void %1 DebugExpression
8917 %16 = OpExtInst %void %1 DebugSource %3 %3
8918 %17 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %16 %uint_0
8919 %18 = OpExtInst %void %1 DebugTypeBasic %3 %uint_0 %uint_0 %uint_0
8920 %19 = OpExtInst %void %1 DebugTypeVector %18 %uint_3
8921 %20 = OpExtInst %void %1 DebugTypePointer %19 %uint_0 %uint_0
8922 %21 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void %20
8923 %22 = OpExtInst %void %1 DebugFunction %3 %21 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0
8924 %23 = OpExtInst %void %1 DebugTypeBasic %3 %uint_0 %uint_3 %uint_0
8925 %24 = OpExtInst %void %1 DebugTypeMember %3 %23 %16 %uint_0 %uint_0 %uint_0 %uint_0 %uint_0
8926 %25 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0 %24
8927 %26 = OpExtInst %void %1 DebugTypeFunction %uint_0 %23 %25 %23
8928 %27 = OpExtInst %void %1 DebugFunction %3 %26 %16 %uint_0 %uint_0 %17 %3 %uint_0 %uint_0
8929 %28 = OpExtInst %void %1 DebugLocalVariable %3 %23 %16 %uint_0 %uint_0 %27 %uint_0 %uint_0
8930 %29 = OpExtInst %void %1 DebugInlinedAt %uint_0 %22
8931 %2 = OpFunction %void None %6
8932 %30 = OpLabel
8933 %31 = OpVariable %_ptr_Function_float Function
8934 %32 = OpExtInst %void %1 DebugScope %27 %29
8935 %33 = OpExtInst %void %1 DebugNoLine
8936 %34 = OpExtInst %void %1 DebugDeclare %28 %31 %15
8937 OpStore %31 %float_0
8938 %35 = OpExtInst %void %1 DebugScope %22
8939 %36 = OpExtInst %void %1 DebugLine %16 %uint_0 %uint_0 %uint_0 %uint_0
8940 OpStore %13 %float_0
8941 OpReturn
8942 %37 = OpExtInst %void %1 DebugNoScope
8943 OpFunctionEnd
8944 )";
8945
8946 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
8947 }
8948
8949 TEST_F(AggressiveDCETest, DebugValueWithDeadOperandKeepsDebugScope) {
8950 const std::string spirv = R"(
8951 ; CHECK: [[ext:%\w+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8952 ; CHECK-DAG: [[inlined:%\w+]] = OpExtInst %void [[ext]] DebugInlinedAt
8953 ; CHECK-DAG: [[undef:%\w+]] = OpUndef %float
8954 ; CHECK: [[scope:%\w+]] = OpExtInst %void [[ext]] DebugScope {{%\w+}} [[inlined]]
8955 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue {{%\w+}} [[undef]] {{%\w+}}
8956 OpCapability Shader
8957 OpExtension "SPV_KHR_non_semantic_info"
8958 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
8959 OpMemoryModel Logical GLSL450
8960 OpEntryPoint GLCompute %2 "main"
8961 OpExecutionMode %2 LocalSize 1 1 1
8962 %4 = OpString ""
8963 %5 = OpString "file.slang"
8964 %6 = OpString "uint"
8965 %7 = OpString "computeMain"
8966 %8 = OpString "float"
8967 %9 = OpString "x"
8968 %10 = OpString "A"
8969 %11 = OpString "test"
8970 %12 = OpString "v"
8971 %void = OpTypeVoid
8972 %uint = OpTypeInt 32 0
8973 %uint_11 = OpConstant %uint 11
8974 %uint_5 = OpConstant %uint 5
8975 %uint_100 = OpConstant %uint 100
8976 %21 = OpTypeFunction %void
8977 %float = OpTypeFloat 32
8978 %uint_32 = OpConstant %uint 32
8979 %uint_6 = OpConstant %uint 6
8980 %uint_131072 = OpConstant %uint 131072
8981 %uint_3 = OpConstant %uint 3
8982 %uint_7 = OpConstant %uint 7
8983 %uint_0 = OpConstant %uint 0
8984 %uint_46 = OpConstant %uint 46
8985 %uint_1 = OpConstant %uint 1
8986 %uint_8 = OpConstant %uint 8
8987 %_ptr_Function_float = OpTypePointer Function %float
8988 %int = OpTypeInt 32 1
8989 %int_0 = OpConstant %int 0
8990 %float_0 = OpConstant %float 0
8991 %uint_50 = OpConstant %uint 50
8992 %uint_34 = OpConstant %uint 34
8993 %uint_2 = OpConstant %uint 2
8994 %uint_56 = OpConstant %uint 56
8995 %_runtimearr_float = OpTypeRuntimeArray %float
8996 %uint_57 = OpConstant %uint 57
8997 %42 = OpExtInst %void %1 DebugExpression
8998 %43 = OpExtInst %void %1 DebugSource %5 %4
8999 %44 = OpExtInst %void %1 DebugCompilationUnit %uint_100 %uint_5 %43 %uint_11
9000 %45 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 %uint_6 %uint_131072
9001 %46 = OpExtInst %void %1 DebugTypeVector %45 %uint_3
9002 %47 = OpExtInst %void %1 DebugTypePointer %46 %uint_7 %uint_131072
9003 %48 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void %47
9004 %49 = OpExtInst %void %1 DebugFunction %7 %48 %43 %uint_46 %uint_6 %44 %7 %uint_0 %uint_46
9005 %50 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 %uint_3 %uint_131072
9006 %51 = OpExtInst %void %1 DebugTypeMember %9 %50 %43 %uint_8 %uint_11 %uint_0 %uint_32 %uint_0
9007 %52 = OpExtInst %void %1 DebugTypeComposite %10 %uint_1 %43 %uint_6 %uint_8 %44 %10 %uint_32 %uint_131072 %51
9008 %53 = OpExtInst %void %1 DebugTypeFunction %uint_0 %50 %52 %50
9009 %54 = OpExtInst %void %1 DebugFunction %11 %53 %43 %uint_34 %uint_7 %44 %11 %uint_0 %uint_34
9010 %55 = OpExtInst %void %1 DebugLocalVariable %12 %50 %43 %uint_34 %uint_7 %54 %uint_0 %uint_2
9011 %56 = OpExtInst %void %1 DebugInlinedAt %uint_50 %49
9012 %2 = OpFunction %void None %21
9013 %57 = OpLabel
9014 %59 = OpExtInst %void %1 DebugScope %54 %56
9015 %60 = OpExtInst %void %1 DebugNoLine
9016 %val = OpFAdd %float %float_0 %float_0
9017 %61 = OpExtInst %void %1 DebugValue %55 %val %42
9018 %62 = OpExtInst %void %1 DebugScope %49
9019 %63 = OpExtInst %void %1 DebugLine %43 %uint_56 %uint_56 %uint_5 %uint_6
9020 OpReturn
9021 %66 = OpExtInst %void %1 DebugNoScope
9022 OpFunctionEnd
9023 )";
9024
9025 SinglePassRunAndMatch<AggressiveDCEPass>(spirv, true);
9026 }
9027
9028 } // namespace
9029 } // namespace opt
9030 } // namespace spvtools