• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 Valve Corporation
2 // Copyright (c) 2017 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <string>
17 
18 #include "test/opt/pass_fixture.h"
19 #include "test/opt/pass_utils.h"
20 
21 namespace spvtools {
22 namespace opt {
23 namespace {
24 
25 using LocalSingleStoreElimTest = PassTest<::testing::Test>;
26 
TEST_F(LocalSingleStoreElimTest,PositiveAndNegative)27 TEST_F(LocalSingleStoreElimTest, PositiveAndNegative) {
28   // Single store to v is optimized. Multiple store to
29   // f is not optimized.
30   //
31   // #version 140
32   //
33   // in vec4 BaseColor;
34   // in float fi;
35   //
36   // void main()
37   // {
38   //     vec4 v = BaseColor;
39   //     float f = fi;
40   //     if (f < 0)
41   //         f = 0.0;
42   //     gl_FragColor = v + f;
43   // }
44 
45   const std::string predefs =
46       R"(OpCapability Shader
47 %1 = OpExtInstImport "GLSL.std.450"
48 OpMemoryModel Logical GLSL450
49 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
50 OpExecutionMode %main OriginUpperLeft
51 OpSource GLSL 140
52 OpName %main "main"
53 OpName %v "v"
54 OpName %BaseColor "BaseColor"
55 OpName %f "f"
56 OpName %fi "fi"
57 OpName %gl_FragColor "gl_FragColor"
58 %void = OpTypeVoid
59 %9 = OpTypeFunction %void
60 %float = OpTypeFloat 32
61 %v4float = OpTypeVector %float 4
62 %_ptr_Function_v4float = OpTypePointer Function %v4float
63 %_ptr_Input_v4float = OpTypePointer Input %v4float
64 %BaseColor = OpVariable %_ptr_Input_v4float Input
65 %_ptr_Function_float = OpTypePointer Function %float
66 %_ptr_Input_float = OpTypePointer Input %float
67 %fi = OpVariable %_ptr_Input_float Input
68 %float_0 = OpConstant %float 0
69 %bool = OpTypeBool
70 %_ptr_Output_v4float = OpTypePointer Output %v4float
71 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
72 )";
73 
74   const std::string before =
75       R"(%main = OpFunction %void None %9
76 %19 = OpLabel
77 %v = OpVariable %_ptr_Function_v4float Function
78 %f = OpVariable %_ptr_Function_float Function
79 %20 = OpLoad %v4float %BaseColor
80 OpStore %v %20
81 %21 = OpLoad %float %fi
82 OpStore %f %21
83 %22 = OpLoad %float %f
84 %23 = OpFOrdLessThan %bool %22 %float_0
85 OpSelectionMerge %24 None
86 OpBranchConditional %23 %25 %24
87 %25 = OpLabel
88 OpStore %f %float_0
89 OpBranch %24
90 %24 = OpLabel
91 %26 = OpLoad %v4float %v
92 %27 = OpLoad %float %f
93 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
94 %29 = OpFAdd %v4float %26 %28
95 OpStore %gl_FragColor %29
96 OpReturn
97 OpFunctionEnd
98 )";
99 
100   const std::string after =
101       R"(%main = OpFunction %void None %9
102 %19 = OpLabel
103 %v = OpVariable %_ptr_Function_v4float Function
104 %f = OpVariable %_ptr_Function_float Function
105 %20 = OpLoad %v4float %BaseColor
106 OpStore %v %20
107 %21 = OpLoad %float %fi
108 OpStore %f %21
109 %22 = OpLoad %float %f
110 %23 = OpFOrdLessThan %bool %22 %float_0
111 OpSelectionMerge %24 None
112 OpBranchConditional %23 %25 %24
113 %25 = OpLabel
114 OpStore %f %float_0
115 OpBranch %24
116 %24 = OpLabel
117 %27 = OpLoad %float %f
118 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
119 %29 = OpFAdd %v4float %20 %28
120 OpStore %gl_FragColor %29
121 OpReturn
122 OpFunctionEnd
123 )";
124 
125   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
126                                                   predefs + after, true, true);
127 }
128 
TEST_F(LocalSingleStoreElimTest,ThreeStores)129 TEST_F(LocalSingleStoreElimTest, ThreeStores) {
130   // Three stores to multiple loads of v is not optimized.
131 
132   const std::string predefs =
133       R"(OpCapability Shader
134 %1 = OpExtInstImport "GLSL.std.450"
135 OpMemoryModel Logical GLSL450
136 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
137 OpExecutionMode %main OriginUpperLeft
138 OpSource GLSL 140
139 OpName %main "main"
140 OpName %v "v"
141 OpName %BaseColor "BaseColor"
142 OpName %fi "fi"
143 OpName %r "r"
144 OpName %gl_FragColor "gl_FragColor"
145 %void = OpTypeVoid
146 %9 = OpTypeFunction %void
147 %float = OpTypeFloat 32
148 %v4float = OpTypeVector %float 4
149 %_ptr_Function_v4float = OpTypePointer Function %v4float
150 %_ptr_Input_v4float = OpTypePointer Input %v4float
151 %BaseColor = OpVariable %_ptr_Input_v4float Input
152 %_ptr_Input_float = OpTypePointer Input %float
153 %fi = OpVariable %_ptr_Input_float Input
154 %float_0 = OpConstant %float 0
155 %bool = OpTypeBool
156 %float_1 = OpConstant %float 1
157 %_ptr_Output_v4float = OpTypePointer Output %v4float
158 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
159 )";
160 
161   const std::string before =
162       R"(%main = OpFunction %void None %9
163 %19 = OpLabel
164 %v = OpVariable %_ptr_Function_v4float Function
165 %r = OpVariable %_ptr_Function_v4float Function
166 %20 = OpLoad %v4float %BaseColor
167 OpStore %v %20
168 %21 = OpLoad %float %fi
169 %22 = OpFOrdLessThan %bool %21 %float_0
170 OpSelectionMerge %23 None
171 OpBranchConditional %22 %24 %25
172 %24 = OpLabel
173 %26 = OpLoad %v4float %v
174 OpStore %v %26
175 OpStore %r %26
176 OpBranch %23
177 %25 = OpLabel
178 %27 = OpLoad %v4float %v
179 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
180 OpStore %v %28
181 %29 = OpFSub %v4float %28 %27
182 OpStore %r %29
183 OpBranch %23
184 %23 = OpLabel
185 %30 = OpLoad %v4float %r
186 OpStore %gl_FragColor %30
187 OpReturn
188 OpFunctionEnd
189 )";
190 
191   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
192                                                   predefs + before, true, true);
193 }
194 
TEST_F(LocalSingleStoreElimTest,MultipleLoads)195 TEST_F(LocalSingleStoreElimTest, MultipleLoads) {
196   // Single store to multiple loads of v is optimized.
197   //
198   // #version 140
199   //
200   // in vec4 BaseColor;
201   // in float fi;
202   //
203   // void main()
204   // {
205   //     vec4 v = BaseColor;
206   //     float f = fi;
207   //     if (f < 0)
208   //         f = 0.0;
209   //     gl_FragColor = v + f;
210   // }
211 
212   const std::string predefs =
213       R"(OpCapability Shader
214 %1 = OpExtInstImport "GLSL.std.450"
215 OpMemoryModel Logical GLSL450
216 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
217 OpExecutionMode %main OriginUpperLeft
218 OpSource GLSL 140
219 OpName %main "main"
220 OpName %v "v"
221 OpName %BaseColor "BaseColor"
222 OpName %fi "fi"
223 OpName %r "r"
224 OpName %gl_FragColor "gl_FragColor"
225 %void = OpTypeVoid
226 %9 = OpTypeFunction %void
227 %float = OpTypeFloat 32
228 %v4float = OpTypeVector %float 4
229 %_ptr_Function_v4float = OpTypePointer Function %v4float
230 %_ptr_Input_v4float = OpTypePointer Input %v4float
231 %BaseColor = OpVariable %_ptr_Input_v4float Input
232 %_ptr_Input_float = OpTypePointer Input %float
233 %fi = OpVariable %_ptr_Input_float Input
234 %float_0 = OpConstant %float 0
235 %bool = OpTypeBool
236 %float_1 = OpConstant %float 1
237 %_ptr_Output_v4float = OpTypePointer Output %v4float
238 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
239 )";
240 
241   const std::string before =
242       R"(%main = OpFunction %void None %9
243 %19 = OpLabel
244 %v = OpVariable %_ptr_Function_v4float Function
245 %r = OpVariable %_ptr_Function_v4float Function
246 %20 = OpLoad %v4float %BaseColor
247 OpStore %v %20
248 %21 = OpLoad %float %fi
249 %22 = OpFOrdLessThan %bool %21 %float_0
250 OpSelectionMerge %23 None
251 OpBranchConditional %22 %24 %25
252 %24 = OpLabel
253 %26 = OpLoad %v4float %v
254 OpStore %r %26
255 OpBranch %23
256 %25 = OpLabel
257 %27 = OpLoad %v4float %v
258 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
259 %29 = OpFSub %v4float %28 %27
260 OpStore %r %29
261 OpBranch %23
262 %23 = OpLabel
263 %30 = OpLoad %v4float %r
264 OpStore %gl_FragColor %30
265 OpReturn
266 OpFunctionEnd
267 )";
268 
269   const std::string after =
270       R"(%main = OpFunction %void None %9
271 %19 = OpLabel
272 %v = OpVariable %_ptr_Function_v4float Function
273 %r = OpVariable %_ptr_Function_v4float Function
274 %20 = OpLoad %v4float %BaseColor
275 OpStore %v %20
276 %21 = OpLoad %float %fi
277 %22 = OpFOrdLessThan %bool %21 %float_0
278 OpSelectionMerge %23 None
279 OpBranchConditional %22 %24 %25
280 %24 = OpLabel
281 OpStore %r %20
282 OpBranch %23
283 %25 = OpLabel
284 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
285 %29 = OpFSub %v4float %28 %20
286 OpStore %r %29
287 OpBranch %23
288 %23 = OpLabel
289 %30 = OpLoad %v4float %r
290 OpStore %gl_FragColor %30
291 OpReturn
292 OpFunctionEnd
293 )";
294 
295   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
296                                                   predefs + after, true, true);
297 }
298 
TEST_F(LocalSingleStoreElimTest,NoStoreElimWithInterveningAccessChainLoad)299 TEST_F(LocalSingleStoreElimTest, NoStoreElimWithInterveningAccessChainLoad) {
300   // Last load of v is eliminated, but access chain load and store of v isn't
301   //
302   // #version 140
303   //
304   // in vec4 BaseColor;
305   //
306   // void main()
307   // {
308   //     vec4 v = BaseColor;
309   //     float f = v[3];
310   //     gl_FragColor = v * f;
311   // }
312 
313   const std::string predefs =
314       R"(OpCapability Shader
315 %1 = OpExtInstImport "GLSL.std.450"
316 OpMemoryModel Logical GLSL450
317 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
318 OpExecutionMode %main OriginUpperLeft
319 OpSource GLSL 140
320 OpName %main "main"
321 OpName %v "v"
322 OpName %BaseColor "BaseColor"
323 OpName %f "f"
324 OpName %gl_FragColor "gl_FragColor"
325 %void = OpTypeVoid
326 %8 = OpTypeFunction %void
327 %float = OpTypeFloat 32
328 %v4float = OpTypeVector %float 4
329 %_ptr_Function_v4float = OpTypePointer Function %v4float
330 %_ptr_Input_v4float = OpTypePointer Input %v4float
331 %BaseColor = OpVariable %_ptr_Input_v4float Input
332 %_ptr_Function_float = OpTypePointer Function %float
333 %uint = OpTypeInt 32 0
334 %uint_3 = OpConstant %uint 3
335 %_ptr_Output_v4float = OpTypePointer Output %v4float
336 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
337 )";
338 
339   const std::string before =
340       R"(%main = OpFunction %void None %8
341 %17 = OpLabel
342 %v = OpVariable %_ptr_Function_v4float Function
343 %f = OpVariable %_ptr_Function_float Function
344 %18 = OpLoad %v4float %BaseColor
345 OpStore %v %18
346 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
347 %20 = OpLoad %float %19
348 OpStore %f %20
349 %21 = OpLoad %v4float %v
350 %22 = OpLoad %float %f
351 %23 = OpVectorTimesScalar %v4float %21 %22
352 OpStore %gl_FragColor %23
353 OpReturn
354 OpFunctionEnd
355 )";
356 
357   const std::string after =
358       R"(%main = OpFunction %void None %8
359 %17 = OpLabel
360 %v = OpVariable %_ptr_Function_v4float Function
361 %f = OpVariable %_ptr_Function_float Function
362 %18 = OpLoad %v4float %BaseColor
363 OpStore %v %18
364 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
365 %20 = OpLoad %float %19
366 OpStore %f %20
367 %23 = OpVectorTimesScalar %v4float %18 %20
368 OpStore %gl_FragColor %23
369 OpReturn
370 OpFunctionEnd
371 )";
372 
373   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
374                                                   predefs + after, true, true);
375 }
376 
TEST_F(LocalSingleStoreElimTest,NoReplaceOfDominatingPartialStore)377 TEST_F(LocalSingleStoreElimTest, NoReplaceOfDominatingPartialStore) {
378   // Note: SPIR-V hand edited to initialize v to vec4(0.0)
379   //
380   // #version 140
381   //
382   // in vec4 BaseColor;
383   //
384   // void main()
385   // {
386   //     vec4 v;
387   //     float v[1] = 1.0;
388   //     gl_FragColor = v;
389   // }
390 
391   const std::string assembly =
392       R"(OpCapability Shader
393 %1 = OpExtInstImport "GLSL.std.450"
394 OpMemoryModel Logical GLSL450
395 OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor
396 OpExecutionMode %main OriginUpperLeft
397 OpSource GLSL 140
398 OpName %main "main"
399 OpName %v "v"
400 OpName %gl_FragColor "gl_FragColor"
401 OpName %BaseColor "BaseColor"
402 %void = OpTypeVoid
403 %7 = OpTypeFunction %void
404 %float = OpTypeFloat 32
405 %v4float = OpTypeVector %float 4
406 %_ptr_Function_v4float = OpTypePointer Function %v4float
407 %float_0 = OpConstant %float 0
408 %12 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
409 %float_1 = OpConstant %float 1
410 %uint = OpTypeInt 32 0
411 %uint_1 = OpConstant %uint 1
412 %_ptr_Function_float = OpTypePointer Function %float
413 %_ptr_Output_v4float = OpTypePointer Output %v4float
414 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
415 %_ptr_Input_v4float = OpTypePointer Input %v4float
416 %BaseColor = OpVariable %_ptr_Input_v4float Input
417 %main = OpFunction %void None %7
418 %19 = OpLabel
419 %v = OpVariable %_ptr_Function_v4float Function %12
420 %20 = OpAccessChain %_ptr_Function_float %v %uint_1
421 OpStore %20 %float_1
422 %21 = OpLoad %v4float %v
423 OpStore %gl_FragColor %21
424 OpReturn
425 OpFunctionEnd
426 )";
427 
428   SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
429                                                   true);
430 }
431 
TEST_F(LocalSingleStoreElimTest,ElimIfCopyObjectInFunction)432 TEST_F(LocalSingleStoreElimTest, ElimIfCopyObjectInFunction) {
433   // Note: hand edited to insert OpCopyObject
434   //
435   // #version 140
436   //
437   // in vec4 BaseColor;
438   // in float fi;
439   //
440   // void main()
441   // {
442   //     vec4 v = BaseColor;
443   //     float f = fi;
444   //     if (f < 0)
445   //         f = 0.0;
446   //     gl_FragColor = v + f;
447   // }
448 
449   const std::string predefs =
450       R"(OpCapability Shader
451 %1 = OpExtInstImport "GLSL.std.450"
452 OpMemoryModel Logical GLSL450
453 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
454 OpExecutionMode %main OriginUpperLeft
455 OpSource GLSL 140
456 OpName %main "main"
457 OpName %v "v"
458 OpName %BaseColor "BaseColor"
459 OpName %f "f"
460 OpName %fi "fi"
461 OpName %gl_FragColor "gl_FragColor"
462 %void = OpTypeVoid
463 %9 = OpTypeFunction %void
464 %float = OpTypeFloat 32
465 %v4float = OpTypeVector %float 4
466 %_ptr_Function_v4float = OpTypePointer Function %v4float
467 %_ptr_Input_v4float = OpTypePointer Input %v4float
468 %BaseColor = OpVariable %_ptr_Input_v4float Input
469 %_ptr_Function_float = OpTypePointer Function %float
470 %_ptr_Input_float = OpTypePointer Input %float
471 %fi = OpVariable %_ptr_Input_float Input
472 %float_0 = OpConstant %float 0
473 %bool = OpTypeBool
474 %_ptr_Output_v4float = OpTypePointer Output %v4float
475 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
476 )";
477 
478   const std::string before =
479       R"(%main = OpFunction %void None %9
480 %19 = OpLabel
481 %v = OpVariable %_ptr_Function_v4float Function
482 %f = OpVariable %_ptr_Function_float Function
483 %20 = OpLoad %v4float %BaseColor
484 OpStore %v %20
485 %21 = OpLoad %float %fi
486 OpStore %f %21
487 %22 = OpLoad %float %f
488 %23 = OpFOrdLessThan %bool %22 %float_0
489 OpSelectionMerge %24 None
490 OpBranchConditional %23 %25 %24
491 %25 = OpLabel
492 OpStore %f %float_0
493 OpBranch %24
494 %24 = OpLabel
495 %26 = OpCopyObject %_ptr_Function_v4float %v
496 %27 = OpLoad %v4float %26
497 %28 = OpLoad %float %f
498 %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
499 %30 = OpFAdd %v4float %27 %29
500 OpStore %gl_FragColor %30
501 OpReturn
502 OpFunctionEnd
503 )";
504 
505   const std::string after =
506       R"(%main = OpFunction %void None %9
507 %19 = OpLabel
508 %v = OpVariable %_ptr_Function_v4float Function
509 %f = OpVariable %_ptr_Function_float Function
510 %20 = OpLoad %v4float %BaseColor
511 OpStore %v %20
512 %21 = OpLoad %float %fi
513 OpStore %f %21
514 %22 = OpLoad %float %f
515 %23 = OpFOrdLessThan %bool %22 %float_0
516 OpSelectionMerge %24 None
517 OpBranchConditional %23 %25 %24
518 %25 = OpLabel
519 OpStore %f %float_0
520 OpBranch %24
521 %24 = OpLabel
522 %26 = OpCopyObject %_ptr_Function_v4float %v
523 %28 = OpLoad %float %f
524 %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
525 %30 = OpFAdd %v4float %20 %29
526 OpStore %gl_FragColor %30
527 OpReturn
528 OpFunctionEnd
529 )";
530 
531   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
532                                                   predefs + after, true, true);
533 }
534 
TEST_F(LocalSingleStoreElimTest,NoOptIfStoreNotDominating)535 TEST_F(LocalSingleStoreElimTest, NoOptIfStoreNotDominating) {
536   // Single store to f not optimized because it does not dominate
537   // the load.
538   //
539   // #version 140
540   //
541   // in vec4 BaseColor;
542   // in float fi;
543   //
544   // void main()
545   // {
546   //     float f;
547   //     if (fi < 0)
548   //         f = 0.5;
549   //     if (fi < 0)
550   //         gl_FragColor = BaseColor * f;
551   //     else
552   //         gl_FragColor = BaseColor;
553   // }
554 
555   const std::string assembly =
556       R"(OpCapability Shader
557 %1 = OpExtInstImport "GLSL.std.450"
558 OpMemoryModel Logical GLSL450
559 OpEntryPoint Fragment %main "main" %fi %gl_FragColor %BaseColor
560 OpExecutionMode %main OriginUpperLeft
561 OpSource GLSL 140
562 OpName %main "main"
563 OpName %fi "fi"
564 OpName %f "f"
565 OpName %gl_FragColor "gl_FragColor"
566 OpName %BaseColor "BaseColor"
567 %void = OpTypeVoid
568 %8 = OpTypeFunction %void
569 %float = OpTypeFloat 32
570 %_ptr_Input_float = OpTypePointer Input %float
571 %fi = OpVariable %_ptr_Input_float Input
572 %float_0 = OpConstant %float 0
573 %bool = OpTypeBool
574 %_ptr_Function_float = OpTypePointer Function %float
575 %float_0_5 = OpConstant %float 0.5
576 %v4float = OpTypeVector %float 4
577 %_ptr_Output_v4float = OpTypePointer Output %v4float
578 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
579 %_ptr_Input_v4float = OpTypePointer Input %v4float
580 %BaseColor = OpVariable %_ptr_Input_v4float Input
581 %main = OpFunction %void None %8
582 %18 = OpLabel
583 %f = OpVariable %_ptr_Function_float Function
584 %19 = OpLoad %float %fi
585 %20 = OpFOrdLessThan %bool %19 %float_0
586 OpSelectionMerge %21 None
587 OpBranchConditional %20 %22 %21
588 %22 = OpLabel
589 OpStore %f %float_0_5
590 OpBranch %21
591 %21 = OpLabel
592 %23 = OpLoad %float %fi
593 %24 = OpFOrdLessThan %bool %23 %float_0
594 OpSelectionMerge %25 None
595 OpBranchConditional %24 %26 %27
596 %26 = OpLabel
597 %28 = OpLoad %v4float %BaseColor
598 %29 = OpLoad %float %f
599 %30 = OpVectorTimesScalar %v4float %28 %29
600 OpStore %gl_FragColor %30
601 OpBranch %25
602 %27 = OpLabel
603 %31 = OpLoad %v4float %BaseColor
604 OpStore %gl_FragColor %31
605 OpBranch %25
606 %25 = OpLabel
607 OpReturn
608 OpFunctionEnd
609 )";
610 
611   SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
612                                                   true);
613 }
614 
TEST_F(LocalSingleStoreElimTest,OptInitializedVariableLikeStore)615 TEST_F(LocalSingleStoreElimTest, OptInitializedVariableLikeStore) {
616   // Initialized variable f is optimized like it was a store.
617   // Note: The SPIR-V was edited to turn the store to f to an
618   // an initialization.
619   //
620   // #version 140
621   //
622   // void main()
623   // {
624   //     float f = 0.0;
625   //     gl_FragColor = vec4(f);
626   // }
627 
628   const std::string predefs =
629       R"(OpCapability Shader
630 %1 = OpExtInstImport "GLSL.std.450"
631 OpMemoryModel Logical GLSL450
632 OpEntryPoint Fragment %main "main" %gl_FragColor
633 OpExecutionMode %main OriginUpperLeft
634 OpSource GLSL 140
635 OpName %main "main"
636 OpName %f "f"
637 OpName %gl_FragColor "gl_FragColor"
638 OpDecorate %gl_FragColor Location 0
639 %void = OpTypeVoid
640 %6 = OpTypeFunction %void
641 %float = OpTypeFloat 32
642 %_ptr_Function_float = OpTypePointer Function %float
643 %float_0 = OpConstant %float 0
644 %v4float = OpTypeVector %float 4
645 %_ptr_Output_v4float = OpTypePointer Output %v4float
646 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
647 )";
648 
649   const std::string before =
650       R"(%main = OpFunction %void None %6
651 %12 = OpLabel
652 %f = OpVariable %_ptr_Function_float Function %float_0
653 %13 = OpLoad %float %f
654 %14 = OpCompositeConstruct %v4float %13 %13 %13 %13
655 OpStore %gl_FragColor %14
656 OpReturn
657 OpFunctionEnd
658 )";
659 
660   const std::string after =
661       R"(%main = OpFunction %void None %6
662 %12 = OpLabel
663 %f = OpVariable %_ptr_Function_float Function %float_0
664 %14 = OpCompositeConstruct %v4float %float_0 %float_0 %float_0 %float_0
665 OpStore %gl_FragColor %14
666 OpReturn
667 OpFunctionEnd
668 )";
669 
670   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
671                                                   predefs + after, true, true);
672 }
673 
TEST_F(LocalSingleStoreElimTest,PointerVariable)674 TEST_F(LocalSingleStoreElimTest, PointerVariable) {
675   // Test that checks if a pointer variable is removed.
676 
677   const std::string before =
678       R"(OpCapability Shader
679 OpMemoryModel Logical GLSL450
680 OpEntryPoint Fragment %1 "main" %2
681 OpExecutionMode %1 OriginUpperLeft
682 OpMemberDecorate %_struct_3 0 Offset 0
683 OpDecorate %_runtimearr__struct_3 ArrayStride 16
684 OpMemberDecorate %_struct_5 0 Offset 0
685 OpDecorate %_struct_5 BufferBlock
686 OpMemberDecorate %_struct_6 0 Offset 0
687 OpDecorate %_struct_6 BufferBlock
688 OpDecorate %2 Location 0
689 OpDecorate %7 DescriptorSet 0
690 OpDecorate %7 Binding 0
691 %void = OpTypeVoid
692 %10 = OpTypeFunction %void
693 %int = OpTypeInt 32 1
694 %uint = OpTypeInt 32 0
695 %float = OpTypeFloat 32
696 %v4float = OpTypeVector %float 4
697 %_ptr_Output_v4float = OpTypePointer Output %v4float
698 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
699 %_struct_3 = OpTypeStruct %v4float
700 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
701 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
702 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
703 %_struct_6 = OpTypeStruct %int
704 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
705 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
706 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
707 %int_0 = OpConstant %int 0
708 %uint_0 = OpConstant %uint 0
709 %2 = OpVariable %_ptr_Output_v4float Output
710 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
711 %1 = OpFunction %void None %10
712 %23 = OpLabel
713 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
714 OpStore %24 %7
715 %26 = OpLoad %_ptr_Uniform__struct_5 %24
716 %27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0
717 %28 = OpLoad %v4float %27
718 %29 = OpCopyObject %v4float %28
719 OpStore %2 %28
720 OpReturn
721 OpFunctionEnd
722 )";
723 
724   const std::string after =
725       R"(OpCapability Shader
726 OpMemoryModel Logical GLSL450
727 OpEntryPoint Fragment %1 "main" %2
728 OpExecutionMode %1 OriginUpperLeft
729 OpMemberDecorate %_struct_3 0 Offset 0
730 OpDecorate %_runtimearr__struct_3 ArrayStride 16
731 OpMemberDecorate %_struct_5 0 Offset 0
732 OpDecorate %_struct_5 BufferBlock
733 OpMemberDecorate %_struct_6 0 Offset 0
734 OpDecorate %_struct_6 BufferBlock
735 OpDecorate %2 Location 0
736 OpDecorate %7 DescriptorSet 0
737 OpDecorate %7 Binding 0
738 %void = OpTypeVoid
739 %10 = OpTypeFunction %void
740 %int = OpTypeInt 32 1
741 %uint = OpTypeInt 32 0
742 %float = OpTypeFloat 32
743 %v4float = OpTypeVector %float 4
744 %_ptr_Output_v4float = OpTypePointer Output %v4float
745 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
746 %_struct_3 = OpTypeStruct %v4float
747 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
748 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
749 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
750 %_struct_6 = OpTypeStruct %int
751 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
752 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
753 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
754 %int_0 = OpConstant %int 0
755 %uint_0 = OpConstant %uint 0
756 %2 = OpVariable %_ptr_Output_v4float Output
757 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
758 %1 = OpFunction %void None %10
759 %23 = OpLabel
760 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
761 OpStore %24 %7
762 %27 = OpAccessChain %_ptr_Uniform_v4float %7 %int_0 %uint_0 %int_0
763 %28 = OpLoad %v4float %27
764 %29 = OpCopyObject %v4float %28
765 OpStore %2 %28
766 OpReturn
767 OpFunctionEnd
768 )";
769 
770   // Relax logical pointers to allow pointer allocations.
771   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
772   ValidatorOptions()->relax_logical_pointer = true;
773   SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true);
774 }
775 
776 // Test that that an unused OpAccessChain between a store and a use does does
777 // not hinders the replacement of the use.  We need to check this because
778 // local-access-chain-convert does always remove the OpAccessChain instructions
779 // that become dead.
780 
TEST_F(LocalSingleStoreElimTest,StoreElimWithUnusedInterveningAccessChainLoad)781 TEST_F(LocalSingleStoreElimTest,
782        StoreElimWithUnusedInterveningAccessChainLoad) {
783   // Last load of v is eliminated, but access chain load and store of v isn't
784   //
785   // #version 140
786   //
787   // in vec4 BaseColor;
788   //
789   // void main()
790   // {
791   //     vec4 v = BaseColor;
792   //     float f = v[3];
793   //     gl_FragColor = v * f;
794   // }
795 
796   const std::string predefs =
797       R"(OpCapability Shader
798 %1 = OpExtInstImport "GLSL.std.450"
799 OpMemoryModel Logical GLSL450
800 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
801 OpExecutionMode %main OriginUpperLeft
802 OpSource GLSL 140
803 OpName %main "main"
804 OpName %v "v"
805 OpName %BaseColor "BaseColor"
806 OpName %gl_FragColor "gl_FragColor"
807 %void = OpTypeVoid
808 %8 = OpTypeFunction %void
809 %float = OpTypeFloat 32
810 %v4float = OpTypeVector %float 4
811 %_ptr_Function_v4float = OpTypePointer Function %v4float
812 %_ptr_Input_v4float = OpTypePointer Input %v4float
813 %BaseColor = OpVariable %_ptr_Input_v4float Input
814 %_ptr_Function_float = OpTypePointer Function %float
815 %uint = OpTypeInt 32 0
816 %uint_3 = OpConstant %uint 3
817 %_ptr_Output_v4float = OpTypePointer Output %v4float
818 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
819 )";
820 
821   const std::string before =
822       R"(%main = OpFunction %void None %8
823 %17 = OpLabel
824 %v = OpVariable %_ptr_Function_v4float Function
825 %18 = OpLoad %v4float %BaseColor
826 OpStore %v %18
827 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
828 %21 = OpLoad %v4float %v
829 OpStore %gl_FragColor %21
830 OpReturn
831 OpFunctionEnd
832 )";
833 
834   const std::string after =
835       R"(%main = OpFunction %void None %8
836 %17 = OpLabel
837 %v = OpVariable %_ptr_Function_v4float Function
838 %18 = OpLoad %v4float %BaseColor
839 OpStore %v %18
840 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
841 OpStore %gl_FragColor %18
842 OpReturn
843 OpFunctionEnd
844 )";
845 
846   SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
847   SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
848                                                   predefs + after, true, true);
849 }
850 // TODO(greg-lunarg): Add tests to verify handling of these cases:
851 //
852 //    Other types
853 //    Others?
854 
855 }  // namespace
856 }  // namespace opt
857 }  // namespace spvtools
858