Debian Codegraph @

test/opt/local_single_store_elim_test.cpp

raw
1 // Copyright (c) 2017 Valve Corporation
2 // Copyright (c) 2017 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include <string>
17
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
27 TEST_F(LocalSingleStoreElimTest, DoSomethingWithExtensions) {
28 const std::string capabilities_and_extensions =
29 R"(OpCapability Shader
30 OpExtension "SPV_EXT_fragment_shader_interlock"
31 OpExtension "SPV_NV_compute_shader_derivatives"
32 OpExtension "SPV_KHR_ray_query"
33 OpExtension "SPV_NV_shader_subgroup_partitioned"
34 OpExtension "SPV_KHR_ray_tracing"
35 OpExtension "SPV_EXT_descriptor_indexing"
36 )";
37
38 const std::string before = capabilities_and_extensions +
39 R"(%1 = OpExtInstImport "GLSL.std.450"
40 OpMemoryModel Logical GLSL450
41 OpEntryPoint Fragment %2 "main"
42 OpExecutionMode %2 OriginUpperLeft
43 OpSource GLSL 140
44 %void = OpTypeVoid
45 %4 = OpTypeFunction %void
46 %float = OpTypeFloat 32
47 %_ptr_Function_float = OpTypePointer Function %float
48 %float_0 = OpConstant %float 0
49 %2 = OpFunction %void None %4
50 %8 = OpLabel
51 %9 = OpVariable %_ptr_Function_float Function
52 OpStore %9 %float_0
53 %10 = OpLoad %float %9
54 OpReturn
55 OpFunctionEnd
56 )";
57 const std::string after = capabilities_and_extensions +
58 R"(%1 = OpExtInstImport "GLSL.std.450"
59 OpMemoryModel Logical GLSL450
60 OpEntryPoint Fragment %2 "main"
61 OpExecutionMode %2 OriginUpperLeft
62 OpSource GLSL 140
63 %void = OpTypeVoid
64 %4 = OpTypeFunction %void
65 %float = OpTypeFloat 32
66 %_ptr_Function_float = OpTypePointer Function %float
67 %float_0 = OpConstant %float 0
68 %2 = OpFunction %void None %4
69 %8 = OpLabel
70 %9 = OpVariable %_ptr_Function_float Function
71 OpStore %9 %float_0
72 OpReturn
73 OpFunctionEnd
74 )";
75 SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true);
76 }
77 TEST_F(LocalSingleStoreElimTest, PositiveAndNegative) {
78 // Single store to v is optimized. Multiple store to
79 // f is not optimized.
80 //
81 // #version 140
82 //
83 // in vec4 BaseColor;
84 // in float fi;
85 //
86 // void main()
87 // {
88 // vec4 v = BaseColor;
89 // float f = fi;
90 // if (f < 0)
91 // f = 0.0;
92 // gl_FragColor = v + f;
93 // }
94
95 const std::string predefs =
96 R"(OpCapability Shader
97 %1 = OpExtInstImport "GLSL.std.450"
98 OpMemoryModel Logical GLSL450
99 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
100 OpExecutionMode %main OriginUpperLeft
101 OpSource GLSL 140
102 OpName %main "main"
103 OpName %v "v"
104 OpName %BaseColor "BaseColor"
105 OpName %f "f"
106 OpName %fi "fi"
107 OpName %gl_FragColor "gl_FragColor"
108 %void = OpTypeVoid
109 %9 = OpTypeFunction %void
110 %float = OpTypeFloat 32
111 %v4float = OpTypeVector %float 4
112 %_ptr_Function_v4float = OpTypePointer Function %v4float
113 %_ptr_Input_v4float = OpTypePointer Input %v4float
114 %BaseColor = OpVariable %_ptr_Input_v4float Input
115 %_ptr_Function_float = OpTypePointer Function %float
116 %_ptr_Input_float = OpTypePointer Input %float
117 %fi = OpVariable %_ptr_Input_float Input
118 %float_0 = OpConstant %float 0
119 %bool = OpTypeBool
120 %_ptr_Output_v4float = OpTypePointer Output %v4float
121 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
122 )";
123
124 const std::string before =
125 R"(%main = OpFunction %void None %9
126 %19 = OpLabel
127 %v = OpVariable %_ptr_Function_v4float Function
128 %f = OpVariable %_ptr_Function_float Function
129 %20 = OpLoad %v4float %BaseColor
130 OpStore %v %20
131 %21 = OpLoad %float %fi
132 OpStore %f %21
133 %22 = OpLoad %float %f
134 %23 = OpFOrdLessThan %bool %22 %float_0
135 OpSelectionMerge %24 None
136 OpBranchConditional %23 %25 %24
137 %25 = OpLabel
138 OpStore %f %float_0
139 OpBranch %24
140 %24 = OpLabel
141 %26 = OpLoad %v4float %v
142 %27 = OpLoad %float %f
143 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
144 %29 = OpFAdd %v4float %26 %28
145 OpStore %gl_FragColor %29
146 OpReturn
147 OpFunctionEnd
148 )";
149
150 const std::string after =
151 R"(%main = OpFunction %void None %9
152 %19 = OpLabel
153 %v = OpVariable %_ptr_Function_v4float Function
154 %f = OpVariable %_ptr_Function_float Function
155 %20 = OpLoad %v4float %BaseColor
156 OpStore %v %20
157 %21 = OpLoad %float %fi
158 OpStore %f %21
159 %22 = OpLoad %float %f
160 %23 = OpFOrdLessThan %bool %22 %float_0
161 OpSelectionMerge %24 None
162 OpBranchConditional %23 %25 %24
163 %25 = OpLabel
164 OpStore %f %float_0
165 OpBranch %24
166 %24 = OpLabel
167 %27 = OpLoad %float %f
168 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
169 %29 = OpFAdd %v4float %20 %28
170 OpStore %gl_FragColor %29
171 OpReturn
172 OpFunctionEnd
173 )";
174
175 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
176 predefs + after, true, true);
177 }
178
179 TEST_F(LocalSingleStoreElimTest, LSSElimForLinkage) {
180 const std::string predefs =
181 R"(OpCapability Shader
182 OpCapability Linkage
183 %1 = OpExtInstImport "GLSL.std.450"
184 OpMemoryModel Logical GLSL450
185 OpSource HLSL 630
186 OpName %main "main"
187 OpName %v "v"
188 OpName %BaseColor "BaseColor"
189 OpName %f "f"
190 OpName %fi "fi"
191 OpName %gl_FragColor "gl_FragColor"
192 OpDecorate %main LinkageAttributes "main" Export
193 %void = OpTypeVoid
194 %9 = OpTypeFunction %void
195 %float = OpTypeFloat 32
196 %v4float = OpTypeVector %float 4
197 %_ptr_Function_v4float = OpTypePointer Function %v4float
198 %_ptr_Input_v4float = OpTypePointer Input %v4float
199 %BaseColor = OpVariable %_ptr_Input_v4float Input
200 %_ptr_Function_float = OpTypePointer Function %float
201 %_ptr_Input_float = OpTypePointer Input %float
202 %fi = OpVariable %_ptr_Input_float Input
203 %float_0 = OpConstant %float 0
204 %bool = OpTypeBool
205 %_ptr_Output_v4float = OpTypePointer Output %v4float
206 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
207 )";
208
209 const std::string before =
210 R"(%main = OpFunction %void None %9
211 %19 = OpLabel
212 %v = OpVariable %_ptr_Function_v4float Function
213 %f = OpVariable %_ptr_Function_float Function
214 %20 = OpLoad %v4float %BaseColor
215 OpStore %v %20
216 %21 = OpLoad %float %fi
217 OpStore %f %21
218 %22 = OpLoad %float %f
219 %23 = OpFOrdLessThan %bool %22 %float_0
220 OpSelectionMerge %24 None
221 OpBranchConditional %23 %25 %24
222 %25 = OpLabel
223 OpStore %f %float_0
224 OpBranch %24
225 %24 = OpLabel
226 %26 = OpLoad %v4float %v
227 %27 = OpLoad %float %f
228 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
229 %29 = OpFAdd %v4float %26 %28
230 OpStore %gl_FragColor %29
231 OpReturn
232 OpFunctionEnd
233 )";
234
235 const std::string after =
236 R"(%main = OpFunction %void None %9
237 %19 = OpLabel
238 %v = OpVariable %_ptr_Function_v4float Function
239 %f = OpVariable %_ptr_Function_float Function
240 %20 = OpLoad %v4float %BaseColor
241 OpStore %v %20
242 %21 = OpLoad %float %fi
243 OpStore %f %21
244 %22 = OpLoad %float %f
245 %23 = OpFOrdLessThan %bool %22 %float_0
246 OpSelectionMerge %24 None
247 OpBranchConditional %23 %25 %24
248 %25 = OpLabel
249 OpStore %f %float_0
250 OpBranch %24
251 %24 = OpLabel
252 %27 = OpLoad %float %f
253 %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
254 %29 = OpFAdd %v4float %20 %28
255 OpStore %gl_FragColor %29
256 OpReturn
257 OpFunctionEnd
258 )";
259
260 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
261 predefs + after, true, true);
262 }
263
264 TEST_F(LocalSingleStoreElimTest, ThreeStores) {
265 // Three stores to multiple loads of v is not optimized.
266
267 const std::string predefs =
268 R"(OpCapability Shader
269 %1 = OpExtInstImport "GLSL.std.450"
270 OpMemoryModel Logical GLSL450
271 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
272 OpExecutionMode %main OriginUpperLeft
273 OpSource GLSL 140
274 OpName %main "main"
275 OpName %v "v"
276 OpName %BaseColor "BaseColor"
277 OpName %fi "fi"
278 OpName %r "r"
279 OpName %gl_FragColor "gl_FragColor"
280 %void = OpTypeVoid
281 %9 = OpTypeFunction %void
282 %float = OpTypeFloat 32
283 %v4float = OpTypeVector %float 4
284 %_ptr_Function_v4float = OpTypePointer Function %v4float
285 %_ptr_Input_v4float = OpTypePointer Input %v4float
286 %BaseColor = OpVariable %_ptr_Input_v4float Input
287 %_ptr_Input_float = OpTypePointer Input %float
288 %fi = OpVariable %_ptr_Input_float Input
289 %float_0 = OpConstant %float 0
290 %bool = OpTypeBool
291 %float_1 = OpConstant %float 1
292 %_ptr_Output_v4float = OpTypePointer Output %v4float
293 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
294 )";
295
296 const std::string before =
297 R"(%main = OpFunction %void None %9
298 %19 = OpLabel
299 %v = OpVariable %_ptr_Function_v4float Function
300 %r = OpVariable %_ptr_Function_v4float Function
301 %20 = OpLoad %v4float %BaseColor
302 OpStore %v %20
303 %21 = OpLoad %float %fi
304 %22 = OpFOrdLessThan %bool %21 %float_0
305 OpSelectionMerge %23 None
306 OpBranchConditional %22 %24 %25
307 %24 = OpLabel
308 %26 = OpLoad %v4float %v
309 OpStore %v %26
310 OpStore %r %26
311 OpBranch %23
312 %25 = OpLabel
313 %27 = OpLoad %v4float %v
314 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
315 OpStore %v %28
316 %29 = OpFSub %v4float %28 %27
317 OpStore %r %29
318 OpBranch %23
319 %23 = OpLabel
320 %30 = OpLoad %v4float %r
321 OpStore %gl_FragColor %30
322 OpReturn
323 OpFunctionEnd
324 )";
325
326 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
327 predefs + before, true, true);
328 }
329
330 TEST_F(LocalSingleStoreElimTest, MultipleLoads) {
331 // Single store to multiple loads of v is optimized.
332 //
333 // #version 140
334 //
335 // in vec4 BaseColor;
336 // in float fi;
337 //
338 // void main()
339 // {
340 // vec4 v = BaseColor;
341 // float f = fi;
342 // if (f < 0)
343 // f = 0.0;
344 // gl_FragColor = v + f;
345 // }
346
347 const std::string predefs =
348 R"(OpCapability Shader
349 %1 = OpExtInstImport "GLSL.std.450"
350 OpMemoryModel Logical GLSL450
351 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
352 OpExecutionMode %main OriginUpperLeft
353 OpSource GLSL 140
354 OpName %main "main"
355 OpName %v "v"
356 OpName %BaseColor "BaseColor"
357 OpName %fi "fi"
358 OpName %r "r"
359 OpName %gl_FragColor "gl_FragColor"
360 %void = OpTypeVoid
361 %9 = OpTypeFunction %void
362 %float = OpTypeFloat 32
363 %v4float = OpTypeVector %float 4
364 %_ptr_Function_v4float = OpTypePointer Function %v4float
365 %_ptr_Input_v4float = OpTypePointer Input %v4float
366 %BaseColor = OpVariable %_ptr_Input_v4float Input
367 %_ptr_Input_float = OpTypePointer Input %float
368 %fi = OpVariable %_ptr_Input_float Input
369 %float_0 = OpConstant %float 0
370 %bool = OpTypeBool
371 %float_1 = OpConstant %float 1
372 %_ptr_Output_v4float = OpTypePointer Output %v4float
373 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
374 )";
375
376 const std::string before =
377 R"(%main = OpFunction %void None %9
378 %19 = OpLabel
379 %v = OpVariable %_ptr_Function_v4float Function
380 %r = OpVariable %_ptr_Function_v4float Function
381 %20 = OpLoad %v4float %BaseColor
382 OpStore %v %20
383 %21 = OpLoad %float %fi
384 %22 = OpFOrdLessThan %bool %21 %float_0
385 OpSelectionMerge %23 None
386 OpBranchConditional %22 %24 %25
387 %24 = OpLabel
388 %26 = OpLoad %v4float %v
389 OpStore %r %26
390 OpBranch %23
391 %25 = OpLabel
392 %27 = OpLoad %v4float %v
393 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
394 %29 = OpFSub %v4float %28 %27
395 OpStore %r %29
396 OpBranch %23
397 %23 = OpLabel
398 %30 = OpLoad %v4float %r
399 OpStore %gl_FragColor %30
400 OpReturn
401 OpFunctionEnd
402 )";
403
404 const std::string after =
405 R"(%main = OpFunction %void None %9
406 %19 = OpLabel
407 %v = OpVariable %_ptr_Function_v4float Function
408 %r = OpVariable %_ptr_Function_v4float Function
409 %20 = OpLoad %v4float %BaseColor
410 OpStore %v %20
411 %21 = OpLoad %float %fi
412 %22 = OpFOrdLessThan %bool %21 %float_0
413 OpSelectionMerge %23 None
414 OpBranchConditional %22 %24 %25
415 %24 = OpLabel
416 OpStore %r %20
417 OpBranch %23
418 %25 = OpLabel
419 %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
420 %29 = OpFSub %v4float %28 %20
421 OpStore %r %29
422 OpBranch %23
423 %23 = OpLabel
424 %30 = OpLoad %v4float %r
425 OpStore %gl_FragColor %30
426 OpReturn
427 OpFunctionEnd
428 )";
429
430 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
431 predefs + after, true, true);
432 }
433
434 TEST_F(LocalSingleStoreElimTest, NoStoreElimWithInterveningAccessChainLoad) {
435 // Last load of v is eliminated, but access chain load and store of v isn't
436 //
437 // #version 140
438 //
439 // in vec4 BaseColor;
440 //
441 // void main()
442 // {
443 // vec4 v = BaseColor;
444 // float f = v[3];
445 // gl_FragColor = v * f;
446 // }
447
448 const std::string predefs =
449 R"(OpCapability Shader
450 %1 = OpExtInstImport "GLSL.std.450"
451 OpMemoryModel Logical GLSL450
452 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
453 OpExecutionMode %main OriginUpperLeft
454 OpSource GLSL 140
455 OpName %main "main"
456 OpName %v "v"
457 OpName %BaseColor "BaseColor"
458 OpName %f "f"
459 OpName %gl_FragColor "gl_FragColor"
460 %void = OpTypeVoid
461 %8 = OpTypeFunction %void
462 %float = OpTypeFloat 32
463 %v4float = OpTypeVector %float 4
464 %_ptr_Function_v4float = OpTypePointer Function %v4float
465 %_ptr_Input_v4float = OpTypePointer Input %v4float
466 %BaseColor = OpVariable %_ptr_Input_v4float Input
467 %_ptr_Function_float = OpTypePointer Function %float
468 %uint = OpTypeInt 32 0
469 %uint_3 = OpConstant %uint 3
470 %_ptr_Output_v4float = OpTypePointer Output %v4float
471 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
472 )";
473
474 const std::string before =
475 R"(%main = OpFunction %void None %8
476 %17 = OpLabel
477 %v = OpVariable %_ptr_Function_v4float Function
478 %f = OpVariable %_ptr_Function_float Function
479 %18 = OpLoad %v4float %BaseColor
480 OpStore %v %18
481 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
482 %20 = OpLoad %float %19
483 OpStore %f %20
484 %21 = OpLoad %v4float %v
485 %22 = OpLoad %float %f
486 %23 = OpVectorTimesScalar %v4float %21 %22
487 OpStore %gl_FragColor %23
488 OpReturn
489 OpFunctionEnd
490 )";
491
492 const std::string after =
493 R"(%main = OpFunction %void None %8
494 %17 = OpLabel
495 %v = OpVariable %_ptr_Function_v4float Function
496 %f = OpVariable %_ptr_Function_float Function
497 %18 = OpLoad %v4float %BaseColor
498 OpStore %v %18
499 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
500 %20 = OpLoad %float %19
501 OpStore %f %20
502 %23 = OpVectorTimesScalar %v4float %18 %20
503 OpStore %gl_FragColor %23
504 OpReturn
505 OpFunctionEnd
506 )";
507
508 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
509 predefs + after, true, true);
510 }
511
512 TEST_F(LocalSingleStoreElimTest, NoReplaceOfDominatingPartialStore) {
513 // Note: SPIR-V hand edited to initialize v to vec4(0.0)
514 //
515 // #version 140
516 //
517 // in vec4 BaseColor;
518 //
519 // void main()
520 // {
521 // vec4 v;
522 // float v[1] = 1.0;
523 // gl_FragColor = v;
524 // }
525
526 const std::string assembly =
527 R"(OpCapability Shader
528 %1 = OpExtInstImport "GLSL.std.450"
529 OpMemoryModel Logical GLSL450
530 OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor
531 OpExecutionMode %main OriginUpperLeft
532 OpSource GLSL 140
533 OpName %main "main"
534 OpName %v "v"
535 OpName %gl_FragColor "gl_FragColor"
536 OpName %BaseColor "BaseColor"
537 %void = OpTypeVoid
538 %7 = OpTypeFunction %void
539 %float = OpTypeFloat 32
540 %v4float = OpTypeVector %float 4
541 %_ptr_Function_v4float = OpTypePointer Function %v4float
542 %float_0 = OpConstant %float 0
543 %12 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
544 %float_1 = OpConstant %float 1
545 %uint = OpTypeInt 32 0
546 %uint_1 = OpConstant %uint 1
547 %_ptr_Function_float = OpTypePointer Function %float
548 %_ptr_Output_v4float = OpTypePointer Output %v4float
549 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
550 %_ptr_Input_v4float = OpTypePointer Input %v4float
551 %BaseColor = OpVariable %_ptr_Input_v4float Input
552 %main = OpFunction %void None %7
553 %19 = OpLabel
554 %v = OpVariable %_ptr_Function_v4float Function %12
555 %20 = OpAccessChain %_ptr_Function_float %v %uint_1
556 OpStore %20 %float_1
557 %21 = OpLoad %v4float %v
558 OpStore %gl_FragColor %21
559 OpReturn
560 OpFunctionEnd
561 )";
562
563 SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
564 true);
565 }
566
567 TEST_F(LocalSingleStoreElimTest, ElimIfCopyObjectInFunction) {
568 // Note: hand edited to insert OpCopyObject
569 //
570 // #version 140
571 //
572 // in vec4 BaseColor;
573 // in float fi;
574 //
575 // void main()
576 // {
577 // vec4 v = BaseColor;
578 // float f = fi;
579 // if (f < 0)
580 // f = 0.0;
581 // gl_FragColor = v + f;
582 // }
583
584 const std::string predefs =
585 R"(OpCapability Shader
586 %1 = OpExtInstImport "GLSL.std.450"
587 OpMemoryModel Logical GLSL450
588 OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
589 OpExecutionMode %main OriginUpperLeft
590 OpSource GLSL 140
591 OpName %main "main"
592 OpName %v "v"
593 OpName %BaseColor "BaseColor"
594 OpName %f "f"
595 OpName %fi "fi"
596 OpName %gl_FragColor "gl_FragColor"
597 %void = OpTypeVoid
598 %9 = OpTypeFunction %void
599 %float = OpTypeFloat 32
600 %v4float = OpTypeVector %float 4
601 %_ptr_Function_v4float = OpTypePointer Function %v4float
602 %_ptr_Input_v4float = OpTypePointer Input %v4float
603 %BaseColor = OpVariable %_ptr_Input_v4float Input
604 %_ptr_Function_float = OpTypePointer Function %float
605 %_ptr_Input_float = OpTypePointer Input %float
606 %fi = OpVariable %_ptr_Input_float Input
607 %float_0 = OpConstant %float 0
608 %bool = OpTypeBool
609 %_ptr_Output_v4float = OpTypePointer Output %v4float
610 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
611 )";
612
613 const std::string before =
614 R"(%main = OpFunction %void None %9
615 %19 = OpLabel
616 %v = OpVariable %_ptr_Function_v4float Function
617 %f = OpVariable %_ptr_Function_float Function
618 %20 = OpLoad %v4float %BaseColor
619 OpStore %v %20
620 %21 = OpLoad %float %fi
621 OpStore %f %21
622 %22 = OpLoad %float %f
623 %23 = OpFOrdLessThan %bool %22 %float_0
624 OpSelectionMerge %24 None
625 OpBranchConditional %23 %25 %24
626 %25 = OpLabel
627 OpStore %f %float_0
628 OpBranch %24
629 %24 = OpLabel
630 %26 = OpCopyObject %_ptr_Function_v4float %v
631 %27 = OpLoad %v4float %26
632 %28 = OpLoad %float %f
633 %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
634 %30 = OpFAdd %v4float %27 %29
635 OpStore %gl_FragColor %30
636 OpReturn
637 OpFunctionEnd
638 )";
639
640 const std::string after =
641 R"(%main = OpFunction %void None %9
642 %19 = OpLabel
643 %v = OpVariable %_ptr_Function_v4float Function
644 %f = OpVariable %_ptr_Function_float Function
645 %20 = OpLoad %v4float %BaseColor
646 OpStore %v %20
647 %21 = OpLoad %float %fi
648 OpStore %f %21
649 %22 = OpLoad %float %f
650 %23 = OpFOrdLessThan %bool %22 %float_0
651 OpSelectionMerge %24 None
652 OpBranchConditional %23 %25 %24
653 %25 = OpLabel
654 OpStore %f %float_0
655 OpBranch %24
656 %24 = OpLabel
657 %26 = OpCopyObject %_ptr_Function_v4float %v
658 %28 = OpLoad %float %f
659 %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
660 %30 = OpFAdd %v4float %20 %29
661 OpStore %gl_FragColor %30
662 OpReturn
663 OpFunctionEnd
664 )";
665
666 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
667 predefs + after, true, true);
668 }
669
670 TEST_F(LocalSingleStoreElimTest, NoOptIfStoreNotDominating) {
671 // Single store to f not optimized because it does not dominate
672 // the load.
673 //
674 // #version 140
675 //
676 // in vec4 BaseColor;
677 // in float fi;
678 //
679 // void main()
680 // {
681 // float f;
682 // if (fi < 0)
683 // f = 0.5;
684 // if (fi < 0)
685 // gl_FragColor = BaseColor * f;
686 // else
687 // gl_FragColor = BaseColor;
688 // }
689
690 const std::string assembly =
691 R"(OpCapability Shader
692 %1 = OpExtInstImport "GLSL.std.450"
693 OpMemoryModel Logical GLSL450
694 OpEntryPoint Fragment %main "main" %fi %gl_FragColor %BaseColor
695 OpExecutionMode %main OriginUpperLeft
696 OpSource GLSL 140
697 OpName %main "main"
698 OpName %fi "fi"
699 OpName %f "f"
700 OpName %gl_FragColor "gl_FragColor"
701 OpName %BaseColor "BaseColor"
702 %void = OpTypeVoid
703 %8 = OpTypeFunction %void
704 %float = OpTypeFloat 32
705 %_ptr_Input_float = OpTypePointer Input %float
706 %fi = OpVariable %_ptr_Input_float Input
707 %float_0 = OpConstant %float 0
708 %bool = OpTypeBool
709 %_ptr_Function_float = OpTypePointer Function %float
710 %float_0_5 = OpConstant %float 0.5
711 %v4float = OpTypeVector %float 4
712 %_ptr_Output_v4float = OpTypePointer Output %v4float
713 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
714 %_ptr_Input_v4float = OpTypePointer Input %v4float
715 %BaseColor = OpVariable %_ptr_Input_v4float Input
716 %main = OpFunction %void None %8
717 %18 = OpLabel
718 %f = OpVariable %_ptr_Function_float Function
719 %19 = OpLoad %float %fi
720 %20 = OpFOrdLessThan %bool %19 %float_0
721 OpSelectionMerge %21 None
722 OpBranchConditional %20 %22 %21
723 %22 = OpLabel
724 OpStore %f %float_0_5
725 OpBranch %21
726 %21 = OpLabel
727 %23 = OpLoad %float %fi
728 %24 = OpFOrdLessThan %bool %23 %float_0
729 OpSelectionMerge %25 None
730 OpBranchConditional %24 %26 %27
731 %26 = OpLabel
732 %28 = OpLoad %v4float %BaseColor
733 %29 = OpLoad %float %f
734 %30 = OpVectorTimesScalar %v4float %28 %29
735 OpStore %gl_FragColor %30
736 OpBranch %25
737 %27 = OpLabel
738 %31 = OpLoad %v4float %BaseColor
739 OpStore %gl_FragColor %31
740 OpBranch %25
741 %25 = OpLabel
742 OpReturn
743 OpFunctionEnd
744 )";
745
746 SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
747 true);
748 }
749
750 TEST_F(LocalSingleStoreElimTest, OptInitializedVariableLikeStore) {
751 // Initialized variable f is optimized like it was a store.
752 // Note: The SPIR-V was edited to turn the store to f to an
753 // an initialization.
754 //
755 // #version 140
756 //
757 // void main()
758 // {
759 // float f = 0.0;
760 // gl_FragColor = vec4(f);
761 // }
762
763 const std::string predefs =
764 R"(OpCapability Shader
765 %1 = OpExtInstImport "GLSL.std.450"
766 OpMemoryModel Logical GLSL450
767 OpEntryPoint Fragment %main "main" %gl_FragColor
768 OpExecutionMode %main OriginUpperLeft
769 OpSource GLSL 140
770 OpName %main "main"
771 OpName %f "f"
772 OpName %gl_FragColor "gl_FragColor"
773 OpDecorate %gl_FragColor Location 0
774 %void = OpTypeVoid
775 %6 = OpTypeFunction %void
776 %float = OpTypeFloat 32
777 %_ptr_Function_float = OpTypePointer Function %float
778 %float_0 = OpConstant %float 0
779 %v4float = OpTypeVector %float 4
780 %_ptr_Output_v4float = OpTypePointer Output %v4float
781 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
782 )";
783
784 const std::string before =
785 R"(%main = OpFunction %void None %6
786 %12 = OpLabel
787 %f = OpVariable %_ptr_Function_float Function %float_0
788 %13 = OpLoad %float %f
789 %14 = OpCompositeConstruct %v4float %13 %13 %13 %13
790 OpStore %gl_FragColor %14
791 OpReturn
792 OpFunctionEnd
793 )";
794
795 const std::string after =
796 R"(%main = OpFunction %void None %6
797 %12 = OpLabel
798 %f = OpVariable %_ptr_Function_float Function %float_0
799 %14 = OpCompositeConstruct %v4float %float_0 %float_0 %float_0 %float_0
800 OpStore %gl_FragColor %14
801 OpReturn
802 OpFunctionEnd
803 )";
804
805 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
806 predefs + after, true, true);
807 }
808
809 TEST_F(LocalSingleStoreElimTest, PointerVariable) {
810 // Test that checks if a pointer variable is removed.
811
812 const std::string before =
813 R"(OpCapability Shader
814 OpMemoryModel Logical GLSL450
815 OpEntryPoint Fragment %1 "main" %2
816 OpExecutionMode %1 OriginUpperLeft
817 OpMemberDecorate %_struct_3 0 Offset 0
818 OpDecorate %_runtimearr__struct_3 ArrayStride 16
819 OpMemberDecorate %_struct_5 0 Offset 0
820 OpDecorate %_struct_5 BufferBlock
821 OpMemberDecorate %_struct_6 0 Offset 0
822 OpDecorate %_struct_6 BufferBlock
823 OpDecorate %2 Location 0
824 OpDecorate %7 DescriptorSet 0
825 OpDecorate %7 Binding 0
826 %void = OpTypeVoid
827 %10 = OpTypeFunction %void
828 %int = OpTypeInt 32 1
829 %uint = OpTypeInt 32 0
830 %float = OpTypeFloat 32
831 %v4float = OpTypeVector %float 4
832 %_ptr_Output_v4float = OpTypePointer Output %v4float
833 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
834 %_struct_3 = OpTypeStruct %v4float
835 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
836 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
837 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
838 %_struct_6 = OpTypeStruct %int
839 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
840 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
841 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
842 %int_0 = OpConstant %int 0
843 %uint_0 = OpConstant %uint 0
844 %2 = OpVariable %_ptr_Output_v4float Output
845 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
846 %1 = OpFunction %void None %10
847 %23 = OpLabel
848 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
849 OpStore %24 %7
850 %26 = OpLoad %_ptr_Uniform__struct_5 %24
851 %27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0
852 %28 = OpLoad %v4float %27
853 %29 = OpCopyObject %v4float %28
854 OpStore %2 %28
855 OpReturn
856 OpFunctionEnd
857 )";
858
859 const std::string after =
860 R"(OpCapability Shader
861 OpMemoryModel Logical GLSL450
862 OpEntryPoint Fragment %1 "main" %2
863 OpExecutionMode %1 OriginUpperLeft
864 OpMemberDecorate %_struct_3 0 Offset 0
865 OpDecorate %_runtimearr__struct_3 ArrayStride 16
866 OpMemberDecorate %_struct_5 0 Offset 0
867 OpDecorate %_struct_5 BufferBlock
868 OpMemberDecorate %_struct_6 0 Offset 0
869 OpDecorate %_struct_6 BufferBlock
870 OpDecorate %2 Location 0
871 OpDecorate %7 DescriptorSet 0
872 OpDecorate %7 Binding 0
873 %void = OpTypeVoid
874 %10 = OpTypeFunction %void
875 %int = OpTypeInt 32 1
876 %uint = OpTypeInt 32 0
877 %float = OpTypeFloat 32
878 %v4float = OpTypeVector %float 4
879 %_ptr_Output_v4float = OpTypePointer Output %v4float
880 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
881 %_struct_3 = OpTypeStruct %v4float
882 %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
883 %_struct_5 = OpTypeStruct %_runtimearr__struct_3
884 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
885 %_struct_6 = OpTypeStruct %int
886 %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
887 %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
888 %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
889 %int_0 = OpConstant %int 0
890 %uint_0 = OpConstant %uint 0
891 %2 = OpVariable %_ptr_Output_v4float Output
892 %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
893 %1 = OpFunction %void None %10
894 %23 = OpLabel
895 %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
896 OpStore %24 %7
897 %27 = OpAccessChain %_ptr_Uniform_v4float %7 %int_0 %uint_0 %int_0
898 %28 = OpLoad %v4float %27
899 %29 = OpCopyObject %v4float %28
900 OpStore %2 %28
901 OpReturn
902 OpFunctionEnd
903 )";
904
905 // Relax logical pointers to allow pointer allocations.
906 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
907 ValidatorOptions()->relax_logical_pointer = true;
908 SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true);
909 }
910
911 // Test that that an unused OpAccessChain between a store and a use does does
912 // not hinders the replacement of the use. We need to check this because
913 // local-access-chain-convert does always remove the OpAccessChain instructions
914 // that become dead.
915
916 TEST_F(LocalSingleStoreElimTest,
917 StoreElimWithUnusedInterveningAccessChainLoad) {
918 // Last load of v is eliminated, but access chain load and store of v isn't
919 //
920 // #version 140
921 //
922 // in vec4 BaseColor;
923 //
924 // void main()
925 // {
926 // vec4 v = BaseColor;
927 // float f = v[3];
928 // gl_FragColor = v * f;
929 // }
930
931 const std::string predefs =
932 R"(OpCapability Shader
933 %1 = OpExtInstImport "GLSL.std.450"
934 OpMemoryModel Logical GLSL450
935 OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
936 OpExecutionMode %main OriginUpperLeft
937 OpSource GLSL 140
938 OpName %main "main"
939 OpName %v "v"
940 OpName %BaseColor "BaseColor"
941 OpName %gl_FragColor "gl_FragColor"
942 %void = OpTypeVoid
943 %8 = OpTypeFunction %void
944 %float = OpTypeFloat 32
945 %v4float = OpTypeVector %float 4
946 %_ptr_Function_v4float = OpTypePointer Function %v4float
947 %_ptr_Input_v4float = OpTypePointer Input %v4float
948 %BaseColor = OpVariable %_ptr_Input_v4float Input
949 %_ptr_Function_float = OpTypePointer Function %float
950 %uint = OpTypeInt 32 0
951 %uint_3 = OpConstant %uint 3
952 %_ptr_Output_v4float = OpTypePointer Output %v4float
953 %gl_FragColor = OpVariable %_ptr_Output_v4float Output
954 )";
955
956 const std::string before =
957 R"(%main = OpFunction %void None %8
958 %17 = OpLabel
959 %v = OpVariable %_ptr_Function_v4float Function
960 %18 = OpLoad %v4float %BaseColor
961 OpStore %v %18
962 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
963 %21 = OpLoad %v4float %v
964 OpStore %gl_FragColor %21
965 OpReturn
966 OpFunctionEnd
967 )";
968
969 const std::string after =
970 R"(%main = OpFunction %void None %8
971 %17 = OpLabel
972 %v = OpVariable %_ptr_Function_v4float Function
973 %18 = OpLoad %v4float %BaseColor
974 OpStore %v %18
975 %19 = OpAccessChain %_ptr_Function_float %v %uint_3
976 OpStore %gl_FragColor %18
977 OpReturn
978 OpFunctionEnd
979 )";
980
981 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
982 SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
983 predefs + after, true, true);
984 }
985
986 TEST_F(LocalSingleStoreElimTest, VariablePointerTest) {
987 // Check that the load of the first variable is still used and that the load
988 // of the third variable is propagated. The first load has to remain because
989 // of the store to the variable pointer.
990 const std::string text = R"(
991 ; CHECK: [[v1:%\w+]] = OpVariable
992 ; CHECK: [[v2:%\w+]] = OpVariable
993 ; CHECK: [[v3:%\w+]] = OpVariable
994 ; CHECK: [[ld1:%\w+]] = OpLoad %int [[v1]]
995 ; CHECK: OpIAdd %int [[ld1]] %int_0
996 OpCapability Shader
997 OpCapability VariablePointers
998 %1 = OpExtInstImport "GLSL.std.450"
999 OpMemoryModel Logical GLSL450
1000 OpEntryPoint GLCompute %2 "main"
1001 OpExecutionMode %2 LocalSize 1 1 1
1002 OpSource GLSL 450
1003 OpMemberDecorate %_struct_3 0 Offset 0
1004 OpMemberDecorate %_struct_3 1 Offset 4
1005 %void = OpTypeVoid
1006 %5 = OpTypeFunction %void
1007 %int = OpTypeInt 32 1
1008 %bool = OpTypeBool
1009 %_struct_3 = OpTypeStruct %int %int
1010 %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
1011 %_ptr_Function_int = OpTypePointer Function %int
1012 %true = OpConstantTrue %bool
1013 %int_0 = OpConstant %int 0
1014 %int_1 = OpConstant %int 1
1015 %13 = OpConstantNull %_struct_3
1016 %2 = OpFunction %void None %5
1017 %14 = OpLabel
1018 %15 = OpVariable %_ptr_Function_int Function
1019 %16 = OpVariable %_ptr_Function_int Function
1020 %17 = OpVariable %_ptr_Function_int Function
1021 OpStore %15 %int_1
1022 OpStore %17 %int_0
1023 OpSelectionMerge %18 None
1024 OpBranchConditional %true %19 %20
1025 %19 = OpLabel
1026 OpBranch %18
1027 %20 = OpLabel
1028 OpBranch %18
1029 %18 = OpLabel
1030 %21 = OpPhi %_ptr_Function_int %15 %19 %16 %20
1031 OpStore %21 %int_0
1032 %22 = OpLoad %int %15
1033 %23 = OpLoad %int %17
1034 %24 = OpIAdd %int %22 %23
1035 OpReturn
1036 OpFunctionEnd
1037 )";
1038 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1039 }
1040
1041 TEST_F(LocalSingleStoreElimTest, DebugDeclareTest) {
1042 // If OpenCL.DebugInfo.100 enabled, check that store/load is still
1043 // optimized, DebugValue placed after the store and the associated
1044 // DebugDeclare is removed.
1045 const std::string text = R"(
1046 OpCapability Shader
1047 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1048 OpMemoryModel Logical GLSL450
1049 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
1050 OpExecutionMode %MainPs OriginUpperLeft
1051 %20 = OpString "foo.frag"
1052 %24 = OpString "PS_OUTPUT"
1053 %28 = OpString "float"
1054 %31 = OpString "vColor"
1055 %33 = OpString "PS_INPUT"
1056 %38 = OpString "vTextureCoords"
1057 %40 = OpString "@type.2d.image"
1058 %41 = OpString "type.2d.image"
1059 %43 = OpString "Texture2D.TemplateParam"
1060 %47 = OpString "src.MainPs"
1061 %51 = OpString "tc"
1062 %53 = OpString "ps_output"
1063 %56 = OpString "i"
1064 %58 = OpString "@type.sampler"
1065 %59 = OpString "type.sampler"
1066 %61 = OpString "g_sAniso"
1067 %63 = OpString "g_tColor"
1068 OpName %type_2d_image "type.2d.image"
1069 OpName %g_tColor "g_tColor"
1070 OpName %type_sampler "type.sampler"
1071 OpName %g_sAniso "g_sAniso"
1072 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
1073 OpName %out_var_SV_Target0 "out.var.SV_Target0"
1074 OpName %MainPs "MainPs"
1075 OpName %PS_INPUT "PS_INPUT"
1076 OpMemberName %PS_INPUT 0 "vTextureCoords"
1077 OpName %param_var_i "param.var.i"
1078 OpName %PS_OUTPUT "PS_OUTPUT"
1079 OpMemberName %PS_OUTPUT 0 "vColor"
1080 OpName %type_sampled_image "type.sampled.image"
1081 OpDecorate %in_var_TEXCOORD2 Location 0
1082 OpDecorate %out_var_SV_Target0 Location 0
1083 OpDecorate %g_tColor DescriptorSet 0
1084 OpDecorate %g_tColor Binding 0
1085 OpDecorate %g_sAniso DescriptorSet 0
1086 OpDecorate %g_sAniso Binding 1
1087 %int = OpTypeInt 32 1
1088 %int_0 = OpConstant %int 0
1089 %uint = OpTypeInt 32 0
1090 %uint_32 = OpConstant %uint 32
1091 %float = OpTypeFloat 32
1092 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
1093 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
1094 %type_sampler = OpTypeSampler
1095 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
1096 %v2float = OpTypeVector %float 2
1097 %_ptr_Input_v2float = OpTypePointer Input %v2float
1098 %v4float = OpTypeVector %float 4
1099 %_ptr_Output_v4float = OpTypePointer Output %v4float
1100 %void = OpTypeVoid
1101 %uint_128 = OpConstant %uint 128
1102 %uint_0 = OpConstant %uint 0
1103 %uint_64 = OpConstant %uint 64
1104 %65 = OpTypeFunction %void
1105 %PS_INPUT = OpTypeStruct %v2float
1106 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
1107 %PS_OUTPUT = OpTypeStruct %v4float
1108 %75 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
1109 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
1110 %_ptr_Function_v2float = OpTypePointer Function %v2float
1111 %type_sampled_image = OpTypeSampledImage %type_2d_image
1112 %_ptr_Function_v4float = OpTypePointer Function %v4float
1113 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
1114 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
1115 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
1116 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
1117 %39 = OpExtInst %void %1 DebugInfoNone
1118 %55 = OpExtInst %void %1 DebugExpression
1119 %22 = OpExtInst %void %1 DebugSource %20
1120 %23 = OpExtInst %void %1 DebugCompilationUnit 1 4 %22 HLSL
1121 %26 = OpExtInst %void %1 DebugTypeComposite %24 Structure %22 10 1 %23 %24 %uint_128 FlagIsProtected|FlagIsPrivate %27
1122 %29 = OpExtInst %void %1 DebugTypeBasic %28 %uint_32 Float
1123 %30 = OpExtInst %void %1 DebugTypeVector %29 4
1124 %27 = OpExtInst %void %1 DebugTypeMember %31 %30 %22 12 5 %26 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
1125 %35 = OpExtInst %void %1 DebugTypeComposite %33 Structure %22 5 1 %23 %33 %uint_64 FlagIsProtected|FlagIsPrivate %36
1126 %37 = OpExtInst %void %1 DebugTypeVector %29 2
1127 %36 = OpExtInst %void %1 DebugTypeMember %38 %37 %22 7 5 %35 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
1128 %42 = OpExtInst %void %1 DebugTypeComposite %40 Class %22 0 0 %23 %41 %39 FlagIsProtected|FlagIsPrivate
1129 %44 = OpExtInst %void %1 DebugTypeTemplateParameter %43 %29 %39 %22 0 0
1130 %45 = OpExtInst %void %1 DebugTypeTemplate %42 %44
1131 %46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %26 %35
1132 %48 = OpExtInst %void %1 DebugFunction %47 %46 %22 15 1 %23 %47 FlagIsProtected|FlagIsPrivate 16 %39
1133 %50 = OpExtInst %void %1 DebugLexicalBlock %22 16 1 %48
1134 %52 = OpExtInst %void %1 DebugLocalVariable %51 %37 %22 19 12 %50 FlagIsLocal
1135 %54 = OpExtInst %void %1 DebugLocalVariable %53 %26 %22 17 15 %50 FlagIsLocal
1136 %57 = OpExtInst %void %1 DebugLocalVariable %56 %35 %22 15 29 %48 FlagIsLocal 1
1137 %60 = OpExtInst %void %1 DebugTypeComposite %58 Structure %22 0 0 %23 %59 %39 FlagIsProtected|FlagIsPrivate
1138 %62 = OpExtInst %void %1 DebugGlobalVariable %61 %60 %22 3 14 %23 %61 %g_sAniso FlagIsDefinition
1139 %64 = OpExtInst %void %1 DebugGlobalVariable %63 %42 %22 1 11 %23 %63 %g_tColor FlagIsDefinition
1140 %MainPs = OpFunction %void None %65
1141 %66 = OpLabel
1142 %114 = OpExtInst %void %1 DebugScope %50
1143 %98 = OpVariable %_ptr_Function_PS_OUTPUT Function
1144 %99 = OpVariable %_ptr_Function_v2float Function
1145 %115 = OpExtInst %void %1 DebugNoScope
1146 %100 = OpVariable %_ptr_Function_PS_OUTPUT Function
1147 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
1148 %70 = OpLoad %v2float %in_var_TEXCOORD2
1149 %71 = OpCompositeConstruct %PS_INPUT %70
1150 OpStore %param_var_i %71
1151 %116 = OpExtInst %void %1 DebugScope %48
1152 %102 = OpExtInst %void %1 DebugDeclare %57 %param_var_i %55
1153 %117 = OpExtInst %void %1 DebugScope %50
1154 %103 = OpExtInst %void %1 DebugDeclare %54 %98 %55
1155 OpLine %20 19 17
1156 %104 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
1157 %105 = OpLoad %v2float %104
1158 OpLine %20 19 12
1159 OpStore %99 %105
1160 %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55
1161 ;CHECK-NOT: %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55
1162 ;CHECK: %119 = OpExtInst %void %1 DebugValue %52 %105 %55
1163 OpLine %20 20 26
1164 %107 = OpLoad %type_2d_image %g_tColor
1165 OpLine %20 20 46
1166 %108 = OpLoad %type_sampler %g_sAniso
1167 OpLine %20 20 57
1168 %109 = OpLoad %v2float %99
1169 ;CHECK-NOT: %109 = OpLoad %v2float %99
1170 OpLine %20 20 26
1171 %110 = OpSampledImage %type_sampled_image %107 %108
1172 %111 = OpImageSampleImplicitLod %v4float %110 %109 None
1173 ;CHECK-NOT: %111 = OpImageSampleImplicitLod %v4float %110 %109 None
1174 ;CHECK: %111 = OpImageSampleImplicitLod %v4float %110 %105 None
1175 OpLine %20 20 5
1176 %112 = OpAccessChain %_ptr_Function_v4float %98 %int_0
1177 OpStore %112 %111
1178 OpLine %20 21 12
1179 %113 = OpLoad %PS_OUTPUT %98
1180 OpLine %20 21 5
1181 OpStore %100 %113
1182 %118 = OpExtInst %void %1 DebugNoScope
1183 %73 = OpLoad %PS_OUTPUT %100
1184 %74 = OpCompositeExtract %v4float %73 0
1185 OpStore %out_var_SV_Target0 %74
1186 OpReturn
1187 OpFunctionEnd
1188 )";
1189
1190 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1191 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1192 }
1193
1194 TEST_F(LocalSingleStoreElimTest, DebugValueTest) {
1195 // If OpenCL.DebugInfo.100 enabled, check that store/load is still
1196 // optimized, DebugValue placed after the store and the associated
1197 // DebugValue Deref is removed.
1198 const std::string text = R"(
1199 OpCapability Shader
1200 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1201 OpMemoryModel Logical GLSL450
1202 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
1203 OpExecutionMode %MainPs OriginUpperLeft
1204 %7 = OpString "foo.frag"
1205 %8 = OpString "PS_OUTPUT"
1206 %9 = OpString "float"
1207 %10 = OpString "vColor"
1208 %11 = OpString "PS_INPUT"
1209 %12 = OpString "vTextureCoords"
1210 %13 = OpString "@type.2d.image"
1211 %14 = OpString "type.2d.image"
1212 %15 = OpString "Texture2D.TemplateParam"
1213 %16 = OpString "src.MainPs"
1214 %17 = OpString "tc"
1215 %18 = OpString "ps_output"
1216 %19 = OpString "i"
1217 %20 = OpString "@type.sampler"
1218 %21 = OpString "type.sampler"
1219 %22 = OpString "g_sAniso"
1220 %23 = OpString "g_tColor"
1221 OpName %type_2d_image "type.2d.image"
1222 OpName %g_tColor "g_tColor"
1223 OpName %type_sampler "type.sampler"
1224 OpName %g_sAniso "g_sAniso"
1225 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
1226 OpName %out_var_SV_Target0 "out.var.SV_Target0"
1227 OpName %MainPs "MainPs"
1228 OpName %PS_INPUT "PS_INPUT"
1229 OpMemberName %PS_INPUT 0 "vTextureCoords"
1230 OpName %param_var_i "param.var.i"
1231 OpName %PS_OUTPUT "PS_OUTPUT"
1232 OpMemberName %PS_OUTPUT 0 "vColor"
1233 OpName %type_sampled_image "type.sampled.image"
1234 OpDecorate %in_var_TEXCOORD2 Location 0
1235 OpDecorate %out_var_SV_Target0 Location 0
1236 OpDecorate %g_tColor DescriptorSet 0
1237 OpDecorate %g_tColor Binding 0
1238 OpDecorate %g_sAniso DescriptorSet 0
1239 OpDecorate %g_sAniso Binding 1
1240 %int = OpTypeInt 32 1
1241 %int_0 = OpConstant %int 0
1242 %uint = OpTypeInt 32 0
1243 %uint_32 = OpConstant %uint 32
1244 %float = OpTypeFloat 32
1245 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
1246 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
1247 %type_sampler = OpTypeSampler
1248 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
1249 %v2float = OpTypeVector %float 2
1250 %_ptr_Input_v2float = OpTypePointer Input %v2float
1251 %v4float = OpTypeVector %float 4
1252 %_ptr_Output_v4float = OpTypePointer Output %v4float
1253 %void = OpTypeVoid
1254 %uint_128 = OpConstant %uint 128
1255 %uint_0 = OpConstant %uint 0
1256 %uint_64 = OpConstant %uint 64
1257 %45 = OpTypeFunction %void
1258 %PS_INPUT = OpTypeStruct %v2float
1259 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
1260 %PS_OUTPUT = OpTypeStruct %v4float
1261 %47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
1262 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
1263 %_ptr_Function_v2float = OpTypePointer Function %v2float
1264 %type_sampled_image = OpTypeSampledImage %type_2d_image
1265 %_ptr_Function_v4float = OpTypePointer Function %v4float
1266 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
1267 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
1268 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
1269 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
1270 %51 = OpExtInst %void %1 DebugInfoNone
1271 %52 = OpExtInst %void %1 DebugExpression
1272 %53 = OpExtInst %void %1 DebugOperation Deref
1273 %54 = OpExtInst %void %1 DebugExpression %53
1274 %55 = OpExtInst %void %1 DebugSource %7
1275 %56 = OpExtInst %void %1 DebugCompilationUnit 1 4 %55 HLSL
1276 %57 = OpExtInst %void %1 DebugTypeComposite %8 Structure %55 10 1 %56 %8 %uint_128 FlagIsProtected|FlagIsPrivate %58
1277 %59 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 Float
1278 %60 = OpExtInst %void %1 DebugTypeVector %59 4
1279 %58 = OpExtInst %void %1 DebugTypeMember %10 %60 %55 12 5 %57 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
1280 %61 = OpExtInst %void %1 DebugTypeComposite %11 Structure %55 5 1 %56 %11 %uint_64 FlagIsProtected|FlagIsPrivate %62
1281 %63 = OpExtInst %void %1 DebugTypeVector %59 2
1282 %62 = OpExtInst %void %1 DebugTypeMember %12 %63 %55 7 5 %61 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
1283 %64 = OpExtInst %void %1 DebugTypeComposite %13 Class %55 0 0 %56 %14 %51 FlagIsProtected|FlagIsPrivate
1284 %65 = OpExtInst %void %1 DebugTypeTemplateParameter %15 %59 %51 %55 0 0
1285 %66 = OpExtInst %void %1 DebugTypeTemplate %64 %65
1286 %67 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %57 %61
1287 %68 = OpExtInst %void %1 DebugFunction %16 %67 %55 15 1 %56 %16 FlagIsProtected|FlagIsPrivate 16 %51
1288 %69 = OpExtInst %void %1 DebugLexicalBlock %55 16 1 %68
1289 %70 = OpExtInst %void %1 DebugLocalVariable %17 %63 %55 19 12 %69 FlagIsLocal
1290 %71 = OpExtInst %void %1 DebugLocalVariable %18 %57 %55 17 15 %69 FlagIsLocal
1291 %72 = OpExtInst %void %1 DebugLocalVariable %19 %61 %55 15 29 %68 FlagIsLocal 1
1292 %73 = OpExtInst %void %1 DebugTypeComposite %20 Structure %55 0 0 %56 %21 %51 FlagIsProtected|FlagIsPrivate
1293 %74 = OpExtInst %void %1 DebugGlobalVariable %22 %73 %55 3 14 %56 %22 %g_sAniso FlagIsDefinition
1294 %75 = OpExtInst %void %1 DebugGlobalVariable %23 %64 %55 1 11 %56 %23 %g_tColor FlagIsDefinition
1295 %MainPs = OpFunction %void None %45
1296 %76 = OpLabel
1297 %101 = OpExtInst %void %1 DebugScope %69
1298 %78 = OpVariable %_ptr_Function_PS_OUTPUT Function
1299 %79 = OpVariable %_ptr_Function_v2float Function
1300 %102 = OpExtInst %void %1 DebugNoScope
1301 %81 = OpVariable %_ptr_Function_PS_OUTPUT Function
1302 %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
1303 %82 = OpLoad %v2float %in_var_TEXCOORD2
1304 %83 = OpCompositeConstruct %PS_INPUT %82
1305 OpStore %param_var_i %83
1306 %103 = OpExtInst %void %1 DebugScope %68
1307 %85 = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52
1308 %104 = OpExtInst %void %1 DebugScope %69
1309 %87 = OpExtInst %void %1 DebugDeclare %71 %78 %52
1310 OpLine %7 19 17
1311 %88 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
1312 %89 = OpLoad %v2float %88
1313 OpLine %7 19 12
1314 OpStore %79 %89
1315 %90 = OpExtInst %void %1 DebugValue %70 %79 %54
1316 ;CHECK-NOT: %90 = OpExtInst %void %1 DebugValue %70 %79 %54
1317 ;CHECK: %106 = OpExtInst %void %1 DebugValue %70 %89 %52
1318 OpLine %7 20 26
1319 %91 = OpLoad %type_2d_image %g_tColor
1320 OpLine %7 20 46
1321 %92 = OpLoad %type_sampler %g_sAniso
1322 OpLine %7 20 57
1323 %93 = OpLoad %v2float %79
1324 ;CHECK-NOT: %93 = OpLoad %v2float %79
1325 OpLine %7 20 26
1326 %94 = OpSampledImage %type_sampled_image %91 %92
1327 %95 = OpImageSampleImplicitLod %v4float %94 %93 None
1328 ;CHECK-NOT: %95 = OpImageSampleImplicitLod %v4float %94 %93 None
1329 ;CHECK: %95 = OpImageSampleImplicitLod %v4float %94 %89 None
1330 OpLine %7 20 5
1331 %96 = OpAccessChain %_ptr_Function_v4float %78 %int_0
1332 OpStore %96 %95
1333 OpLine %7 21 12
1334 %97 = OpLoad %PS_OUTPUT %78
1335 OpLine %7 21 5
1336 OpStore %81 %97
1337 %105 = OpExtInst %void %1 DebugNoScope
1338 %99 = OpLoad %PS_OUTPUT %81
1339 %100 = OpCompositeExtract %v4float %99 0
1340 OpStore %out_var_SV_Target0 %100
1341 OpReturn
1342 OpFunctionEnd
1343 )";
1344
1345 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1346 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1347 }
1348
1349 TEST_F(LocalSingleStoreElimTest, UseStoreLineInfoForDebugValueLine) {
1350 // When the store is in the scope of OpenCL.DebugInfo.100 DebugDeclare,
1351 // the OpLine of the added OpenCL.DebugInfo.100 DebugValue must be the
1352 // same with the OpLine of the store.
1353 const std::string text = R"(
1354 OpCapability Shader
1355 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1356 OpMemoryModel Logical GLSL450
1357 OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
1358 %7 = OpString "simple.hlsl"
1359 %8 = OpString "float"
1360 %9 = OpString "VS_OUTPUT"
1361 %10 = OpString "color"
1362 %11 = OpString "pos"
1363 %12 = OpString "main"
1364 %13 = OpString ""
1365 %14 = OpString "vout"
1366 OpName %in_var_POSITION "in.var.POSITION"
1367 OpName %in_var_COLOR "in.var.COLOR"
1368 OpName %out_var_COLOR "out.var.COLOR"
1369 OpName %main "main"
1370 OpName %VS_OUTPUT "VS_OUTPUT"
1371 OpMemberName %VS_OUTPUT 0 "pos"
1372 OpMemberName %VS_OUTPUT 1 "color"
1373 OpDecorate %gl_Position BuiltIn Position
1374 OpDecorate %in_var_POSITION Location 0
1375 OpDecorate %in_var_COLOR Location 1
1376 OpDecorate %out_var_COLOR Location 0
1377 %int = OpTypeInt 32 1
1378 %int_0 = OpConstant %int 0
1379 %int_1 = OpConstant %int 1
1380 %uint = OpTypeInt 32 0
1381 %uint_32 = OpConstant %uint 32
1382 %float = OpTypeFloat 32
1383 %v4float = OpTypeVector %float 4
1384 %_ptr_Input_v4float = OpTypePointer Input %v4float
1385 %_ptr_Output_v4float = OpTypePointer Output %v4float
1386 %void = OpTypeVoid
1387 %uint_256 = OpConstant %uint 256
1388 %uint_128 = OpConstant %uint 128
1389 %uint_0 = OpConstant %uint 0
1390 %36 = OpTypeFunction %void
1391 %_ptr_Function_v4float = OpTypePointer Function %v4float
1392 %VS_OUTPUT = OpTypeStruct %v4float %v4float
1393 %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
1394 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
1395 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
1396 %gl_Position = OpVariable %_ptr_Output_v4float Output
1397 %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
1398 %85 = OpExtInst %void %1 DebugOperation Deref
1399 %81 = OpExtInst %void %1 DebugInfoNone
1400 %52 = OpExtInst %void %1 DebugExpression
1401 %40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float
1402 %41 = OpExtInst %void %1 DebugTypeVector %40 4
1403 %42 = OpExtInst %void %1 DebugSource %7
1404 %43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL
1405 %44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46
1406 %46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
1407 %45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
1408 %47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41
1409 %48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81
1410 %49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48
1411 %50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal
1412 %84 = OpExtInst %void %1 DebugExpression %85
1413 %main = OpFunction %void None %36
1414 %54 = OpLabel
1415 %91 = OpExtInst %void %1 DebugScope %49
1416 OpLine %7 7 23
1417 %83 = OpVariable %_ptr_Function_v4float Function
1418 OpLine %7 8 13
1419 %87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1
1420 OpLine %7 7 23
1421 %82 = OpVariable %_ptr_Function_v4float Function
1422 OpLine %7 8 13
1423 %86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0
1424 OpNoLine
1425 %92 = OpExtInst %void %1 DebugNoScope
1426 %55 = OpLoad %v4float %in_var_POSITION
1427 %56 = OpLoad %v4float %in_var_COLOR
1428 ;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION
1429 ;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR
1430
1431 %94 = OpExtInst %void %1 DebugScope %49
1432 OpLine %7 9 3
1433 OpStore %82 %55
1434 ;CHECK: OpLine [[file:%\w+]] 9 3
1435 ;CHECK: OpStore {{%\w+}} [[pos]]
1436 ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout:%\w+]] [[pos]] [[empty_expr:%\w+]] %int_0
1437 ;CHECK: OpLine [[file]] 10 3
1438 ;CHECK: OpStore {{%\w+}} [[color]]
1439 ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout]] [[color]] [[empty_expr]] %int_1
1440
1441 OpLine %7 10 3
1442 OpStore %83 %56
1443 OpLine %7 11 10
1444 %90 = OpCompositeConstruct %VS_OUTPUT %55 %56
1445 OpNoLine
1446 %95 = OpExtInst %void %1 DebugNoScope
1447 %58 = OpCompositeExtract %v4float %90 0
1448 OpStore %gl_Position %58
1449 %59 = OpCompositeExtract %v4float %90 1
1450 OpStore %out_var_COLOR %59
1451 OpReturn
1452 OpFunctionEnd
1453 )";
1454
1455 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1456 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1457 }
1458
1459 TEST_F(LocalSingleStoreElimTest, AddDebugValueforStoreOutOfDebugDeclareScope) {
1460 const std::string text = R"(
1461 OpCapability Shader
1462 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1463 OpMemoryModel Logical GLSL450
1464 OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
1465 %7 = OpString "simple.hlsl"
1466 %8 = OpString "float"
1467 %9 = OpString "VS_OUTPUT"
1468 %10 = OpString "color"
1469 %11 = OpString "pos"
1470 %12 = OpString "main"
1471 %13 = OpString ""
1472 %14 = OpString "vout"
1473 OpName %in_var_POSITION "in.var.POSITION"
1474 OpName %in_var_COLOR "in.var.COLOR"
1475 OpName %out_var_COLOR "out.var.COLOR"
1476 OpName %main "main"
1477 OpName %VS_OUTPUT "VS_OUTPUT"
1478 OpMemberName %VS_OUTPUT 0 "pos"
1479 OpMemberName %VS_OUTPUT 1 "color"
1480 OpDecorate %gl_Position BuiltIn Position
1481 OpDecorate %in_var_POSITION Location 0
1482 OpDecorate %in_var_COLOR Location 1
1483 OpDecorate %out_var_COLOR Location 0
1484 %int = OpTypeInt 32 1
1485 %int_0 = OpConstant %int 0
1486 %int_1 = OpConstant %int 1
1487 %uint = OpTypeInt 32 0
1488 %uint_32 = OpConstant %uint 32
1489 %float = OpTypeFloat 32
1490 %v4float = OpTypeVector %float 4
1491 %_ptr_Input_v4float = OpTypePointer Input %v4float
1492 %_ptr_Output_v4float = OpTypePointer Output %v4float
1493 %void = OpTypeVoid
1494 %uint_256 = OpConstant %uint 256
1495 %uint_128 = OpConstant %uint 128
1496 %uint_0 = OpConstant %uint 0
1497 %36 = OpTypeFunction %void
1498 %_ptr_Function_v4float = OpTypePointer Function %v4float
1499 %VS_OUTPUT = OpTypeStruct %v4float %v4float
1500 %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
1501 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
1502 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
1503 %gl_Position = OpVariable %_ptr_Output_v4float Output
1504 %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
1505 %85 = OpExtInst %void %1 DebugOperation Deref
1506 %81 = OpExtInst %void %1 DebugInfoNone
1507 %52 = OpExtInst %void %1 DebugExpression
1508 %40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float
1509 %41 = OpExtInst %void %1 DebugTypeVector %40 4
1510 %42 = OpExtInst %void %1 DebugSource %7
1511 %43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL
1512 %44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46
1513 %46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
1514 %45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
1515 %47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41
1516 %48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81
1517 %49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48
1518 %50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal
1519 %51 = OpExtInst %void %1 DebugLocalVariable %10 %41 %42 7 23 %48 FlagIsLocal 2
1520 %53 = OpExtInst %void %1 DebugLocalVariable %11 %41 %42 6 23 %48 FlagIsLocal 1
1521 ;CHECK: [[dbg_color:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 7 23 {{%\w+}} FlagIsLocal 2
1522 ;CHECK: [[dbg_pos:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 6 23 {{%\w+}} FlagIsLocal 1
1523
1524 %84 = OpExtInst %void %1 DebugExpression %85
1525 %main = OpFunction %void None %36
1526 %54 = OpLabel
1527 %91 = OpExtInst %void %1 DebugScope %49
1528 OpLine %7 7 23
1529 %83 = OpVariable %_ptr_Function_v4float Function
1530 OpLine %7 8 13
1531 %87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1
1532 OpLine %7 7 23
1533 %82 = OpVariable %_ptr_Function_v4float Function
1534 OpLine %7 8 13
1535 %86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0
1536 OpNoLine
1537 %92 = OpExtInst %void %1 DebugNoScope
1538 %param_var_pos = OpVariable %_ptr_Function_v4float Function
1539 %param_var_color = OpVariable %_ptr_Function_v4float Function
1540 %55 = OpLoad %v4float %in_var_POSITION
1541 OpLine %7 6 23
1542 OpStore %param_var_pos %55
1543 OpNoLine
1544 %56 = OpLoad %v4float %in_var_COLOR
1545 ;CHECK: DebugNoScope
1546 ;CHECK-NOT: OpLine
1547 OpLine %7 7 23
1548 OpStore %param_var_color %56
1549 OpNoLine
1550 %93 = OpExtInst %void %1 DebugScope %48
1551 %73 = OpExtInst %void %1 DebugDeclare %53 %param_var_pos %52
1552 %74 = OpExtInst %void %1 DebugDeclare %51 %param_var_color %52
1553 ;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION
1554 ;CHECK: OpLine [[file:%\w+]] 6 23
1555 ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_pos]] [[pos]] [[empty_expr:%\w+]]
1556 ;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR
1557 ;CHECK: OpLine [[file]] 7 23
1558 ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_color]] [[color]] [[empty_expr]]
1559 ;CHECK: OpLine [[file]] 9 3
1560
1561 %94 = OpExtInst %void %1 DebugScope %49
1562 OpLine %7 9 3
1563 OpStore %82 %55
1564 OpLine %7 10 3
1565 OpStore %83 %56
1566 OpLine %7 11 10
1567 %90 = OpCompositeConstruct %VS_OUTPUT %55 %56
1568 OpNoLine
1569 %95 = OpExtInst %void %1 DebugNoScope
1570 %58 = OpCompositeExtract %v4float %90 0
1571 OpStore %gl_Position %58
1572 %59 = OpCompositeExtract %v4float %90 1
1573 OpStore %out_var_COLOR %59
1574 OpReturn
1575 OpFunctionEnd
1576 )";
1577
1578 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1579 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1580 }
1581
1582 TEST_F(LocalSingleStoreElimTest, DebugValuesForAllLocalsAndParams) {
1583 // Texture2D g_tColor;
1584 //
1585 // SamplerState g_sAniso;
1586 //
1587 // struct PS_INPUT
1588 // {
1589 // float2 vTextureCoords : TEXCOORD2 ;
1590 // } ;
1591 //
1592 // struct PS_OUTPUT
1593 // {
1594 // float4 vColor : SV_Target0 ;
1595 // } ;
1596 //
1597 // void do_sample ( in float2 tc, out float4 c ) {
1598 // c = g_tColor . Sample ( g_sAniso , tc ) ;
1599 // }
1600 //
1601 // PS_OUTPUT MainPs ( PS_INPUT i )
1602 // {
1603 // PS_OUTPUT ps_output ;
1604 // float4 color;
1605 //
1606 // do_sample ( i . vTextureCoords . xy , color ) ;
1607 // ps_output . vColor = color;
1608 // return ps_output ;
1609 // }
1610 const std::string text = R"(
1611 OpCapability Shader
1612 %1 = OpExtInstImport "OpenCL.DebugInfo.100"
1613 ;CHECK: [[set:%\w+]] = OpExtInstImport "OpenCL.DebugInfo.100"
1614 OpMemoryModel Logical GLSL450
1615 OpEntryPoint Fragment %MainPs "MainPs" %in_var_TEXCOORD2 %out_var_SV_Target0 %g_tColor %g_sAniso
1616 OpExecutionMode %MainPs OriginUpperLeft
1617 %7 = OpString "foo2.frag"
1618 %21 = OpString "float"
1619 %27 = OpString "PS_INPUT"
1620 %31 = OpString "vTextureCoords"
1621 %34 = OpString "PS_OUTPUT"
1622 %38 = OpString "vColor"
1623 %40 = OpString "do_sample"
1624 %41 = OpString ""
1625 %45 = OpString "c"
1626 %47 = OpString "tc"
1627 %50 = OpString "MainPs"
1628 %54 = OpString "color"
1629 %56 = OpString "ps_output"
1630 %59 = OpString "i"
1631 %62 = OpString "@type.sampler"
1632 %63 = OpString "type.sampler"
1633 %65 = OpString "g_sAniso"
1634 %67 = OpString "@type.2d.image"
1635 %68 = OpString "type.2d.image"
1636 %70 = OpString "TemplateParam"
1637 %73 = OpString "g_tColor"
1638 ;CHECK: [[str_c:%\w+]] = OpString "c"
1639 ;CHECK: [[str_tc:%\w+]] = OpString "tc"
1640 ;CHECK: [[str_color:%\w+]] = OpString "color"
1641 ;CHECK: [[str_ps_output:%\w+]] = OpString "ps_output"
1642 ;CHECK: [[str_i:%\w+]] = OpString "i"
1643 OpName %type_2d_image "type.2d.image"
1644 OpName %g_tColor "g_tColor"
1645 OpName %type_sampler "type.sampler"
1646 OpName %g_sAniso "g_sAniso"
1647 OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
1648 OpName %out_var_SV_Target0 "out.var.SV_Target0"
1649 OpName %MainPs "MainPs"
1650 OpName %PS_INPUT "PS_INPUT"
1651 OpMemberName %PS_INPUT 0 "vTextureCoords"
1652 OpName %PS_OUTPUT "PS_OUTPUT"
1653 OpMemberName %PS_OUTPUT 0 "vColor"
1654 OpName %type_sampled_image "type.sampled.image"
1655 OpDecorate %in_var_TEXCOORD2 Location 0
1656 OpDecorate %out_var_SV_Target0 Location 0
1657 OpDecorate %g_tColor DescriptorSet 0
1658 OpDecorate %g_tColor Binding 0
1659 OpDecorate %g_sAniso DescriptorSet 0
1660 OpDecorate %g_sAniso Binding 1
1661 %int = OpTypeInt 32 1
1662 %int_0 = OpConstant %int 0
1663 %uint = OpTypeInt 32 0
1664 %uint_32 = OpConstant %uint 32
1665 %float = OpTypeFloat 32
1666 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
1667 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
1668 %type_sampler = OpTypeSampler
1669 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
1670 %v2float = OpTypeVector %float 2
1671 %_ptr_Input_v2float = OpTypePointer Input %v2float
1672 %v4float = OpTypeVector %float 4
1673 %_ptr_Output_v4float = OpTypePointer Output %v4float
1674 %void = OpTypeVoid
1675 %uint_64 = OpConstant %uint 64
1676 %uint_0 = OpConstant %uint 0
1677 %uint_128 = OpConstant %uint 128
1678 %75 = OpTypeFunction %void
1679 %PS_INPUT = OpTypeStruct %v2float
1680 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
1681 %PS_OUTPUT = OpTypeStruct %v4float
1682 %85 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
1683 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
1684 %_ptr_Function_v4float = OpTypePointer Function %v4float
1685 %_ptr_Function_v2float = OpTypePointer Function %v2float
1686 %105 = OpTypeFunction %void %_ptr_Function_v2float %_ptr_Function_v4float
1687 %type_sampled_image = OpTypeSampledImage %type_2d_image
1688 %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
1689 %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
1690 %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
1691 %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
1692 %145 = OpExtInst %void %1 DebugOperation Deref
1693 %61 = OpExtInst %void %1 DebugInfoNone
1694 %58 = OpExtInst %void %1 DebugExpression
1695 %23 = OpExtInst %void %1 DebugTypeBasic %21 %uint_32 Float
1696 %24 = OpExtInst %void %1 DebugTypeVector %23 2
1697 %25 = OpExtInst %void %1 DebugSource %7
1698 %26 = OpExtInst %void %1 DebugCompilationUnit 1 4 %25 HLSL
1699 %29 = OpExtInst %void %1 DebugTypeComposite %27 Structure %25 5 8 %26 %27 %uint_64 FlagIsProtected|FlagIsPrivate %30
1700 %30 = OpExtInst %void %1 DebugTypeMember %31 %24 %25 7 12 %29 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
1701 %33 = OpExtInst %void %1 DebugTypeVector %23 4
1702 %36 = OpExtInst %void %1 DebugTypeComposite %34 Structure %25 10 8 %26 %34 %uint_128 FlagIsProtected|FlagIsPrivate %37
1703 %37 = OpExtInst %void %1 DebugTypeMember %38 %33 %25 12 12 %36 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
1704 %39 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %void %24 %33
1705 %42 = OpExtInst %void %1 DebugFunction %40 %39 %25 15 1 %26 %41 FlagIsProtected|FlagIsPrivate 15 %61
1706 %44 = OpExtInst %void %1 DebugLexicalBlock %25 15 47 %42
1707 %46 = OpExtInst %void %1 DebugLocalVariable %45 %33 %25 15 43 %42 FlagIsLocal 2
1708 %48 = OpExtInst %void %1 DebugLocalVariable %47 %24 %25 15 28 %42 FlagIsLocal 1
1709 %49 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %29
1710 %51 = OpExtInst %void %1 DebugFunction %50 %49 %25 19 1 %26 %41 FlagIsProtected|FlagIsPrivate 20 %61
1711 %53 = OpExtInst %void %1 DebugLexicalBlock %25 20 1 %51
1712 %55 = OpExtInst %void %1 DebugLocalVariable %54 %33 %25 22 12 %53 FlagIsLocal
1713 %57 = OpExtInst %void %1 DebugLocalVariable %56 %36 %25 21 15 %53 FlagIsLocal
1714 %60 = OpExtInst %void %1 DebugLocalVariable %59 %29 %25 19 29 %51 FlagIsLocal 1
1715 %64 = OpExtInst %void %1 DebugTypeComposite %62 Structure %25 0 0 %26 %63 %61 FlagIsProtected|FlagIsPrivate
1716 %66 = OpExtInst %void %1 DebugGlobalVariable %65 %64 %25 3 14 %26 %65 %g_sAniso FlagIsDefinition
1717 %69 = OpExtInst %void %1 DebugTypeComposite %67 Class %25 0 0 %26 %68 %61 FlagIsProtected|FlagIsPrivate
1718 %71 = OpExtInst %void %1 DebugTypeTemplateParameter %70 %33 %61 %25 0 0
1719 %72 = OpExtInst %void %1 DebugTypeTemplate %69 %71
1720 %74 = OpExtInst %void %1 DebugGlobalVariable %73 %72 %25 1 11 %26 %73 %g_tColor FlagIsDefinition
1721 %142 = OpExtInst %void %1 DebugInlinedAt 24 %53
1722 %144 = OpExtInst %void %1 DebugExpression %145
1723 %155 = OpExtInst %void %1 DebugExpression %145
1724 ;CHECK: [[var_c:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_c]]
1725 ;CHECK: [[var_tc:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_tc]]
1726 ;CHECK: [[var_color:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_color]]
1727 ;CHECK: [[var_ps_output:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_ps_output]]
1728 ;CHECK: [[var_i:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_i]]
1729 %MainPs = OpFunction %void None %75
1730 %76 = OpLabel
1731 %153 = OpVariable %_ptr_Function_v2float Function
1732 %149 = OpVariable %_ptr_Function_v4float Function
1733 %157 = OpExtInst %void %1 DebugScope %53
1734 %143 = OpVariable %_ptr_Function_v4float Function
1735 %121 = OpVariable %_ptr_Function_v4float Function
1736 %122 = OpVariable %_ptr_Function_v2float Function
1737 %158 = OpExtInst %void %1 DebugScope %51
1738 OpLine %7 19 29
1739 %156 = OpExtInst %void %1 DebugValue %60 %153 %155 %int_0
1740 %159 = OpExtInst %void %1 DebugScope %53
1741 OpLine %7 21 15
1742 %146 = OpExtInst %void %1 DebugValue %57 %143 %144 %int_0
1743 OpNoLine
1744 %160 = OpExtInst %void %1 DebugNoScope
1745 %80 = OpLoad %v2float %in_var_TEXCOORD2
1746 %81 = OpCompositeConstruct %PS_INPUT %80
1747 %154 = OpCompositeExtract %v2float %81 0
1748 OpStore %153 %154
1749 %161 = OpExtInst %void %1 DebugScope %53
1750 OpLine %7 22 12
1751 %127 = OpExtInst %void %1 DebugDeclare %55 %121 %58
1752 OpLine %7 24 17
1753 %129 = OpLoad %v2float %153
1754 OpStore %122 %129
1755 %162 = OpExtInst %void %1 DebugScope %42 %142
1756 OpLine %7 15 28
1757 %135 = OpExtInst %void %1 DebugDeclare %48 %122 %58
1758 OpLine %7 15 43
1759 %136 = OpExtInst %void %1 DebugDeclare %46 %121 %58
1760 %163 = OpExtInst %void %1 DebugScope %44 %142
1761 OpLine %7 16 9
1762 %137 = OpLoad %type_2d_image %g_tColor
1763 OpLine %7 16 29
1764 %138 = OpLoad %type_sampler %g_sAniso
1765 OpLine %7 16 40
1766 %139 = OpLoad %v2float %122
1767 OpLine %7 16 9
1768 %140 = OpSampledImage %type_sampled_image %137 %138
1769 %141 = OpImageSampleImplicitLod %v4float %140 %139 None
1770 OpLine %7 16 5
1771 OpStore %121 %141
1772 %164 = OpExtInst %void %1 DebugScope %53
1773 OpLine %7 25 26
1774 %131 = OpLoad %v4float %121
1775 OpLine %7 25 5
1776 OpStore %143 %131
1777 OpLine %7 26 12
1778 %147 = OpLoad %v4float %143
1779 %148 = OpCompositeConstruct %PS_OUTPUT %147
1780 OpLine %7 26 5
1781 %150 = OpCompositeExtract %v4float %148 0
1782 OpStore %149 %150
1783 OpNoLine
1784 %165 = OpExtInst %void %1 DebugNoScope
1785 %151 = OpLoad %v4float %149
1786 %152 = OpCompositeConstruct %PS_OUTPUT %151
1787 %84 = OpCompositeExtract %v4float %152 0
1788 OpStore %out_var_SV_Target0 %84
1789 OpLine %7 27 1
1790 OpReturn
1791 OpFunctionEnd
1792 ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_i]]
1793 ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_tc]]
1794 ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_c]]
1795 ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_color]]
1796 ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_ps_output]]
1797 )";
1798
1799 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1800 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1801 }
1802
1803 TEST_F(LocalSingleStoreElimTest, VkMemoryModelTest) {
1804 const std::string text =
1805 R"(
1806 ; CHECK: OpCapability Shader
1807 ; CHECK: OpCapability VulkanMemoryModel
1808 ; CHECK: OpExtension "SPV_KHR_vulkan_memory_model"
1809 OpCapability Shader
1810 OpCapability VulkanMemoryModel
1811 OpExtension "SPV_KHR_vulkan_memory_model"
1812 %1 = OpExtInstImport "GLSL.std.450"
1813 OpMemoryModel Logical Vulkan
1814 OpEntryPoint GLCompute %main "main"
1815 OpExecutionMode %main LocalSize 1 1 1
1816 OpSource GLSL 450
1817 %void = OpTypeVoid
1818 %3 = OpTypeFunction %void
1819 %int = OpTypeInt 32 1
1820 %_ptr_Function_int = OpTypePointer Function %int
1821 %int_0 = OpConstant %int 0
1822 %int_1 = OpConstant %int 1
1823 %bool = OpTypeBool
1824 %false = OpConstantFalse %bool
1825 ; CHECK: OpFunction
1826 ; CHECK-NEXT: OpLabel
1827 ; CHECK-NEXT: [[a:%\w+]] = OpVariable
1828 ; CHECK-NEXT: [[b:%\w+]] = OpVariable
1829 ; CHECK: OpStore [[a]] [[v:%\w+]]
1830 ; CHECK: OpStore [[b]]
1831 ; Make sure the load was removed.
1832 ; CHECK: OpLabel
1833 ; CHECK-NOT: OpLoad %int [[a]]
1834 ; CHECK: OpStore [[b]] [[v]]
1835 %main = OpFunction %void None %3
1836 %5 = OpLabel
1837 %a = OpVariable %_ptr_Function_int Function
1838 %b = OpVariable %_ptr_Function_int Function
1839 OpStore %a %int_0
1840 OpStore %b %int_1
1841 OpSelectionMerge %15 None
1842 OpBranchConditional %false %14 %15
1843 %14 = OpLabel
1844 %16 = OpLoad %int %a
1845 OpStore %b %16
1846 OpBranch %15
1847 %15 = OpLabel
1848 OpReturn
1849 OpFunctionEnd
1850 )";
1851
1852 SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
1853 }
1854
1855 // TODO(greg-lunarg): Add tests to verify handling of these cases:
1856 //
1857 // Other types
1858 // Others?
1859
1860 } // namespace
1861 } // namespace opt
1862 } // namespace spvtools