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 <memory>
17 #include <string>
18 
19 #include "test/opt/pass_fixture.h"
20 #include "test/opt/pass_utils.h"
21 
22 namespace spvtools {
23 namespace opt {
24 namespace {
25 
26 using LocalSSAElimTest = PassTest<::testing::Test>;
27 
TEST_F(LocalSSAElimTest,ForLoop)28 TEST_F(LocalSSAElimTest, ForLoop) {
29   // #version 140
30   //
31   // in vec4 BC;
32   // out float fo;
33   //
34   // void main()
35   // {
36   //     float f = 0.0;
37   //     for (int i=0; i<4; i++) {
38   //       f = f + BC[i];
39   //     }
40   //     fo = f;
41   // }
42 
43   const std::string predefs =
44       R"(OpCapability Shader
45 %1 = OpExtInstImport "GLSL.std.450"
46 OpMemoryModel Logical GLSL450
47 OpEntryPoint Fragment %main "main" %BC %fo
48 OpExecutionMode %main OriginUpperLeft
49 OpSource GLSL 140
50 OpName %main "main"
51 OpName %f "f"
52 OpName %i "i"
53 OpName %BC "BC"
54 OpName %fo "fo"
55 %void = OpTypeVoid
56 %8 = OpTypeFunction %void
57 %float = OpTypeFloat 32
58 %_ptr_Function_float = OpTypePointer Function %float
59 %float_0 = OpConstant %float 0
60 %int = OpTypeInt 32 1
61 %_ptr_Function_int = OpTypePointer Function %int
62 %int_0 = OpConstant %int 0
63 %int_4 = OpConstant %int 4
64 %bool = OpTypeBool
65 %v4float = OpTypeVector %float 4
66 %_ptr_Input_v4float = OpTypePointer Input %v4float
67 %BC = OpVariable %_ptr_Input_v4float Input
68 %_ptr_Input_float = OpTypePointer Input %float
69 %int_1 = OpConstant %int 1
70 %_ptr_Output_float = OpTypePointer Output %float
71 %fo = OpVariable %_ptr_Output_float Output
72 )";
73 
74   const std::string before =
75       R"(%main = OpFunction %void None %8
76 %22 = OpLabel
77 %f = OpVariable %_ptr_Function_float Function
78 %i = OpVariable %_ptr_Function_int Function
79 OpStore %f %float_0
80 OpStore %i %int_0
81 OpBranch %23
82 %23 = OpLabel
83 OpLoopMerge %24 %25 None
84 OpBranch %26
85 %26 = OpLabel
86 %27 = OpLoad %int %i
87 %28 = OpSLessThan %bool %27 %int_4
88 OpBranchConditional %28 %29 %24
89 %29 = OpLabel
90 %30 = OpLoad %float %f
91 %31 = OpLoad %int %i
92 %32 = OpAccessChain %_ptr_Input_float %BC %31
93 %33 = OpLoad %float %32
94 %34 = OpFAdd %float %30 %33
95 OpStore %f %34
96 OpBranch %25
97 %25 = OpLabel
98 %35 = OpLoad %int %i
99 %36 = OpIAdd %int %35 %int_1
100 OpStore %i %36
101 OpBranch %23
102 %24 = OpLabel
103 %37 = OpLoad %float %f
104 OpStore %fo %37
105 OpReturn
106 OpFunctionEnd
107 )";
108 
109   const std::string after =
110       R"(%main = OpFunction %void None %8
111 %22 = OpLabel
112 %f = OpVariable %_ptr_Function_float Function
113 %i = OpVariable %_ptr_Function_int Function
114 OpStore %f %float_0
115 OpStore %i %int_0
116 OpBranch %23
117 %23 = OpLabel
118 %39 = OpPhi %float %float_0 %22 %34 %25
119 %38 = OpPhi %int %int_0 %22 %36 %25
120 OpLoopMerge %24 %25 None
121 OpBranch %26
122 %26 = OpLabel
123 %28 = OpSLessThan %bool %38 %int_4
124 OpBranchConditional %28 %29 %24
125 %29 = OpLabel
126 %32 = OpAccessChain %_ptr_Input_float %BC %38
127 %33 = OpLoad %float %32
128 %34 = OpFAdd %float %39 %33
129 OpStore %f %34
130 OpBranch %25
131 %25 = OpLabel
132 %36 = OpIAdd %int %38 %int_1
133 OpStore %i %36
134 OpBranch %23
135 %24 = OpLabel
136 OpStore %fo %39
137 OpReturn
138 OpFunctionEnd
139 )";
140 
141   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
142                                         true);
143 }
144 
TEST_F(LocalSSAElimTest,NestedForLoop)145 TEST_F(LocalSSAElimTest, NestedForLoop) {
146   // #version 450
147   //
148   // layout (location=0) in mat4 BC;
149   // layout (location=0) out float fo;
150   //
151   // void main()
152   // {
153   //     float f = 0.0;
154   //     for (int i=0; i<4; i++)
155   //       for (int j=0; j<4; j++)
156   //         f = f + BC[i][j];
157   //     fo = f;
158   // }
159 
160   const std::string predefs =
161       R"(OpCapability Shader
162 %1 = OpExtInstImport "GLSL.std.450"
163 OpMemoryModel Logical GLSL450
164 OpEntryPoint Fragment %main "main" %BC %fo
165 OpExecutionMode %main OriginUpperLeft
166 OpSource GLSL 450
167 OpName %main "main"
168 OpName %f "f"
169 OpName %i "i"
170 OpName %j "j"
171 OpName %BC "BC"
172 OpName %fo "fo"
173 OpDecorate %BC Location 0
174 OpDecorate %fo Location 0
175 %void = OpTypeVoid
176 %9 = OpTypeFunction %void
177 %float = OpTypeFloat 32
178 %_ptr_Function_float = OpTypePointer Function %float
179 %float_0 = OpConstant %float 0
180 %int = OpTypeInt 32 1
181 %_ptr_Function_int = OpTypePointer Function %int
182 %int_0 = OpConstant %int 0
183 %int_4 = OpConstant %int 4
184 %bool = OpTypeBool
185 %v4float = OpTypeVector %float 4
186 %mat4v4float = OpTypeMatrix %v4float 4
187 %_ptr_Input_mat4v4float = OpTypePointer Input %mat4v4float
188 %BC = OpVariable %_ptr_Input_mat4v4float Input
189 %_ptr_Input_float = OpTypePointer Input %float
190 %int_1 = OpConstant %int 1
191 %_ptr_Output_float = OpTypePointer Output %float
192 %fo = OpVariable %_ptr_Output_float Output
193 )";
194 
195   const std::string before =
196       R"(
197 ; CHECK: = OpFunction
198 ; CHECK-NEXT: [[entry:%\w+]] = OpLabel
199 ; CHECK: [[outer_header:%\w+]] = OpLabel
200 ; CHECK-NEXT: [[outer_f:%\w+]] = OpPhi %float %float_0 [[entry]] [[inner_f:%\w+]] [[outer_be:%\w+]]
201 ; CHECK-NEXT: [[i:%\w+]] = OpPhi %int %int_0 [[entry]] [[i_next:%\w+]] [[outer_be]]
202 ; CHECK-NEXT: OpSLessThan {{%\w+}} [[i]]
203 ; CHECK: [[inner_pre_header:%\w+]] = OpLabel
204 ; CHECK: [[inner_header:%\w+]] = OpLabel
205 ; CHECK-NEXT: [[inner_f]] = OpPhi %float [[outer_f]] [[inner_pre_header]] [[f_next:%\w+]] [[inner_be:%\w+]]
206 ; CHECK-NEXT: [[j:%\w+]] = OpPhi %int %int_0 [[inner_pre_header]] [[j_next:%\w+]] [[inner_be]]
207 ; CHECK: [[inner_be]] = OpLabel
208 ; CHECK: [[f_next]] = OpFAdd %float [[inner_f]]
209 ; CHECK: [[j_next]] = OpIAdd %int [[j]] %int_1
210 ; CHECK: [[outer_be]] = OpLabel
211 ; CHECK: [[i_next]] = OpIAdd
212 ; CHECK: OpStore %fo [[outer_f]]
213 %main = OpFunction %void None %9
214 %24 = OpLabel
215 %f = OpVariable %_ptr_Function_float Function
216 %i = OpVariable %_ptr_Function_int Function
217 %j = OpVariable %_ptr_Function_int Function
218 OpStore %f %float_0
219 OpStore %i %int_0
220 OpBranch %25
221 %25 = OpLabel
222 %26 = OpLoad %int %i
223 %27 = OpSLessThan %bool %26 %int_4
224 OpLoopMerge %28 %29 None
225 OpBranchConditional %27 %30 %28
226 %30 = OpLabel
227 OpStore %j %int_0
228 OpBranch %31
229 %31 = OpLabel
230 %32 = OpLoad %int %j
231 %33 = OpSLessThan %bool %32 %int_4
232 OpLoopMerge %50 %34 None
233 OpBranchConditional %33 %34 %50
234 %34 = OpLabel
235 %35 = OpLoad %float %f
236 %36 = OpLoad %int %i
237 %37 = OpLoad %int %j
238 %38 = OpAccessChain %_ptr_Input_float %BC %36 %37
239 %39 = OpLoad %float %38
240 %40 = OpFAdd %float %35 %39
241 OpStore %f %40
242 %41 = OpLoad %int %j
243 %42 = OpIAdd %int %41 %int_1
244 OpStore %j %42
245 OpBranch %31
246 %50 = OpLabel
247 OpBranch %29
248 %29 = OpLabel
249 %43 = OpLoad %int %i
250 %44 = OpIAdd %int %43 %int_1
251 OpStore %i %44
252 OpBranch %25
253 %28 = OpLabel
254 %45 = OpLoad %float %f
255 OpStore %fo %45
256 OpReturn
257 OpFunctionEnd
258 )";
259 
260   SinglePassRunAndMatch<SSARewritePass>(predefs + before, true);
261 }
262 
TEST_F(LocalSSAElimTest,ForLoopWithContinue)263 TEST_F(LocalSSAElimTest, ForLoopWithContinue) {
264   // #version 140
265   //
266   // in vec4 BC;
267   // out float fo;
268   //
269   // void main()
270   // {
271   //     float f = 0.0;
272   //     for (int i=0; i<4; i++) {
273   //       float t = BC[i];
274   //       if (t < 0.0)
275   //         continue;
276   //       f = f + t;
277   //     }
278   //     fo = f;
279   // }
280 
281   const std::string predefs =
282       R"(OpCapability Shader
283 %1 = OpExtInstImport "GLSL.std.450"
284 OpMemoryModel Logical GLSL450
285 OpEntryPoint Fragment %main "main" %BC %fo
286 OpExecutionMode %main OriginUpperLeft
287 OpSource GLSL 140
288 )";
289 
290   const std::string names =
291       R"(OpName %main "main"
292 OpName %f "f"
293 OpName %i "i"
294 OpName %t "t"
295 OpName %BC "BC"
296 OpName %fo "fo"
297 )";
298 
299   const std::string predefs2 =
300       R"(%void = OpTypeVoid
301 %9 = OpTypeFunction %void
302 %float = OpTypeFloat 32
303 %_ptr_Function_float = OpTypePointer Function %float
304 %float_0 = OpConstant %float 0
305 %int = OpTypeInt 32 1
306 %_ptr_Function_int = OpTypePointer Function %int
307 %int_0 = OpConstant %int 0
308 %int_4 = OpConstant %int 4
309 %bool = OpTypeBool
310 %v4float = OpTypeVector %float 4
311 %_ptr_Input_v4float = OpTypePointer Input %v4float
312 %BC = OpVariable %_ptr_Input_v4float Input
313 %_ptr_Input_float = OpTypePointer Input %float
314 %int_1 = OpConstant %int 1
315 %_ptr_Output_float = OpTypePointer Output %float
316 %fo = OpVariable %_ptr_Output_float Output
317 )";
318 
319   const std::string before =
320       R"(%main = OpFunction %void None %9
321 %23 = OpLabel
322 %f = OpVariable %_ptr_Function_float Function
323 %i = OpVariable %_ptr_Function_int Function
324 %t = OpVariable %_ptr_Function_float Function
325 OpStore %f %float_0
326 OpStore %i %int_0
327 OpBranch %24
328 %24 = OpLabel
329 OpLoopMerge %25 %26 None
330 OpBranch %27
331 %27 = OpLabel
332 %28 = OpLoad %int %i
333 %29 = OpSLessThan %bool %28 %int_4
334 OpBranchConditional %29 %30 %25
335 %30 = OpLabel
336 %31 = OpLoad %int %i
337 %32 = OpAccessChain %_ptr_Input_float %BC %31
338 %33 = OpLoad %float %32
339 OpStore %t %33
340 %34 = OpLoad %float %t
341 %35 = OpFOrdLessThan %bool %34 %float_0
342 OpSelectionMerge %36 None
343 OpBranchConditional %35 %37 %36
344 %37 = OpLabel
345 OpBranch %26
346 %36 = OpLabel
347 %38 = OpLoad %float %f
348 %39 = OpLoad %float %t
349 %40 = OpFAdd %float %38 %39
350 OpStore %f %40
351 OpBranch %26
352 %26 = OpLabel
353 %41 = OpLoad %int %i
354 %42 = OpIAdd %int %41 %int_1
355 OpStore %i %42
356 OpBranch %24
357 %25 = OpLabel
358 %43 = OpLoad %float %f
359 OpStore %fo %43
360 OpReturn
361 OpFunctionEnd
362 )";
363 
364   const std::string after =
365       R"(%main = OpFunction %void None %9
366 %23 = OpLabel
367 %f = OpVariable %_ptr_Function_float Function
368 %i = OpVariable %_ptr_Function_int Function
369 %t = OpVariable %_ptr_Function_float Function
370 OpStore %f %float_0
371 OpStore %i %int_0
372 OpBranch %24
373 %24 = OpLabel
374 %45 = OpPhi %float %float_0 %23 %47 %26
375 %44 = OpPhi %int %int_0 %23 %42 %26
376 OpLoopMerge %25 %26 None
377 OpBranch %27
378 %27 = OpLabel
379 %29 = OpSLessThan %bool %44 %int_4
380 OpBranchConditional %29 %30 %25
381 %30 = OpLabel
382 %32 = OpAccessChain %_ptr_Input_float %BC %44
383 %33 = OpLoad %float %32
384 OpStore %t %33
385 %35 = OpFOrdLessThan %bool %33 %float_0
386 OpSelectionMerge %36 None
387 OpBranchConditional %35 %37 %36
388 %37 = OpLabel
389 OpBranch %26
390 %36 = OpLabel
391 %40 = OpFAdd %float %45 %33
392 OpStore %f %40
393 OpBranch %26
394 %26 = OpLabel
395 %47 = OpPhi %float %45 %37 %40 %36
396 %42 = OpIAdd %int %44 %int_1
397 OpStore %i %42
398 OpBranch %24
399 %25 = OpLabel
400 OpStore %fo %45
401 OpReturn
402 OpFunctionEnd
403 )";
404 
405   SinglePassRunAndCheck<SSARewritePass>(predefs + names + predefs2 + before,
406                                         predefs + names + predefs2 + after,
407                                         true, true);
408 }
409 
TEST_F(LocalSSAElimTest,ForLoopWithBreak)410 TEST_F(LocalSSAElimTest, ForLoopWithBreak) {
411   // #version 140
412   //
413   // in vec4 BC;
414   // out float fo;
415   //
416   // void main()
417   // {
418   //     float f = 0.0;
419   //     for (int i=0; i<4; i++) {
420   //       float t = f + BC[i];
421   //       if (t > 1.0)
422   //         break;
423   //       f = t;
424   //     }
425   //     fo = f;
426   // }
427 
428   const std::string predefs =
429       R"(OpCapability Shader
430 %1 = OpExtInstImport "GLSL.std.450"
431 OpMemoryModel Logical GLSL450
432 OpEntryPoint Fragment %main "main" %BC %fo
433 OpExecutionMode %main OriginUpperLeft
434 OpSource GLSL 140
435 OpName %main "main"
436 OpName %f "f"
437 OpName %i "i"
438 OpName %t "t"
439 OpName %BC "BC"
440 OpName %fo "fo"
441 %void = OpTypeVoid
442 %9 = OpTypeFunction %void
443 %float = OpTypeFloat 32
444 %_ptr_Function_float = OpTypePointer Function %float
445 %float_0 = OpConstant %float 0
446 %int = OpTypeInt 32 1
447 %_ptr_Function_int = OpTypePointer Function %int
448 %int_0 = OpConstant %int 0
449 %int_4 = OpConstant %int 4
450 %bool = OpTypeBool
451 %v4float = OpTypeVector %float 4
452 %_ptr_Input_v4float = OpTypePointer Input %v4float
453 %BC = OpVariable %_ptr_Input_v4float Input
454 %_ptr_Input_float = OpTypePointer Input %float
455 %float_1 = OpConstant %float 1
456 %int_1 = OpConstant %int 1
457 %_ptr_Output_float = OpTypePointer Output %float
458 %fo = OpVariable %_ptr_Output_float Output
459 )";
460 
461   const std::string before =
462       R"(%main = OpFunction %void None %9
463 %24 = OpLabel
464 %f = OpVariable %_ptr_Function_float Function
465 %i = OpVariable %_ptr_Function_int Function
466 %t = OpVariable %_ptr_Function_float Function
467 OpStore %f %float_0
468 OpStore %i %int_0
469 OpBranch %25
470 %25 = OpLabel
471 OpLoopMerge %26 %27 None
472 OpBranch %28
473 %28 = OpLabel
474 %29 = OpLoad %int %i
475 %30 = OpSLessThan %bool %29 %int_4
476 OpBranchConditional %30 %31 %26
477 %31 = OpLabel
478 %32 = OpLoad %float %f
479 %33 = OpLoad %int %i
480 %34 = OpAccessChain %_ptr_Input_float %BC %33
481 %35 = OpLoad %float %34
482 %36 = OpFAdd %float %32 %35
483 OpStore %t %36
484 %37 = OpLoad %float %t
485 %38 = OpFOrdGreaterThan %bool %37 %float_1
486 OpSelectionMerge %39 None
487 OpBranchConditional %38 %40 %39
488 %40 = OpLabel
489 OpBranch %26
490 %39 = OpLabel
491 %41 = OpLoad %float %t
492 OpStore %f %41
493 OpBranch %27
494 %27 = OpLabel
495 %42 = OpLoad %int %i
496 %43 = OpIAdd %int %42 %int_1
497 OpStore %i %43
498 OpBranch %25
499 %26 = OpLabel
500 %44 = OpLoad %float %f
501 OpStore %fo %44
502 OpReturn
503 OpFunctionEnd
504 )";
505 
506   const std::string after =
507       R"(%main = OpFunction %void None %9
508 %24 = OpLabel
509 %f = OpVariable %_ptr_Function_float Function
510 %i = OpVariable %_ptr_Function_int Function
511 %t = OpVariable %_ptr_Function_float Function
512 OpStore %f %float_0
513 OpStore %i %int_0
514 OpBranch %25
515 %25 = OpLabel
516 %46 = OpPhi %float %float_0 %24 %36 %27
517 %45 = OpPhi %int %int_0 %24 %43 %27
518 OpLoopMerge %26 %27 None
519 OpBranch %28
520 %28 = OpLabel
521 %30 = OpSLessThan %bool %45 %int_4
522 OpBranchConditional %30 %31 %26
523 %31 = OpLabel
524 %34 = OpAccessChain %_ptr_Input_float %BC %45
525 %35 = OpLoad %float %34
526 %36 = OpFAdd %float %46 %35
527 OpStore %t %36
528 %38 = OpFOrdGreaterThan %bool %36 %float_1
529 OpSelectionMerge %39 None
530 OpBranchConditional %38 %40 %39
531 %40 = OpLabel
532 OpBranch %26
533 %39 = OpLabel
534 OpStore %f %36
535 OpBranch %27
536 %27 = OpLabel
537 %43 = OpIAdd %int %45 %int_1
538 OpStore %i %43
539 OpBranch %25
540 %26 = OpLabel
541 OpStore %fo %46
542 OpReturn
543 OpFunctionEnd
544 )";
545 
546   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
547                                         true);
548 }
549 
TEST_F(LocalSSAElimTest,SwapProblem)550 TEST_F(LocalSSAElimTest, SwapProblem) {
551   // #version 140
552   //
553   // in float fe;
554   // out float fo;
555   //
556   // void main()
557   // {
558   //     float f1 = 0.0;
559   //     float f2 = 1.0;
560   //     int ie = int(fe);
561   //     for (int i=0; i<ie; i++) {
562   //       float t = f1;
563   //       f1 = f2;
564   //       f2 = t;
565   //     }
566   //     fo = f1;
567   // }
568 
569   const std::string predefs =
570       R"(OpCapability Shader
571 %1 = OpExtInstImport "GLSL.std.450"
572 OpMemoryModel Logical GLSL450
573 OpEntryPoint Fragment %main "main" %fe %fo
574 OpExecutionMode %main OriginUpperLeft
575 OpSource GLSL 140
576 OpName %main "main"
577 OpName %f1 "f1"
578 OpName %f2 "f2"
579 OpName %ie "ie"
580 OpName %fe "fe"
581 OpName %i "i"
582 OpName %t "t"
583 OpName %fo "fo"
584 %void = OpTypeVoid
585 %11 = OpTypeFunction %void
586 %float = OpTypeFloat 32
587 %_ptr_Function_float = OpTypePointer Function %float
588 %float_0 = OpConstant %float 0
589 %float_1 = OpConstant %float 1
590 %int = OpTypeInt 32 1
591 %_ptr_Function_int = OpTypePointer Function %int
592 %_ptr_Input_float = OpTypePointer Input %float
593 %fe = OpVariable %_ptr_Input_float Input
594 %int_0 = OpConstant %int 0
595 %bool = OpTypeBool
596 %int_1 = OpConstant %int 1
597 %_ptr_Output_float = OpTypePointer Output %float
598 %fo = OpVariable %_ptr_Output_float Output
599 )";
600 
601   const std::string before =
602       R"(%main = OpFunction %void None %11
603 %23 = OpLabel
604 %f1 = OpVariable %_ptr_Function_float Function
605 %f2 = OpVariable %_ptr_Function_float Function
606 %ie = OpVariable %_ptr_Function_int Function
607 %i = OpVariable %_ptr_Function_int Function
608 %t = OpVariable %_ptr_Function_float Function
609 OpStore %f1 %float_0
610 OpStore %f2 %float_1
611 %24 = OpLoad %float %fe
612 %25 = OpConvertFToS %int %24
613 OpStore %ie %25
614 OpStore %i %int_0
615 OpBranch %26
616 %26 = OpLabel
617 OpLoopMerge %27 %28 None
618 OpBranch %29
619 %29 = OpLabel
620 %30 = OpLoad %int %i
621 %31 = OpLoad %int %ie
622 %32 = OpSLessThan %bool %30 %31
623 OpBranchConditional %32 %33 %27
624 %33 = OpLabel
625 %34 = OpLoad %float %f1
626 OpStore %t %34
627 %35 = OpLoad %float %f2
628 OpStore %f1 %35
629 %36 = OpLoad %float %t
630 OpStore %f2 %36
631 OpBranch %28
632 %28 = OpLabel
633 %37 = OpLoad %int %i
634 %38 = OpIAdd %int %37 %int_1
635 OpStore %i %38
636 OpBranch %26
637 %27 = OpLabel
638 %39 = OpLoad %float %f1
639 OpStore %fo %39
640 OpReturn
641 OpFunctionEnd
642 )";
643 
644   const std::string after =
645       R"(%main = OpFunction %void None %11
646 %23 = OpLabel
647 %f1 = OpVariable %_ptr_Function_float Function
648 %f2 = OpVariable %_ptr_Function_float Function
649 %ie = OpVariable %_ptr_Function_int Function
650 %i = OpVariable %_ptr_Function_int Function
651 %t = OpVariable %_ptr_Function_float Function
652 OpStore %f1 %float_0
653 OpStore %f2 %float_1
654 %24 = OpLoad %float %fe
655 %25 = OpConvertFToS %int %24
656 OpStore %ie %25
657 OpStore %i %int_0
658 OpBranch %26
659 %26 = OpLabel
660 %43 = OpPhi %float %float_1 %23 %42 %28
661 %42 = OpPhi %float %float_0 %23 %43 %28
662 %40 = OpPhi %int %int_0 %23 %38 %28
663 OpLoopMerge %27 %28 None
664 OpBranch %29
665 %29 = OpLabel
666 %32 = OpSLessThan %bool %40 %25
667 OpBranchConditional %32 %33 %27
668 %33 = OpLabel
669 OpStore %t %42
670 OpStore %f1 %43
671 OpStore %f2 %42
672 OpBranch %28
673 %28 = OpLabel
674 %38 = OpIAdd %int %40 %int_1
675 OpStore %i %38
676 OpBranch %26
677 %27 = OpLabel
678 OpStore %fo %42
679 OpReturn
680 OpFunctionEnd
681 )";
682 
683   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
684                                         true);
685 }
686 
TEST_F(LocalSSAElimTest,LostCopyProblem)687 TEST_F(LocalSSAElimTest, LostCopyProblem) {
688   // #version 140
689   //
690   // in vec4 BC;
691   // out float fo;
692   //
693   // void main()
694   // {
695   //     float f = 0.0;
696   //     float t;
697   //     for (int i=0; i<4; i++) {
698   //       t = f;
699   //       f = f + BC[i];
700   //       if (f > 1.0)
701   //         break;
702   //     }
703   //     fo = t;
704   // }
705 
706   const std::string predefs =
707       R"(OpCapability Shader
708 %1 = OpExtInstImport "GLSL.std.450"
709 OpMemoryModel Logical GLSL450
710 OpEntryPoint Fragment %main "main" %BC %fo
711 OpExecutionMode %main OriginUpperLeft
712 OpSource GLSL 140
713 OpName %main "main"
714 OpName %f "f"
715 OpName %i "i"
716 OpName %t "t"
717 OpName %BC "BC"
718 OpName %fo "fo"
719 %void = OpTypeVoid
720 %9 = OpTypeFunction %void
721 %float = OpTypeFloat 32
722 %_ptr_Function_float = OpTypePointer Function %float
723 %float_0 = OpConstant %float 0
724 %int = OpTypeInt 32 1
725 %_ptr_Function_int = OpTypePointer Function %int
726 %int_0 = OpConstant %int 0
727 %int_4 = OpConstant %int 4
728 %bool = OpTypeBool
729 %v4float = OpTypeVector %float 4
730 %_ptr_Input_v4float = OpTypePointer Input %v4float
731 %BC = OpVariable %_ptr_Input_v4float Input
732 %_ptr_Input_float = OpTypePointer Input %float
733 %float_1 = OpConstant %float 1
734 %int_1 = OpConstant %int 1
735 %_ptr_Output_float = OpTypePointer Output %float
736 %fo = OpVariable %_ptr_Output_float Output
737 )";
738 
739   const std::string before =
740       R"(%main = OpFunction %void None %9
741 %24 = OpLabel
742 %f = OpVariable %_ptr_Function_float Function
743 %i = OpVariable %_ptr_Function_int Function
744 %t = OpVariable %_ptr_Function_float Function
745 OpStore %f %float_0
746 OpStore %i %int_0
747 OpBranch %25
748 %25 = OpLabel
749 OpLoopMerge %26 %27 None
750 OpBranch %28
751 %28 = OpLabel
752 %29 = OpLoad %int %i
753 %30 = OpSLessThan %bool %29 %int_4
754 OpBranchConditional %30 %31 %26
755 %31 = OpLabel
756 %32 = OpLoad %float %f
757 OpStore %t %32
758 %33 = OpLoad %float %f
759 %34 = OpLoad %int %i
760 %35 = OpAccessChain %_ptr_Input_float %BC %34
761 %36 = OpLoad %float %35
762 %37 = OpFAdd %float %33 %36
763 OpStore %f %37
764 %38 = OpLoad %float %f
765 %39 = OpFOrdGreaterThan %bool %38 %float_1
766 OpSelectionMerge %40 None
767 OpBranchConditional %39 %41 %40
768 %41 = OpLabel
769 OpBranch %26
770 %40 = OpLabel
771 OpBranch %27
772 %27 = OpLabel
773 %42 = OpLoad %int %i
774 %43 = OpIAdd %int %42 %int_1
775 OpStore %i %43
776 OpBranch %25
777 %26 = OpLabel
778 %44 = OpLoad %float %t
779 OpStore %fo %44
780 OpReturn
781 OpFunctionEnd
782 )";
783 
784   const std::string after =
785       R"(%49 = OpUndef %float
786 %main = OpFunction %void None %9
787 %24 = OpLabel
788 %f = OpVariable %_ptr_Function_float Function
789 %i = OpVariable %_ptr_Function_int Function
790 %t = OpVariable %_ptr_Function_float Function
791 OpStore %f %float_0
792 OpStore %i %int_0
793 OpBranch %25
794 %25 = OpLabel
795 %46 = OpPhi %float %float_0 %24 %37 %27
796 %45 = OpPhi %int %int_0 %24 %43 %27
797 %48 = OpPhi %float %49 %24 %46 %27
798 OpLoopMerge %26 %27 None
799 OpBranch %28
800 %28 = OpLabel
801 %30 = OpSLessThan %bool %45 %int_4
802 OpBranchConditional %30 %31 %26
803 %31 = OpLabel
804 OpStore %t %46
805 %35 = OpAccessChain %_ptr_Input_float %BC %45
806 %36 = OpLoad %float %35
807 %37 = OpFAdd %float %46 %36
808 OpStore %f %37
809 %39 = OpFOrdGreaterThan %bool %37 %float_1
810 OpSelectionMerge %40 None
811 OpBranchConditional %39 %41 %40
812 %41 = OpLabel
813 OpBranch %26
814 %40 = OpLabel
815 OpBranch %27
816 %27 = OpLabel
817 %43 = OpIAdd %int %45 %int_1
818 OpStore %i %43
819 OpBranch %25
820 %26 = OpLabel
821 %47 = OpPhi %float %48 %28 %46 %41
822 OpStore %fo %47
823 OpReturn
824 OpFunctionEnd
825 )";
826 
827   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
828                                         true);
829 }
830 
TEST_F(LocalSSAElimTest,IfThenElse)831 TEST_F(LocalSSAElimTest, IfThenElse) {
832   // #version 140
833   //
834   // in vec4 BaseColor;
835   // in float f;
836   //
837   // void main()
838   // {
839   //     vec4 v;
840   //     if (f >= 0)
841   //       v = BaseColor * 0.5;
842   //     else
843   //       v = BaseColor + vec4(1.0,1.0,1.0,1.0);
844   //     gl_FragColor = v;
845   // }
846 
847   const std::string predefs =
848       R"(OpCapability Shader
849 %1 = OpExtInstImport "GLSL.std.450"
850 OpMemoryModel Logical GLSL450
851 OpEntryPoint Fragment %main "main" %f %BaseColor %gl_FragColor
852 OpExecutionMode %main OriginUpperLeft
853 OpSource GLSL 140
854 OpName %main "main"
855 OpName %f "f"
856 OpName %v "v"
857 OpName %BaseColor "BaseColor"
858 OpName %gl_FragColor "gl_FragColor"
859 %void = OpTypeVoid
860 %8 = OpTypeFunction %void
861 %float = OpTypeFloat 32
862 %_ptr_Input_float = OpTypePointer Input %float
863 %f = OpVariable %_ptr_Input_float Input
864 %float_0 = OpConstant %float 0
865 %bool = OpTypeBool
866 %v4float = OpTypeVector %float 4
867 %_ptr_Function_v4float = OpTypePointer Function %v4float
868 %_ptr_Input_v4float = OpTypePointer Input %v4float
869 %BaseColor = OpVariable %_ptr_Input_v4float Input
870 %float_0_5 = OpConstant %float 0.5
871 %float_1 = OpConstant %float 1
872 %18 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
873 %_ptr_Output_v4float = OpTypePointer Output %v4float
874 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
875 )";
876 
877   const std::string before =
878       R"(%main = OpFunction %void None %8
879 %20 = OpLabel
880 %v = OpVariable %_ptr_Function_v4float Function
881 %21 = OpLoad %float %f
882 %22 = OpFOrdGreaterThanEqual %bool %21 %float_0
883 OpSelectionMerge %23 None
884 OpBranchConditional %22 %24 %25
885 %24 = OpLabel
886 %26 = OpLoad %v4float %BaseColor
887 %27 = OpVectorTimesScalar %v4float %26 %float_0_5
888 OpStore %v %27
889 OpBranch %23
890 %25 = OpLabel
891 %28 = OpLoad %v4float %BaseColor
892 %29 = OpFAdd %v4float %28 %18
893 OpStore %v %29
894 OpBranch %23
895 %23 = OpLabel
896 %30 = OpLoad %v4float %v
897 OpStore %gl_FragColor %30
898 OpReturn
899 OpFunctionEnd
900 )";
901 
902   const std::string after =
903       R"(%main = OpFunction %void None %8
904 %20 = OpLabel
905 %v = OpVariable %_ptr_Function_v4float Function
906 %21 = OpLoad %float %f
907 %22 = OpFOrdGreaterThanEqual %bool %21 %float_0
908 OpSelectionMerge %23 None
909 OpBranchConditional %22 %24 %25
910 %24 = OpLabel
911 %26 = OpLoad %v4float %BaseColor
912 %27 = OpVectorTimesScalar %v4float %26 %float_0_5
913 OpStore %v %27
914 OpBranch %23
915 %25 = OpLabel
916 %28 = OpLoad %v4float %BaseColor
917 %29 = OpFAdd %v4float %28 %18
918 OpStore %v %29
919 OpBranch %23
920 %23 = OpLabel
921 %31 = OpPhi %v4float %27 %24 %29 %25
922 OpStore %gl_FragColor %31
923 OpReturn
924 OpFunctionEnd
925 )";
926 
927   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
928                                         true);
929 }
930 
TEST_F(LocalSSAElimTest,IfThen)931 TEST_F(LocalSSAElimTest, IfThen) {
932   // #version 140
933   //
934   // in vec4 BaseColor;
935   // in float f;
936   //
937   // void main()
938   // {
939   //     vec4 v = BaseColor;
940   //     if (f <= 0)
941   //       v = v * 0.5;
942   //     gl_FragColor = v;
943   // }
944 
945   const std::string predefs =
946       R"(OpCapability Shader
947 %1 = OpExtInstImport "GLSL.std.450"
948 OpMemoryModel Logical GLSL450
949 OpEntryPoint Fragment %main "main" %BaseColor %f %gl_FragColor
950 OpExecutionMode %main OriginUpperLeft
951 OpSource GLSL 140
952 OpName %main "main"
953 OpName %v "v"
954 OpName %BaseColor "BaseColor"
955 OpName %f "f"
956 OpName %gl_FragColor "gl_FragColor"
957 %void = OpTypeVoid
958 %8 = OpTypeFunction %void
959 %float = OpTypeFloat 32
960 %v4float = OpTypeVector %float 4
961 %_ptr_Function_v4float = OpTypePointer Function %v4float
962 %_ptr_Input_v4float = OpTypePointer Input %v4float
963 %BaseColor = OpVariable %_ptr_Input_v4float Input
964 %_ptr_Input_float = OpTypePointer Input %float
965 %f = OpVariable %_ptr_Input_float Input
966 %float_0 = OpConstant %float 0
967 %bool = OpTypeBool
968 %float_0_5 = OpConstant %float 0.5
969 %_ptr_Output_v4float = OpTypePointer Output %v4float
970 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
971 )";
972 
973   const std::string before =
974       R"(%main = OpFunction %void None %8
975 %18 = OpLabel
976 %v = OpVariable %_ptr_Function_v4float Function
977 %19 = OpLoad %v4float %BaseColor
978 OpStore %v %19
979 %20 = OpLoad %float %f
980 %21 = OpFOrdLessThanEqual %bool %20 %float_0
981 OpSelectionMerge %22 None
982 OpBranchConditional %21 %23 %22
983 %23 = OpLabel
984 %24 = OpLoad %v4float %v
985 %25 = OpVectorTimesScalar %v4float %24 %float_0_5
986 OpStore %v %25
987 OpBranch %22
988 %22 = OpLabel
989 %26 = OpLoad %v4float %v
990 OpStore %gl_FragColor %26
991 OpReturn
992 OpFunctionEnd
993 )";
994 
995   const std::string after =
996       R"(%main = OpFunction %void None %8
997 %18 = OpLabel
998 %v = OpVariable %_ptr_Function_v4float Function
999 %19 = OpLoad %v4float %BaseColor
1000 OpStore %v %19
1001 %20 = OpLoad %float %f
1002 %21 = OpFOrdLessThanEqual %bool %20 %float_0
1003 OpSelectionMerge %22 None
1004 OpBranchConditional %21 %23 %22
1005 %23 = OpLabel
1006 %25 = OpVectorTimesScalar %v4float %19 %float_0_5
1007 OpStore %v %25
1008 OpBranch %22
1009 %22 = OpLabel
1010 %27 = OpPhi %v4float %19 %18 %25 %23
1011 OpStore %gl_FragColor %27
1012 OpReturn
1013 OpFunctionEnd
1014 )";
1015 
1016   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
1017                                         true);
1018 }
1019 
TEST_F(LocalSSAElimTest,Switch)1020 TEST_F(LocalSSAElimTest, Switch) {
1021   // #version 140
1022   //
1023   // in vec4 BaseColor;
1024   // in float f;
1025   //
1026   // void main()
1027   // {
1028   //     vec4 v = BaseColor;
1029   //     int i = int(f);
1030   //     switch (i) {
1031   //       case 0:
1032   //         v = v * 0.25;
1033   //         break;
1034   //       case 1:
1035   //         v = v * 0.625;
1036   //         break;
1037   //       case 2:
1038   //         v = v * 0.75;
1039   //         break;
1040   //       default:
1041   //         break;
1042   //     }
1043   //     gl_FragColor = v;
1044   // }
1045 
1046   const std::string predefs =
1047       R"(OpCapability Shader
1048 %1 = OpExtInstImport "GLSL.std.450"
1049 OpMemoryModel Logical GLSL450
1050 OpEntryPoint Fragment %main "main" %BaseColor %f %gl_FragColor
1051 OpExecutionMode %main OriginUpperLeft
1052 OpSource GLSL 140
1053 OpName %main "main"
1054 OpName %v "v"
1055 OpName %BaseColor "BaseColor"
1056 OpName %i "i"
1057 OpName %f "f"
1058 OpName %gl_FragColor "gl_FragColor"
1059 %void = OpTypeVoid
1060 %9 = OpTypeFunction %void
1061 %float = OpTypeFloat 32
1062 %v4float = OpTypeVector %float 4
1063 %_ptr_Function_v4float = OpTypePointer Function %v4float
1064 %_ptr_Input_v4float = OpTypePointer Input %v4float
1065 %BaseColor = OpVariable %_ptr_Input_v4float Input
1066 %int = OpTypeInt 32 1
1067 %_ptr_Function_int = OpTypePointer Function %int
1068 %_ptr_Input_float = OpTypePointer Input %float
1069 %f = OpVariable %_ptr_Input_float Input
1070 %float_0_25 = OpConstant %float 0.25
1071 %float_0_625 = OpConstant %float 0.625
1072 %float_0_75 = OpConstant %float 0.75
1073 %_ptr_Output_v4float = OpTypePointer Output %v4float
1074 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1075 )";
1076 
1077   const std::string before =
1078       R"(%main = OpFunction %void None %9
1079 %21 = OpLabel
1080 %v = OpVariable %_ptr_Function_v4float Function
1081 %i = OpVariable %_ptr_Function_int Function
1082 %22 = OpLoad %v4float %BaseColor
1083 OpStore %v %22
1084 %23 = OpLoad %float %f
1085 %24 = OpConvertFToS %int %23
1086 OpStore %i %24
1087 %25 = OpLoad %int %i
1088 OpSelectionMerge %26 None
1089 OpSwitch %25 %27 0 %28 1 %29 2 %30
1090 %27 = OpLabel
1091 OpBranch %26
1092 %28 = OpLabel
1093 %31 = OpLoad %v4float %v
1094 %32 = OpVectorTimesScalar %v4float %31 %float_0_25
1095 OpStore %v %32
1096 OpBranch %26
1097 %29 = OpLabel
1098 %33 = OpLoad %v4float %v
1099 %34 = OpVectorTimesScalar %v4float %33 %float_0_625
1100 OpStore %v %34
1101 OpBranch %26
1102 %30 = OpLabel
1103 %35 = OpLoad %v4float %v
1104 %36 = OpVectorTimesScalar %v4float %35 %float_0_75
1105 OpStore %v %36
1106 OpBranch %26
1107 %26 = OpLabel
1108 %37 = OpLoad %v4float %v
1109 OpStore %gl_FragColor %37
1110 OpReturn
1111 OpFunctionEnd
1112 )";
1113 
1114   const std::string after =
1115       R"(%main = OpFunction %void None %9
1116 %21 = OpLabel
1117 %v = OpVariable %_ptr_Function_v4float Function
1118 %i = OpVariable %_ptr_Function_int Function
1119 %22 = OpLoad %v4float %BaseColor
1120 OpStore %v %22
1121 %23 = OpLoad %float %f
1122 %24 = OpConvertFToS %int %23
1123 OpStore %i %24
1124 OpSelectionMerge %26 None
1125 OpSwitch %24 %27 0 %28 1 %29 2 %30
1126 %27 = OpLabel
1127 OpBranch %26
1128 %28 = OpLabel
1129 %32 = OpVectorTimesScalar %v4float %22 %float_0_25
1130 OpStore %v %32
1131 OpBranch %26
1132 %29 = OpLabel
1133 %34 = OpVectorTimesScalar %v4float %22 %float_0_625
1134 OpStore %v %34
1135 OpBranch %26
1136 %30 = OpLabel
1137 %36 = OpVectorTimesScalar %v4float %22 %float_0_75
1138 OpStore %v %36
1139 OpBranch %26
1140 %26 = OpLabel
1141 %38 = OpPhi %v4float %22 %27 %32 %28 %34 %29 %36 %30
1142 OpStore %gl_FragColor %38
1143 OpReturn
1144 OpFunctionEnd
1145 )";
1146 
1147   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
1148                                         true);
1149 }
1150 
TEST_F(LocalSSAElimTest,SwitchWithFallThrough)1151 TEST_F(LocalSSAElimTest, SwitchWithFallThrough) {
1152   // #version 140
1153   //
1154   // in vec4 BaseColor;
1155   // in float f;
1156   //
1157   // void main()
1158   // {
1159   //     vec4 v = BaseColor;
1160   //     int i = int(f);
1161   //     switch (i) {
1162   //       case 0:
1163   //         v = v * 0.25;
1164   //         break;
1165   //       case 1:
1166   //         v = v + 0.25;
1167   //       case 2:
1168   //         v = v * 0.75;
1169   //         break;
1170   //       default:
1171   //         break;
1172   //     }
1173   //     gl_FragColor = v;
1174   // }
1175 
1176   const std::string predefs =
1177       R"(OpCapability Shader
1178 %1 = OpExtInstImport "GLSL.std.450"
1179 OpMemoryModel Logical GLSL450
1180 OpEntryPoint Fragment %main "main" %BaseColor %f %gl_FragColor
1181 OpExecutionMode %main OriginUpperLeft
1182 OpSource GLSL 140
1183 OpName %main "main"
1184 OpName %v "v"
1185 OpName %BaseColor "BaseColor"
1186 OpName %i "i"
1187 OpName %f "f"
1188 OpName %gl_FragColor "gl_FragColor"
1189 %void = OpTypeVoid
1190 %9 = OpTypeFunction %void
1191 %float = OpTypeFloat 32
1192 %v4float = OpTypeVector %float 4
1193 %_ptr_Function_v4float = OpTypePointer Function %v4float
1194 %_ptr_Input_v4float = OpTypePointer Input %v4float
1195 %BaseColor = OpVariable %_ptr_Input_v4float Input
1196 %int = OpTypeInt 32 1
1197 %_ptr_Function_int = OpTypePointer Function %int
1198 %_ptr_Input_float = OpTypePointer Input %float
1199 %f = OpVariable %_ptr_Input_float Input
1200 %float_0_25 = OpConstant %float 0.25
1201 %float_0_75 = OpConstant %float 0.75
1202 %_ptr_Output_v4float = OpTypePointer Output %v4float
1203 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
1204 )";
1205 
1206   const std::string before =
1207       R"(%main = OpFunction %void None %9
1208 %20 = OpLabel
1209 %v = OpVariable %_ptr_Function_v4float Function
1210 %i = OpVariable %_ptr_Function_int Function
1211 %21 = OpLoad %v4float %BaseColor
1212 OpStore %v %21
1213 %22 = OpLoad %float %f
1214 %23 = OpConvertFToS %int %22
1215 OpStore %i %23
1216 %24 = OpLoad %int %i
1217 OpSelectionMerge %25 None
1218 OpSwitch %24 %26 0 %27 1 %28 2 %29
1219 %26 = OpLabel
1220 OpBranch %25
1221 %27 = OpLabel
1222 %30 = OpLoad %v4float %v
1223 %31 = OpVectorTimesScalar %v4float %30 %float_0_25
1224 OpStore %v %31
1225 OpBranch %25
1226 %28 = OpLabel
1227 %32 = OpLoad %v4float %v
1228 %33 = OpCompositeConstruct %v4float %float_0_25 %float_0_25 %float_0_25 %float_0_25
1229 %34 = OpFAdd %v4float %32 %33
1230 OpStore %v %34
1231 OpBranch %29
1232 %29 = OpLabel
1233 %35 = OpLoad %v4float %v
1234 %36 = OpVectorTimesScalar %v4float %35 %float_0_75
1235 OpStore %v %36
1236 OpBranch %25
1237 %25 = OpLabel
1238 %37 = OpLoad %v4float %v
1239 OpStore %gl_FragColor %37
1240 OpReturn
1241 OpFunctionEnd
1242 )";
1243 
1244   const std::string after =
1245       R"(%main = OpFunction %void None %9
1246 %20 = OpLabel
1247 %v = OpVariable %_ptr_Function_v4float Function
1248 %i = OpVariable %_ptr_Function_int Function
1249 %21 = OpLoad %v4float %BaseColor
1250 OpStore %v %21
1251 %22 = OpLoad %float %f
1252 %23 = OpConvertFToS %int %22
1253 OpStore %i %23
1254 OpSelectionMerge %25 None
1255 OpSwitch %23 %26 0 %27 1 %28 2 %29
1256 %26 = OpLabel
1257 OpBranch %25
1258 %27 = OpLabel
1259 %31 = OpVectorTimesScalar %v4float %21 %float_0_25
1260 OpStore %v %31
1261 OpBranch %25
1262 %28 = OpLabel
1263 %33 = OpCompositeConstruct %v4float %float_0_25 %float_0_25 %float_0_25 %float_0_25
1264 %34 = OpFAdd %v4float %21 %33
1265 OpStore %v %34
1266 OpBranch %29
1267 %29 = OpLabel
1268 %38 = OpPhi %v4float %21 %20 %34 %28
1269 %36 = OpVectorTimesScalar %v4float %38 %float_0_75
1270 OpStore %v %36
1271 OpBranch %25
1272 %25 = OpLabel
1273 %39 = OpPhi %v4float %21 %26 %31 %27 %36 %29
1274 OpStore %gl_FragColor %39
1275 OpReturn
1276 OpFunctionEnd
1277 )";
1278 
1279   SinglePassRunAndCheck<SSARewritePass>(predefs + before, predefs + after, true,
1280                                         true);
1281 }
1282 
TEST_F(LocalSSAElimTest,DontPatchPhiInLoopHeaderThatIsNotAVar)1283 TEST_F(LocalSSAElimTest, DontPatchPhiInLoopHeaderThatIsNotAVar) {
1284   // From https://github.com/KhronosGroup/SPIRV-Tools/issues/826
1285   // Don't try patching the (%16 %7) value/predecessor pair in the OpPhi.
1286   // That OpPhi is unrelated to this optimization: we did not set that up
1287   // in the SSA initialization for the loop header block.
1288   // The pass should be a no-op on this module.
1289 
1290   const std::string before = R"(OpCapability Shader
1291 OpMemoryModel Logical GLSL450
1292 OpEntryPoint GLCompute %1 "main"
1293 %void = OpTypeVoid
1294 %3 = OpTypeFunction %void
1295 %float = OpTypeFloat 32
1296 %float_1 = OpConstant %float 1
1297 %1 = OpFunction %void None %3
1298 %6 = OpLabel
1299 OpBranch %7
1300 %7 = OpLabel
1301 %8 = OpPhi %float %float_1 %6 %9 %7
1302 %9 = OpFAdd %float %8 %float_1
1303 OpLoopMerge %10 %7 None
1304 OpBranch %7
1305 %10 = OpLabel
1306 OpReturn
1307 OpFunctionEnd
1308 )";
1309 
1310   SinglePassRunAndCheck<SSARewritePass>(before, before, true, true);
1311 }
1312 
TEST_F(LocalSSAElimTest,OptInitializedVariableLikeStore)1313 TEST_F(LocalSSAElimTest, OptInitializedVariableLikeStore) {
1314   // Note: SPIR-V edited to change store to v into variable initialization
1315   //
1316   // #version 450
1317   //
1318   // layout (location=0) in vec4 iColor;
1319   // layout (location=1) in float fi;
1320   // layout (location=0) out vec4 oColor;
1321   //
1322   // void main()
1323   // {
1324   //     vec4 v = vec4(0.0);
1325   //     if (fi < 0.0)
1326   //       v.x = iColor.x;
1327   //     oColor = v;
1328   // }
1329 
1330   const std::string predefs =
1331       R"(OpCapability Shader
1332 %1 = OpExtInstImport "GLSL.std.450"
1333 OpMemoryModel Logical GLSL450
1334 OpEntryPoint Fragment %main "main" %fi %iColor %oColor
1335 OpExecutionMode %main OriginUpperLeft
1336 OpSource GLSL 450
1337 OpName %main "main"
1338 OpName %v "v"
1339 OpName %fi "fi"
1340 OpName %iColor "iColor"
1341 OpName %oColor "oColor"
1342 OpDecorate %fi Location 1
1343 OpDecorate %iColor Location 0
1344 OpDecorate %oColor Location 0
1345 %void = OpTypeVoid
1346 %8 = OpTypeFunction %void
1347 %float = OpTypeFloat 32
1348 %v4float = OpTypeVector %float 4
1349 %_ptr_Function_v4float = OpTypePointer Function %v4float
1350 %float_0 = OpConstant %float 0
1351 %13 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
1352 %_ptr_Input_float = OpTypePointer Input %float
1353 %fi = OpVariable %_ptr_Input_float Input
1354 %bool = OpTypeBool
1355 %_ptr_Input_v4float = OpTypePointer Input %v4float
1356 %iColor = OpVariable %_ptr_Input_v4float Input
1357 %uint = OpTypeInt 32 0
1358 %uint_0 = OpConstant %uint 0
1359 %_ptr_Function_float = OpTypePointer Function %float
1360 %_ptr_Output_v4float = OpTypePointer Output %v4float
1361 %oColor = OpVariable %_ptr_Output_v4float Output
1362 )";
1363 
1364   const std::string func_before =
1365       R"(%main = OpFunction %void None %8
1366 %21 = OpLabel
1367 %v = OpVariable %_ptr_Function_v4float Function %13
1368 %22 = OpLoad %float %fi
1369 %23 = OpFOrdLessThan %bool %22 %float_0
1370 OpSelectionMerge %24 None
1371 OpBranchConditional %23 %25 %24
1372 %25 = OpLabel
1373 %26 = OpAccessChain %_ptr_Input_float %iColor %uint_0
1374 %27 = OpLoad %float %26
1375 %28 = OpLoad %v4float %v
1376 %29 = OpCompositeInsert %v4float %27 %28 0
1377 OpStore %v %29
1378 OpBranch %24
1379 %24 = OpLabel
1380 %30 = OpLoad %v4float %v
1381 OpStore %oColor %30
1382 OpReturn
1383 OpFunctionEnd
1384 )";
1385 
1386   const std::string func_after =
1387       R"(%main = OpFunction %void None %8
1388 %21 = OpLabel
1389 %v = OpVariable %_ptr_Function_v4float Function %13
1390 %22 = OpLoad %float %fi
1391 %23 = OpFOrdLessThan %bool %22 %float_0
1392 OpSelectionMerge %24 None
1393 OpBranchConditional %23 %25 %24
1394 %25 = OpLabel
1395 %26 = OpAccessChain %_ptr_Input_float %iColor %uint_0
1396 %27 = OpLoad %float %26
1397 %29 = OpCompositeInsert %v4float %27 %13 0
1398 OpStore %v %29
1399 OpBranch %24
1400 %24 = OpLabel
1401 %31 = OpPhi %v4float %13 %21 %29 %25
1402 OpStore %oColor %31
1403 OpReturn
1404 OpFunctionEnd
1405 )";
1406 
1407   SinglePassRunAndCheck<SSARewritePass>(predefs + func_before,
1408                                         predefs + func_after, true, true);
1409 }
1410 
TEST_F(LocalSSAElimTest,PointerVariable)1411 TEST_F(LocalSSAElimTest, PointerVariable) {
1412   // Test that checks if a pointer variable is removed.
1413 
1414   const std::string before =
1415       R"(OpCapability Shader
1416 OpMemoryModel Logical GLSL450
1417 OpEntryPoint Fragment %1 "main" %2
1418 OpExecutionMode %1 OriginUpperLeft
1419 OpMemberDecorate %_struct_3 0 Offset 0
1420 OpDecorate %_runtimearr__struct_3 ArrayStride 16
1421 OpMemberDecorate %_struct_5 0 Offset 0
1422 OpDecorate %_struct_5 BufferBlock
1423 OpMemberDecorate %_struct_6 0 Offset 0
1424 OpDecorate %_struct_6 BufferBlock
1425 OpDecorate %2 Location 0
1426 OpDecorate %7 DescriptorSet 0
1427 OpDecorate %7 Binding 0
1428 %void = OpTypeVoid
1429 %10 = OpTypeFunction %void
1430 %int = OpTypeInt 32 1
1431 %uint = OpTypeInt 32 0
1432 %float = OpTypeFloat 32
1433 %v4float = OpTypeVector %float 4
1434 %_ptr_Output_v4float = OpTypePointer Output %v4float
1435 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
1436 %_struct_3 = OpTypeStruct %v4float
1437 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
1438 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
1439 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
1440 %_struct_6 = OpTypeStruct %int
1441 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
1442 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
1443 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
1444 %int_0 = OpConstant %int 0
1445 %uint_0 = OpConstant %uint 0
1446 %2 = OpVariable %_ptr_Output_v4float Output
1447 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
1448 %1 = OpFunction %void None %10
1449 %23 = OpLabel
1450 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
1451 OpStore %24 %7
1452 %26 = OpLoad %_ptr_Uniform__struct_5 %24
1453 %27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0
1454 %28 = OpLoad %v4float %27
1455 %29 = OpCopyObject %v4float %28
1456 OpStore %2 %28
1457 OpReturn
1458 OpFunctionEnd
1459 )";
1460 
1461   const std::string after =
1462       R"(OpCapability Shader
1463 OpMemoryModel Logical GLSL450
1464 OpEntryPoint Fragment %1 "main" %2
1465 OpExecutionMode %1 OriginUpperLeft
1466 OpMemberDecorate %_struct_3 0 Offset 0
1467 OpDecorate %_runtimearr__struct_3 ArrayStride 16
1468 OpMemberDecorate %_struct_5 0 Offset 0
1469 OpDecorate %_struct_5 BufferBlock
1470 OpMemberDecorate %_struct_6 0 Offset 0
1471 OpDecorate %_struct_6 BufferBlock
1472 OpDecorate %2 Location 0
1473 OpDecorate %7 DescriptorSet 0
1474 OpDecorate %7 Binding 0
1475 %void = OpTypeVoid
1476 %10 = OpTypeFunction %void
1477 %int = OpTypeInt 32 1
1478 %uint = OpTypeInt 32 0
1479 %float = OpTypeFloat 32
1480 %v4float = OpTypeVector %float 4
1481 %_ptr_Output_v4float = OpTypePointer Output %v4float
1482 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
1483 %_struct_3 = OpTypeStruct %v4float
1484 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
1485 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
1486 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
1487 %_struct_6 = OpTypeStruct %int
1488 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
1489 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
1490 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
1491 %int_0 = OpConstant %int 0
1492 %uint_0 = OpConstant %uint 0
1493 %2 = OpVariable %_ptr_Output_v4float Output
1494 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
1495 %1 = OpFunction %void None %10
1496 %23 = OpLabel
1497 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
1498 OpStore %24 %7
1499 %27 = OpAccessChain %_ptr_Uniform_v4float %7 %int_0 %uint_0 %int_0
1500 %28 = OpLoad %v4float %27
1501 %29 = OpCopyObject %v4float %28
1502 OpStore %2 %28
1503 OpReturn
1504 OpFunctionEnd
1505 )";
1506 
1507   // Relax logical pointers to allow pointer allocations.
1508   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1509   ValidatorOptions()->relax_logical_pointer = true;
1510   SinglePassRunAndCheck<SSARewritePass>(before, after, true, true);
1511 }
1512 
TEST_F(LocalSSAElimTest,VerifyInstToBlockMap)1513 TEST_F(LocalSSAElimTest, VerifyInstToBlockMap) {
1514   // #version 140
1515   //
1516   // in vec4 BC;
1517   // out float fo;
1518   //
1519   // void main()
1520   // {
1521   //     float f = 0.0;
1522   //     for (int i=0; i<4; i++) {
1523   //       f = f + BC[i];
1524   //     }
1525   //     fo = f;
1526   // }
1527 
1528   const std::string text = R"(
1529 OpCapability Shader
1530 %1 = OpExtInstImport "GLSL.std.450"
1531 OpMemoryModel Logical GLSL450
1532 OpEntryPoint Fragment %main "main" %BC %fo
1533 OpExecutionMode %main OriginUpperLeft
1534 OpSource GLSL 140
1535 OpName %main "main"
1536 OpName %f "f"
1537 OpName %i "i"
1538 OpName %BC "BC"
1539 OpName %fo "fo"
1540 %void = OpTypeVoid
1541 %8 = OpTypeFunction %void
1542 %float = OpTypeFloat 32
1543 %_ptr_Function_float = OpTypePointer Function %float
1544 %float_0 = OpConstant %float 0
1545 %int = OpTypeInt 32 1
1546 %_ptr_Function_int = OpTypePointer Function %int
1547 %int_0 = OpConstant %int 0
1548 %int_4 = OpConstant %int 4
1549 %bool = OpTypeBool
1550 %v4float = OpTypeVector %float 4
1551 %_ptr_Input_v4float = OpTypePointer Input %v4float
1552 %BC = OpVariable %_ptr_Input_v4float Input
1553 %_ptr_Input_float = OpTypePointer Input %float
1554 %int_1 = OpConstant %int 1
1555 %_ptr_Output_float = OpTypePointer Output %float
1556 %fo = OpVariable %_ptr_Output_float Output
1557 %main = OpFunction %void None %8
1558 %22 = OpLabel
1559 %f = OpVariable %_ptr_Function_float Function
1560 %i = OpVariable %_ptr_Function_int Function
1561 OpStore %f %float_0
1562 OpStore %i %int_0
1563 OpBranch %23
1564 %23 = OpLabel
1565 OpLoopMerge %24 %25 None
1566 OpBranch %26
1567 %26 = OpLabel
1568 %27 = OpLoad %int %i
1569 %28 = OpSLessThan %bool %27 %int_4
1570 OpBranchConditional %28 %29 %24
1571 %29 = OpLabel
1572 %30 = OpLoad %float %f
1573 %31 = OpLoad %int %i
1574 %32 = OpAccessChain %_ptr_Input_float %BC %31
1575 %33 = OpLoad %float %32
1576 %34 = OpFAdd %float %30 %33
1577 OpStore %f %34
1578 OpBranch %25
1579 %25 = OpLabel
1580 %35 = OpLoad %int %i
1581 %36 = OpIAdd %int %35 %int_1
1582 OpStore %i %36
1583 OpBranch %23
1584 %24 = OpLabel
1585 %37 = OpLoad %float %f
1586 OpStore %fo %37
1587 OpReturn
1588 OpFunctionEnd
1589 )";
1590 
1591   std::unique_ptr<IRContext> context =
1592       BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
1593                   SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1594   EXPECT_NE(nullptr, context);
1595 
1596   // Force the instruction to block mapping to get built.
1597   context->get_instr_block(27u);
1598 
1599   auto pass = MakeUnique<SSARewritePass>();
1600   pass->SetMessageConsumer(nullptr);
1601   const auto status = pass->Run(context.get());
1602   EXPECT_TRUE(status == Pass::Status::SuccessWithChange);
1603 }
1604 
TEST_F(LocalSSAElimTest,CompositeExtractProblem)1605 TEST_F(LocalSSAElimTest, CompositeExtractProblem) {
1606   const std::string spv_asm = R"(
1607                OpCapability Tessellation
1608           %1 = OpExtInstImport "GLSL.std.450"
1609                OpMemoryModel Logical GLSL450
1610                OpEntryPoint TessellationControl %2 "main" %16 %17 %18 %20 %22 %26 %27 %30 %31
1611        %void = OpTypeVoid
1612           %4 = OpTypeFunction %void
1613       %float = OpTypeFloat 32
1614     %v4float = OpTypeVector %float 4
1615        %uint = OpTypeInt 32 0
1616      %uint_3 = OpConstant %uint 3
1617     %v3float = OpTypeVector %float 3
1618     %v2float = OpTypeVector %float 2
1619  %_struct_11 = OpTypeStruct %v4float %v4float %v4float %v3float %v3float %v2float %v2float
1620 %_arr__struct_11_uint_3 = OpTypeArray %_struct_11 %uint_3
1621 %_ptr_Function__arr__struct_11_uint_3 = OpTypePointer Function %_arr__struct_11_uint_3
1622 %_arr_v4float_uint_3 = OpTypeArray %v4float %uint_3
1623 %_ptr_Input__arr_v4float_uint_3 = OpTypePointer Input %_arr_v4float_uint_3
1624          %16 = OpVariable %_ptr_Input__arr_v4float_uint_3 Input
1625          %17 = OpVariable %_ptr_Input__arr_v4float_uint_3 Input
1626          %18 = OpVariable %_ptr_Input__arr_v4float_uint_3 Input
1627 %_ptr_Input_uint = OpTypePointer Input %uint
1628          %20 = OpVariable %_ptr_Input_uint Input
1629 %_ptr_Output__arr_v4float_uint_3 = OpTypePointer Output %_arr_v4float_uint_3
1630          %22 = OpVariable %_ptr_Output__arr_v4float_uint_3 Output
1631 %_ptr_Output_v4float = OpTypePointer Output %v4float
1632 %_arr_v3float_uint_3 = OpTypeArray %v3float %uint_3
1633 %_ptr_Input__arr_v3float_uint_3 = OpTypePointer Input %_arr_v3float_uint_3
1634          %26 = OpVariable %_ptr_Input__arr_v3float_uint_3 Input
1635          %27 = OpVariable %_ptr_Input__arr_v3float_uint_3 Input
1636 %_arr_v2float_uint_3 = OpTypeArray %v2float %uint_3
1637 %_ptr_Input__arr_v2float_uint_3 = OpTypePointer Input %_arr_v2float_uint_3
1638          %30 = OpVariable %_ptr_Input__arr_v2float_uint_3 Input
1639          %31 = OpVariable %_ptr_Input__arr_v2float_uint_3 Input
1640 %_ptr_Function__struct_11 = OpTypePointer Function %_struct_11
1641           %2 = OpFunction %void None %4
1642          %33 = OpLabel
1643          %66 = OpVariable %_ptr_Function__arr__struct_11_uint_3 Function
1644          %34 = OpLoad %_arr_v4float_uint_3 %16
1645          %35 = OpLoad %_arr_v4float_uint_3 %17
1646          %36 = OpLoad %_arr_v4float_uint_3 %18
1647          %37 = OpLoad %_arr_v3float_uint_3 %26
1648          %38 = OpLoad %_arr_v3float_uint_3 %27
1649          %39 = OpLoad %_arr_v2float_uint_3 %30
1650          %40 = OpLoad %_arr_v2float_uint_3 %31
1651          %41 = OpCompositeExtract %v4float %34 0
1652          %42 = OpCompositeExtract %v4float %35 0
1653          %43 = OpCompositeExtract %v4float %36 0
1654          %44 = OpCompositeExtract %v3float %37 0
1655          %45 = OpCompositeExtract %v3float %38 0
1656          %46 = OpCompositeExtract %v2float %39 0
1657          %47 = OpCompositeExtract %v2float %40 0
1658          %48 = OpCompositeConstruct %_struct_11 %41 %42 %43 %44 %45 %46 %47
1659          %49 = OpCompositeExtract %v4float %34 1
1660          %50 = OpCompositeExtract %v4float %35 1
1661          %51 = OpCompositeExtract %v4float %36 1
1662          %52 = OpCompositeExtract %v3float %37 1
1663          %53 = OpCompositeExtract %v3float %38 1
1664          %54 = OpCompositeExtract %v2float %39 1
1665          %55 = OpCompositeExtract %v2float %40 1
1666          %56 = OpCompositeConstruct %_struct_11 %49 %50 %51 %52 %53 %54 %55
1667          %57 = OpCompositeExtract %v4float %34 2
1668          %58 = OpCompositeExtract %v4float %35 2
1669          %59 = OpCompositeExtract %v4float %36 2
1670          %60 = OpCompositeExtract %v3float %37 2
1671          %61 = OpCompositeExtract %v3float %38 2
1672          %62 = OpCompositeExtract %v2float %39 2
1673          %63 = OpCompositeExtract %v2float %40 2
1674          %64 = OpCompositeConstruct %_struct_11 %57 %58 %59 %60 %61 %62 %63
1675          %65 = OpCompositeConstruct %_arr__struct_11_uint_3 %48 %56 %64
1676          %67 = OpLoad %uint %20
1677 
1678 ; CHECK OpStore {{%\d+}} [[store_source:%\d+]]
1679                OpStore %66 %65
1680          %68 = OpAccessChain %_ptr_Function__struct_11 %66 %67
1681 
1682 ; This load was being removed, because %_ptr_Function__struct_11 was being
1683 ; wrongfully considered an SSA target.
1684 ; CHECK OpLoad %_struct_11 %68
1685          %69 = OpLoad %_struct_11 %68
1686 
1687 ; Similarly, %69 cannot be replaced with %65.
1688 ; CHECK-NOT: OpCompositeExtract %v4float [[store_source]] 0
1689          %70 = OpCompositeExtract %v4float %69 0
1690 
1691          %71 = OpAccessChain %_ptr_Output_v4float %22 %67
1692                OpStore %71 %70
1693                OpReturn
1694                OpFunctionEnd)";
1695 
1696   SinglePassRunAndMatch<SSARewritePass>(spv_asm, true);
1697 }
1698 
1699 // Test that the RelaxedPrecision decoration on the variable to added to the
1700 // result of the OpPhi instruction.
TEST_F(LocalSSAElimTest,DecoratedVariable)1701 TEST_F(LocalSSAElimTest, DecoratedVariable) {
1702   const std::string spv_asm = R"(
1703 ; CHECK: OpDecorate [[var:%\w+]] RelaxedPrecision
1704 ; CHECK: OpDecorate [[phi_id:%\w+]] RelaxedPrecision
1705 ; CHECK: [[phi_id]] = OpPhi
1706                OpCapability Shader
1707           %1 = OpExtInstImport "GLSL.std.450"
1708                OpMemoryModel Logical GLSL450
1709                OpEntryPoint GLCompute %2 "main"
1710                OpDecorate %v RelaxedPrecision
1711        %void = OpTypeVoid
1712      %func_t = OpTypeFunction %void
1713        %bool = OpTypeBool
1714        %true = OpConstantTrue %bool
1715        %int  = OpTypeInt 32 0
1716       %int_p = OpTypePointer Function %int
1717       %int_1 = OpConstant %int 1
1718       %int_0 = OpConstant %int 0
1719           %2 = OpFunction %void None %func_t
1720          %33 = OpLabel
1721          %v  = OpVariable %int_p Function
1722                OpSelectionMerge %merge None
1723                OpBranchConditional %true %l1 %l2
1724          %l1 = OpLabel
1725                OpStore %v %int_1
1726                OpBranch %merge
1727          %l2 = OpLabel
1728                OpStore %v %int_0
1729                OpBranch %merge
1730       %merge = OpLabel
1731          %ld = OpLoad %int %v
1732                OpReturn
1733                OpFunctionEnd)";
1734 
1735   SinglePassRunAndMatch<SSARewritePass>(spv_asm, true);
1736 }
1737 
1738 // Test that the RelaxedPrecision decoration on the variable to added to the
1739 // result of the OpPhi instruction.
TEST_F(LocalSSAElimTest,MultipleEdges)1740 TEST_F(LocalSSAElimTest, MultipleEdges) {
1741   const std::string spv_asm = R"(
1742   ; CHECK: OpSelectionMerge
1743   ; CHECK: [[header_bb:%\w+]] = OpLabel
1744   ; CHECK-NOT: OpLabel
1745   ; CHECK: OpSwitch {{%\w+}} {{%\w+}} 76 [[bb1:%\w+]] 17 [[bb2:%\w+]]
1746   ; CHECK-SAME: 4 [[bb2]]
1747   ; CHECK: [[bb2]] = OpLabel
1748   ; CHECK-NEXT: OpPhi [[type:%\w+]] [[val:%\w+]] [[header_bb]] %int_0 [[bb1]]
1749           OpCapability Shader
1750      %1 = OpExtInstImport "GLSL.std.450"
1751           OpMemoryModel Logical GLSL450
1752           OpEntryPoint Fragment %4 "main"
1753           OpExecutionMode %4 OriginUpperLeft
1754           OpSource ESSL 310
1755   %void = OpTypeVoid
1756      %3 = OpTypeFunction %void
1757    %int = OpTypeInt 32 1
1758   %_ptr_Function_int = OpTypePointer Function %int
1759   %int_0 = OpConstant %int 0
1760   %bool = OpTypeBool
1761   %true = OpConstantTrue %bool
1762   %false = OpConstantFalse %bool
1763   %int_1 = OpConstant %int 1
1764      %4 = OpFunction %void None %3
1765      %5 = OpLabel
1766      %8 = OpVariable %_ptr_Function_int Function
1767           OpBranch %10
1768     %10 = OpLabel
1769           OpLoopMerge %12 %13 None
1770           OpBranch %14
1771     %14 = OpLabel
1772           OpBranchConditional %true %11 %12
1773     %11 = OpLabel
1774           OpSelectionMerge %19 None
1775           OpBranchConditional %false %18 %19
1776     %18 = OpLabel
1777           OpSelectionMerge %22 None
1778           OpSwitch %int_0 %22 76 %20 17 %21 4 %21
1779     %20 = OpLabel
1780     %23 = OpLoad %int %8
1781           OpStore %8 %int_0
1782           OpBranch %21
1783     %21 = OpLabel
1784           OpBranch %22
1785     %22 = OpLabel
1786           OpBranch %19
1787     %19 = OpLabel
1788           OpBranch %13
1789     %13 = OpLabel
1790           OpBranch %10
1791     %12 = OpLabel
1792           OpReturn
1793           OpFunctionEnd
1794   )";
1795 
1796   SinglePassRunAndMatch<SSARewritePass>(spv_asm, true);
1797 }
1798 
TEST_F(LocalSSAElimTest,VariablePointerTest1)1799 TEST_F(LocalSSAElimTest, VariablePointerTest1) {
1800   // Check that the load of the first variable is still used and that the load
1801   // of the third variable is propagated.  The first load has to remain because
1802   // of the store to the variable pointer.
1803   const std::string text = R"(
1804 ; CHECK: [[v1:%\w+]] = OpVariable
1805 ; CHECK: [[v2:%\w+]] = OpVariable
1806 ; CHECK: [[v3:%\w+]] = OpVariable
1807 ; CHECK: [[ld1:%\w+]] = OpLoad %int [[v1]]
1808 ; CHECK: OpIAdd %int [[ld1]] %int_0
1809                OpCapability Shader
1810                OpCapability VariablePointers
1811           %1 = OpExtInstImport "GLSL.std.450"
1812                OpMemoryModel Logical GLSL450
1813                OpEntryPoint GLCompute %2 "main"
1814                OpExecutionMode %2 LocalSize 1 1 1
1815                OpSource GLSL 450
1816                OpMemberDecorate %_struct_3 0 Offset 0
1817                OpMemberDecorate %_struct_3 1 Offset 4
1818        %void = OpTypeVoid
1819           %5 = OpTypeFunction %void
1820         %int = OpTypeInt 32 1
1821        %bool = OpTypeBool
1822   %_struct_3 = OpTypeStruct %int %int
1823 %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
1824 %_ptr_Function_int = OpTypePointer Function %int
1825        %true = OpConstantTrue %bool
1826       %int_0 = OpConstant %int 0
1827       %int_1 = OpConstant %int 1
1828          %13 = OpConstantNull %_struct_3
1829           %2 = OpFunction %void None %5
1830          %14 = OpLabel
1831          %15 = OpVariable %_ptr_Function_int Function
1832          %16 = OpVariable %_ptr_Function_int Function
1833          %17 = OpVariable %_ptr_Function_int Function
1834                OpStore %15 %int_1
1835                OpStore %17 %int_0
1836                OpSelectionMerge %18 None
1837                OpBranchConditional %true %19 %20
1838          %19 = OpLabel
1839                OpBranch %18
1840          %20 = OpLabel
1841                OpBranch %18
1842          %18 = OpLabel
1843          %21 = OpPhi %_ptr_Function_int %15 %19 %16 %20
1844                OpStore %21 %int_0
1845          %22 = OpLoad %int %15
1846          %23 = OpLoad %int %17
1847          %24 = OpIAdd %int %22 %23
1848                OpReturn
1849                OpFunctionEnd
1850   )";
1851   SinglePassRunAndMatch<SSARewritePass>(text, false);
1852 }
1853 
TEST_F(LocalSSAElimTest,VariablePointerTest2)1854 TEST_F(LocalSSAElimTest, VariablePointerTest2) {
1855   // Check that the load of the first variable is still used and that the load
1856   // of the third variable is propagated.  The first load has to remain because
1857   // of the store to the variable pointer.
1858   const std::string text = R"(
1859 ; CHECK: [[v1:%\w+]] = OpVariable
1860 ; CHECK: [[v2:%\w+]] = OpVariable
1861 ; CHECK: [[v3:%\w+]] = OpVariable
1862 ; CHECK: [[ld1:%\w+]] = OpLoad %int [[v1]]
1863 ; CHECK: OpIAdd %int [[ld1]] %int_0
1864                OpCapability Shader
1865                OpCapability VariablePointers
1866           %1 = OpExtInstImport "GLSL.std.450"
1867                OpMemoryModel Logical GLSL450
1868                OpEntryPoint GLCompute %2 "main"
1869                OpExecutionMode %2 LocalSize 1 1 1
1870                OpSource GLSL 450
1871                OpMemberDecorate %_struct_3 0 Offset 0
1872                OpMemberDecorate %_struct_3 1 Offset 4
1873        %void = OpTypeVoid
1874           %5 = OpTypeFunction %void
1875         %int = OpTypeInt 32 1
1876        %bool = OpTypeBool
1877   %_struct_3 = OpTypeStruct %int %int
1878 %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
1879 %_ptr_Function_int = OpTypePointer Function %int
1880        %true = OpConstantTrue %bool
1881       %int_0 = OpConstant %int 0
1882       %int_1 = OpConstant %int 1
1883          %13 = OpConstantNull %_struct_3
1884           %2 = OpFunction %void None %5
1885          %14 = OpLabel
1886          %15 = OpVariable %_ptr_Function_int Function
1887          %16 = OpVariable %_ptr_Function_int Function
1888          %17 = OpVariable %_ptr_Function_int Function
1889                OpStore %15 %int_1
1890                OpStore %17 %int_0
1891                OpSelectionMerge %18 None
1892                OpBranchConditional %true %19 %20
1893          %19 = OpLabel
1894                OpBranch %18
1895          %20 = OpLabel
1896                OpBranch %18
1897          %18 = OpLabel
1898          %21 = OpPhi %_ptr_Function_int %15 %19 %16 %20
1899                OpStore %21 %int_0
1900          %22 = OpLoad %int %15
1901          %23 = OpLoad %int %17
1902          %24 = OpIAdd %int %22 %23
1903                OpReturn
1904                OpFunctionEnd
1905   )";
1906   SinglePassRunAndMatch<SSARewritePass>(text, false);
1907 }
1908 
TEST_F(LocalSSAElimTest,ChainedTrivialPhis)1909 TEST_F(LocalSSAElimTest, ChainedTrivialPhis) {
1910   // Check that the copy object get the undef value implicitly assigned in the
1911   // entry block.
1912   const std::string text = R"(
1913 ; CHECK: [[undef:%\w+]] = OpUndef %v4float
1914 ; CHECK: OpCopyObject %v4float [[undef]]
1915                OpCapability Shader
1916           %1 = OpExtInstImport "GLSL.std.450"
1917                OpMemoryModel Logical GLSL450
1918                OpEntryPoint GLCompute %2 "main"
1919                OpExecutionMode %2 LocalSize 1 18 6
1920                OpSource ESSL 310
1921        %void = OpTypeVoid
1922           %4 = OpTypeFunction %void
1923        %bool = OpTypeBool
1924       %float = OpTypeFloat 32
1925     %v4float = OpTypeVector %float 4
1926 %_ptr_Function_v4float = OpTypePointer Function %v4float
1927           %2 = OpFunction %void None %4
1928           %9 = OpLabel
1929          %10 = OpVariable %_ptr_Function_v4float Function
1930                OpBranch %11
1931          %11 = OpLabel
1932                OpLoopMerge %12 %13 None
1933                OpBranch %14
1934          %14 = OpLabel
1935          %15 = OpUndef %bool
1936                OpBranchConditional %15 %16 %12
1937          %16 = OpLabel
1938          %17 = OpUndef %bool
1939                OpSelectionMerge %18 None
1940                OpBranchConditional %17 %19 %18
1941          %19 = OpLabel
1942          %20 = OpUndef %bool
1943                OpLoopMerge %21 %22 None
1944                OpBranchConditional %20 %23 %21
1945          %23 = OpLabel
1946          %24 = OpLoad %v4float %10
1947          %25 = OpCopyObject %v4float %24
1948          %26 = OpUndef %bool
1949                OpBranch %22
1950          %22 = OpLabel
1951                OpBranch %19
1952          %21 = OpLabel
1953                OpBranch %12
1954          %18 = OpLabel
1955                OpBranch %13
1956          %13 = OpLabel
1957                OpBranch %11
1958          %12 = OpLabel
1959          %27 = OpLoad %v4float %10
1960                OpReturn
1961                OpFunctionEnd
1962   )";
1963   SinglePassRunAndMatch<SSARewritePass>(text, false);
1964 }
1965 
TEST_F(LocalSSAElimTest,Overflowtest1)1966 TEST_F(LocalSSAElimTest, Overflowtest1) {
1967   // Check that the copy object get the undef value implicitly assigned in the
1968   // entry block.
1969   const std::string text = R"(
1970 OpCapability Geometry
1971 OpMemoryModel Logical GLSL450
1972 OpEntryPoint Fragment %4 "P2Mai" %12 %17
1973 OpExecutionMode %4 OriginUpperLeft
1974 %2 = OpTypeVoid
1975 %3 = OpTypeFunction %2
1976 %6 = OpTypeFloat 32
1977 %7 = OpTypeVector %6 4
1978 %11 = OpTypePointer Input %7
1979 %16 = OpTypePointer Output %7
1980 %23 = OpTypePointer Function %7
1981 %12 = OpVariable %11 Input
1982 %17 = OpVariable %16 Output
1983 %4 = OpFunction %2 None %3
1984 %2177 = OpLabel
1985 %4194302 = OpVariable %23 Function
1986 %4194301 = OpLoad %7 %4194302
1987 OpStore %17 %4194301
1988 OpReturn
1989 OpFunctionEnd
1990   )";
1991 
1992   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1993 
1994   std::vector<Message> messages = {
1995       {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
1996   SetMessageConsumer(GetTestMessageConsumer(messages));
1997   auto result = SinglePassRunToBinary<SSARewritePass>(text, true);
1998   EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1999 }
2000 
TEST_F(LocalSSAElimTest,OpConstantNull)2001 TEST_F(LocalSSAElimTest, OpConstantNull) {
2002   const std::string text = R"(
2003 OpCapability Addresses
2004 OpCapability Kernel
2005 OpCapability Int64
2006 OpMemoryModel Physical64 OpenCL
2007 OpEntryPoint Kernel %4 "A"
2008 OpSource OpenCL_C 200000
2009 %2 = OpTypeVoid
2010 %3 = OpTypeFunction %2
2011 %6 = OpTypeInt 32 0
2012 %11 = OpTypePointer CrossWorkgroup %6
2013 %16 = OpConstantNull %11
2014 %20 = OpConstant %6 269484031
2015 %4 = OpFunction %2 None %3
2016 %17 = OpLabel
2017 %18 = OpLoad %6 %16 Aligned 536870912
2018 %19 = OpBitwiseXor %6 %18 %20
2019 OpStore %16 %19 Aligned 536870912
2020 OpReturn
2021 OpFunctionEnd
2022   )";
2023 
2024   SinglePassRunToBinary<SSARewritePass>(text, false);
2025 }
2026 
TEST_F(LocalSSAElimTest,DebugForLoop)2027 TEST_F(LocalSSAElimTest, DebugForLoop) {
2028   // #version 140
2029   //
2030   // in vec4 BC;
2031   // out float fo;
2032   //
2033   // void main()
2034   // {
2035   //     float f = 0.0;
2036   //     for (int i=0; i<4; i++) {
2037   //       f = f + BC[i];
2038   //     }
2039   //     fo = f;
2040   // }
2041 
2042   const std::string text = R"(
2043 ; CHECK: [[f_name:%\w+]] = OpString "f"
2044 ; CHECK: [[i_name:%\w+]] = OpString "i"
2045 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugLocalVariable [[f_name]]
2046 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]]
2047 
2048 ; CHECK:      OpStore %f %float_0
2049 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] %float_0
2050 ; CHECK-NEXT: OpStore %i %int_0
2051 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] %int_0
2052 
2053 ; CHECK-NOT:  DebugDeclare
2054 
2055 ; CHECK:      [[loop_head:%\w+]] = OpLabel
2056 ; CHECK:      [[phi0:%\w+]] = OpPhi %float %float_0
2057 ; CHECK:      [[phi1:%\w+]] = OpPhi %int %int_0
2058 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[phi0]]
2059 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[phi1]]
2060 ; CHECK:      OpLoopMerge [[loop_merge:%\w+]] [[loop_cont:%\w+]] None
2061 ; CHECK-NEXT: OpBranch [[loop_body:%\w+]]
2062 
2063 ; CHECK-NEXT: [[loop_body]] = OpLabel
2064 ; CHECK:      OpBranchConditional {{%\w+}} [[bb:%\w+]] [[loop_merge]]
2065 
2066 ; CHECK:      [[bb]] = OpLabel
2067 ; CHECK:      OpStore %f [[f_val:%\w+]]
2068 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_val]]
2069 ; CHECK-NEXT: OpBranch [[loop_cont]]
2070 
2071 ; CHECK:      [[loop_cont]] = OpLabel
2072 ; CHECK:      OpStore %i [[i_val:%\w+]]
2073 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[i_val]]
2074 ; CHECK-NEXT: OpBranch [[loop_head]]
2075 
2076 ; CHECK:      [[loop_merge]] = OpLabel
2077 
2078 OpCapability Shader
2079 %1 = OpExtInstImport "GLSL.std.450"
2080 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2081 OpMemoryModel Logical GLSL450
2082 OpEntryPoint Fragment %main "main" %BC %fo
2083 OpExecutionMode %main OriginUpperLeft
2084 %file_name = OpString "test"
2085 OpSource GLSL 140
2086 %float_name = OpString "float"
2087 %main_name = OpString "main"
2088 %f_name = OpString "f"
2089 %i_name = OpString "i"
2090 OpName %main "main"
2091 OpName %f "f"
2092 OpName %i "i"
2093 OpName %BC "BC"
2094 OpName %fo "fo"
2095 %void = OpTypeVoid
2096 %8 = OpTypeFunction %void
2097 %float = OpTypeFloat 32
2098 %_ptr_Function_float = OpTypePointer Function %float
2099 %float_0 = OpConstant %float 0
2100 %int = OpTypeInt 32 1
2101 %uint = OpTypeInt 32 0
2102 %uint_32 = OpConstant %uint 32
2103 %_ptr_Function_int = OpTypePointer Function %int
2104 %int_0 = OpConstant %int 0
2105 %int_4 = OpConstant %int 4
2106 %bool = OpTypeBool
2107 %v4float = OpTypeVector %float 4
2108 %_ptr_Input_v4float = OpTypePointer Input %v4float
2109 %BC = OpVariable %_ptr_Input_v4float Input
2110 %_ptr_Input_float = OpTypePointer Input %float
2111 %int_1 = OpConstant %int 1
2112 %_ptr_Output_float = OpTypePointer Output %float
2113 %fo = OpVariable %_ptr_Output_float Output
2114 %null_expr = OpExtInst %void %ext DebugExpression
2115 %src = OpExtInst %void %ext DebugSource %file_name
2116 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
2117 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
2118 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
2119 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
2120 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
2121 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2122 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2123 %main = OpFunction %void None %8
2124 %22 = OpLabel
2125 %s0 = OpExtInst %void %ext DebugScope %dbg_main
2126 %f = OpVariable %_ptr_Function_float Function
2127 %i = OpVariable %_ptr_Function_int Function
2128 OpStore %f %float_0
2129 OpStore %i %int_0
2130 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
2131 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_i %i %null_expr
2132 OpBranch %23
2133 %23 = OpLabel
2134 %s1 = OpExtInst %void %ext DebugScope %dbg_main
2135 OpLoopMerge %24 %25 None
2136 OpBranch %26
2137 %26 = OpLabel
2138 %s2 = OpExtInst %void %ext DebugScope %dbg_main
2139 %27 = OpLoad %int %i
2140 %28 = OpSLessThan %bool %27 %int_4
2141 OpBranchConditional %28 %29 %24
2142 %29 = OpLabel
2143 %s3 = OpExtInst %void %ext DebugScope %dbg_main
2144 %30 = OpLoad %float %f
2145 %31 = OpLoad %int %i
2146 %32 = OpAccessChain %_ptr_Input_float %BC %31
2147 %33 = OpLoad %float %32
2148 %34 = OpFAdd %float %30 %33
2149 OpStore %f %34
2150 OpBranch %25
2151 %25 = OpLabel
2152 %s4 = OpExtInst %void %ext DebugScope %dbg_main
2153 %35 = OpLoad %int %i
2154 %36 = OpIAdd %int %35 %int_1
2155 OpStore %i %36
2156 OpBranch %23
2157 %24 = OpLabel
2158 %s5 = OpExtInst %void %ext DebugScope %dbg_main
2159 %37 = OpLoad %float %f
2160 OpStore %fo %37
2161 OpReturn
2162 OpFunctionEnd
2163 )";
2164 
2165   SinglePassRunAndMatch<SSARewritePass>(text, true);
2166 }
2167 
TEST_F(LocalSSAElimTest,AddDebugValueForFunctionParameterWithPhi)2168 TEST_F(LocalSSAElimTest, AddDebugValueForFunctionParameterWithPhi) {
2169   // Test the distribution of DebugValue for a parameter of an inlined function
2170   // and the visibility of Phi instruction. The ssa-rewrite pass must add
2171   // DebugValue for the value assignment of function argument even when it is an
2172   // inlined function. It has to check the visibility Phi through all its value
2173   // operands. See the DebugValue for "int i" of "foo()" in the following code.
2174   //
2175   // struct VS_OUTPUT {
2176   //   float4 pos : SV_POSITION;
2177   //   float4 color : COLOR;
2178   // };
2179   //
2180   // float4 foo(int i, float4 pos) {
2181   //   while (i < pos.x) {
2182   //     pos = pos.x + i;
2183   //     ++i;
2184   //   }
2185   //   return pos;
2186   // }
2187   //
2188   // VS_OUTPUT main(float4 pos : POSITION,
2189   //                float4 color : COLOR) {
2190   //   VS_OUTPUT vout;
2191   //   vout.pos = foo(4, pos);
2192   //   vout.color = color;
2193   //   return vout;
2194   // }
2195   const std::string text = R"(
2196                OpCapability Shader
2197           %1 = OpExtInstImport "OpenCL.DebugInfo.100"
2198                OpMemoryModel Logical GLSL450
2199                OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
2200           %7 = OpString "vertex.hlsl"
2201           %8 = OpString "float"
2202           %9 = OpString "VS_OUTPUT"
2203          %10 = OpString "color"
2204          %11 = OpString "pos"
2205          %12 = OpString "int"
2206          %13 = OpString "foo"
2207          %14 = OpString ""
2208          %15 = OpString "i"
2209          %16 = OpString "main"
2210          %17 = OpString "vout"
2211                OpName %in_var_POSITION "in.var.POSITION"
2212                OpName %in_var_COLOR "in.var.COLOR"
2213                OpName %out_var_COLOR "out.var.COLOR"
2214                OpName %main "main"
2215                OpName %param_var_pos "param.var.pos"
2216                OpName %param_var_color "param.var.color"
2217                OpName %VS_OUTPUT "VS_OUTPUT"
2218                OpMemberName %VS_OUTPUT 0 "pos"
2219                OpMemberName %VS_OUTPUT 1 "color"
2220                OpDecorate %gl_Position BuiltIn Position
2221                OpDecorate %in_var_POSITION Location 0
2222                OpDecorate %in_var_COLOR Location 1
2223                OpDecorate %out_var_COLOR Location 0
2224         %int = OpTypeInt 32 1
2225       %int_4 = OpConstant %int 4
2226       %int_0 = OpConstant %int 0
2227       %int_1 = OpConstant %int 1
2228        %uint = OpTypeInt 32 0
2229     %uint_32 = OpConstant %uint 32
2230       %float = OpTypeFloat 32
2231     %v4float = OpTypeVector %float 4
2232 %_ptr_Input_v4float = OpTypePointer Input %v4float
2233 %_ptr_Output_v4float = OpTypePointer Output %v4float
2234        %void = OpTypeVoid
2235    %uint_256 = OpConstant %uint 256
2236    %uint_128 = OpConstant %uint 128
2237      %uint_0 = OpConstant %uint 0
2238          %50 = OpTypeFunction %void
2239 %_ptr_Function_v4float = OpTypePointer Function %v4float
2240   %VS_OUTPUT = OpTypeStruct %v4float %v4float
2241 %_ptr_Function_int = OpTypePointer Function %int
2242 %_ptr_Function_float = OpTypePointer Function %float
2243        %bool = OpTypeBool
2244 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
2245 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
2246 %gl_Position = OpVariable %_ptr_Output_v4float Output
2247 %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
2248         %156 = OpExtInst %void %1 DebugInfoNone
2249          %77 = OpExtInst %void %1 DebugExpression
2250          %58 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float
2251          %59 = OpExtInst %void %1 DebugTypeVector %58 4
2252          %60 = OpExtInst %void %1 DebugSource %7
2253          %61 = OpExtInst %void %1 DebugCompilationUnit 1 4 %60 HLSL
2254          %62 = OpExtInst %void %1 DebugTypeComposite %9 Structure %60 1 8 %61 %9 %uint_256 FlagIsProtected|FlagIsPrivate %63 %64
2255          %64 = OpExtInst %void %1 DebugTypeMember %10 %59 %60 3 10 %62 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
2256          %63 = OpExtInst %void %1 DebugTypeMember %11 %59 %60 2 10 %62 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
2257          %65 = OpExtInst %void %1 DebugTypeBasic %12 %uint_32 Signed
2258          %66 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %59 %65 %59
2259          %67 = OpExtInst %void %1 DebugFunction %13 %66 %60 6 1 %61 %14 FlagIsProtected|FlagIsPrivate 6 %156
2260          %68 = OpExtInst %void %1 DebugLexicalBlock %60 6 31 %67
2261          %69 = OpExtInst %void %1 DebugLexicalBlock %60 7 21 %68
2262          %70 = OpExtInst %void %1 DebugLocalVariable %11 %59 %60 6 26 %67 FlagIsLocal 2
2263 
2264 ; CHECK: [[i_name:%\w+]] = OpString "i"
2265 ; CHECK: [[null_expr:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugExpression
2266 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]] {{%\w+}} {{%\w+}} 6 16 {{%\w+}} FlagIsLocal 1
2267          %71 = OpExtInst %void %1 DebugLocalVariable %15 %65 %60 6 16 %67 FlagIsLocal 1
2268          %72 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %62 %59 %59
2269          %73 = OpExtInst %void %1 DebugFunction %16 %72 %60 14 1 %61 %14 FlagIsProtected|FlagIsPrivate 15 %156
2270          %74 = OpExtInst %void %1 DebugLexicalBlock %60 15 38 %73
2271          %75 = OpExtInst %void %1 DebugLocalVariable %17 %62 %60 16 13 %74 FlagIsLocal
2272          %76 = OpExtInst %void %1 DebugLocalVariable %10 %59 %60 15 23 %73 FlagIsLocal 2
2273          %78 = OpExtInst %void %1 DebugLocalVariable %11 %59 %60 14 23 %73 FlagIsLocal 1
2274         %155 = OpExtInst %void %1 DebugInlinedAt 17 %74
2275        %main = OpFunction %void None %50
2276          %79 = OpLabel
2277         %168 = OpExtInst %void %1 DebugScope %74
2278 
2279 ; CHECK: [[i:%\w+]] = OpVariable %_ptr_Function_int Function
2280         %120 = OpVariable %_ptr_Function_int Function
2281         %121 = OpVariable %_ptr_Function_v4float Function
2282         %169 = OpExtInst %void %1 DebugNoScope
2283 %param_var_pos = OpVariable %_ptr_Function_v4float Function
2284 %param_var_color = OpVariable %_ptr_Function_v4float Function
2285          %80 = OpLoad %v4float %in_var_POSITION
2286                OpStore %param_var_pos %80
2287          %81 = OpLoad %v4float %in_var_COLOR
2288                OpStore %param_var_color %81
2289         %170 = OpExtInst %void %1 DebugScope %73
2290         %124 = OpExtInst %void %1 DebugDeclare %78 %param_var_pos %77
2291         %125 = OpExtInst %void %1 DebugDeclare %76 %param_var_color %77
2292         %171 = OpExtInst %void %1 DebugScope %74
2293                OpLine %7 17 18
2294 
2295 ; CHECK: OpStore {{%\w+}} %int_4
2296 ; CHECK: DebugValue [[dbg_i]] %int_4 [[null_expr]]
2297                OpStore %120 %int_4
2298                OpStore %121 %80
2299         %172 = OpExtInst %void %1 DebugScope %67 %155
2300         %135 = OpExtInst %void %1 DebugDeclare %71 %120 %77
2301         %136 = OpExtInst %void %1 DebugDeclare %70 %121 %77
2302         %173 = OpExtInst %void %1 DebugScope %68 %155
2303                OpLine %7 7 3
2304                OpBranch %137
2305         %174 = OpExtInst %void %1 DebugNoScope
2306         %137 = OpLabel
2307 
2308 ; CHECK: [[phi:%\w+]] = OpPhi %int %int_4
2309 ; CHECK: DebugValue [[dbg_i]] [[phi]] [[null_expr]]
2310         %175 = OpExtInst %void %1 DebugScope %68 %155
2311                OpLine %7 7 10
2312         %138 = OpLoad %int %120
2313         %139 = OpConvertSToF %float %138
2314                OpLine %7 7 14
2315         %140 = OpAccessChain %_ptr_Function_float %121 %int_0
2316         %141 = OpLoad %float %140
2317                OpLine %7 7 12
2318         %142 = OpFOrdLessThan %bool %139 %141
2319                OpLine %7 7 3
2320         %176 = OpExtInst %void %1 DebugNoScope
2321                OpLoopMerge %153 %152 None
2322                OpBranchConditional %142 %143 %153
2323         %177 = OpExtInst %void %1 DebugNoScope
2324         %143 = OpLabel
2325         %178 = OpExtInst %void %1 DebugScope %69 %155
2326                OpLine %7 8 11
2327         %144 = OpAccessChain %_ptr_Function_float %121 %int_0
2328         %145 = OpLoad %float %144
2329                OpLine %7 8 19
2330         %146 = OpLoad %int %120
2331         %147 = OpConvertSToF %float %146
2332                OpLine %7 8 17
2333         %148 = OpFAdd %float %145 %147
2334                OpLine %7 8 11
2335         %149 = OpCompositeConstruct %v4float %148 %148 %148 %148
2336                OpLine %7 8 5
2337                OpStore %121 %149
2338                OpLine %7 9 5
2339         %151 = OpIAdd %int %146 %int_1
2340                OpLine %7 9 7
2341 
2342 ; CHECK: OpStore [[i]] [[value:%\w+]]
2343 ; CHECK: DebugValue [[dbg_i]] [[value]] [[null_expr]]
2344                OpStore %120 %151
2345         %179 = OpExtInst %void %1 DebugScope %68 %155
2346                OpLine %7 10 3
2347                OpBranch %152
2348         %180 = OpExtInst %void %1 DebugNoScope
2349         %152 = OpLabel
2350         %181 = OpExtInst %void %1 DebugScope %68 %155
2351                OpBranch %137
2352         %182 = OpExtInst %void %1 DebugNoScope
2353         %153 = OpLabel
2354         %183 = OpExtInst %void %1 DebugScope %68 %155
2355                OpLine %7 11 10
2356         %154 = OpLoad %v4float %121
2357         %184 = OpExtInst %void %1 DebugScope %74
2358         %167 = OpExtInst %void %1 DebugValue %75 %154 %77 %int_0
2359         %166 = OpExtInst %void %1 DebugValue %75 %81 %77 %int_1
2360                OpLine %7 19 10
2361         %165 = OpCompositeConstruct %VS_OUTPUT %154 %81
2362         %185 = OpExtInst %void %1 DebugNoScope
2363          %83 = OpCompositeExtract %v4float %165 0
2364                OpStore %gl_Position %83
2365          %84 = OpCompositeExtract %v4float %165 1
2366                OpStore %out_var_COLOR %84
2367                OpReturn
2368                OpFunctionEnd
2369 )";
2370 
2371   SinglePassRunAndMatch<SSARewritePass>(text, true);
2372 }
2373 
TEST_F(LocalSSAElimTest,DebugValueWithIndexesInForLoop)2374 TEST_F(LocalSSAElimTest, DebugValueWithIndexesInForLoop) {
2375   // #version 140
2376   //
2377   // in vec4 BC;
2378   // out float fo;
2379   //
2380   // struct T {
2381   //   float a;
2382   //   float f;
2383   // };
2384   //
2385   // struct value {
2386   //   int x;
2387   //   int y;
2388   //   T z;
2389   // };
2390   //
2391   // void main()
2392   // {
2393   //     value v;
2394   //     v.z.f = 0.0;
2395   //     for (int i=0; i<4; i++) {
2396   //       v.z.f = v.z.f + BC[i];
2397   //     }
2398   //     fo = v.z.f;
2399   // }
2400 
2401   const std::string text = R"(
2402 ; CHECK: [[f_name:%\w+]] = OpString "f"
2403 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugLocalVariable [[f_name]]
2404 
2405 ; CHECK:      OpStore %f %float_0
2406 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] %float_0 [[null_expr:%\d+]] %int_2 %int_1
2407 
2408 ; CHECK-NOT:  DebugDeclare
2409 
2410 ; CHECK:      [[loop_head:%\w+]] = OpLabel
2411 ; CHECK:      [[phi0:%\w+]] = OpPhi %float %float_0
2412 ; CHECK:      OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[phi0]] [[null_expr]] %int_2 %int_1
2413 ; CHECK:      OpLoopMerge [[loop_merge:%\w+]] [[loop_cont:%\w+]] None
2414 ; CHECK-NEXT: OpBranch [[loop_body:%\w+]]
2415 
2416 ; CHECK-NEXT: [[loop_body]] = OpLabel
2417 ; CHECK:      OpBranchConditional {{%\w+}} [[bb:%\w+]] [[loop_merge]]
2418 
2419 ; CHECK:      [[bb]] = OpLabel
2420 ; CHECK:      OpStore %f [[f_val:%\w+]]
2421 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_val]] [[null_expr]] %int_2 %int_1
2422 ; CHECK-NEXT: OpBranch [[loop_cont]]
2423 
2424 ; CHECK: [[loop_cont]] = OpLabel
2425 ; CHECK: OpBranch [[loop_head]]
2426 
2427 ; CHECK: [[loop_merge]] = OpLabel
2428 
2429 OpCapability Shader
2430 %1 = OpExtInstImport "GLSL.std.450"
2431 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2432 OpMemoryModel Logical GLSL450
2433 OpEntryPoint Fragment %main "main" %BC %fo
2434 OpExecutionMode %main OriginUpperLeft
2435 %file_name = OpString "test"
2436 OpSource GLSL 140
2437 %float_name = OpString "float"
2438 %main_name = OpString "main"
2439 %f_name = OpString "f"
2440 %i_name = OpString "i"
2441 OpName %main "main"
2442 OpName %f "f"
2443 OpName %i "i"
2444 OpName %BC "BC"
2445 OpName %fo "fo"
2446 %void = OpTypeVoid
2447 %8 = OpTypeFunction %void
2448 %float = OpTypeFloat 32
2449 %_ptr_Function_float = OpTypePointer Function %float
2450 %float_0 = OpConstant %float 0
2451 %int = OpTypeInt 32 1
2452 %uint = OpTypeInt 32 0
2453 %uint_32 = OpConstant %uint 32
2454 %_ptr_Function_int = OpTypePointer Function %int
2455 %int_0 = OpConstant %int 0
2456 %int_4 = OpConstant %int 4
2457 %bool = OpTypeBool
2458 %v4float = OpTypeVector %float 4
2459 %_ptr_Input_v4float = OpTypePointer Input %v4float
2460 %BC = OpVariable %_ptr_Input_v4float Input
2461 %_ptr_Input_float = OpTypePointer Input %float
2462 %int_1 = OpConstant %int 1
2463 %int_2 = OpConstant %int 2
2464 %_ptr_Output_float = OpTypePointer Output %float
2465 %fo = OpVariable %_ptr_Output_float Output
2466 %deref = OpExtInst %void %ext DebugOperation Deref
2467 %null_expr = OpExtInst %void %ext DebugExpression
2468 %deref_expr = OpExtInst %void %ext DebugExpression %deref
2469 %src = OpExtInst %void %ext DebugSource %file_name
2470 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
2471 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
2472 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
2473 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
2474 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
2475 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2476 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2477 %main = OpFunction %void None %8
2478 %22 = OpLabel
2479 %s0 = OpExtInst %void %ext DebugScope %dbg_main
2480 %f = OpVariable %_ptr_Function_float Function
2481 %i = OpVariable %_ptr_Function_int Function
2482 OpStore %f %float_0
2483 OpStore %i %int_0
2484 %decl0 = OpExtInst %void %ext DebugValue %dbg_f %f %deref_expr %int_2 %int_1
2485 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_i %i %null_expr
2486 OpBranch %23
2487 %23 = OpLabel
2488 %s1 = OpExtInst %void %ext DebugScope %dbg_main
2489 OpLoopMerge %24 %25 None
2490 OpBranch %26
2491 %26 = OpLabel
2492 %s2 = OpExtInst %void %ext DebugScope %dbg_main
2493 %27 = OpLoad %int %i
2494 %28 = OpSLessThan %bool %27 %int_4
2495 OpBranchConditional %28 %29 %24
2496 %29 = OpLabel
2497 %s3 = OpExtInst %void %ext DebugScope %dbg_main
2498 %30 = OpLoad %float %f
2499 %31 = OpLoad %int %i
2500 %32 = OpAccessChain %_ptr_Input_float %BC %31
2501 %33 = OpLoad %float %32
2502 %34 = OpFAdd %float %30 %33
2503 OpStore %f %34
2504 OpBranch %25
2505 %25 = OpLabel
2506 %s4 = OpExtInst %void %ext DebugScope %dbg_main
2507 %35 = OpLoad %int %i
2508 %36 = OpIAdd %int %35 %int_1
2509 OpStore %i %36
2510 OpBranch %23
2511 %24 = OpLabel
2512 %s5 = OpExtInst %void %ext DebugScope %dbg_main
2513 %37 = OpLoad %float %f
2514 OpStore %fo %37
2515 OpReturn
2516 OpFunctionEnd
2517 )";
2518 
2519   SinglePassRunAndMatch<SSARewritePass>(text, true);
2520 }
2521 
TEST_F(LocalSSAElimTest,PartiallyKillDebugDeclare)2522 TEST_F(LocalSSAElimTest, PartiallyKillDebugDeclare) {
2523   // For a reference variable e.g., int i in the following example,
2524   // we do not propagate DebugValue for a store or phi instruction
2525   // out of the variable's scope. In that case, we should not remove
2526   // DebugDeclare for the variable that we did not add its DebugValue.
2527   //
2528   // #version 140
2529   //
2530   // in vec4 BC;
2531   // out float fo;
2532   //
2533   // int j;
2534   // void main()
2535   // {
2536   //     float f = 0.0;
2537   //     for (j=0; j<4; j++) {
2538   //       int& i = j;
2539   //       f = f + BC[i];
2540   //     }
2541   //     fo = f;
2542   // }
2543 
2544   const std::string text = R"(
2545 ; CHECK: [[f_name:%\w+]] = OpString "f"
2546 ; CHECK: [[i_name:%\w+]] = OpString "i"
2547 ; CHECK: [[fn:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugFunction
2548 ; CHECK: [[bb:%\w+]] = OpExtInst %void [[ext]] DebugLexicalBlock
2549 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[f_name]] {{%\w+}} {{%\w+}} 0 0 [[fn]]
2550 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]] {{%\w+}} {{%\w+}} 0 0 [[bb]]
2551 
2552 ; CHECK:      OpStore %f %float_0
2553 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] %float_0
2554 ; CHECK-NOT:  DebugDeclare [[dbg_f]]
2555 ; CHECK:      OpExtInst %void [[ext]] DebugDeclare [[dbg_i]] %j
2556 
2557 OpCapability Shader
2558 %1 = OpExtInstImport "GLSL.std.450"
2559 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2560 OpMemoryModel Logical GLSL450
2561 OpEntryPoint Fragment %main "main" %BC %fo
2562 OpExecutionMode %main OriginUpperLeft
2563 %file_name = OpString "test"
2564 OpSource GLSL 140
2565 %float_name = OpString "float"
2566 %main_name = OpString "main"
2567 %f_name = OpString "f"
2568 %i_name = OpString "i"
2569 %j_name = OpString "j"
2570 OpName %main "main"
2571 OpName %f "f"
2572 OpName %j "j"
2573 OpName %BC "BC"
2574 OpName %fo "fo"
2575 %void = OpTypeVoid
2576 %8 = OpTypeFunction %void
2577 %float = OpTypeFloat 32
2578 %_ptr_Function_float = OpTypePointer Function %float
2579 %float_0 = OpConstant %float 0
2580 %int = OpTypeInt 32 1
2581 %uint = OpTypeInt 32 0
2582 %uint_32 = OpConstant %uint 32
2583 %_ptr_Function_int = OpTypePointer Function %int
2584 %_ptr_Private_int = OpTypePointer Private %int
2585 %int_0 = OpConstant %int 0
2586 %int_4 = OpConstant %int 4
2587 %bool = OpTypeBool
2588 %v4float = OpTypeVector %float 4
2589 %_ptr_Input_v4float = OpTypePointer Input %v4float
2590 %BC = OpVariable %_ptr_Input_v4float Input
2591 %_ptr_Input_float = OpTypePointer Input %float
2592 %int_1 = OpConstant %int 1
2593 %_ptr_Output_float = OpTypePointer Output %float
2594 %fo = OpVariable %_ptr_Output_float Output
2595 %j = OpVariable %_ptr_Private_int Private
2596 %null_expr = OpExtInst %void %ext DebugExpression
2597 %src = OpExtInst %void %ext DebugSource %file_name
2598 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
2599 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
2600 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
2601 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
2602 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
2603 %bb = OpExtInst %void %ext DebugLexicalBlock %src 0 0 %dbg_main
2604 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2605 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 0 0 %bb FlagIsLocal
2606 %dbg_j = OpExtInst %void %ext DebugGlobalVariable %j_name %dbg_v4f %src 0 0 %dbg_main %j_name %j FlagIsPrivate
2607 %main = OpFunction %void None %8
2608 %22 = OpLabel
2609 %s0 = OpExtInst %void %ext DebugScope %dbg_main
2610 %f = OpVariable %_ptr_Function_float Function
2611 OpStore %f %float_0
2612 OpStore %j %int_0
2613 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
2614 OpBranch %23
2615 %23 = OpLabel
2616 %s1 = OpExtInst %void %ext DebugScope %dbg_main
2617 OpLoopMerge %24 %25 None
2618 OpBranch %26
2619 %26 = OpLabel
2620 %s2 = OpExtInst %void %ext DebugScope %dbg_main
2621 %27 = OpLoad %int %j
2622 %28 = OpSLessThan %bool %27 %int_4
2623 OpBranchConditional %28 %29 %24
2624 %29 = OpLabel
2625 %s3 = OpExtInst %void %ext DebugScope %bb
2626 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_i %j %null_expr
2627 %30 = OpLoad %float %f
2628 %31 = OpLoad %int %j
2629 %32 = OpAccessChain %_ptr_Input_float %BC %31
2630 %33 = OpLoad %float %32
2631 %34 = OpFAdd %float %30 %33
2632 OpStore %f %34
2633 OpBranch %25
2634 %25 = OpLabel
2635 %s4 = OpExtInst %void %ext DebugScope %dbg_main
2636 %35 = OpLoad %int %j
2637 %36 = OpIAdd %int %35 %int_1
2638 OpStore %j %36
2639 OpBranch %23
2640 %24 = OpLabel
2641 %s5 = OpExtInst %void %ext DebugScope %dbg_main
2642 %37 = OpLoad %float %f
2643 OpStore %fo %37
2644 OpReturn
2645 OpFunctionEnd
2646 )";
2647 
2648   SinglePassRunAndMatch<SSARewritePass>(text, true);
2649 }
2650 
TEST_F(LocalSSAElimTest,DebugValueForReferenceVariable)2651 TEST_F(LocalSSAElimTest, DebugValueForReferenceVariable) {
2652   // #version 140
2653   //
2654   // in vec4 BC;
2655   // out float fo;
2656   //
2657   // void main()
2658   // {
2659   //     float f = 0.0;
2660   //     float& x = f;
2661   //     for (int i=0; i<4; i++) {
2662   //       x = x + BC[i];
2663   //     }
2664   //     fo = f;
2665   // }
2666 
2667   const std::string text = R"(
2668 ; CHECK: [[f_name:%\w+]] = OpString "f"
2669 ; CHECK: [[i_name:%\w+]] = OpString "i"
2670 ; CHECK: [[x_name:%\w+]] = OpString "x"
2671 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugLocalVariable [[f_name]]
2672 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]]
2673 ; CHECK: [[dbg_x:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[x_name]]
2674 
2675 ; CHECK:      OpStore %f %float_0
2676 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_f]] %float_0
2677 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_x]] %float_0
2678 ; CHECK:      OpStore %i %int_0
2679 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] %int_0
2680 
2681 ; CHECK-NOT:  DebugDeclare
2682 
2683 ; CHECK:      [[loop_head:%\w+]] = OpLabel
2684 ; CHECK:      [[phi0:%\w+]] = OpPhi %float %float_0
2685 ; CHECK:      [[phi1:%\w+]] = OpPhi %int %int_0
2686 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[phi0]]
2687 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_x]] [[phi0]]
2688 ; CHECK:      OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[phi1]]
2689 ; CHECK:      OpLoopMerge [[loop_merge:%\w+]] [[loop_cont:%\w+]] None
2690 ; CHECK-NEXT: OpBranch [[loop_body:%\w+]]
2691 
2692 ; CHECK:      [[loop_body]] = OpLabel
2693 ; CHECK:      OpBranchConditional {{%\w+}} [[bb:%\w+]] [[loop_merge]]
2694 
2695 ; CHECK:      [[bb]] = OpLabel
2696 ; CHECK:      OpStore %f [[f_val:%\w+]]
2697 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_val]]
2698 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_x]] [[f_val]]
2699 ; CHECK:      OpBranch [[loop_cont]]
2700 
2701 ; CHECK:      [[loop_cont]] = OpLabel
2702 ; CHECK:      OpStore %i [[i_val:%\w+]]
2703 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[i_val]]
2704 ; CHECK-NEXT: OpBranch [[loop_head]]
2705 
2706 ; CHECK:      [[loop_merge]] = OpLabel
2707 
2708 OpCapability Shader
2709 %1 = OpExtInstImport "GLSL.std.450"
2710 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2711 OpMemoryModel Logical GLSL450
2712 OpEntryPoint Fragment %main "main" %BC %fo
2713 OpExecutionMode %main OriginUpperLeft
2714 %file_name = OpString "test"
2715 OpSource GLSL 140
2716 %float_name = OpString "float"
2717 %main_name = OpString "main"
2718 %f_name = OpString "f"
2719 %i_name = OpString "i"
2720 %x_name = OpString "x"
2721 OpName %main "main"
2722 OpName %f "f"
2723 OpName %i "i"
2724 OpName %BC "BC"
2725 OpName %fo "fo"
2726 %void = OpTypeVoid
2727 %8 = OpTypeFunction %void
2728 %float = OpTypeFloat 32
2729 %_ptr_Function_float = OpTypePointer Function %float
2730 %float_0 = OpConstant %float 0
2731 %int = OpTypeInt 32 1
2732 %uint = OpTypeInt 32 0
2733 %uint_32 = OpConstant %uint 32
2734 %_ptr_Function_int = OpTypePointer Function %int
2735 %int_0 = OpConstant %int 0
2736 %int_4 = OpConstant %int 4
2737 %bool = OpTypeBool
2738 %v4float = OpTypeVector %float 4
2739 %_ptr_Input_v4float = OpTypePointer Input %v4float
2740 %BC = OpVariable %_ptr_Input_v4float Input
2741 %_ptr_Input_float = OpTypePointer Input %float
2742 %int_1 = OpConstant %int 1
2743 %_ptr_Output_float = OpTypePointer Output %float
2744 %fo = OpVariable %_ptr_Output_float Output
2745 %null_expr = OpExtInst %void %ext DebugExpression
2746 %src = OpExtInst %void %ext DebugSource %file_name
2747 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
2748 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
2749 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
2750 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
2751 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
2752 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2753 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 1 0 %dbg_main FlagIsLocal
2754 %dbg_x = OpExtInst %void %ext DebugLocalVariable %x_name %dbg_v4f %src 2 0 %dbg_main FlagIsLocal
2755 %main = OpFunction %void None %8
2756 %22 = OpLabel
2757 %s0 = OpExtInst %void %ext DebugScope %dbg_main
2758 %f = OpVariable %_ptr_Function_float Function
2759 %i = OpVariable %_ptr_Function_int Function
2760 OpStore %f %float_0
2761 OpStore %i %int_0
2762 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
2763 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_i %i %null_expr
2764 %decl2 = OpExtInst %void %ext DebugDeclare %dbg_x %f %null_expr
2765 OpBranch %23
2766 %23 = OpLabel
2767 %s1 = OpExtInst %void %ext DebugScope %dbg_main
2768 OpLoopMerge %24 %25 None
2769 OpBranch %26
2770 %26 = OpLabel
2771 %s2 = OpExtInst %void %ext DebugScope %dbg_main
2772 %27 = OpLoad %int %i
2773 %28 = OpSLessThan %bool %27 %int_4
2774 OpBranchConditional %28 %29 %24
2775 %29 = OpLabel
2776 %s3 = OpExtInst %void %ext DebugScope %dbg_main
2777 %30 = OpLoad %float %f
2778 %31 = OpLoad %int %i
2779 %32 = OpAccessChain %_ptr_Input_float %BC %31
2780 %33 = OpLoad %float %32
2781 %34 = OpFAdd %float %30 %33
2782 OpStore %f %34
2783 OpBranch %25
2784 %25 = OpLabel
2785 %s4 = OpExtInst %void %ext DebugScope %dbg_main
2786 %35 = OpLoad %int %i
2787 %36 = OpIAdd %int %35 %int_1
2788 OpStore %i %36
2789 OpBranch %23
2790 %24 = OpLabel
2791 %s5 = OpExtInst %void %ext DebugScope %dbg_main
2792 %37 = OpLoad %float %f
2793 OpStore %fo %37
2794 OpReturn
2795 OpFunctionEnd
2796 )";
2797 
2798   SinglePassRunAndMatch<SSARewritePass>(text, true);
2799 }
2800 
TEST_F(LocalSSAElimTest,DebugValueForReferenceVariableInBB)2801 TEST_F(LocalSSAElimTest, DebugValueForReferenceVariableInBB) {
2802   // #version 140
2803   //
2804   // in vec4 BC;
2805   // out float fo;
2806   //
2807   // void main()
2808   // {
2809   //     float f = 0.0;
2810   //     for (int i=0; i<4; i++) {
2811   //       float& x = f;
2812   //       x = x + BC[i];
2813   //       {
2814   //         x = x + BC[i];
2815   //       }
2816   //     }
2817   //     fo = f;
2818   // }
2819 
2820   const std::string text = R"(
2821 ; CHECK: [[f_name:%\w+]] = OpString "f"
2822 ; CHECK: [[i_name:%\w+]] = OpString "i"
2823 ; CHECK: [[x_name:%\w+]] = OpString "x"
2824 ; CHECK: [[dbg_main:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugFunction
2825 ; CHECK: [[dbg_bb:%\w+]] = OpExtInst %void [[ext]] DebugLexicalBlock
2826 ; CHECK: [[dbg_bb_child:%\w+]] = OpExtInst %void [[ext]] DebugLexicalBlock
2827 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[f_name]]
2828 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]]
2829 ; CHECK: [[dbg_x:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[x_name]]
2830 
2831 ; CHECK:      OpExtInst %void [[ext]] DebugScope [[dbg_main]]
2832 ; CHECK:      OpStore %f %float_0
2833 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] %float_0
2834 ; CHECK-NEXT: OpStore %i %int_0
2835 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] %int_0
2836 
2837 ; CHECK-NOT:  DebugDeclare
2838 
2839 ; CHECK:      [[loop_head:%\w+]] = OpLabel
2840 ; CHECK:      OpExtInst %void [[ext]] DebugScope [[dbg_main]]
2841 ; CHECK:      [[phi0:%\w+]] = OpPhi %float %float_0
2842 ; CHECK:      [[phi1:%\w+]] = OpPhi %int %int_0
2843 ; CHECK-DAG: OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[phi0]]
2844 ; CHECK-DAG: OpExtInst %void [[ext]] DebugValue [[dbg_x]] [[phi0]]
2845 ; CHECK-DAG: OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[phi1]]
2846 ; CHECK:      OpLoopMerge [[loop_merge:%\w+]] [[loop_cont:%\w+]] None
2847 ; CHECK-NEXT: OpBranch [[loop_body:%\w+]]
2848 
2849 ; CHECK-NEXT: [[loop_body]] = OpLabel
2850 ; CHECK:      OpBranchConditional {{%\w+}} [[bb:%\w+]] [[loop_merge]]
2851 
2852 ; CHECK:      [[bb]] = OpLabel
2853 ; CHECK:      OpExtInst %void [[ext]] DebugScope [[dbg_bb]]
2854 ; CHECK:      OpStore %f [[f_val:%\w+]]
2855 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_val]]
2856 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_x]] [[f_val]]
2857 ; CHECK:      OpBranch [[bb_child:%\w+]]
2858 
2859 ; CHECK:      [[bb_child]] = OpLabel
2860 ; CHECK:      OpExtInst %void [[ext]] DebugScope [[dbg_bb_child]]
2861 ; CHECK:      OpStore %f [[new_f_val:%\w+]]
2862 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[new_f_val]]
2863 ; CHECK-DAG:  OpExtInst %void [[ext]] DebugValue [[dbg_x]] [[new_f_val]]
2864 ; CHECK:      OpBranch [[loop_cont]]
2865 
2866 ; CHECK:      [[loop_cont]] = OpLabel
2867 ; CHECK:      OpStore %i [[i_val:%\w+]]
2868 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[i_val]]
2869 ; CHECK-NEXT: OpBranch [[loop_head]]
2870 
2871 ; CHECK:      [[loop_merge]] = OpLabel
2872 
2873 OpCapability Shader
2874 %1 = OpExtInstImport "GLSL.std.450"
2875 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
2876 OpMemoryModel Logical GLSL450
2877 OpEntryPoint Fragment %main "main" %BC %fo
2878 OpExecutionMode %main OriginUpperLeft
2879 %file_name = OpString "test"
2880 OpSource GLSL 140
2881 %float_name = OpString "float"
2882 %main_name = OpString "main"
2883 %f_name = OpString "f"
2884 %i_name = OpString "i"
2885 %x_name = OpString "x"
2886 OpName %main "main"
2887 OpName %f "f"
2888 OpName %i "i"
2889 OpName %BC "BC"
2890 OpName %fo "fo"
2891 %void = OpTypeVoid
2892 %8 = OpTypeFunction %void
2893 %float = OpTypeFloat 32
2894 %_ptr_Function_float = OpTypePointer Function %float
2895 %float_0 = OpConstant %float 0
2896 %int = OpTypeInt 32 1
2897 %uint = OpTypeInt 32 0
2898 %uint_32 = OpConstant %uint 32
2899 %_ptr_Function_int = OpTypePointer Function %int
2900 %int_0 = OpConstant %int 0
2901 %int_4 = OpConstant %int 4
2902 %bool = OpTypeBool
2903 %v4float = OpTypeVector %float 4
2904 %_ptr_Input_v4float = OpTypePointer Input %v4float
2905 %BC = OpVariable %_ptr_Input_v4float Input
2906 %_ptr_Input_float = OpTypePointer Input %float
2907 %int_1 = OpConstant %int 1
2908 %_ptr_Output_float = OpTypePointer Output %float
2909 %fo = OpVariable %_ptr_Output_float Output
2910 %null_expr = OpExtInst %void %ext DebugExpression
2911 %src = OpExtInst %void %ext DebugSource %file_name
2912 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
2913 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
2914 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
2915 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
2916 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
2917 %bb = OpExtInst %void %ext DebugLexicalBlock %src 0 0 %dbg_main
2918 %bb_child = OpExtInst %void %ext DebugLexicalBlock %src 1 0 %bb
2919 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
2920 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 1 0 %dbg_main FlagIsLocal
2921 %dbg_x = OpExtInst %void %ext DebugLocalVariable %x_name %dbg_v4f %src 2 0 %bb FlagIsLocal
2922 %main = OpFunction %void None %8
2923 %22 = OpLabel
2924 %s0 = OpExtInst %void %ext DebugScope %dbg_main
2925 %f = OpVariable %_ptr_Function_float Function
2926 %i = OpVariable %_ptr_Function_int Function
2927 OpStore %f %float_0
2928 OpStore %i %int_0
2929 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
2930 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_i %i %null_expr
2931 OpBranch %23
2932 %23 = OpLabel
2933 %s1 = OpExtInst %void %ext DebugScope %dbg_main
2934 OpLoopMerge %24 %25 None
2935 OpBranch %26
2936 %26 = OpLabel
2937 %s2 = OpExtInst %void %ext DebugScope %dbg_main
2938 %27 = OpLoad %int %i
2939 %28 = OpSLessThan %bool %27 %int_4
2940 OpBranchConditional %28 %29 %24
2941 %29 = OpLabel
2942 %scope = OpExtInst %void %ext DebugScope %bb
2943 %decl2 = OpExtInst %void %ext DebugDeclare %dbg_x %f %null_expr
2944 %30 = OpLoad %float %f
2945 %31 = OpLoad %int %i
2946 %32 = OpAccessChain %_ptr_Input_float %BC %31
2947 %33 = OpLoad %float %32
2948 %34 = OpFAdd %float %30 %33
2949 OpStore %f %34
2950 OpBranch %38
2951 %38 = OpLabel
2952 %child_scope = OpExtInst %void %ext DebugScope %bb_child
2953 %39 = OpLoad %float %f
2954 %40 = OpFAdd %float %39 %33
2955 OpStore %f %40
2956 OpBranch %25
2957 %25 = OpLabel
2958 %s3 = OpExtInst %void %ext DebugScope %dbg_main
2959 %35 = OpLoad %int %i
2960 %36 = OpIAdd %int %35 %int_1
2961 OpStore %i %36
2962 OpBranch %23
2963 %24 = OpLabel
2964 %s4 = OpExtInst %void %ext DebugScope %dbg_main
2965 %37 = OpLoad %float %f
2966 OpStore %fo %37
2967 OpReturn
2968 OpFunctionEnd
2969 )";
2970 
2971   SinglePassRunAndMatch<SSARewritePass>(text, true);
2972 }
2973 
TEST_F(LocalSSAElimTest,DebugForLoopUseDebugValueInsteadOfDebugDeclare)2974 TEST_F(LocalSSAElimTest, DebugForLoopUseDebugValueInsteadOfDebugDeclare) {
2975   // #version 140
2976   //
2977   // in vec4 BC;
2978   // out float fo;
2979   //
2980   // struct S {
2981   //     float f;
2982   //     int i;
2983   // };
2984   //
2985   // void main()
2986   // {
2987   //     S foo = {0.0, 0};
2988   //     for (; foo.i<4; foo.i++) {
2989   //       foo.f = foo.f + BC[foo.i];
2990   //     }
2991   //     fo = foo.f;
2992   // }
2993 
2994   const std::string text = R"(
2995 ; CHECK: [[f_name:%\w+]] = OpString "f"
2996 ; CHECK: [[empty_expr:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugExpression
2997 ; CHECK: [[deref_op:%\w+]] = OpExtInst %void [[ext]] DebugOperation Deref
2998 ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref_op]]
2999 ; CHECK: [[dbg_foo:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[f_name]]
3000 
3001 ; CHECK:      OpStore %f %float_0
3002 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] %float_0 [[empty_expr]] %uint_0
3003 ; CHECK-NEXT: OpStore %i %int_0
3004 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] %int_0 [[empty_expr]] %uint_1
3005 
3006 ; CHECK:      [[loop_head:%\w+]] = OpLabel
3007 ; CHECK:      [[phi0:%\w+]] = OpPhi %float %float_0
3008 ; CHECK:      [[phi1:%\w+]] = OpPhi %int %int_0
3009 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] [[phi0]] [[empty_expr]] %uint_0
3010 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] [[phi1]] [[empty_expr]] %uint_1
3011 ; CHECK:      OpLoopMerge [[loop_merge:%\w+]] [[loop_cont:%\w+]] None
3012 ; CHECK-NEXT: OpBranch [[loop_body:%\w+]]
3013 
3014 ; CHECK:      [[loop_body]] = OpLabel
3015 ; CHECK:      OpBranchConditional {{%\w+}} [[bb:%\w+]] [[loop_merge]]
3016 
3017 ; CHECK:      [[bb]] = OpLabel
3018 ; CHECK:      OpStore %f [[f_val:%\w+]]
3019 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] [[f_val]] [[empty_expr]] %uint_0
3020 ; CHECK-NEXT: OpBranch [[loop_cont]]
3021 
3022 ; CHECK:      [[loop_cont]] = OpLabel
3023 ; CHECK:      OpStore %i [[i_val:%\w+]]
3024 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_foo]] [[i_val]] [[empty_expr]] %uint_1
3025 ; CHECK-NEXT: OpBranch [[loop_head]]
3026 
3027 OpCapability Shader
3028 %1 = OpExtInstImport "GLSL.std.450"
3029 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3030 OpMemoryModel Logical GLSL450
3031 OpEntryPoint Fragment %main "main" %BC %fo
3032 OpExecutionMode %main OriginUpperLeft
3033 %file_name = OpString "test"
3034 OpSource GLSL 140
3035 %float_name = OpString "float"
3036 %main_name = OpString "main"
3037 %f_name = OpString "f"
3038 OpName %main "main"
3039 OpName %f "f"
3040 OpName %i "i"
3041 OpName %BC "BC"
3042 OpName %fo "fo"
3043 %void = OpTypeVoid
3044 %8 = OpTypeFunction %void
3045 %float = OpTypeFloat 32
3046 %_ptr_Function_float = OpTypePointer Function %float
3047 %float_0 = OpConstant %float 0
3048 %int = OpTypeInt 32 1
3049 %uint = OpTypeInt 32 0
3050 %uint_32 = OpConstant %uint 32
3051 %uint_0 = OpConstant %uint 0
3052 %uint_1 = OpConstant %uint 1
3053 %_ptr_Function_int = OpTypePointer Function %int
3054 %int_0 = OpConstant %int 0
3055 %int_4 = OpConstant %int 4
3056 %bool = OpTypeBool
3057 %v4float = OpTypeVector %float 4
3058 %_ptr_Input_v4float = OpTypePointer Input %v4float
3059 %BC = OpVariable %_ptr_Input_v4float Input
3060 %_ptr_Input_float = OpTypePointer Input %float
3061 %int_1 = OpConstant %int 1
3062 %_ptr_Output_float = OpTypePointer Output %float
3063 %fo = OpVariable %_ptr_Output_float Output
3064 %deref_op = OpExtInst %void %ext DebugOperation Deref
3065 %deref = OpExtInst %void %ext DebugExpression %deref_op
3066 %src = OpExtInst %void %ext DebugSource %file_name
3067 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3068 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3069 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
3070 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3071 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3072 %dbg_foo = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
3073 %main = OpFunction %void None %8
3074 %22 = OpLabel
3075 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3076 %f = OpVariable %_ptr_Function_float Function
3077 %i = OpVariable %_ptr_Function_int Function
3078 OpStore %f %float_0
3079 OpStore %i %int_0
3080 %decl0 = OpExtInst %void %ext DebugValue %dbg_foo %f %deref %uint_0
3081 %decl1 = OpExtInst %void %ext DebugValue %dbg_foo %i %deref %uint_1
3082 OpBranch %23
3083 %23 = OpLabel
3084 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3085 OpLoopMerge %24 %25 None
3086 OpBranch %26
3087 %26 = OpLabel
3088 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3089 %27 = OpLoad %int %i
3090 %28 = OpSLessThan %bool %27 %int_4
3091 OpBranchConditional %28 %29 %24
3092 %29 = OpLabel
3093 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3094 %30 = OpLoad %float %f
3095 %31 = OpLoad %int %i
3096 %32 = OpAccessChain %_ptr_Input_float %BC %31
3097 %33 = OpLoad %float %32
3098 %34 = OpFAdd %float %30 %33
3099 OpStore %f %34
3100 OpBranch %25
3101 %25 = OpLabel
3102 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3103 %35 = OpLoad %int %i
3104 %36 = OpIAdd %int %35 %int_1
3105 OpStore %i %36
3106 OpBranch %23
3107 %24 = OpLabel
3108 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3109 %37 = OpLoad %float %f
3110 OpStore %fo %37
3111 OpReturn
3112 OpFunctionEnd
3113 )";
3114 
3115   SinglePassRunAndMatch<SSARewritePass>(text, true);
3116 }
3117 
TEST_F(LocalSSAElimTest,DebugValueNotUsedForDebugDeclare)3118 TEST_F(LocalSSAElimTest, DebugValueNotUsedForDebugDeclare) {
3119   // #version 140
3120   //
3121   // in vec4 BC;
3122   // out float fo;
3123   //
3124   // void main()
3125   // {
3126   //     float f = 0.0;
3127   //     for (int i=0; i<4; i++) {
3128   //       f = f + BC[i];
3129   //     }
3130   //     fo = f;
3131   // }
3132 
3133   const std::string text = R"(
3134 ; CHECK: [[f_name:%\w+]] = OpString "f"
3135 ; CHECK: [[i_name:%\w+]] = OpString "i"
3136 ; CHECK: [[dbg_f:%\w+]] = OpExtInst %void [[ext:%\d+]] DebugLocalVariable [[f_name]]
3137 ; CHECK: [[dbg_i:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable [[i_name]]
3138 
3139 ; CHECK:      OpStore %f %float_0
3140 ; CHECK-NEXT: OpStore %i %int_0
3141 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_f]] %f
3142 ; CHECK-NEXT: OpExtInst %void [[ext]] DebugValue [[dbg_i]] %i
3143 
3144 ; CHECK-NOT:  DebugValue
3145 ; CHECK-NOT:  DebugDeclare
3146 
3147 OpCapability Shader
3148 %1 = OpExtInstImport "GLSL.std.450"
3149 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3150 OpMemoryModel Logical GLSL450
3151 OpEntryPoint Fragment %main "main" %BC %fo
3152 OpExecutionMode %main OriginUpperLeft
3153 %file_name = OpString "test"
3154 OpSource GLSL 140
3155 %float_name = OpString "float"
3156 %main_name = OpString "main"
3157 %f_name = OpString "f"
3158 %i_name = OpString "i"
3159 OpName %main "main"
3160 OpName %f "f"
3161 OpName %i "i"
3162 OpName %BC "BC"
3163 OpName %fo "fo"
3164 %void = OpTypeVoid
3165 %8 = OpTypeFunction %void
3166 %float = OpTypeFloat 32
3167 %_ptr_Function_float = OpTypePointer Function %float
3168 %float_0 = OpConstant %float 0
3169 %int = OpTypeInt 32 1
3170 %uint = OpTypeInt 32 0
3171 %uint_32 = OpConstant %uint 32
3172 %_ptr_Function_int = OpTypePointer Function %int
3173 %int_0 = OpConstant %int 0
3174 %int_4 = OpConstant %int 4
3175 %bool = OpTypeBool
3176 %v4float = OpTypeVector %float 4
3177 %_ptr_Input_v4float = OpTypePointer Input %v4float
3178 %BC = OpVariable %_ptr_Input_v4float Input
3179 %_ptr_Input_float = OpTypePointer Input %float
3180 %int_1 = OpConstant %int 1
3181 %_ptr_Output_float = OpTypePointer Output %float
3182 %fo = OpVariable %_ptr_Output_float Output
3183 %null_expr = OpExtInst %void %ext DebugExpression
3184 %src = OpExtInst %void %ext DebugSource %file_name
3185 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3186 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3187 %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_tf 4
3188 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
3189 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3190 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
3191 %dbg_i = OpExtInst %void %ext DebugLocalVariable %i_name %dbg_v4f %src 1 0 %dbg_main FlagIsLocal
3192 %main = OpFunction %void None %8
3193 %22 = OpLabel
3194 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3195 %f = OpVariable %_ptr_Function_float Function
3196 %i = OpVariable %_ptr_Function_int Function
3197 OpStore %f %float_0
3198 OpStore %i %int_0
3199 %decl0 = OpExtInst %void %ext DebugValue %dbg_f %f %null_expr
3200 %decl1 = OpExtInst %void %ext DebugValue %dbg_i %i %null_expr
3201 OpBranch %23
3202 %23 = OpLabel
3203 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3204 OpLoopMerge %24 %25 None
3205 OpBranch %26
3206 %26 = OpLabel
3207 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3208 %27 = OpLoad %int %i
3209 %28 = OpSLessThan %bool %27 %int_4
3210 OpBranchConditional %28 %29 %24
3211 %29 = OpLabel
3212 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3213 %30 = OpLoad %float %f
3214 %31 = OpLoad %int %i
3215 %32 = OpAccessChain %_ptr_Input_float %BC %31
3216 %33 = OpLoad %float %32
3217 %34 = OpFAdd %float %30 %33
3218 OpStore %f %34
3219 OpBranch %25
3220 %25 = OpLabel
3221 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3222 %35 = OpLoad %int %i
3223 %36 = OpIAdd %int %35 %int_1
3224 OpStore %i %36
3225 OpBranch %23
3226 %24 = OpLabel
3227 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3228 %37 = OpLoad %float %f
3229 OpStore %fo %37
3230 OpReturn
3231 OpFunctionEnd
3232 )";
3233 
3234   SinglePassRunAndMatch<SSARewritePass>(text, true);
3235 }
3236 
TEST_F(LocalSSAElimTest,DebugNestedForLoop)3237 TEST_F(LocalSSAElimTest, DebugNestedForLoop) {
3238   const std::string text = R"(
3239 ; CHECK: = OpFunction
3240 ; CHECK-NEXT: [[entry:%\w+]] = OpLabel
3241 ; CHECK: OpStore %f %float_0
3242 ; CHECK-NEXT: = OpExtInst %void [[ext:%\w+]] DebugValue [[dbg_f:%\w+]] %float_0
3243 
3244 ; CHECK: [[outer_header:%\w+]] = OpLabel
3245 ; CHECK: [[outer_f:%\w+]] = OpPhi %float %float_0 [[entry]] [[inner_f:%\w+]] [[outer_be:%\w+]]
3246 ; CHECK: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[outer_f]]
3247 
3248 ; CHECK: [[inner_pre_header:%\w+]] = OpLabel
3249 ; CHECK: [[inner_header:%\w+]] = OpLabel
3250 ; CHECK: [[inner_f]] = OpPhi %float [[outer_f]] [[inner_pre_header]] [[f_next:%\w+]] [[inner_be:%\w+]]
3251 ; CHECK: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[inner_f]]
3252 
3253 ; CHECK: [[inner_be]] = OpLabel
3254 ; CHECK: [[f_next]] = OpFAdd %float [[inner_f]]
3255 ; CHECK-NEXT: OpStore %f [[f_next]]
3256 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_next]]
3257 
3258 OpCapability Shader
3259 %1 = OpExtInstImport "GLSL.std.450"
3260 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3261 OpMemoryModel Logical GLSL450
3262 OpEntryPoint Fragment %main "main" %BC %fo
3263 OpExecutionMode %main OriginUpperLeft
3264 %file_name = OpString "test"
3265 %float_name = OpString "float"
3266 %main_name = OpString "main"
3267 %f_name = OpString "f"
3268 OpSource GLSL 450
3269 OpName %main "main"
3270 OpName %f "f"
3271 OpName %i "i"
3272 OpName %j "j"
3273 OpName %BC "BC"
3274 OpName %fo "fo"
3275 OpDecorate %BC Location 0
3276 OpDecorate %fo Location 0
3277 %void = OpTypeVoid
3278 %9 = OpTypeFunction %void
3279 %float = OpTypeFloat 32
3280 %_ptr_Function_float = OpTypePointer Function %float
3281 %float_0 = OpConstant %float 0
3282 %int = OpTypeInt 32 1
3283 %_ptr_Function_int = OpTypePointer Function %int
3284 %int_0 = OpConstant %int 0
3285 %int_4 = OpConstant %int 4
3286 %uint = OpTypeInt 32 0
3287 %uint_32 = OpConstant %uint 32
3288 %bool = OpTypeBool
3289 %v4float = OpTypeVector %float 4
3290 %mat4v4float = OpTypeMatrix %v4float 4
3291 %_ptr_Input_mat4v4float = OpTypePointer Input %mat4v4float
3292 %BC = OpVariable %_ptr_Input_mat4v4float Input
3293 %_ptr_Input_float = OpTypePointer Input %float
3294 %int_1 = OpConstant %int 1
3295 %_ptr_Output_float = OpTypePointer Output %float
3296 %fo = OpVariable %_ptr_Output_float Output
3297 
3298 ; Debug information
3299 %null_expr = OpExtInst %void %ext DebugExpression
3300 %src = OpExtInst %void %ext DebugSource %file_name
3301 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3302 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3303 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
3304 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3305 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3306 
3307 %main = OpFunction %void None %9
3308 %24 = OpLabel
3309 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3310 %f = OpVariable %_ptr_Function_float Function
3311 %i = OpVariable %_ptr_Function_int Function
3312 %j = OpVariable %_ptr_Function_int Function
3313 
3314 ; DebugDeclare
3315 OpStore %f %float_0
3316 %decl = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
3317 
3318 OpStore %i %int_0
3319 OpBranch %25
3320 %25 = OpLabel
3321 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3322 %26 = OpLoad %int %i
3323 %27 = OpSLessThan %bool %26 %int_4
3324 OpLoopMerge %28 %29 None
3325 OpBranchConditional %27 %30 %28
3326 %30 = OpLabel
3327 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3328 OpStore %j %int_0
3329 OpBranch %31
3330 %31 = OpLabel
3331 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3332 %32 = OpLoad %int %j
3333 %33 = OpSLessThan %bool %32 %int_4
3334 OpLoopMerge %50 %34 None
3335 OpBranchConditional %33 %34 %50
3336 %34 = OpLabel
3337 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3338 %35 = OpLoad %float %f
3339 %36 = OpLoad %int %i
3340 %37 = OpLoad %int %j
3341 %38 = OpAccessChain %_ptr_Input_float %BC %36 %37
3342 %39 = OpLoad %float %38
3343 %40 = OpFAdd %float %35 %39
3344 OpStore %f %40
3345 %41 = OpLoad %int %j
3346 %42 = OpIAdd %int %41 %int_1
3347 OpStore %j %42
3348 OpBranch %31
3349 %50 = OpLabel
3350 %s6 = OpExtInst %void %ext DebugScope %dbg_main
3351 OpBranch %29
3352 %29 = OpLabel
3353 %s7 = OpExtInst %void %ext DebugScope %dbg_main
3354 %43 = OpLoad %int %i
3355 %44 = OpIAdd %int %43 %int_1
3356 OpStore %i %44
3357 OpBranch %25
3358 %28 = OpLabel
3359 %s8 = OpExtInst %void %ext DebugScope %dbg_main
3360 %45 = OpLoad %float %f
3361 OpStore %fo %45
3362 OpReturn
3363 OpFunctionEnd
3364 )";
3365 
3366   SinglePassRunAndMatch<SSARewritePass>(text, true);
3367 }
3368 
TEST_F(LocalSSAElimTest,DebugForLoopWithContinue)3369 TEST_F(LocalSSAElimTest, DebugForLoopWithContinue) {
3370   const std::string text = R"(
3371 ; CHECK: = OpFunction
3372 ; CHECK-NEXT: [[entry:%\w+]] = OpLabel
3373 ; CHECK: OpStore %f %float_0
3374 ; CHECK-NEXT: = OpExtInst %void [[ext:%\w+]] DebugValue [[dbg_f:%\w+]] %float_0
3375 
3376 ; CHECK: [[outer_header:%\w+]] = OpLabel
3377 ; CHECK: [[outer_f:%\w+]] = OpPhi %float %float_0 [[entry]] [[inner_f:%\w+]] [[cont:%\w+]]
3378 ; CHECK: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[outer_f]]
3379 
3380 ; CHECK: [[f_next:%\w+]] = OpFAdd %float [[outer_f]]
3381 ; CHECK-NEXT: OpStore %f [[f_next]]
3382 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[f_next]]
3383 
3384 ; CHECK: [[cont]] = OpLabel
3385 ; CHECK: [[inner_f]] = OpPhi %float [[outer_f]] {{%\d+}} [[f_next]] {{%\d+}}
3386 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f]] [[inner_f]]
3387 
3388 OpCapability Shader
3389 %1 = OpExtInstImport "GLSL.std.450"
3390 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3391 OpMemoryModel Logical GLSL450
3392 OpEntryPoint Fragment %main "main" %BC %fo
3393 OpExecutionMode %main OriginUpperLeft
3394 %file_name = OpString "test"
3395 %float_name = OpString "float"
3396 %main_name = OpString "main"
3397 %f_name = OpString "f"
3398 OpSource GLSL 140
3399 OpName %main "main"
3400 OpName %f "f"
3401 OpName %i "i"
3402 OpName %t "t"
3403 OpName %BC "BC"
3404 OpName %fo "fo"
3405 %void = OpTypeVoid
3406 %9 = OpTypeFunction %void
3407 %float = OpTypeFloat 32
3408 %_ptr_Function_float = OpTypePointer Function %float
3409 %float_0 = OpConstant %float 0
3410 %int = OpTypeInt 32 1
3411 %_ptr_Function_int = OpTypePointer Function %int
3412 %int_0 = OpConstant %int 0
3413 %int_4 = OpConstant %int 4
3414 %uint = OpTypeInt 32 0
3415 %uint_32 = OpConstant %uint 32
3416 %bool = OpTypeBool
3417 %v4float = OpTypeVector %float 4
3418 %_ptr_Input_v4float = OpTypePointer Input %v4float
3419 %BC = OpVariable %_ptr_Input_v4float Input
3420 %_ptr_Input_float = OpTypePointer Input %float
3421 %int_1 = OpConstant %int 1
3422 %_ptr_Output_float = OpTypePointer Output %float
3423 %fo = OpVariable %_ptr_Output_float Output
3424 
3425 ; Debug information
3426 %null_expr = OpExtInst %void %ext DebugExpression
3427 %src = OpExtInst %void %ext DebugSource %file_name
3428 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3429 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3430 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
3431 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3432 %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3433 
3434 %main = OpFunction %void None %9
3435 %23 = OpLabel
3436 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3437 %f = OpVariable %_ptr_Function_float Function
3438 %i = OpVariable %_ptr_Function_int Function
3439 %t = OpVariable %_ptr_Function_float Function
3440 
3441 ; DebugDeclare
3442 OpStore %f %float_0
3443 %decl = OpExtInst %void %ext DebugDeclare %dbg_f %f %null_expr
3444 
3445 OpStore %i %int_0
3446 OpBranch %24
3447 %24 = OpLabel
3448 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3449 OpLoopMerge %25 %26 None
3450 OpBranch %27
3451 %27 = OpLabel
3452 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3453 %28 = OpLoad %int %i
3454 %29 = OpSLessThan %bool %28 %int_4
3455 OpBranchConditional %29 %30 %25
3456 %30 = OpLabel
3457 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3458 %31 = OpLoad %int %i
3459 %32 = OpAccessChain %_ptr_Input_float %BC %31
3460 %33 = OpLoad %float %32
3461 OpStore %t %33
3462 %34 = OpLoad %float %t
3463 %35 = OpFOrdLessThan %bool %34 %float_0
3464 OpSelectionMerge %36 None
3465 OpBranchConditional %35 %37 %36
3466 %37 = OpLabel
3467 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3468 OpBranch %26
3469 %36 = OpLabel
3470 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3471 %38 = OpLoad %float %f
3472 %39 = OpLoad %float %t
3473 %40 = OpFAdd %float %38 %39
3474 OpStore %f %40
3475 OpBranch %26
3476 %26 = OpLabel
3477 %s6 = OpExtInst %void %ext DebugScope %dbg_main
3478 %41 = OpLoad %int %i
3479 %42 = OpIAdd %int %41 %int_1
3480 OpStore %i %42
3481 OpBranch %24
3482 %25 = OpLabel
3483 %s7 = OpExtInst %void %ext DebugScope %dbg_main
3484 %43 = OpLoad %float %f
3485 OpStore %fo %43
3486 OpReturn
3487 OpFunctionEnd
3488 )";
3489 
3490   SinglePassRunAndMatch<SSARewritePass>(text, true);
3491 }
3492 
TEST_F(LocalSSAElimTest,DebugIfElse)3493 TEST_F(LocalSSAElimTest, DebugIfElse) {
3494   const std::string text = R"(
3495 OpCapability Shader
3496 %1 = OpExtInstImport "GLSL.std.450"
3497 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3498 OpMemoryModel Logical GLSL450
3499 OpEntryPoint Fragment %main "main" %f %BaseColor %gl_FragColor
3500 OpExecutionMode %main OriginUpperLeft
3501 %file_name = OpString "test"
3502 %float_name = OpString "float"
3503 %main_name = OpString "main"
3504 %v_name = OpString "v"
3505 OpSource GLSL 140
3506 OpName %main "main"
3507 OpName %f "f"
3508 OpName %v "v"
3509 OpName %BaseColor "BaseColor"
3510 OpName %gl_FragColor "gl_FragColor"
3511 %void = OpTypeVoid
3512 %8 = OpTypeFunction %void
3513 %float = OpTypeFloat 32
3514 %_ptr_Input_float = OpTypePointer Input %float
3515 %f = OpVariable %_ptr_Input_float Input
3516 %float_0 = OpConstant %float 0
3517 %uint = OpTypeInt 32 0
3518 %uint_32 = OpConstant %uint 32
3519 %bool = OpTypeBool
3520 %v4float = OpTypeVector %float 4
3521 %_ptr_Function_v4float = OpTypePointer Function %v4float
3522 %_ptr_Input_v4float = OpTypePointer Input %v4float
3523 %BaseColor = OpVariable %_ptr_Input_v4float Input
3524 %float_0_5 = OpConstant %float 0.5
3525 %float_1 = OpConstant %float 1
3526 %18 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
3527 %_ptr_Output_v4float = OpTypePointer Output %v4float
3528 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
3529 
3530 ; Debug information
3531 %null_expr = OpExtInst %void %ext DebugExpression
3532 %src = OpExtInst %void %ext DebugSource %file_name
3533 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3534 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3535 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
3536 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3537 %dbg_v = OpExtInst %void %ext DebugLocalVariable %v_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3538 
3539 %main = OpFunction %void None %8
3540 %20 = OpLabel
3541 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3542 
3543 ; DebugDeclare
3544 %v = OpVariable %_ptr_Function_v4float Function
3545 %decl = OpExtInst %void %ext DebugDeclare %dbg_v %v %null_expr
3546 
3547 %21 = OpLoad %float %f
3548 %22 = OpFOrdGreaterThanEqual %bool %21 %float_0
3549 OpSelectionMerge %23 None
3550 OpBranchConditional %22 %24 %25
3551 
3552 ; CHECK: OpBranchConditional
3553 ; CHECK-NEXT: [[br0:%\w+]] = OpLabel
3554 ; CHECK: OpStore %v [[v0:%\w+]]
3555 ; CHECK-NEXT: = OpExtInst %void [[ext:%\w+]] DebugValue [[dbg_v:%\w+]] [[v0]]
3556 %24 = OpLabel
3557 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3558 %26 = OpLoad %v4float %BaseColor
3559 %27 = OpVectorTimesScalar %v4float %26 %float_0_5
3560 OpStore %v %27
3561 OpBranch %23
3562 
3563 ; CHECK: [[br1:%\w+]] = OpLabel
3564 ; CHECK: OpStore %v [[v1:%\w+]]
3565 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[v1]]
3566 %25 = OpLabel
3567 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3568 %28 = OpLoad %v4float %BaseColor
3569 %29 = OpFAdd %v4float %28 %18
3570 OpStore %v %29
3571 OpBranch %23
3572 
3573 ; CHECK: [[phi:%\w+]] = OpPhi %v4float [[v0]] [[br0]] [[v1]] [[br1]]
3574 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[phi]]
3575 %23 = OpLabel
3576 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3577 %30 = OpLoad %v4float %v
3578 OpStore %gl_FragColor %30
3579 OpReturn
3580 OpFunctionEnd
3581 )";
3582 
3583   SinglePassRunAndMatch<SSARewritePass>(text, true);
3584 }
3585 
TEST_F(LocalSSAElimTest,DebugSwitch)3586 TEST_F(LocalSSAElimTest, DebugSwitch) {
3587   const std::string text = R"(
3588 OpCapability Shader
3589 %1 = OpExtInstImport "GLSL.std.450"
3590 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3591 OpMemoryModel Logical GLSL450
3592 OpEntryPoint Fragment %main "main" %BaseColor %f %gl_FragColor
3593 OpExecutionMode %main OriginUpperLeft
3594 %file_name = OpString "test"
3595 %float_name = OpString "float"
3596 %main_name = OpString "main"
3597 %v_name = OpString "v"
3598 OpSource GLSL 140
3599 OpName %main "main"
3600 OpName %v "v"
3601 OpName %BaseColor "BaseColor"
3602 OpName %i "i"
3603 OpName %f "f"
3604 OpName %gl_FragColor "gl_FragColor"
3605 %void = OpTypeVoid
3606 %9 = OpTypeFunction %void
3607 %float = OpTypeFloat 32
3608 %v4float = OpTypeVector %float 4
3609 %uint = OpTypeInt 32 0
3610 %uint_32 = OpConstant %uint 32
3611 %_ptr_Function_v4float = OpTypePointer Function %v4float
3612 %_ptr_Input_v4float = OpTypePointer Input %v4float
3613 %BaseColor = OpVariable %_ptr_Input_v4float Input
3614 %int = OpTypeInt 32 1
3615 %_ptr_Function_int = OpTypePointer Function %int
3616 %_ptr_Input_float = OpTypePointer Input %float
3617 %f = OpVariable %_ptr_Input_float Input
3618 %float_0_25 = OpConstant %float 0.25
3619 %float_0_75 = OpConstant %float 0.75
3620 %_ptr_Output_v4float = OpTypePointer Output %v4float
3621 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
3622 
3623 ; Debug information
3624 %null_expr = OpExtInst %void %ext DebugExpression
3625 %src = OpExtInst %void %ext DebugSource %file_name
3626 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3627 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3628 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
3629 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3630 %dbg_v = OpExtInst %void %ext DebugLocalVariable %v_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3631 
3632 %main = OpFunction %void None %9
3633 %20 = OpLabel
3634 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3635 %v = OpVariable %_ptr_Function_v4float Function
3636 %i = OpVariable %_ptr_Function_int Function
3637 %21 = OpLoad %v4float %BaseColor
3638 
3639 ; DebugDeclare
3640 OpStore %v %21
3641 %decl = OpExtInst %void %ext DebugDeclare %dbg_v %v %null_expr
3642 
3643 ; CHECK: %main = OpFunction %void None
3644 ; CHECK-NEXT: [[entry:%\w+]] = OpLabel
3645 ; CHECK: OpStore %v [[v0:%\w+]]
3646 ; CHECK-NEXT: = OpExtInst %void [[ext:%\w+]] DebugValue [[dbg_v:%\w+]] [[v0]]
3647 ; CHECK: OpSwitch {{%\w+}} [[case0:%\w+]] 0 [[case1:%\w+]] 1 [[case2:%\w+]] 2 [[case3:%\w+]]
3648 ; CHECK: OpStore %v [[v1:%\w+]]
3649 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[v1]]
3650 ; CHECK: OpStore %v [[v2:%\w+]]
3651 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[v2]]
3652 ; CHECK: [[phi0:%\w+]] = OpPhi %v4float [[v0]] [[entry]] [[v2]] [[case2]]
3653 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[phi0]]
3654 ; CHECK: OpStore %v [[v3:%\w+]]
3655 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[v3]]
3656 ; CHECK: [[phi1:%\w+]] = OpPhi %v4float [[v0]] [[case0]] [[v1]] [[case1]] [[v3]] [[case3]]
3657 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_v]] [[phi1]]
3658 
3659 %22 = OpLoad %float %f
3660 %23 = OpConvertFToS %int %22
3661 OpStore %i %23
3662 %24 = OpLoad %int %i
3663 OpSelectionMerge %25 None
3664 OpSwitch %24 %26 0 %27 1 %28 2 %29
3665 %26 = OpLabel
3666 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3667 OpBranch %25
3668 %27 = OpLabel
3669 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3670 %30 = OpLoad %v4float %v
3671 %31 = OpVectorTimesScalar %v4float %30 %float_0_25
3672 OpStore %v %31
3673 OpBranch %25
3674 %28 = OpLabel
3675 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3676 %32 = OpLoad %v4float %v
3677 %33 = OpCompositeConstruct %v4float %float_0_25 %float_0_25 %float_0_25 %float_0_25
3678 %34 = OpFAdd %v4float %32 %33
3679 OpStore %v %34
3680 OpBranch %29
3681 %29 = OpLabel
3682 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3683 %35 = OpLoad %v4float %v
3684 %36 = OpVectorTimesScalar %v4float %35 %float_0_75
3685 OpStore %v %36
3686 OpBranch %25
3687 %25 = OpLabel
3688 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3689 %37 = OpLoad %v4float %v
3690 OpStore %gl_FragColor %37
3691 OpReturn
3692 OpFunctionEnd
3693 )";
3694 
3695   SinglePassRunAndMatch<SSARewritePass>(text, true);
3696 }
3697 
TEST_F(LocalSSAElimTest,DebugSwapProblem)3698 TEST_F(LocalSSAElimTest, DebugSwapProblem) {
3699   // #version 140
3700   //
3701   // in float fe;
3702   // out float fo;
3703   //
3704   // void main()
3705   // {
3706   //     float f1 = 0.0;
3707   //     float f2 = 1.0;
3708   //     int ie = int(fe);
3709   //     for (int i=0; i<ie; i++) {
3710   //       float t = f1;
3711   //       f1 = f2;
3712   //       f2 = t;
3713   //     }
3714   //     fo = f1;
3715   // }
3716   //
3717   // Because of the swap in the for loop, it generates the following phi
3718   // instructions:
3719   //
3720   // [[phi_f2]] = OpPhi %float %float_1 [[entry]] [[phi_f1]] ..
3721   // [[phi_f1]] = OpPhi %float %float_0 [[entry]] [[phi_f2]] ..
3722   //
3723   // Since they are used as operands by each other, we want to clearly check
3724   // what DebugValue we have to add for them.
3725 
3726   const std::string text = R"(
3727 OpCapability Shader
3728 %1 = OpExtInstImport "GLSL.std.450"
3729 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
3730 OpMemoryModel Logical GLSL450
3731 OpEntryPoint Fragment %main "main" %fe %fo
3732 OpExecutionMode %main OriginUpperLeft
3733 OpSource GLSL 140
3734 %file_name = OpString "test"
3735 %float_name = OpString "float"
3736 %main_name = OpString "main"
3737 %t_name = OpString "t"
3738 OpName %main "main"
3739 OpName %f1 "f1"
3740 OpName %f2 "f2"
3741 OpName %ie "ie"
3742 OpName %fe "fe"
3743 OpName %i "i"
3744 OpName %t "t"
3745 OpName %fo "fo"
3746 %void = OpTypeVoid
3747 %11 = OpTypeFunction %void
3748 %float = OpTypeFloat 32
3749 %_ptr_Function_float = OpTypePointer Function %float
3750 %float_0 = OpConstant %float 0
3751 %float_1 = OpConstant %float 1
3752 %int = OpTypeInt 32 1
3753 %_ptr_Function_int = OpTypePointer Function %int
3754 %_ptr_Input_float = OpTypePointer Input %float
3755 %fe = OpVariable %_ptr_Input_float Input
3756 %int_0 = OpConstant %int 0
3757 %bool = OpTypeBool
3758 %int_1 = OpConstant %int 1
3759 %uint = OpTypeInt 32 0
3760 %uint_32 = OpConstant %uint 32
3761 %_ptr_Output_float = OpTypePointer Output %float
3762 %fo = OpVariable %_ptr_Output_float Output
3763 
3764 ; Debug information
3765 %null_expr = OpExtInst %void %ext DebugExpression
3766 %src = OpExtInst %void %ext DebugSource %file_name
3767 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
3768 %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
3769 %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %void
3770 %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
3771 %dbg_f1 = OpExtInst %void %ext DebugLocalVariable %t_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3772 %dbg_f2 = OpExtInst %void %ext DebugLocalVariable %t_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3773 %dbg_i = OpExtInst %void %ext DebugLocalVariable %t_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
3774 
3775 %main = OpFunction %void None %11
3776 %23 = OpLabel
3777 %s0 = OpExtInst %void %ext DebugScope %dbg_main
3778 %f1 = OpVariable %_ptr_Function_float Function
3779 %f2 = OpVariable %_ptr_Function_float Function
3780 %ie = OpVariable %_ptr_Function_int Function
3781 %i = OpVariable %_ptr_Function_int Function
3782 %t = OpVariable %_ptr_Function_float Function
3783 OpStore %f1 %float_0
3784 OpStore %f2 %float_1
3785 %24 = OpLoad %float %fe
3786 %25 = OpConvertFToS %int %24
3787 OpStore %ie %25
3788 OpStore %i %int_0
3789 
3790 ; DebugDeclare
3791 %decl0 = OpExtInst %void %ext DebugDeclare %dbg_f1 %f1 %null_expr
3792 %decl1 = OpExtInst %void %ext DebugDeclare %dbg_f2 %f2 %null_expr
3793 %decl2 = OpExtInst %void %ext DebugDeclare %dbg_i  %i  %null_expr
3794 
3795 ; CHECK: %main = OpFunction %void None
3796 ; CHECK-NEXT: [[entry:%\w+]] = OpLabel
3797 
3798 ; CHECK: OpStore %f1 %float_0
3799 ; CHECK-NEXT: = OpExtInst %void [[ext:%\w+]] DebugValue [[dbg_f1:%\w+]] %float_0
3800 ; CHECK: OpStore %f2 %float_1
3801 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f2:%\w+]] %float_1
3802 ; CHECK: OpStore %i %int_0
3803 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_i:%\w+]] %int_0
3804 
3805 ; CHECK: [[phi_f2:%\w+]] = OpPhi %float %float_1 [[entry]] [[phi_f1:%\w+]]
3806 ; CHECK: [[phi_f1]] = OpPhi %float %float_0 [[entry]] [[phi_f2]]
3807 ; CHECK: [[phi_i:%\w+]] = OpPhi %int %int_0 [[entry]]
3808 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f2]] [[phi_f2]]
3809 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_f1]] [[phi_f1]]
3810 ; CHECK-NEXT: = OpExtInst %void [[ext]] DebugValue [[dbg_i]] [[phi_i]]
3811 
3812 OpBranch %26
3813 %26 = OpLabel
3814 %s1 = OpExtInst %void %ext DebugScope %dbg_main
3815 OpLoopMerge %27 %28 None
3816 OpBranch %29
3817 %29 = OpLabel
3818 %s2 = OpExtInst %void %ext DebugScope %dbg_main
3819 %30 = OpLoad %int %i
3820 %31 = OpLoad %int %ie
3821 %32 = OpSLessThan %bool %30 %31
3822 OpBranchConditional %32 %33 %27
3823 %33 = OpLabel
3824 %s3 = OpExtInst %void %ext DebugScope %dbg_main
3825 %34 = OpLoad %float %f1
3826 OpStore %t %34
3827 %35 = OpLoad %float %f2
3828 OpStore %f1 %35
3829 %36 = OpLoad %float %t
3830 OpStore %f2 %36
3831 OpBranch %28
3832 %28 = OpLabel
3833 %s4 = OpExtInst %void %ext DebugScope %dbg_main
3834 %37 = OpLoad %int %i
3835 %38 = OpIAdd %int %37 %int_1
3836 OpStore %i %38
3837 OpBranch %26
3838 %27 = OpLabel
3839 %s5 = OpExtInst %void %ext DebugScope %dbg_main
3840 %39 = OpLoad %float %f1
3841 OpStore %fo %39
3842 OpReturn
3843 OpFunctionEnd
3844 )";
3845 
3846   SinglePassRunAndMatch<SSARewritePass>(text, true);
3847 }
3848 
TEST_F(LocalSSAElimTest,RemoveDebugDeclareWithoutLoads)3849 TEST_F(LocalSSAElimTest, RemoveDebugDeclareWithoutLoads) {
3850   // Check that the DebugDeclare for c is removed even though its loads
3851   // had been removed previously by single block store/load optimization.
3852   // In the presence of DebugDeclare, single-block can and does remove loads,
3853   // but cannot change the stores into DebugValues and remove the DebugDeclare
3854   // because it is only a per block optimization, not a function optimization.
3855   // So SSA-rewrite must perform this role.
3856   //
3857   // Texture2D g_tColor;
3858   // SamplerState g_sAniso;
3859   //
3860   // struct PS_INPUT
3861   // {
3862   //   float2 vTextureCoords2 : TEXCOORD2;
3863   //   float2 vTextureCoords3 : TEXCOORD3;
3864   // };
3865   //
3866   // struct PS_OUTPUT
3867   // {
3868   //   float4 vColor : SV_Target0;
3869   // };
3870   //
3871   // PS_OUTPUT MainPs(PS_INPUT i)
3872   // {
3873   //   PS_OUTPUT ps_output;
3874   //   float4 c;
3875   //   c = g_tColor.Sample(g_sAniso, i.vTextureCoords2.xy);
3876   //   c += g_tColor.Sample(g_sAniso, i.vTextureCoords3.xy);
3877   //   ps_output.vColor = c;
3878   //   return ps_output;
3879   // }
3880 
3881   const std::string text = R"(
3882                OpCapability Shader
3883           %1 = OpExtInstImport "OpenCL.DebugInfo.100"
3884                OpMemoryModel Logical GLSL450
3885                OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %in_var_TEXCOORD3 %out_var_SV_Target0
3886                OpExecutionMode %MainPs OriginUpperLeft
3887          %22 = OpString "foo.frag"
3888          %26 = OpString "PS_OUTPUT"
3889          %30 = OpString "float"
3890          %33 = OpString "vColor"
3891          %35 = OpString "PS_INPUT"
3892          %40 = OpString "vTextureCoords3"
3893          %42 = OpString "vTextureCoords2"
3894          %44 = OpString "@type.2d.image"
3895          %45 = OpString "type.2d.image"
3896          %47 = OpString "Texture2D.TemplateParam"
3897          %51 = OpString "src.MainPs"
3898          %55 = OpString "c"
3899          %57 = OpString "ps_output"
3900          %60 = OpString "i"
3901          %62 = OpString "@type.sampler"
3902          %63 = OpString "type.sampler"
3903          %65 = OpString "g_sAniso"
3904          %67 = OpString "g_tColor"
3905                OpName %type_2d_image "type.2d.image"
3906                OpName %g_tColor "g_tColor"
3907                OpName %type_sampler "type.sampler"
3908                OpName %g_sAniso "g_sAniso"
3909                OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
3910                OpName %in_var_TEXCOORD3 "in.var.TEXCOORD3"
3911                OpName %out_var_SV_Target0 "out.var.SV_Target0"
3912                OpName %MainPs "MainPs"
3913                OpName %PS_INPUT "PS_INPUT"
3914                OpMemberName %PS_INPUT 0 "vTextureCoords2"
3915                OpMemberName %PS_INPUT 1 "vTextureCoords3"
3916                OpName %param_var_i "param.var.i"
3917                OpName %PS_OUTPUT "PS_OUTPUT"
3918                OpMemberName %PS_OUTPUT 0 "vColor"
3919                OpName %type_sampled_image "type.sampled.image"
3920                OpDecorate %in_var_TEXCOORD2 Location 0
3921                OpDecorate %in_var_TEXCOORD3 Location 1
3922                OpDecorate %out_var_SV_Target0 Location 0
3923                OpDecorate %g_tColor DescriptorSet 0
3924                OpDecorate %g_tColor Binding 0
3925                OpDecorate %g_sAniso DescriptorSet 0
3926                OpDecorate %g_sAniso Binding 1
3927         %int = OpTypeInt 32 1
3928       %int_0 = OpConstant %int 0
3929       %int_1 = OpConstant %int 1
3930        %uint = OpTypeInt 32 0
3931     %uint_32 = OpConstant %uint 32
3932       %float = OpTypeFloat 32
3933 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
3934 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
3935 %type_sampler = OpTypeSampler
3936 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
3937     %v2float = OpTypeVector %float 2
3938 %_ptr_Input_v2float = OpTypePointer Input %v2float
3939     %v4float = OpTypeVector %float 4
3940 %_ptr_Output_v4float = OpTypePointer Output %v4float
3941        %void = OpTypeVoid
3942    %uint_128 = OpConstant %uint 128
3943      %uint_0 = OpConstant %uint 0
3944     %uint_64 = OpConstant %uint 64
3945          %69 = OpTypeFunction %void
3946    %PS_INPUT = OpTypeStruct %v2float %v2float
3947 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
3948   %PS_OUTPUT = OpTypeStruct %v4float
3949 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
3950 %_ptr_Function_v4float = OpTypePointer Function %v4float
3951 %_ptr_Function_v2float = OpTypePointer Function %v2float
3952 %type_sampled_image = OpTypeSampledImage %type_2d_image
3953    %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
3954    %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
3955 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
3956 %in_var_TEXCOORD3 = OpVariable %_ptr_Input_v2float Input
3957 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
3958          %43 = OpExtInst %void %1 DebugInfoNone
3959          %59 = OpExtInst %void %1 DebugExpression
3960          %24 = OpExtInst %void %1 DebugSource %22
3961          %25 = OpExtInst %void %1 DebugCompilationUnit 1 4 %24 HLSL
3962          %28 = OpExtInst %void %1 DebugTypeComposite %26 Structure %24 11 1 %25 %26 %uint_128 FlagIsProtected|FlagIsPrivate %29
3963          %31 = OpExtInst %void %1 DebugTypeBasic %30 %uint_32 Float
3964          %32 = OpExtInst %void %1 DebugTypeVector %31 4
3965          %29 = OpExtInst %void %1 DebugTypeMember %33 %32 %24 13 5 %28 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
3966          %36 = OpExtInst %void %1 DebugTypeComposite %35 Structure %24 5 1 %25 %35 %uint_128 FlagIsProtected|FlagIsPrivate %37 %38
3967          %39 = OpExtInst %void %1 DebugTypeVector %31 2
3968          %38 = OpExtInst %void %1 DebugTypeMember %40 %39 %24 8 5 %36 %uint_64 %uint_64 FlagIsProtected|FlagIsPrivate
3969          %37 = OpExtInst %void %1 DebugTypeMember %42 %39 %24 7 5 %36 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
3970          %46 = OpExtInst %void %1 DebugTypeComposite %44 Class %24 0 0 %25 %45 %43 FlagIsProtected|FlagIsPrivate
3971          %50 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %28 %36
3972          %52 = OpExtInst %void %1 DebugFunction %51 %50 %24 16 1 %25 %51 FlagIsProtected|FlagIsPrivate 17 %43
3973          %54 = OpExtInst %void %1 DebugLexicalBlock %24 17 1 %52
3974          %56 = OpExtInst %void %1 DebugLocalVariable %55 %32 %24 20 12 %54 FlagIsLocal
3975          %58 = OpExtInst %void %1 DebugLocalVariable %57 %28 %24 18 15 %54 FlagIsLocal
3976          %61 = OpExtInst %void %1 DebugLocalVariable %60 %36 %24 16 29 %52 FlagIsLocal 1
3977          %64 = OpExtInst %void %1 DebugTypeComposite %62 Structure %24 0 0 %25 %63 %43 FlagIsProtected|FlagIsPrivate
3978          %66 = OpExtInst %void %1 DebugGlobalVariable %65 %64 %24 3 14 %25 %65 %g_sAniso FlagIsDefinition
3979          %68 = OpExtInst %void %1 DebugGlobalVariable %67 %46 %24 1 11 %25 %67 %g_tColor FlagIsDefinition
3980      %MainPs = OpFunction %void None %69
3981          %70 = OpLabel
3982         %135 = OpExtInst %void %1 DebugScope %54
3983         %111 = OpVariable %_ptr_Function_PS_OUTPUT Function
3984         %112 = OpVariable %_ptr_Function_v4float Function
3985         %136 = OpExtInst %void %1 DebugNoScope
3986 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
3987          %74 = OpLoad %v2float %in_var_TEXCOORD2
3988          %75 = OpLoad %v2float %in_var_TEXCOORD3
3989          %76 = OpCompositeConstruct %PS_INPUT %74 %75
3990                OpStore %param_var_i %76
3991         %137 = OpExtInst %void %1 DebugScope %52
3992         %115 = OpExtInst %void %1 DebugDeclare %61 %param_var_i %59
3993         %138 = OpExtInst %void %1 DebugScope %54
3994         %116 = OpExtInst %void %1 DebugDeclare %58 %111 %59
3995         %117 = OpExtInst %void %1 DebugDeclare %56 %112 %59
3996 ;CHECK-NOT: %117 = OpExtInst %void %1 DebugDeclare %56 %112 %59
3997                OpLine %22 21 9
3998         %118 = OpLoad %type_2d_image %g_tColor
3999                OpLine %22 21 29
4000         %119 = OpLoad %type_sampler %g_sAniso
4001                OpLine %22 21 40
4002         %120 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
4003         %121 = OpLoad %v2float %120
4004                OpLine %22 21 9
4005         %122 = OpSampledImage %type_sampled_image %118 %119
4006         %123 = OpImageSampleImplicitLod %v4float %122 %121 None
4007                OpLine %22 21 5
4008                OpStore %112 %123
4009 ;CHECK: %140 = OpExtInst %void %1 DebugValue %56 %123 %59
4010                OpLine %22 22 10
4011         %124 = OpLoad %type_2d_image %g_tColor
4012                OpLine %22 22 30
4013         %125 = OpLoad %type_sampler %g_sAniso
4014                OpLine %22 22 41
4015         %126 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_1
4016         %127 = OpLoad %v2float %126
4017                OpLine %22 22 10
4018         %128 = OpSampledImage %type_sampled_image %124 %125
4019         %129 = OpImageSampleImplicitLod %v4float %128 %127 None
4020                OpLine %22 22 7
4021         %131 = OpFAdd %v4float %123 %129
4022                OpLine %22 22 5
4023                OpStore %112 %131
4024 ;CHECK: %141 = OpExtInst %void %1 DebugValue %56 %131 %59
4025                OpLine %22 23 5
4026         %133 = OpAccessChain %_ptr_Function_v4float %111 %int_0
4027                OpStore %133 %131
4028                OpLine %22 24 12
4029         %134 = OpLoad %PS_OUTPUT %111
4030         %139 = OpExtInst %void %1 DebugNoScope
4031          %79 = OpCompositeExtract %v4float %134 0
4032                OpStore %out_var_SV_Target0 %79
4033                OpReturn
4034                OpFunctionEnd
4035 )";
4036 
4037   SetTargetEnv(SPV_ENV_VULKAN_1_2);
4038   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
4039   SinglePassRunAndMatch<SSARewritePass>(text, true);
4040 }
4041 
4042 // Check support for pointer variables. When pointer variables are used, the
4043 // computation of reaching definitions may need to follow pointer chains.
4044 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/3873 for details.
TEST_F(LocalSSAElimTest,PointerVariables)4045 TEST_F(LocalSSAElimTest, PointerVariables) {
4046   const std::string text = R"(
4047                OpCapability Shader
4048                OpCapability VariablePointers
4049                OpExtension "SPV_KHR_variable_pointers"
4050                OpMemoryModel Logical Simple
4051                OpEntryPoint Fragment %1 "main" %2 %3
4052                OpExecutionMode %1 OriginUpperLeft
4053       %float = OpTypeFloat 32
4054        %void = OpTypeVoid
4055           %6 = OpTypeFunction %void
4056 %_ptr_Input_float = OpTypePointer Input %float
4057 %_ptr_Output_float = OpTypePointer Output %float
4058 %_ptr_Function__ptr_Input_float = OpTypePointer Function %_ptr_Input_float
4059           %2 = OpVariable %_ptr_Input_float Input
4060           %3 = OpVariable %_ptr_Output_float Output
4061           %1 = OpFunction %void None %6
4062          %10 = OpLabel
4063          %11 = OpVariable %_ptr_Function__ptr_Input_float Function
4064                OpStore %11 %2
4065 
4066 ; CHECK-NOT: %12 = OpLoad %_ptr_Input_float %11
4067          %12 = OpLoad %_ptr_Input_float %11
4068 
4069 ; CHECK: %13 = OpLoad %float %2
4070          %13 = OpLoad %float %12
4071 
4072                OpStore %3 %13
4073                OpReturn
4074                OpFunctionEnd
4075 )";
4076 
4077   SinglePassRunAndMatch<SSARewritePass>(text, true);
4078 }
4079 
4080 // TODO(greg-lunarg): Add tests to verify handling of these cases:
4081 //
4082 //    No optimization in the presence of
4083 //      access chains
4084 //      function calls
4085 //      OpCopyMemory?
4086 //      unsupported extensions
4087 //    Others?
4088 
4089 }  // namespace
4090 }  // namespace opt
4091 }  // namespace spvtools
4092