aboutsummaryrefslogtreecommitdiff
path: root/test/opt/process_lines_test.cpp
blob: 33ad4be8919c95daa7f9bbdb37d6dcf9a8617b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// Copyright (c) 2017 Valve Corporation
// Copyright (c) 2017 LunarG Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>
#include <string>
#include <vector>

#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"

namespace spvtools {
namespace opt {
namespace {

using ProcessLinesTest = PassTest<::testing::Test>;

TEST_F(ProcessLinesTest, SimplePropagation) {
  // Texture2D g_tColor[128];
  //
  // layout(push_constant) cbuffer PerViewConstantBuffer_t
  // {
  //   uint g_nDataIdx;
  //   uint g_nDataIdx2;
  //   bool g_B;
  // };
  //
  // SamplerState g_sAniso;
  //
  // struct PS_INPUT
  // {
  //   float2 vTextureCoords : TEXCOORD2;
  // };
  //
  // struct PS_OUTPUT
  // {
  //   float4 vColor : SV_Target0;
  // };
  //
  // PS_OUTPUT MainPs(PS_INPUT i)
  // {
  //   PS_OUTPUT ps_output;
  //
  //   uint u;
  //   if (g_B)
  //     u = g_nDataIdx;
  //   else
  //     u = g_nDataIdx2;
  //   ps_output.vColor = g_tColor[u].Sample(g_sAniso, i.vTextureCoords.xy);
  //   return ps_output;
  // }

  const std::string predefs =
      R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
OpExecutionMode %MainPs OriginUpperLeft
%5 = OpString "foo.frag"
OpSource HLSL 500
OpName %MainPs "MainPs"
OpName %PS_INPUT "PS_INPUT"
OpMemberName %PS_INPUT 0 "vTextureCoords"
OpName %PS_OUTPUT "PS_OUTPUT"
OpMemberName %PS_OUTPUT 0 "vColor"
OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
OpName %i "i"
OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
OpMemberName %PerViewConstantBuffer_t 1 "g_nDataIdx2"
OpMemberName %PerViewConstantBuffer_t 2 "g_B"
OpName %_ ""
OpName %u "u"
OpName %ps_output "ps_output"
OpName %g_tColor "g_tColor"
OpName %g_sAniso "g_sAniso"
OpName %i_0 "i"
OpName %i_vTextureCoords "i.vTextureCoords"
OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
OpName %param "param"
OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
OpMemberDecorate %PerViewConstantBuffer_t 2 Offset 8
OpDecorate %PerViewConstantBuffer_t Block
OpDecorate %g_tColor DescriptorSet 0
OpDecorate %g_sAniso DescriptorSet 0
OpDecorate %i_vTextureCoords Location 0
OpDecorate %_entryPointOutput_vColor Location 0
)";

  const std::string before =
      R"(%void = OpTypeVoid
%19 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%PS_INPUT = OpTypeStruct %v2float
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
%v4float = OpTypeVector %float 4
%PS_OUTPUT = OpTypeStruct %v4float
%24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
%uint = OpTypeInt 32 0
%PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
%_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
%_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
%int = OpTypeInt 32 1
%int_2 = OpConstant %int 2
%_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
%bool = OpTypeBool
%uint_0 = OpConstant %uint 0
%_ptr_Function_uint = OpTypePointer Function %uint
%int_0 = OpConstant %int 0
%int_1 = OpConstant %int 1
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
%36 = OpTypeImage %float 2D 0 0 0 1 Unknown
%uint_128 = OpConstant %uint 128
%_arr_36_uint_128 = OpTypeArray %36 %uint_128
%_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
%g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
%_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
%41 = OpTypeSampler
%_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
%g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
%43 = OpTypeSampledImage %36
%_ptr_Function_v2float = OpTypePointer Function %v2float
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
%MainPs = OpFunction %void None %19
%48 = OpLabel
%i_0 = OpVariable %_ptr_Function_PS_INPUT Function
%param = OpVariable %_ptr_Function_PS_INPUT Function
OpLine %5 23 0
%49 = OpLoad %v2float %i_vTextureCoords
%50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
OpStore %50 %49
%51 = OpLoad %PS_INPUT %i_0
OpStore %param %51
%52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
%53 = OpCompositeExtract %v4float %52 0
OpStore %_entryPointOutput_vColor %53
OpReturn
OpFunctionEnd
%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
%i = OpFunctionParameter %_ptr_Function_PS_INPUT
%54 = OpLabel
%u = OpVariable %_ptr_Function_uint Function
%ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
OpLine %5 27 0
%55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
%56 = OpLoad %uint %55
%57 = OpINotEqual %bool %56 %uint_0
OpSelectionMerge %58 None
OpBranchConditional %57 %59 %60
%59 = OpLabel
OpLine %5 28 0
%61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
%62 = OpLoad %uint %61
OpStore %u %62
OpBranch %58
%60 = OpLabel
OpLine %5 30 0
%63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
%64 = OpLoad %uint %63
OpStore %u %64
OpBranch %58
%58 = OpLabel
OpLine %5 31 0
%65 = OpLoad %uint %u
%66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
%67 = OpLoad %36 %66
%68 = OpLoad %41 %g_sAniso
%69 = OpSampledImage %43 %67 %68
%70 = OpAccessChain %_ptr_Function_v2float %i %int_0
%71 = OpLoad %v2float %70
%72 = OpImageSampleImplicitLod %v4float %69 %71
%73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
OpStore %73 %72
OpLine %5 32 0
%74 = OpLoad %PS_OUTPUT %ps_output
OpReturnValue %74
OpFunctionEnd
)";

  const std::string after =
      R"(OpNoLine
%void = OpTypeVoid
OpNoLine
%19 = OpTypeFunction %void
OpNoLine
%float = OpTypeFloat 32
OpNoLine
%v2float = OpTypeVector %float 2
OpNoLine
%PS_INPUT = OpTypeStruct %v2float
OpNoLine
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
OpNoLine
%v4float = OpTypeVector %float 4
OpNoLine
%PS_OUTPUT = OpTypeStruct %v4float
OpNoLine
%24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
OpNoLine
%uint = OpTypeInt 32 0
OpNoLine
%PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
OpNoLine
%_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
OpNoLine
%_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
OpNoLine
%int = OpTypeInt 32 1
OpNoLine
%int_2 = OpConstant %int 2
OpNoLine
%_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
OpNoLine
%bool = OpTypeBool
OpNoLine
%uint_0 = OpConstant %uint 0
OpNoLine
%_ptr_Function_uint = OpTypePointer Function %uint
OpNoLine
%int_0 = OpConstant %int 0
OpNoLine
%int_1 = OpConstant %int 1
OpNoLine
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
OpNoLine
%36 = OpTypeImage %float 2D 0 0 0 1 Unknown
OpNoLine
%uint_128 = OpConstant %uint 128
OpNoLine
%_arr_36_uint_128 = OpTypeArray %36 %uint_128
OpNoLine
%_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
OpNoLine
%g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
OpNoLine
%_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
OpNoLine
%41 = OpTypeSampler
OpNoLine
%_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
OpNoLine
%g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
OpNoLine
%43 = OpTypeSampledImage %36
OpNoLine
%_ptr_Function_v2float = OpTypePointer Function %v2float
OpNoLine
%_ptr_Function_v4float = OpTypePointer Function %v4float
OpNoLine
%_ptr_Input_v2float = OpTypePointer Input %v2float
OpNoLine
%i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
OpNoLine
%_ptr_Output_v4float = OpTypePointer Output %v4float
OpNoLine
%_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
OpNoLine
%MainPs = OpFunction %void None %19
OpNoLine
%48 = OpLabel
OpNoLine
%i_0 = OpVariable %_ptr_Function_PS_INPUT Function
OpNoLine
%param = OpVariable %_ptr_Function_PS_INPUT Function
OpLine %5 23 0
%49 = OpLoad %v2float %i_vTextureCoords
OpLine %5 23 0
%50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
OpLine %5 23 0
OpStore %50 %49
OpLine %5 23 0
%51 = OpLoad %PS_INPUT %i_0
OpLine %5 23 0
OpStore %param %51
OpLine %5 23 0
%52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
OpLine %5 23 0
%53 = OpCompositeExtract %v4float %52 0
OpLine %5 23 0
OpStore %_entryPointOutput_vColor %53
OpLine %5 23 0
OpReturn
OpNoLine
OpFunctionEnd
OpNoLine
%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
OpNoLine
%i = OpFunctionParameter %_ptr_Function_PS_INPUT
OpNoLine
%54 = OpLabel
OpNoLine
%u = OpVariable %_ptr_Function_uint Function
OpNoLine
%ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
OpLine %5 27 0
%55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
OpLine %5 27 0
%56 = OpLoad %uint %55
OpLine %5 27 0
%57 = OpINotEqual %bool %56 %uint_0
OpLine %5 27 0
OpSelectionMerge %58 None
OpBranchConditional %57 %59 %60
OpNoLine
%59 = OpLabel
OpLine %5 28 0
%61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
OpLine %5 28 0
%62 = OpLoad %uint %61
OpLine %5 28 0
OpStore %u %62
OpLine %5 28 0
OpBranch %58
OpNoLine
%60 = OpLabel
OpLine %5 30 0
%63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
OpLine %5 30 0
%64 = OpLoad %uint %63
OpLine %5 30 0
OpStore %u %64
OpLine %5 30 0
OpBranch %58
OpNoLine
%58 = OpLabel
OpLine %5 31 0
%65 = OpLoad %uint %u
OpLine %5 31 0
%66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
OpLine %5 31 0
%67 = OpLoad %36 %66
OpLine %5 31 0
%68 = OpLoad %41 %g_sAniso
OpLine %5 31 0
%69 = OpSampledImage %43 %67 %68
OpLine %5 31 0
%70 = OpAccessChain %_ptr_Function_v2float %i %int_0
OpLine %5 31 0
%71 = OpLoad %v2float %70
OpLine %5 31 0
%72 = OpImageSampleImplicitLod %v4float %69 %71
OpLine %5 31 0
%73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
OpLine %5 31 0
OpStore %73 %72
OpLine %5 32 0
%74 = OpLoad %PS_OUTPUT %ps_output
OpLine %5 32 0
OpReturnValue %74
OpNoLine
OpFunctionEnd
)";

  SinglePassRunAndCheck<ProcessLinesPass>(predefs + before, predefs + after,
                                          false, true, kLinesPropagateLines);
}

TEST_F(ProcessLinesTest, SimpleElimination) {
  // Previous test with before and after reversed

  const std::string predefs =
      R"(OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
OpExecutionMode %MainPs OriginUpperLeft
%5 = OpString "foo.frag"
OpSource HLSL 500
OpName %MainPs "MainPs"
OpName %PS_INPUT "PS_INPUT"
OpMemberName %PS_INPUT 0 "vTextureCoords"
OpName %PS_OUTPUT "PS_OUTPUT"
OpMemberName %PS_OUTPUT 0 "vColor"
OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
OpName %i "i"
OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
OpMemberName %PerViewConstantBuffer_t 1 "g_nDataIdx2"
OpMemberName %PerViewConstantBuffer_t 2 "g_B"
OpName %_ ""
OpName %u "u"
OpName %ps_output "ps_output"
OpName %g_tColor "g_tColor"
OpName %g_sAniso "g_sAniso"
OpName %i_0 "i"
OpName %i_vTextureCoords "i.vTextureCoords"
OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
OpName %param "param"
OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
OpMemberDecorate %PerViewConstantBuffer_t 2 Offset 8
OpDecorate %PerViewConstantBuffer_t Block
OpDecorate %g_tColor DescriptorSet 0
OpDecorate %g_sAniso DescriptorSet 0
OpDecorate %i_vTextureCoords Location 0
OpDecorate %_entryPointOutput_vColor Location 0
)";

  const std::string before =
      R"(OpNoLine
%void = OpTypeVoid
OpNoLine
%19 = OpTypeFunction %void
OpNoLine
%float = OpTypeFloat 32
OpNoLine
%v2float = OpTypeVector %float 2
OpNoLine
%PS_INPUT = OpTypeStruct %v2float
OpNoLine
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
OpNoLine
%v4float = OpTypeVector %float 4
OpNoLine
%PS_OUTPUT = OpTypeStruct %v4float
OpNoLine
%24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
OpNoLine
%uint = OpTypeInt 32 0
OpNoLine
%PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
OpNoLine
%_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
OpNoLine
%_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
OpNoLine
%int = OpTypeInt 32 1
OpNoLine
%int_2 = OpConstant %int 2
OpNoLine
%_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
OpNoLine
%bool = OpTypeBool
OpNoLine
%uint_0 = OpConstant %uint 0
OpNoLine
%_ptr_Function_uint = OpTypePointer Function %uint
OpNoLine
%int_0 = OpConstant %int 0
OpNoLine
%int_1 = OpConstant %int 1
OpNoLine
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
OpNoLine
%36 = OpTypeImage %float 2D 0 0 0 1 Unknown
OpNoLine
%uint_128 = OpConstant %uint 128
OpNoLine
%_arr_36_uint_128 = OpTypeArray %36 %uint_128
OpNoLine
%_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
OpNoLine
%g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
OpNoLine
%_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
OpNoLine
%41 = OpTypeSampler
OpNoLine
%_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
OpNoLine
%g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
OpNoLine
%43 = OpTypeSampledImage %36
OpNoLine
%_ptr_Function_v2float = OpTypePointer Function %v2float
OpNoLine
%_ptr_Function_v4float = OpTypePointer Function %v4float
OpNoLine
%_ptr_Input_v2float = OpTypePointer Input %v2float
OpNoLine
%i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
OpNoLine
%_ptr_Output_v4float = OpTypePointer Output %v4float
OpNoLine
%_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
OpNoLine
%MainPs = OpFunction %void None %19
OpNoLine
%48 = OpLabel
OpNoLine
%i_0 = OpVariable %_ptr_Function_PS_INPUT Function
OpNoLine
%param = OpVariable %_ptr_Function_PS_INPUT Function
OpLine %5 23 0
%49 = OpLoad %v2float %i_vTextureCoords
OpLine %5 23 0
%50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
OpLine %5 23 0
OpStore %50 %49
OpLine %5 23 0
%51 = OpLoad %PS_INPUT %i_0
OpLine %5 23 0
OpStore %param %51
OpLine %5 23 0
%52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
OpLine %5 23 0
%53 = OpCompositeExtract %v4float %52 0
OpLine %5 23 0
OpStore %_entryPointOutput_vColor %53
OpLine %5 23 0
OpReturn
OpNoLine
OpFunctionEnd
OpNoLine
%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
OpNoLine
%i = OpFunctionParameter %_ptr_Function_PS_INPUT
OpNoLine
%54 = OpLabel
OpNoLine
%u = OpVariable %_ptr_Function_uint Function
OpNoLine
%ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
OpLine %5 27 0
%55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
OpLine %5 27 0
%56 = OpLoad %uint %55
OpLine %5 27 0
%57 = OpINotEqual %bool %56 %uint_0
OpLine %5 27 0
OpSelectionMerge %58 None
OpBranchConditional %57 %59 %60
OpNoLine
%59 = OpLabel
OpLine %5 28 0
%61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
OpLine %5 28 0
%62 = OpLoad %uint %61
OpLine %5 28 0
OpStore %u %62
OpLine %5 28 0
OpBranch %58
OpNoLine
%60 = OpLabel
OpLine %5 30 0
%63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
OpLine %5 30 0
%64 = OpLoad %uint %63
OpLine %5 30 0
OpStore %u %64
OpLine %5 30 0
OpBranch %58
OpNoLine
%58 = OpLabel
OpLine %5 31 0
%65 = OpLoad %uint %u
OpLine %5 31 0
%66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
OpLine %5 31 0
%67 = OpLoad %36 %66
OpLine %5 31 0
%68 = OpLoad %41 %g_sAniso
OpLine %5 31 0
%69 = OpSampledImage %43 %67 %68
OpLine %5 31 0
%70 = OpAccessChain %_ptr_Function_v2float %i %int_0
OpLine %5 31 0
%71 = OpLoad %v2float %70
OpLine %5 31 0
%72 = OpImageSampleImplicitLod %v4float %69 %71
OpLine %5 31 0
%73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
OpLine %5 31 0
OpStore %73 %72
OpLine %5 32 0
%74 = OpLoad %PS_OUTPUT %ps_output
OpLine %5 32 0
OpReturnValue %74
OpNoLine
OpFunctionEnd
)";

  const std::string after =
      R"(%void = OpTypeVoid
%19 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%PS_INPUT = OpTypeStruct %v2float
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
%v4float = OpTypeVector %float 4
%PS_OUTPUT = OpTypeStruct %v4float
%24 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
%uint = OpTypeInt 32 0
%PerViewConstantBuffer_t = OpTypeStruct %uint %uint %uint
%_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
%_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
%int = OpTypeInt 32 1
%int_2 = OpConstant %int 2
%_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
%bool = OpTypeBool
%uint_0 = OpConstant %uint 0
%_ptr_Function_uint = OpTypePointer Function %uint
%int_0 = OpConstant %int 0
%int_1 = OpConstant %int 1
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
%36 = OpTypeImage %float 2D 0 0 0 1 Unknown
%uint_128 = OpConstant %uint 128
%_arr_36_uint_128 = OpTypeArray %36 %uint_128
%_ptr_UniformConstant__arr_36_uint_128 = OpTypePointer UniformConstant %_arr_36_uint_128
%g_tColor = OpVariable %_ptr_UniformConstant__arr_36_uint_128 UniformConstant
%_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
%41 = OpTypeSampler
%_ptr_UniformConstant_41 = OpTypePointer UniformConstant %41
%g_sAniso = OpVariable %_ptr_UniformConstant_41 UniformConstant
%43 = OpTypeSampledImage %36
%_ptr_Function_v2float = OpTypePointer Function %v2float
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
%MainPs = OpFunction %void None %19
%48 = OpLabel
%i_0 = OpVariable %_ptr_Function_PS_INPUT Function
%param = OpVariable %_ptr_Function_PS_INPUT Function
OpLine %5 23 0
%49 = OpLoad %v2float %i_vTextureCoords
%50 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
OpStore %50 %49
%51 = OpLoad %PS_INPUT %i_0
OpStore %param %51
%52 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
%53 = OpCompositeExtract %v4float %52 0
OpStore %_entryPointOutput_vColor %53
OpReturn
OpFunctionEnd
%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %24
%i = OpFunctionParameter %_ptr_Function_PS_INPUT
%54 = OpLabel
%u = OpVariable %_ptr_Function_uint Function
%ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
OpLine %5 27 0
%55 = OpAccessChain %_ptr_PushConstant_uint %_ %int_2
%56 = OpLoad %uint %55
%57 = OpINotEqual %bool %56 %uint_0
OpSelectionMerge %58 None
OpBranchConditional %57 %59 %60
%59 = OpLabel
OpLine %5 28 0
%61 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
%62 = OpLoad %uint %61
OpStore %u %62
OpBranch %58
%60 = OpLabel
OpLine %5 30 0
%63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
%64 = OpLoad %uint %63
OpStore %u %64
OpBranch %58
%58 = OpLabel
OpLine %5 31 0
%65 = OpLoad %uint %u
%66 = OpAccessChain %_ptr_UniformConstant_36 %g_tColor %65
%67 = OpLoad %36 %66
%68 = OpLoad %41 %g_sAniso
%69 = OpSampledImage %43 %67 %68
%70 = OpAccessChain %_ptr_Function_v2float %i %int_0
%71 = OpLoad %v2float %70
%72 = OpImageSampleImplicitLod %v4float %69 %71
%73 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
OpStore %73 %72
OpLine %5 32 0
%74 = OpLoad %PS_OUTPUT %ps_output
OpReturnValue %74
OpFunctionEnd
)";

  SinglePassRunAndCheck<ProcessLinesPass>(
      predefs + before, predefs + after, false, true, kLinesEliminateDeadLines);
}

// TODO(greg-lunarg): Add tests to verify handling of these cases:
//
//    TODO(greg-lunarg): Think about other tests :)

}  // namespace
}  // namespace opt
}  // namespace spvtools