Debian Codegraph @

test/opt/pass_merge_return_test.cpp

raw
1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16
17 #include "spirv-tools/libspirv.hpp"
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 MergeReturnPassTest = PassTest<::testing::Test>;
26
27 TEST_F(MergeReturnPassTest, OneReturn) {
28 const std::string before =
29 R"(OpCapability Addresses
30 OpCapability Kernel
31 OpCapability GenericPointer
32 OpCapability Linkage
33 OpMemoryModel Physical32 OpenCL
34 OpEntryPoint Kernel %1 "simple_kernel"
35 %2 = OpTypeVoid
36 %3 = OpTypeFunction %2
37 %1 = OpFunction %2 None %3
38 %4 = OpLabel
39 OpReturn
40 OpFunctionEnd
41 )";
42
43 const std::string after = before;
44
45 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
46 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
47 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
48 }
49
50 TEST_F(MergeReturnPassTest, TwoReturnsNoValue) {
51 const std::string before =
52 R"(OpCapability Addresses
53 OpCapability Kernel
54 OpCapability GenericPointer
55 OpCapability Linkage
56 OpMemoryModel Physical32 OpenCL
57 OpEntryPoint Kernel %6 "simple_kernel"
58 %2 = OpTypeVoid
59 %3 = OpTypeBool
60 %4 = OpConstantFalse %3
61 %1 = OpTypeFunction %2
62 %6 = OpFunction %2 None %1
63 %7 = OpLabel
64 OpBranchConditional %4 %8 %9
65 %8 = OpLabel
66 OpReturn
67 %9 = OpLabel
68 OpReturn
69 OpFunctionEnd
70 )";
71
72 const std::string after =
73 R"(OpCapability Addresses
74 OpCapability Kernel
75 OpCapability GenericPointer
76 OpCapability Linkage
77 OpMemoryModel Physical32 OpenCL
78 OpEntryPoint Kernel %6 "simple_kernel"
79 %2 = OpTypeVoid
80 %3 = OpTypeBool
81 %4 = OpConstantFalse %3
82 %1 = OpTypeFunction %2
83 %6 = OpFunction %2 None %1
84 %7 = OpLabel
85 OpBranchConditional %4 %8 %9
86 %8 = OpLabel
87 OpBranch %10
88 %9 = OpLabel
89 OpBranch %10
90 %10 = OpLabel
91 OpReturn
92 OpFunctionEnd
93 )";
94
95 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
96 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
97 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
98 }
99
100 TEST_F(MergeReturnPassTest, DebugTwoReturnsNoValue) {
101 const std::string before =
102 R"(OpCapability Addresses
103 OpCapability Kernel
104 OpCapability GenericPointer
105 OpCapability Linkage
106 %10 = OpExtInstImport "OpenCL.DebugInfo.100"
107 OpMemoryModel Physical32 OpenCL
108 OpEntryPoint Kernel %6 "simple_kernel"
109 %11 = OpString "test"
110 %2 = OpTypeVoid
111 %3 = OpTypeBool
112 %4 = OpConstantFalse %3
113 %1 = OpTypeFunction %2
114 %12 = OpExtInst %2 %10 DebugSource %11
115 %13 = OpExtInst %2 %10 DebugCompilationUnit 1 4 %12 HLSL
116 %14 = OpExtInst %2 %10 DebugTypeFunction FlagIsProtected|FlagIsPrivate %2
117 %15 = OpExtInst %2 %10 DebugFunction %11 %14 %12 0 0 %13 %11 FlagIsProtected|FlagIsPrivate 0 %6
118 %6 = OpFunction %2 None %1
119 %7 = OpLabel
120 OpBranchConditional %4 %8 %9
121 %8 = OpLabel
122 %16 = OpExtInst %2 %10 DebugScope %15
123 OpLine %11 100 0
124 OpReturn
125 %9 = OpLabel
126 %17 = OpExtInst %2 %10 DebugScope %13
127 OpLine %11 200 0
128 OpReturn
129 OpFunctionEnd
130 )";
131
132 const std::string after =
133 R"(OpCapability Addresses
134 OpCapability Kernel
135 OpCapability GenericPointer
136 OpCapability Linkage
137 %10 = OpExtInstImport "OpenCL.DebugInfo.100"
138 OpMemoryModel Physical32 OpenCL
139 OpEntryPoint Kernel %6 "simple_kernel"
140 %11 = OpString "test"
141 %2 = OpTypeVoid
142 %3 = OpTypeBool
143 %4 = OpConstantFalse %3
144 %1 = OpTypeFunction %2
145 %12 = OpExtInst %2 %10 DebugSource %11
146 %13 = OpExtInst %2 %10 DebugCompilationUnit 1 4 %12 HLSL
147 %14 = OpExtInst %2 %10 DebugTypeFunction FlagIsProtected|FlagIsPrivate %2
148 %15 = OpExtInst %2 %10 DebugFunction %11 %14 %12 0 0 %13 %11 FlagIsProtected|FlagIsPrivate 0 %6
149 %6 = OpFunction %2 None %1
150 %7 = OpLabel
151 OpBranchConditional %4 %8 %9
152 %8 = OpLabel
153 %19 = OpExtInst %2 %10 DebugScope %15
154 OpLine %11 100 0
155 OpBranch %18
156 %20 = OpExtInst %2 %10 DebugNoScope
157 %9 = OpLabel
158 %21 = OpExtInst %2 %10 DebugScope %13
159 OpLine %11 200 0
160 OpBranch %18
161 %22 = OpExtInst %2 %10 DebugNoScope
162 %18 = OpLabel
163 OpReturn
164 OpFunctionEnd
165 )";
166
167 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
168 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
169 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
170 }
171
172 TEST_F(MergeReturnPassTest, TwoReturnsWithValues) {
173 const std::string before =
174 R"(OpCapability Linkage
175 OpCapability Kernel
176 OpMemoryModel Logical OpenCL
177 OpDecorate %7 LinkageAttributes "simple_kernel" Export
178 %1 = OpTypeInt 32 0
179 %2 = OpTypeBool
180 %3 = OpConstantFalse %2
181 %4 = OpConstant %1 0
182 %5 = OpConstant %1 1
183 %6 = OpTypeFunction %1
184 %7 = OpFunction %1 None %6
185 %8 = OpLabel
186 OpBranchConditional %3 %9 %10
187 %9 = OpLabel
188 OpReturnValue %4
189 %10 = OpLabel
190 OpReturnValue %5
191 OpFunctionEnd
192 )";
193
194 const std::string after =
195 R"(OpCapability Linkage
196 OpCapability Kernel
197 OpMemoryModel Logical OpenCL
198 OpDecorate %7 LinkageAttributes "simple_kernel" Export
199 %1 = OpTypeInt 32 0
200 %2 = OpTypeBool
201 %3 = OpConstantFalse %2
202 %4 = OpConstant %1 0
203 %5 = OpConstant %1 1
204 %6 = OpTypeFunction %1
205 %7 = OpFunction %1 None %6
206 %8 = OpLabel
207 OpBranchConditional %3 %9 %10
208 %9 = OpLabel
209 OpBranch %11
210 %10 = OpLabel
211 OpBranch %11
212 %11 = OpLabel
213 %12 = OpPhi %1 %4 %9 %5 %10
214 OpReturnValue %12
215 OpFunctionEnd
216 )";
217
218 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
219 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
220 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
221 }
222
223 TEST_F(MergeReturnPassTest, UnreachableReturnsNoValue) {
224 const std::string before =
225 R"(OpCapability Addresses
226 OpCapability Kernel
227 OpCapability GenericPointer
228 OpCapability Linkage
229 OpMemoryModel Physical32 OpenCL
230 OpEntryPoint Kernel %6 "simple_kernel"
231 %2 = OpTypeVoid
232 %3 = OpTypeBool
233 %4 = OpConstantFalse %3
234 %1 = OpTypeFunction %2
235 %6 = OpFunction %2 None %1
236 %7 = OpLabel
237 OpReturn
238 %8 = OpLabel
239 OpBranchConditional %4 %9 %10
240 %9 = OpLabel
241 OpReturn
242 %10 = OpLabel
243 OpReturn
244 OpFunctionEnd
245 )";
246
247 const std::string after =
248 R"(OpCapability Addresses
249 OpCapability Kernel
250 OpCapability GenericPointer
251 OpCapability Linkage
252 OpMemoryModel Physical32 OpenCL
253 OpEntryPoint Kernel %6 "simple_kernel"
254 %2 = OpTypeVoid
255 %3 = OpTypeBool
256 %4 = OpConstantFalse %3
257 %1 = OpTypeFunction %2
258 %6 = OpFunction %2 None %1
259 %7 = OpLabel
260 OpBranch %11
261 %8 = OpLabel
262 OpBranchConditional %4 %9 %10
263 %9 = OpLabel
264 OpBranch %11
265 %10 = OpLabel
266 OpBranch %11
267 %11 = OpLabel
268 OpReturn
269 OpFunctionEnd
270 )";
271
272 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
273 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
274 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
275 }
276
277 TEST_F(MergeReturnPassTest, UnreachableReturnsWithValues) {
278 const std::string before =
279 R"(OpCapability Linkage
280 OpCapability Kernel
281 OpMemoryModel Logical OpenCL
282 OpDecorate %7 LinkageAttributes "simple_kernel" Export
283 %1 = OpTypeInt 32 0
284 %2 = OpTypeBool
285 %3 = OpConstantFalse %2
286 %4 = OpConstant %1 0
287 %5 = OpConstant %1 1
288 %6 = OpTypeFunction %1
289 %7 = OpFunction %1 None %6
290 %8 = OpLabel
291 %9 = OpIAdd %1 %4 %5
292 OpReturnValue %9
293 %10 = OpLabel
294 OpBranchConditional %3 %11 %12
295 %11 = OpLabel
296 OpReturnValue %4
297 %12 = OpLabel
298 OpReturnValue %5
299 OpFunctionEnd
300 )";
301
302 const std::string after =
303 R"(OpCapability Linkage
304 OpCapability Kernel
305 OpMemoryModel Logical OpenCL
306 OpDecorate %7 LinkageAttributes "simple_kernel" Export
307 %1 = OpTypeInt 32 0
308 %2 = OpTypeBool
309 %3 = OpConstantFalse %2
310 %4 = OpConstant %1 0
311 %5 = OpConstant %1 1
312 %6 = OpTypeFunction %1
313 %7 = OpFunction %1 None %6
314 %8 = OpLabel
315 %9 = OpIAdd %1 %4 %5
316 OpBranch %13
317 %10 = OpLabel
318 OpBranchConditional %3 %11 %12
319 %11 = OpLabel
320 OpBranch %13
321 %12 = OpLabel
322 OpBranch %13
323 %13 = OpLabel
324 %14 = OpPhi %1 %9 %8 %4 %11 %5 %12
325 OpReturnValue %14
326 OpFunctionEnd
327 )";
328
329 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
330 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
331 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
332 }
333
334 TEST_F(MergeReturnPassTest, DebugUnreachableReturnsWithValues) {
335 const std::string before =
336 R"(OpCapability Linkage
337 OpCapability Kernel
338 %13 = OpExtInstImport "OpenCL.DebugInfo.100"
339 OpMemoryModel Logical OpenCL
340 %14 = OpString "test"
341 OpDecorate %7 LinkageAttributes "simple_kernel" Export
342 %1 = OpTypeInt 32 0
343 %20 = OpTypeVoid
344 %2 = OpTypeBool
345 %3 = OpConstantFalse %2
346 %4 = OpConstant %1 0
347 %5 = OpConstant %1 1
348 %6 = OpTypeFunction %1
349 %15 = OpExtInst %20 %13 DebugSource %14
350 %16 = OpExtInst %20 %13 DebugCompilationUnit 1 4 %15 HLSL
351 %17 = OpExtInst %20 %13 DebugTypeFunction FlagIsProtected|FlagIsPrivate %20
352 %18 = OpExtInst %20 %13 DebugFunction %14 %17 %15 0 0 %16 %14 FlagIsProtected|FlagIsPrivate 0 %7
353 %7 = OpFunction %1 None %6
354 %8 = OpLabel
355 %9 = OpIAdd %1 %4 %5
356 %19 = OpExtInst %20 %13 DebugScope %18
357 OpLine %14 100 0
358 OpReturnValue %9
359 %10 = OpLabel
360 OpBranchConditional %3 %11 %12
361 %11 = OpLabel
362 %21 = OpExtInst %20 %13 DebugScope %16
363 OpLine %14 200 0
364 OpReturnValue %4
365 %12 = OpLabel
366 %22 = OpExtInst %20 %13 DebugScope %18
367 OpLine %14 300 0
368 OpReturnValue %5
369 OpFunctionEnd
370 )";
371
372 const std::string after =
373 R"(OpCapability Linkage
374 OpCapability Kernel
375 %13 = OpExtInstImport "OpenCL.DebugInfo.100"
376 OpMemoryModel Logical OpenCL
377 %14 = OpString "test"
378 OpDecorate %7 LinkageAttributes "simple_kernel" Export
379 %1 = OpTypeInt 32 0
380 %20 = OpTypeVoid
381 %2 = OpTypeBool
382 %3 = OpConstantFalse %2
383 %4 = OpConstant %1 0
384 %5 = OpConstant %1 1
385 %6 = OpTypeFunction %1
386 %15 = OpExtInst %20 %13 DebugSource %14
387 %16 = OpExtInst %20 %13 DebugCompilationUnit 1 4 %15 HLSL
388 %17 = OpExtInst %20 %13 DebugTypeFunction FlagIsProtected|FlagIsPrivate %20
389 %18 = OpExtInst %20 %13 DebugFunction %14 %17 %15 0 0 %16 %14 FlagIsProtected|FlagIsPrivate 0 %7
390 %7 = OpFunction %1 None %6
391 %8 = OpLabel
392 %9 = OpIAdd %1 %4 %5
393 %25 = OpExtInst %20 %13 DebugScope %18
394 OpLine %14 100 0
395 OpBranch %23
396 %26 = OpExtInst %20 %13 DebugNoScope
397 %10 = OpLabel
398 OpBranchConditional %3 %11 %12
399 %11 = OpLabel
400 %27 = OpExtInst %20 %13 DebugScope %16
401 OpLine %14 200 0
402 OpBranch %23
403 %28 = OpExtInst %20 %13 DebugNoScope
404 %12 = OpLabel
405 %29 = OpExtInst %20 %13 DebugScope %18
406 OpLine %14 300 0
407 OpBranch %23
408 %30 = OpExtInst %20 %13 DebugNoScope
409 %23 = OpLabel
410 %24 = OpPhi %1 %9 %8 %4 %11 %5 %12
411 OpReturnValue %24
412 OpFunctionEnd
413 )";
414
415 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
416 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
417 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
418 }
419
420 TEST_F(MergeReturnPassTest, StructuredControlFlowWithUnreachableMerge) {
421 const std::string before =
422 R"(
423 ; CHECK: [[false:%\w+]] = OpConstantFalse
424 ; CHECK: [[true:%\w+]] = OpConstantTrue
425 ; CHECK: OpFunction
426 ; CHECK: [[var:%\w+]] = OpVariable [[:%\w+]] Function [[false]]
427 ; CHECK: OpSelectionMerge [[return_block:%\w+]]
428 ; CHECK: OpSelectionMerge [[merge_lab:%\w+]]
429 ; CHECK: OpBranchConditional [[cond:%\w+]] [[if_lab:%\w+]] [[then_lab:%\w+]]
430 ; CHECK: [[if_lab]] = OpLabel
431 ; CHECK-NEXT: OpStore [[var]] [[true]]
432 ; CHECK-NEXT: OpBranch [[return_block]]
433 ; CHECK: [[then_lab]] = OpLabel
434 ; CHECK-NEXT: OpStore [[var]] [[true]]
435 ; CHECK-NEXT: OpBranch [[return_block]]
436 ; CHECK: [[merge_lab]] = OpLabel
437 ; CHECK-NEXT: OpBranch [[return_block]]
438 ; CHECK: [[return_block]] = OpLabel
439 ; CHECK-NEXT: OpReturn
440 OpCapability Addresses
441 OpCapability Shader
442 OpCapability Linkage
443 OpMemoryModel Logical GLSL450
444 OpEntryPoint GLCompute %6 "simple_shader"
445 %2 = OpTypeVoid
446 %3 = OpTypeBool
447 %4 = OpConstantFalse %3
448 %1 = OpTypeFunction %2
449 %6 = OpFunction %2 None %1
450 %7 = OpLabel
451 OpSelectionMerge %10 None
452 OpBranchConditional %4 %8 %9
453 %8 = OpLabel
454 OpReturn
455 %9 = OpLabel
456 OpReturn
457 %10 = OpLabel
458 OpUnreachable
459 OpFunctionEnd
460 )";
461
462 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
463 SinglePassRunAndMatch<MergeReturnPass>(before, false);
464 }
465
466 TEST_F(MergeReturnPassTest, DebugStructuredControlFlowWithUnreachableMerge) {
467 const std::string before =
468 R"(
469 ; CHECK: [[false:%\w+]] = OpConstantFalse
470 ; CHECK: [[true:%\w+]] = OpConstantTrue
471 ; CHECK: OpFunction
472 ; CHECK: [[var:%\w+]] = OpVariable [[:%\w+]] Function [[false]]
473 ; CHECK: OpSelectionMerge [[return_block:%\w+]]
474 ; CHECK: OpSelectionMerge [[merge_lab:%\w+]]
475 ; CHECK: OpBranchConditional [[cond:%\w+]] [[if_lab:%\w+]] [[then_lab:%\w+]]
476 ; CHECK: [[if_lab]] = OpLabel
477 ; CHECK-NEXT: OpStore [[var]] [[true]]
478 ; CHECK-NEXT: DebugScope
479 ; CHECK-NEXT: OpLine {{%\d+}} 100 0
480 ; CHECK-NEXT: OpBranch [[return_block]]
481 ; CHECK: [[then_lab]] = OpLabel
482 ; CHECK-NEXT: OpStore [[var]] [[true]]
483 ; CHECK-NEXT: DebugScope
484 ; CHECK-NEXT: OpLine {{%\d+}} 200 0
485 ; CHECK-NEXT: OpBranch [[return_block]]
486 ; CHECK: [[merge_lab]] = OpLabel
487 ; CHECK-NEXT: DebugScope
488 ; CHECK-NEXT: OpLine {{%\d+}} 300 0
489 ; CHECK-NEXT: OpBranch [[return_block]]
490 ; CHECK: [[return_block]] = OpLabel
491 ; CHECK-NEXT: OpReturn
492 OpCapability Addresses
493 OpCapability Shader
494 OpCapability Linkage
495 %12 = OpExtInstImport "OpenCL.DebugInfo.100"
496 OpMemoryModel Logical GLSL450
497 OpEntryPoint GLCompute %6 "simple_shader"
498 %11 = OpString "test"
499 %2 = OpTypeVoid
500 %3 = OpTypeBool
501 %4 = OpConstantFalse %3
502 %1 = OpTypeFunction %2
503 %13 = OpExtInst %2 %12 DebugSource %11
504 %14 = OpExtInst %2 %12 DebugCompilationUnit 1 4 %13 HLSL
505 %6 = OpFunction %2 None %1
506 %7 = OpLabel
507 OpSelectionMerge %10 None
508 OpBranchConditional %4 %8 %9
509 %8 = OpLabel
510 %15 = OpExtInst %2 %12 DebugScope %14
511 OpLine %11 100 0
512 OpReturn
513 %9 = OpLabel
514 %16 = OpExtInst %2 %12 DebugScope %14
515 OpLine %11 200 0
516 OpReturn
517 %10 = OpLabel
518 %17 = OpExtInst %2 %12 DebugScope %14
519 OpLine %11 300 0
520 OpUnreachable
521 OpFunctionEnd
522 )";
523
524 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
525 SinglePassRunAndMatch<MergeReturnPass>(before, false);
526 }
527
528 TEST_F(MergeReturnPassTest, StructuredControlFlowAddPhi) {
529 const std::string before =
530 R"(
531 ; CHECK: [[false:%\w+]] = OpConstantFalse
532 ; CHECK: [[true:%\w+]] = OpConstantTrue
533 ; CHECK: OpFunction
534 ; CHECK: [[var:%\w+]] = OpVariable [[:%\w+]] Function [[false]]
535 ; CHECK: OpSelectionMerge [[single_case_switch_merge:%\w+]]
536 ; CHECK: OpSelectionMerge [[merge_lab:%\w+]]
537 ; CHECK: OpBranchConditional [[cond:%\w+]] [[if_lab:%\w+]] [[then_lab:%\w+]]
538 ; CHECK: [[if_lab]] = OpLabel
539 ; CHECK-NEXT: [[add:%\w+]] = OpIAdd [[type:%\w+]]
540 ; CHECK-NEXT: OpBranch
541 ; CHECK: [[then_lab]] = OpLabel
542 ; CHECK-NEXT: OpStore [[var]] [[true]]
543 ; CHECK-NEXT: OpBranch [[single_case_switch_merge]]
544 ; CHECK: [[merge_lab]] = OpLabel
545 ; CHECK: [[single_case_switch_merge]] = OpLabel
546 ; CHECK-NEXT: OpReturn
547 OpCapability Addresses
548 OpCapability Shader
549 OpCapability Linkage
550 OpMemoryModel Logical GLSL450
551 OpEntryPoint GLCompute %6 "simple_shader"
552 %2 = OpTypeVoid
553 %3 = OpTypeBool
554 %int = OpTypeInt 32 0
555 %int_0 = OpConstant %int 0
556 %4 = OpConstantFalse %3
557 %1 = OpTypeFunction %2
558 %6 = OpFunction %2 None %1
559 %7 = OpLabel
560 OpSelectionMerge %10 None
561 OpBranchConditional %4 %8 %9
562 %8 = OpLabel
563 %11 = OpIAdd %int %int_0 %int_0
564 OpBranch %10
565 %9 = OpLabel
566 OpReturn
567 %10 = OpLabel
568 %12 = OpIAdd %int %11 %11
569 OpReturn
570 OpFunctionEnd
571 )";
572
573 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
574 SinglePassRunAndMatch<MergeReturnPass>(before, false);
575 }
576
577 TEST_F(MergeReturnPassTest, StructuredControlDecoration) {
578 const std::string before =
579 R"(
580 ; CHECK: OpDecorate [[dec_id:%\w+]] RelaxedPrecision
581 ; CHECK: [[false:%\w+]] = OpConstantFalse
582 ; CHECK: [[true:%\w+]] = OpConstantTrue
583 ; CHECK: OpFunction
584 ; CHECK: [[var:%\w+]] = OpVariable [[:%\w+]] Function [[false]]
585 ; CHECK: OpSelectionMerge [[return_block:%\w+]]
586 ; CHECK: OpSelectionMerge [[merge_lab:%\w+]]
587 ; CHECK: OpBranchConditional [[cond:%\w+]] [[if_lab:%\w+]] [[then_lab:%\w+]]
588 ; CHECK: [[if_lab]] = OpLabel
589 ; CHECK-NEXT: [[dec_id]] = OpIAdd [[type:%\w+]]
590 ; CHECK-NEXT: OpBranch
591 ; CHECK: [[then_lab]] = OpLabel
592 ; CHECK-NEXT: OpStore [[var]] [[true]]
593 ; CHECK-NEXT: OpBranch [[return_block]]
594 ; CHECK: [[merge_lab]] = OpLabel
595 ; CHECK-NEXT: OpStore [[var]] [[true]]
596 ; CHECK-NEXT: OpBranch [[return_block]]
597 ; CHECK: [[return_block]] = OpLabel
598 ; CHECK-NEXT: OpReturn
599 OpCapability Addresses
600 OpCapability Shader
601 OpCapability Linkage
602 OpMemoryModel Logical GLSL450
603 OpEntryPoint GLCompute %6 "simple_shader"
604 OpDecorate %11 RelaxedPrecision
605 %2 = OpTypeVoid
606 %3 = OpTypeBool
607 %int = OpTypeInt 32 0
608 %int_0 = OpConstant %int 0
609 %4 = OpConstantFalse %3
610 %1 = OpTypeFunction %2
611 %6 = OpFunction %2 None %1
612 %7 = OpLabel
613 OpSelectionMerge %10 None
614 OpBranchConditional %4 %8 %9
615 %8 = OpLabel
616 %11 = OpIAdd %int %int_0 %int_0
617 OpBranch %10
618 %9 = OpLabel
619 OpReturn
620 %10 = OpLabel
621 OpReturn
622 OpFunctionEnd
623 )";
624
625 SinglePassRunAndMatch<MergeReturnPass>(before, false);
626 }
627
628 TEST_F(MergeReturnPassTest, SplitBlockUsedInPhi) {
629 const std::string before =
630 R"(
631 ; CHECK: OpFunction
632 ; CHECK: OpSelectionMerge [[single_case_switch_merge:%\w+]]
633 ; CHECK: OpLoopMerge [[loop_merge:%\w+]]
634 ; CHECK: [[loop_merge]] = OpLabel
635 ; CHECK: OpBranchConditional {{%\w+}} [[single_case_switch_merge]] [[old_code_path:%\w+]]
636 ; CHECK: [[old_code_path:%\w+]] = OpLabel
637 ; CHECK: OpBranchConditional {{%\w+}} [[side_node:%\w+]] [[phi_block:%\w+]]
638 ; CHECK: [[phi_block]] = OpLabel
639 ; CHECK-NEXT: OpPhi %bool %false [[side_node]] %true [[old_code_path]]
640 OpCapability Addresses
641 OpCapability Shader
642 OpCapability Linkage
643 OpMemoryModel Logical GLSL450
644 OpEntryPoint GLCompute %1 "simple_shader"
645 %void = OpTypeVoid
646 %bool = OpTypeBool
647 %false = OpConstantFalse %bool
648 %true = OpConstantTrue %bool
649 %6 = OpTypeFunction %void
650 %1 = OpFunction %void None %6
651 %7 = OpLabel
652 OpLoopMerge %merge %cont None
653 OpBranchConditional %false %9 %merge
654 %9 = OpLabel
655 OpReturn
656 %cont = OpLabel
657 OpBranch %7
658 %merge = OpLabel
659 OpSelectionMerge %merge2 None
660 OpBranchConditional %false %if %merge2
661 %if = OpLabel
662 OpBranch %merge2
663 %merge2 = OpLabel
664 %12 = OpPhi %bool %false %if %true %merge
665 OpReturn
666 OpFunctionEnd
667 )";
668
669 SinglePassRunAndMatch<MergeReturnPass>(before, false);
670 }
671
672 TEST_F(MergeReturnPassTest, DebugSplitBlockUsedInPhi) {
673 const std::string before =
674 R"(
675 ; CHECK: DebugScope
676 ; CHECK-NEXT: OpLine {{%\d+}} 100 0
677 ; CHECK: OpLoopMerge
678
679 ; CHECK: OpStore [[return_in_loop:%\w+]] %true
680 ; CHECK-NEXT: DebugScope
681 ; CHECK-NEXT: OpLine {{%\d+}} 200 0
682 ; CHECK-NEXT: OpBranch [[check_early_return:%\w+]]
683
684 ; CHECK: [[check_early_return]] = OpLabel
685 ; CHECK-NEXT: [[early_return:%\w+]] = OpLoad %bool [[return_in_loop]]
686 ; CHECK-NEXT: OpSelectionMerge [[not_early_return:%\w+]] None
687 ; CHECK-NEXT: OpBranchConditional [[early_return]] {{%\d+}} [[not_early_return]]
688
689 ; CHECK: [[not_early_return]] = OpLabel
690 ; CHECK-NEXT: DebugScope
691 ; CHECK-NEXT: OpLine {{%\d+}} 400 0
692 ; CHECK: OpSelectionMerge [[merge2:%\w+]] None
693
694 ; CHECK: [[merge2]] = OpLabel
695 ; CHECK-NEXT: DebugScope
696 ; CHECK-NEXT: OpLine {{%\d+}} 600 0
697 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi %bool %false {{%\d+}} %true [[not_early_return]]
698 ; CHECK-NEXT: DebugValue {{%\d+}} [[phi]]
699
700 OpCapability Addresses
701 OpCapability Shader
702 OpCapability Linkage
703 %ext = OpExtInstImport "OpenCL.DebugInfo.100"
704 OpMemoryModel Logical GLSL450
705 OpEntryPoint GLCompute %1 "simple_shader"
706 %tn = OpString "test"
707 %void = OpTypeVoid
708 %bool = OpTypeBool
709 %uint = OpTypeInt 32 0
710 %uint_8 = OpConstant %uint 8
711 %false = OpConstantFalse %bool
712 %true = OpConstantTrue %bool
713 %6 = OpTypeFunction %void
714 %src = OpExtInst %void %ext DebugSource %tn
715 %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
716 %ty = OpExtInst %void %ext DebugTypeBasic %tn %uint_8 Boolean
717 %v = OpExtInst %void %ext DebugLocalVariable %tn %ty %src 0 0 %cu FlagIsLocal
718 %expr = OpExtInst %void %ext DebugExpression
719 %1 = OpFunction %void None %6
720 %7 = OpLabel
721 %s0 = OpExtInst %void %ext DebugScope %cu
722 OpLine %tn 100 0
723 OpLoopMerge %merge %cont None
724 OpBranchConditional %false %9 %merge
725 %9 = OpLabel
726 %s1 = OpExtInst %void %ext DebugScope %cu
727 OpLine %tn 200 0
728 OpReturn
729 %cont = OpLabel
730 %s2 = OpExtInst %void %ext DebugScope %cu
731 OpLine %tn 300 0
732 OpBranch %7
733 %merge = OpLabel
734 %s3 = OpExtInst %void %ext DebugScope %cu
735 OpLine %tn 400 0
736 OpSelectionMerge %merge2 None
737 OpBranchConditional %false %if %merge2
738 %if = OpLabel
739 %s4 = OpExtInst %void %ext DebugScope %cu
740 OpLine %tn 500 0
741 OpBranch %merge2
742 %merge2 = OpLabel
743 %s5 = OpExtInst %void %ext DebugScope %cu
744 OpLine %tn 600 0
745 %12 = OpPhi %bool %false %if %true %merge
746 %dv = OpExtInst %void %ext DebugValue %v %12 %expr
747 OpLine %tn 900 0
748 OpReturn
749 OpFunctionEnd
750 )";
751
752 SinglePassRunAndMatch<MergeReturnPass>(before, false);
753 }
754
755 // TODO(#1861): Reenable these test when the breaks from selection constructs
756 // are reenabled.
757 /*
758 TEST_F(MergeReturnPassTest, UpdateOrderWhenPredicating) {
759 const std::string before =
760 R"(
761 ; CHECK: OpFunction
762 ; CHECK: OpFunction
763 ; CHECK: OpSelectionMerge [[m1:%\w+]] None
764 ; CHECK-NOT: OpReturn
765 ; CHECK: [[m1]] = OpLabel
766 ; CHECK: OpSelectionMerge [[m2:%\w+]] None
767 ; CHECK: OpSelectionMerge [[m3:%\w+]] None
768 ; CHECK: OpSelectionMerge [[m4:%\w+]] None
769 ; CHECK: OpLabel
770 ; CHECK-NEXT: OpStore
771 ; CHECK-NEXT: OpBranch [[m4]]
772 ; CHECK: [[m4]] = OpLabel
773 ; CHECK-NEXT: [[ld4:%\w+]] = OpLoad %bool
774 ; CHECK-NEXT: OpBranchConditional [[ld4]] [[m3]]
775 ; CHECK: [[m3]] = OpLabel
776 ; CHECK-NEXT: [[ld3:%\w+]] = OpLoad %bool
777 ; CHECK-NEXT: OpBranchConditional [[ld3]] [[m2]]
778 ; CHECK: [[m2]] = OpLabel
779 OpCapability SampledBuffer
780 OpCapability StorageImageExtendedFormats
781 OpCapability Shader
782 OpMemoryModel Logical GLSL450
783 OpEntryPoint Fragment %1 "PS_DebugTiles"
784 OpExecutionMode %1 OriginUpperLeft
785 OpSource HLSL 600
786 %void = OpTypeVoid
787 %3 = OpTypeFunction %void
788 %bool = OpTypeBool
789 %1 = OpFunction %void None %3
790 %5 = OpLabel
791 %6 = OpFunctionCall %void %7
792 OpReturn
793 OpFunctionEnd
794 %7 = OpFunction %void None %3
795 %8 = OpLabel
796 %9 = OpUndef %bool
797 OpSelectionMerge %10 None
798 OpBranchConditional %9 %11 %10
799 %11 = OpLabel
800 OpReturn
801 %10 = OpLabel
802 %12 = OpUndef %bool
803 OpSelectionMerge %13 None
804 OpBranchConditional %12 %14 %15
805 %15 = OpLabel
806 %16 = OpUndef %bool
807 OpSelectionMerge %17 None
808 OpBranchConditional %16 %18 %17
809 %18 = OpLabel
810 OpReturn
811 %17 = OpLabel
812 OpBranch %13
813 %14 = OpLabel
814 OpReturn
815 %13 = OpLabel
816 OpReturn
817 OpFunctionEnd
818 )";
819
820 SinglePassRunAndMatch<MergeReturnPass>(before, false);
821 }
822 */
823
824 TEST_F(MergeReturnPassTest, StructuredControlFlowBothMergeAndHeader) {
825 const std::string test =
826 R"(
827 ; CHECK: OpFunction
828 ; CHECK: [[ret_flag:%\w+]] = OpVariable %_ptr_Function_bool Function %false
829 ; CHECK: OpSelectionMerge [[single_case_switch_merge:%\w+]]
830 ; CHECK: OpLoopMerge [[loop1_merge:%\w+]] {{%\w+}}
831 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[if_lab:%\w+]] {{%\w+}}
832 ; CHECK: [[if_lab]] = OpLabel
833 ; CHECK: OpStore [[ret_flag]] %true
834 ; CHECK-NEXT: OpBranch [[loop1_merge]]
835 ; CHECK: [[loop1_merge]] = OpLabel
836 ; CHECK-NEXT: [[ld:%\w+]] = OpLoad %bool [[ret_flag]]
837 ; CHECK-NOT: OpLabel
838 ; CHECK: OpBranchConditional [[ld]] [[single_case_switch_merge]] [[empty_block:%\w+]]
839 ; CHECK: [[empty_block]] = OpLabel
840 ; CHECK-NEXT: OpBranch [[loop2:%\w+]]
841 ; CHECK: [[loop2]] = OpLabel
842 ; CHECK-NOT: OpLabel
843 ; CHECK: OpLoopMerge
844 OpCapability Addresses
845 OpCapability Shader
846 OpCapability Linkage
847 OpMemoryModel Logical GLSL450
848 OpEntryPoint GLCompute %1 "simple_shader"
849 %void = OpTypeVoid
850 %bool = OpTypeBool
851 %uint = OpTypeInt 32 0
852 %uint_0 = OpConstant %uint 0
853 %false = OpConstantFalse %bool
854 %7 = OpTypeFunction %void
855 %1 = OpFunction %void None %7
856 %8 = OpLabel
857 OpBranch %9
858 %9 = OpLabel
859 OpLoopMerge %10 %11 None
860 OpBranchConditional %false %12 %13
861 %12 = OpLabel
862 OpReturn
863 %13 = OpLabel
864 OpBranch %10
865 %11 = OpLabel
866 OpBranch %9
867 %10 = OpLabel
868 OpLoopMerge %14 %15 None
869 OpBranch %15
870 %15 = OpLabel
871 %16 = OpIAdd %uint %uint_0 %uint_0
872 OpBranchConditional %false %10 %14
873 %14 = OpLabel
874 %17 = OpIAdd %uint %16 %16
875 OpReturn
876 OpFunctionEnd
877
878 )";
879
880 const std::string after =
881 R"(OpCapability Addresses
882 OpCapability Shader
883 OpCapability Linkage
884 OpMemoryModel Logical GLSL450
885 OpEntryPoint GLCompute %1 "simple_shader"
886 %void = OpTypeVoid
887 %bool = OpTypeBool
888 %uint = OpTypeInt 32 0
889 %uint_0 = OpConstant %uint 0
890 %false = OpConstantFalse %bool
891 %7 = OpTypeFunction %void
892 %_ptr_Function_bool = OpTypePointer Function %bool
893 %true = OpConstantTrue %bool
894 %1 = OpFunction %void None %7
895 %8 = OpLabel
896 %18 = OpVariable %_ptr_Function_bool Function %false
897 OpSelectionMerge %9 None
898 OpBranchConditional %false %10 %11
899 %10 = OpLabel
900 OpStore %18 %true
901 OpBranch %9
902 %11 = OpLabel
903 OpBranch %9
904 %9 = OpLabel
905 %23 = OpLoad %bool %18
906 OpSelectionMerge %22 None
907 OpBranchConditional %23 %22 %21
908 %21 = OpLabel
909 OpBranch %20
910 %20 = OpLabel
911 OpLoopMerge %12 %13 None
912 OpBranch %13
913 %13 = OpLabel
914 %14 = OpIAdd %uint %uint_0 %uint_0
915 OpBranchConditional %false %20 %12
916 %12 = OpLabel
917 %15 = OpIAdd %uint %14 %14
918 OpStore %18 %true
919 OpBranch %22
920 %22 = OpLabel
921 OpBranch %16
922 %16 = OpLabel
923 OpReturn
924 OpFunctionEnd
925 )";
926
927 SinglePassRunAndMatch<MergeReturnPass>(test, false);
928 }
929
930 // TODO(#1861): Reenable these test when the breaks from selection constructs
931 // are reenabled.
932 /*
933 TEST_F(MergeReturnPassTest, NestedSelectionMerge) {
934 const std::string before =
935 R"(
936 OpCapability Addresses
937 OpCapability Shader
938 OpCapability Linkage
939 OpMemoryModel Logical GLSL450
940 OpEntryPoint GLCompute %1 "simple_shader"
941 %void = OpTypeVoid
942 %bool = OpTypeBool
943 %uint = OpTypeInt 32 0
944 %uint_0 = OpConstant %uint 0
945 %false = OpConstantFalse %bool
946 %7 = OpTypeFunction %void
947 %1 = OpFunction %void None %7
948 %8 = OpLabel
949 OpSelectionMerge %9 None
950 OpBranchConditional %false %10 %11
951 %10 = OpLabel
952 OpReturn
953 %11 = OpLabel
954 OpSelectionMerge %12 None
955 OpBranchConditional %false %13 %14
956 %13 = OpLabel
957 %15 = OpIAdd %uint %uint_0 %uint_0
958 OpBranch %12
959 %14 = OpLabel
960 OpReturn
961 %12 = OpLabel
962 OpBranch %9
963 %9 = OpLabel
964 %16 = OpIAdd %uint %15 %15
965 OpReturn
966 OpFunctionEnd
967 )";
968
969 const std::string after =
970 R"(OpCapability Addresses
971 OpCapability Shader
972 OpCapability Linkage
973 OpMemoryModel Logical GLSL450
974 OpEntryPoint GLCompute %1 "simple_shader"
975 %void = OpTypeVoid
976 %bool = OpTypeBool
977 %uint = OpTypeInt 32 0
978 %uint_0 = OpConstant %uint 0
979 %false = OpConstantFalse %bool
980 %7 = OpTypeFunction %void
981 %_ptr_Function_bool = OpTypePointer Function %bool
982 %true = OpConstantTrue %bool
983 %26 = OpUndef %uint
984 %1 = OpFunction %void None %7
985 %8 = OpLabel
986 %19 = OpVariable %_ptr_Function_bool Function %false
987 OpSelectionMerge %9 None
988 OpBranchConditional %false %10 %11
989 %10 = OpLabel
990 OpStore %19 %true
991 OpBranch %9
992 %11 = OpLabel
993 OpSelectionMerge %12 None
994 OpBranchConditional %false %13 %14
995 %13 = OpLabel
996 %15 = OpIAdd %uint %uint_0 %uint_0
997 OpBranch %12
998 %14 = OpLabel
999 OpStore %19 %true
1000 OpBranch %12
1001 %12 = OpLabel
1002 %27 = OpPhi %uint %15 %13 %26 %14
1003 %22 = OpLoad %bool %19
1004 OpBranchConditional %22 %9 %21
1005 %21 = OpLabel
1006 OpBranch %9
1007 %9 = OpLabel
1008 %28 = OpPhi %uint %27 %21 %26 %10 %26 %12
1009 %25 = OpLoad %bool %19
1010 OpSelectionMerge %24 None
1011 OpBranchConditional %25 %24 %23
1012 %23 = OpLabel
1013 %16 = OpIAdd %uint %28 %28
1014 OpStore %19 %true
1015 OpBranch %24
1016 %24 = OpLabel
1017 OpBranch %17
1018 %17 = OpLabel
1019 OpReturn
1020 OpFunctionEnd
1021 )";
1022
1023 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1024 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
1025 }
1026
1027 // This is essentially the same as NestedSelectionMerge, except
1028 // the order of the first branch is changed. This is to make sure things
1029 // work even if the order of the traversals change.
1030 TEST_F(MergeReturnPassTest, NestedSelectionMerge2) {
1031 const std::string before =
1032 R"( OpCapability Addresses
1033 OpCapability Shader
1034 OpCapability Linkage
1035 OpMemoryModel Logical GLSL450
1036 OpEntryPoint GLCompute %1 "simple_shader"
1037 %void = OpTypeVoid
1038 %bool = OpTypeBool
1039 %uint = OpTypeInt 32 0
1040 %uint_0 = OpConstant %uint 0
1041 %false = OpConstantFalse %bool
1042 %7 = OpTypeFunction %void
1043 %1 = OpFunction %void None %7
1044 %8 = OpLabel
1045 OpSelectionMerge %9 None
1046 OpBranchConditional %false %10 %11
1047 %11 = OpLabel
1048 OpReturn
1049 %10 = OpLabel
1050 OpSelectionMerge %12 None
1051 OpBranchConditional %false %13 %14
1052 %13 = OpLabel
1053 %15 = OpIAdd %uint %uint_0 %uint_0
1054 OpBranch %12
1055 %14 = OpLabel
1056 OpReturn
1057 %12 = OpLabel
1058 OpBranch %9
1059 %9 = OpLabel
1060 %16 = OpIAdd %uint %15 %15
1061 OpReturn
1062 OpFunctionEnd
1063 )";
1064
1065 const std::string after =
1066 R"(OpCapability Addresses
1067 OpCapability Shader
1068 OpCapability Linkage
1069 OpMemoryModel Logical GLSL450
1070 OpEntryPoint GLCompute %1 "simple_shader"
1071 %void = OpTypeVoid
1072 %bool = OpTypeBool
1073 %uint = OpTypeInt 32 0
1074 %uint_0 = OpConstant %uint 0
1075 %false = OpConstantFalse %bool
1076 %7 = OpTypeFunction %void
1077 %_ptr_Function_bool = OpTypePointer Function %bool
1078 %true = OpConstantTrue %bool
1079 %26 = OpUndef %uint
1080 %1 = OpFunction %void None %7
1081 %8 = OpLabel
1082 %19 = OpVariable %_ptr_Function_bool Function %false
1083 OpSelectionMerge %9 None
1084 OpBranchConditional %false %10 %11
1085 %11 = OpLabel
1086 OpStore %19 %true
1087 OpBranch %9
1088 %10 = OpLabel
1089 OpSelectionMerge %12 None
1090 OpBranchConditional %false %13 %14
1091 %13 = OpLabel
1092 %15 = OpIAdd %uint %uint_0 %uint_0
1093 OpBranch %12
1094 %14 = OpLabel
1095 OpStore %19 %true
1096 OpBranch %12
1097 %12 = OpLabel
1098 %27 = OpPhi %uint %15 %13 %26 %14
1099 %25 = OpLoad %bool %19
1100 OpBranchConditional %25 %9 %24
1101 %24 = OpLabel
1102 OpBranch %9
1103 %9 = OpLabel
1104 %28 = OpPhi %uint %27 %24 %26 %11 %26 %12
1105 %23 = OpLoad %bool %19
1106 OpSelectionMerge %22 None
1107 OpBranchConditional %23 %22 %21
1108 %21 = OpLabel
1109 %16 = OpIAdd %uint %28 %28
1110 OpStore %19 %true
1111 OpBranch %22
1112 %22 = OpLabel
1113 OpBranch %17
1114 %17 = OpLabel
1115 OpReturn
1116 OpFunctionEnd
1117 )";
1118
1119 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
1120 }
1121
1122 TEST_F(MergeReturnPassTest, NestedSelectionMerge3) {
1123 const std::string before =
1124 R"( OpCapability Addresses
1125 OpCapability Shader
1126 OpCapability Linkage
1127 OpMemoryModel Logical GLSL450
1128 OpEntryPoint GLCompute %1 "simple_shader"
1129 %void = OpTypeVoid
1130 %bool = OpTypeBool
1131 %uint = OpTypeInt 32 0
1132 %uint_0 = OpConstant %uint 0
1133 %false = OpConstantFalse %bool
1134 %7 = OpTypeFunction %void
1135 %1 = OpFunction %void None %7
1136 %8 = OpLabel
1137 OpSelectionMerge %9 None
1138 OpBranchConditional %false %10 %11
1139 %11 = OpLabel
1140 OpReturn
1141 %10 = OpLabel
1142 %12 = OpIAdd %uint %uint_0 %uint_0
1143 OpSelectionMerge %13 None
1144 OpBranchConditional %false %14 %15
1145 %14 = OpLabel
1146 OpBranch %13
1147 %15 = OpLabel
1148 OpReturn
1149 %13 = OpLabel
1150 OpBranch %9
1151 %9 = OpLabel
1152 %16 = OpIAdd %uint %12 %12
1153 OpReturn
1154 OpFunctionEnd
1155 )";
1156
1157 const std::string after =
1158 R"(OpCapability Addresses
1159 OpCapability Shader
1160 OpCapability Linkage
1161 OpMemoryModel Logical GLSL450
1162 OpEntryPoint GLCompute %1 "simple_shader"
1163 %void = OpTypeVoid
1164 %bool = OpTypeBool
1165 %uint = OpTypeInt 32 0
1166 %uint_0 = OpConstant %uint 0
1167 %false = OpConstantFalse %bool
1168 %7 = OpTypeFunction %void
1169 %_ptr_Function_bool = OpTypePointer Function %bool
1170 %true = OpConstantTrue %bool
1171 %26 = OpUndef %uint
1172 %1 = OpFunction %void None %7
1173 %8 = OpLabel
1174 %19 = OpVariable %_ptr_Function_bool Function %false
1175 OpSelectionMerge %9 None
1176 OpBranchConditional %false %10 %11
1177 %11 = OpLabel
1178 OpStore %19 %true
1179 OpBranch %9
1180 %10 = OpLabel
1181 %12 = OpIAdd %uint %uint_0 %uint_0
1182 OpSelectionMerge %13 None
1183 OpBranchConditional %false %14 %15
1184 %14 = OpLabel
1185 OpBranch %13
1186 %15 = OpLabel
1187 OpStore %19 %true
1188 OpBranch %13
1189 %13 = OpLabel
1190 %25 = OpLoad %bool %19
1191 OpBranchConditional %25 %9 %24
1192 %24 = OpLabel
1193 OpBranch %9
1194 %9 = OpLabel
1195 %27 = OpPhi %uint %12 %24 %26 %11 %26 %13
1196 %23 = OpLoad %bool %19
1197 OpSelectionMerge %22 None
1198 OpBranchConditional %23 %22 %21
1199 %21 = OpLabel
1200 %16 = OpIAdd %uint %27 %27
1201 OpStore %19 %true
1202 OpBranch %22
1203 %22 = OpLabel
1204 OpBranch %17
1205 %17 = OpLabel
1206 OpReturn
1207 OpFunctionEnd
1208 )";
1209
1210 SinglePassRunAndCheck<MergeReturnPass>(before, after, false, true);
1211 }
1212 */
1213
1214 TEST_F(MergeReturnPassTest, NestedLoopMerge) {
1215 const std::string test =
1216 R"(
1217 ; CHECK: OpFunction
1218 ; CHECK: OpSelectionMerge [[single_case_switch_merge:%\w+]]
1219 ; CHECK: OpLoopMerge [[outer_loop_merge:%\w+]]
1220 ; CHECK: OpLoopMerge [[inner_loop_merge:%\w+]]
1221 ; CHECK: OpSelectionMerge
1222 ; CHECK-NEXT: OpBranchConditional %true [[early_exit_block:%\w+]]
1223 ; CHECK: [[early_exit_block]] = OpLabel
1224 ; CHECK-NOT: OpLabel
1225 ; CHECK: OpBranch [[inner_loop_merge]]
1226 ; CHECK: [[inner_loop_merge]] = OpLabel
1227 ; CHECK-NOT: OpLabel
1228 ; CHECK: OpBranchConditional {{%\w+}} [[outer_loop_merge]]
1229 ; CHECK: [[outer_loop_merge]] = OpLabel
1230 ; CHECK-NOT: OpLabel
1231 ; CHECK: OpBranchConditional {{%\w+}} [[single_case_switch_merge]]
1232 ; CHECK: [[single_case_switch_merge]] = OpLabel
1233 ; CHECK-NOT: OpLabel
1234 ; CHECK: OpReturn
1235 OpCapability SampledBuffer
1236 OpCapability StorageImageExtendedFormats
1237 OpCapability Shader
1238 %1 = OpExtInstImport "GLSL.std.450"
1239 OpMemoryModel Logical GLSL450
1240 OpEntryPoint GLCompute %2 "CS"
1241 OpExecutionMode %2 LocalSize 8 8 1
1242 OpSource HLSL 600
1243 %uint = OpTypeInt 32 0
1244 %void = OpTypeVoid
1245 %6 = OpTypeFunction %void
1246 %uint_0 = OpConstant %uint 0
1247 %uint_1 = OpConstant %uint 1
1248 %v3uint = OpTypeVector %uint 3
1249 %bool = OpTypeBool
1250 %true = OpConstantTrue %bool
1251 %_ptr_Function_uint = OpTypePointer Function %uint
1252 %2 = OpFunction %void None %6
1253 %14 = OpLabel
1254 OpBranch %19
1255 %19 = OpLabel
1256 %20 = OpPhi %uint %uint_0 %2 %34 %23
1257 %21 = OpULessThan %bool %20 %uint_1
1258 OpLoopMerge %22 %23 DontUnroll
1259 OpBranchConditional %21 %24 %22
1260 %24 = OpLabel
1261 OpBranch %25
1262 %25 = OpLabel
1263 %27 = OpINotEqual %bool %uint_1 %uint_0
1264 OpLoopMerge %28 %29 DontUnroll
1265 OpBranchConditional %27 %30 %28
1266 %30 = OpLabel
1267 OpSelectionMerge %31 None
1268 OpBranchConditional %true %32 %31
1269 %32 = OpLabel
1270 OpReturn
1271 %31 = OpLabel
1272 OpBranch %29
1273 %29 = OpLabel
1274 OpBranch %25
1275 %28 = OpLabel
1276 OpBranch %23
1277 %23 = OpLabel
1278 %34 = OpIAdd %uint %20 %uint_1
1279 OpBranch %19
1280 %22 = OpLabel
1281 OpReturn
1282 OpFunctionEnd
1283 )";
1284
1285 SinglePassRunAndMatch<MergeReturnPass>(test, false);
1286 }
1287
1288 TEST_F(MergeReturnPassTest, ReturnValueDecoration) {
1289 const std::string test =
1290 R"(
1291 ; CHECK: OpDecorate [[func:%\w+]] RelaxedPrecision
1292 ; CHECK: OpDecorate [[ret_val:%\w+]] RelaxedPrecision
1293 ; CHECK: [[func]] = OpFunction
1294 ; CHECK-NEXT: OpLabel
1295 ; CHECK-NOT: OpLabel
1296 ; CHECK: [[ret_val]] = OpVariable
1297 OpCapability Linkage
1298 OpCapability Shader
1299 OpMemoryModel Logical GLSL450
1300 OpEntryPoint GLCompute %11 "simple_shader"
1301 OpDecorate %7 RelaxedPrecision
1302 %12 = OpTypeVoid
1303 %1 = OpTypeInt 32 0
1304 %2 = OpTypeBool
1305 %3 = OpConstantFalse %2
1306 %4 = OpConstant %1 0
1307 %5 = OpConstant %1 1
1308 %6 = OpTypeFunction %1
1309 %13 = OpTypeFunction %12
1310 %11 = OpFunction %12 None %13
1311 %l1 = OpLabel
1312 %fc = OpFunctionCall %1 %7
1313 OpReturn
1314 OpFunctionEnd
1315 %7 = OpFunction %1 None %6
1316 %8 = OpLabel
1317 OpBranchConditional %3 %9 %10
1318 %9 = OpLabel
1319 OpReturnValue %4
1320 %10 = OpLabel
1321 OpReturnValue %5
1322 OpFunctionEnd
1323 )";
1324
1325 SinglePassRunAndMatch<MergeReturnPass>(test, false);
1326 }
1327
1328 TEST_F(MergeReturnPassTest,
1329 StructuredControlFlowWithNonTrivialUnreachableMerge) {
1330 const std::string before =
1331 R"(
1332 OpCapability Addresses
1333 OpCapability Shader
1334 OpCapability Linkage
1335 OpMemoryModel Logical GLSL450
1336 OpEntryPoint GLCompute %6 "simple_shader"
1337 %2 = OpTypeVoid
1338 %3 = OpTypeBool
1339 %4 = OpConstantFalse %3
1340 %1 = OpTypeFunction %2
1341 %6 = OpFunction %2 None %1
1342 %7 = OpLabel
1343 OpSelectionMerge %10 None
1344 OpBranchConditional %4 %8 %9
1345 %8 = OpLabel
1346 OpReturn
1347 %9 = OpLabel
1348 OpReturn
1349 %10 = OpLabel
1350 %11 = OpUndef %3
1351 OpUnreachable
1352 OpFunctionEnd
1353 )";
1354
1355 std::vector<Message> messages = {
1356 {SPV_MSG_ERROR, nullptr, 0, 0,
1357 "Module contains unreachable blocks during merge return. Run dead "
1358 "branch elimination before merge return."}};
1359 SetMessageConsumer(GetTestMessageConsumer(messages));
1360
1361 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1362 auto result = SinglePassRunToBinary<MergeReturnPass>(before, false);
1363 EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1364 EXPECT_TRUE(messages.empty());
1365 }
1366
1367 TEST_F(MergeReturnPassTest,
1368 StructuredControlFlowWithNonTrivialUnreachableContinue) {
1369 const std::string before =
1370 R"(
1371 OpCapability Addresses
1372 OpCapability Shader
1373 OpCapability Linkage
1374 OpMemoryModel Logical GLSL450
1375 OpEntryPoint GLCompute %6 "simple_shader"
1376 %2 = OpTypeVoid
1377 %3 = OpTypeBool
1378 %4 = OpConstantFalse %3
1379 %1 = OpTypeFunction %2
1380 %6 = OpFunction %2 None %1
1381 %7 = OpLabel
1382 OpBranch %header
1383 %header = OpLabel
1384 OpLoopMerge %merge %continue None
1385 OpBranchConditional %4 %8 %merge
1386 %8 = OpLabel
1387 OpReturn
1388 %continue = OpLabel
1389 %11 = OpUndef %3
1390 OpBranch %header
1391 %merge = OpLabel
1392 OpReturn
1393 OpFunctionEnd
1394 )";
1395
1396 std::vector<Message> messages = {
1397 {SPV_MSG_ERROR, nullptr, 0, 0,
1398 "Module contains unreachable blocks during merge return. Run dead "
1399 "branch elimination before merge return."}};
1400 SetMessageConsumer(GetTestMessageConsumer(messages));
1401
1402 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1403 auto result = SinglePassRunToBinary<MergeReturnPass>(before, false);
1404 EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1405 EXPECT_TRUE(messages.empty());
1406 }
1407
1408 TEST_F(MergeReturnPassTest, StructuredControlFlowWithUnreachableBlock) {
1409 const std::string before =
1410 R"(
1411 OpCapability Addresses
1412 OpCapability Shader
1413 OpCapability Linkage
1414 OpMemoryModel Logical GLSL450
1415 OpEntryPoint GLCompute %6 "simple_shader"
1416 %2 = OpTypeVoid
1417 %3 = OpTypeBool
1418 %4 = OpConstantFalse %3
1419 %1 = OpTypeFunction %2
1420 %6 = OpFunction %2 None %1
1421 %7 = OpLabel
1422 OpBranch %header
1423 %header = OpLabel
1424 OpLoopMerge %merge %continue None
1425 OpBranchConditional %4 %8 %merge
1426 %8 = OpLabel
1427 OpReturn
1428 %continue = OpLabel
1429 OpBranch %header
1430 %merge = OpLabel
1431 OpReturn
1432 %unreachable = OpLabel
1433 OpUnreachable
1434 OpFunctionEnd
1435 )";
1436
1437 std::vector<Message> messages = {
1438 {SPV_MSG_ERROR, nullptr, 0, 0,
1439 "Module contains unreachable blocks during merge return. Run dead "
1440 "branch elimination before merge return."}};
1441 SetMessageConsumer(GetTestMessageConsumer(messages));
1442
1443 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1444 auto result = SinglePassRunToBinary<MergeReturnPass>(before, false);
1445 EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
1446 EXPECT_TRUE(messages.empty());
1447 }
1448
1449 TEST_F(MergeReturnPassTest, StructuredControlFlowDontChangeEntryPhi) {
1450 const std::string before =
1451 R"(
1452 ; CHECK: OpFunction %void
1453 ; CHECK: OpLabel
1454 ; CHECK: [[pre_header:%\w+]] = OpLabel
1455 ; CHECK: [[header:%\w+]] = OpLabel
1456 ; CHECK-NEXT: OpPhi %bool {{%\w+}} [[pre_header]] [[iv:%\w+]] [[continue:%\w+]]
1457 ; CHECK-NEXT: OpLoopMerge [[merge:%\w+]] [[continue]]
1458 ; CHECK: [[continue]] = OpLabel
1459 ; CHECK-NEXT: [[iv]] = Op
1460 ; CHECK: [[merge]] = OpLabel
1461 OpCapability Shader
1462 OpMemoryModel Logical GLSL450
1463 OpEntryPoint Vertex %1 "main"
1464 %void = OpTypeVoid
1465 %bool = OpTypeBool
1466 %4 = OpTypeFunction %void
1467 %1 = OpFunction %void None %4
1468 %5 = OpLabel
1469 %6 = OpUndef %bool
1470 OpBranch %7
1471 %7 = OpLabel
1472 %8 = OpPhi %bool %6 %5 %9 %10
1473 OpLoopMerge %11 %10 None
1474 OpBranch %12
1475 %12 = OpLabel
1476 %13 = OpUndef %bool
1477 OpSelectionMerge %10 DontFlatten
1478 OpBranchConditional %13 %10 %14
1479 %14 = OpLabel
1480 OpReturn
1481 %10 = OpLabel
1482 %9 = OpUndef %bool
1483 OpBranchConditional %13 %7 %11
1484 %11 = OpLabel
1485 OpReturn
1486 OpFunctionEnd
1487
1488 )";
1489
1490 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1491 SinglePassRunAndMatch<MergeReturnPass>(before, false);
1492 }
1493
1494 TEST_F(MergeReturnPassTest, StructuredControlFlowPartialReplacePhi) {
1495 const std::string before =
1496 R"(
1497 ; CHECK: OpFunction %void
1498 ; CHECK: OpLabel
1499 ; CHECK: [[pre_header:%\w+]] = OpLabel
1500 ; CHECK: [[header:%\w+]] = OpLabel
1501 ; CHECK-NEXT: OpPhi
1502 ; CHECK-NEXT: OpLoopMerge [[merge:%\w+]]
1503 ; CHECK: OpLabel
1504 ; CHECK: [[old_ret_block:%\w+]] = OpLabel
1505 ; CHECK: [[bb:%\w+]] = OpLabel
1506 ; CHECK-NEXT: [[val:%\w+]] = OpUndef %bool
1507 ; CHECK: [[merge]] = OpLabel
1508 ; CHECK-NEXT: [[phi1:%\w+]] = OpPhi %bool {{%\w+}} [[old_ret_block]] [[val]] [[bb]]
1509 ; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} [[bb2:%\w+]]
1510 ; CHECK: [[bb2]] = OpLabel
1511 ; CHECK: OpBranch [[header2:%\w+]]
1512 ; CHECK: [[header2]] = OpLabel
1513 ; CHECK-NEXT: [[phi2:%\w+]] = OpPhi %bool [[phi1]] [[continue2:%\w+]] [[phi1]] [[bb2]]
1514 ; CHECK-NEXT: OpLoopMerge {{%\w+}} [[continue2]]
1515 ; CHECK: [[continue2]] = OpLabel
1516 ; CHECK-NEXT: OpBranch [[header2]]
1517 OpCapability Shader
1518 OpMemoryModel Logical GLSL450
1519 OpEntryPoint Vertex %1 "main"
1520 %void = OpTypeVoid
1521 %bool = OpTypeBool
1522 %4 = OpTypeFunction %void
1523 %1 = OpFunction %void None %4
1524 %5 = OpLabel
1525 %6 = OpUndef %bool
1526 OpBranch %7
1527 %7 = OpLabel
1528 %8 = OpPhi %bool %6 %5 %9 %10
1529 OpLoopMerge %11 %10 None
1530 OpBranch %12
1531 %12 = OpLabel
1532 %13 = OpUndef %bool
1533 OpSelectionMerge %10 DontFlatten
1534 OpBranchConditional %13 %10 %14
1535 %14 = OpLabel
1536 OpReturn
1537 %10 = OpLabel
1538 %9 = OpUndef %bool
1539 OpBranchConditional %13 %7 %11
1540 %11 = OpLabel
1541 %phi = OpPhi %bool %9 %10 %9 %cont
1542 OpLoopMerge %ret %cont None
1543 OpBranch %bb
1544 %bb = OpLabel
1545 OpBranchConditional %13 %ret %cont
1546 %cont = OpLabel
1547 OpBranch %11
1548 %ret = OpLabel
1549 OpReturn
1550 OpFunctionEnd
1551 )";
1552
1553 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1554 SinglePassRunAndMatch<MergeReturnPass>(before, false);
1555 }
1556
1557 TEST_F(MergeReturnPassTest, GeneratePhiInOuterLoop) {
1558 const std::string before =
1559 R"(
1560 ; CHECK: OpSelectionMerge
1561 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
1562 ; CHECK-NEXT: [[def_bb1]] = OpLabel
1563 ; CHECK: OpLoopMerge [[merge:%\w+]] [[continue:%\w+]]
1564 ; CHECK: [[continue]] = OpLabel
1565 ; CHECK-NEXT: [[undef:%\w+]] = OpUndef
1566 ; CHECK: [[merge]] = OpLabel
1567 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi %bool {{%\w+}} {{%\w+}} [[undef]] [[continue]]
1568 ; CHECK: OpCopyObject %bool [[phi]]
1569 OpCapability Shader
1570 %1 = OpExtInstImport "GLSL.std.450"
1571 OpMemoryModel Logical GLSL450
1572 OpEntryPoint Fragment %4 "main"
1573 OpExecutionMode %4 OriginUpperLeft
1574 OpSource ESSL 310
1575 %void = OpTypeVoid
1576 %3 = OpTypeFunction %void
1577 %bool = OpTypeBool
1578 %8 = OpTypeFunction %bool
1579 %false = OpConstantFalse %bool
1580 %4 = OpFunction %void None %3
1581 %5 = OpLabel
1582 %63 = OpFunctionCall %bool %9
1583 OpReturn
1584 OpFunctionEnd
1585 %9 = OpFunction %bool None %8
1586 %10 = OpLabel
1587 OpBranch %31
1588 %31 = OpLabel
1589 OpLoopMerge %33 %34 None
1590 OpBranch %32
1591 %32 = OpLabel
1592 OpSelectionMerge %34 None
1593 OpBranchConditional %false %46 %34
1594 %46 = OpLabel
1595 OpLoopMerge %51 %52 None
1596 OpBranch %53
1597 %53 = OpLabel
1598 OpBranchConditional %false %50 %51
1599 %50 = OpLabel
1600 OpReturnValue %false
1601 %52 = OpLabel
1602 OpBranch %46
1603 %51 = OpLabel
1604 OpBranch %34
1605 %34 = OpLabel
1606 %64 = OpUndef %bool
1607 OpBranchConditional %false %31 %33
1608 %33 = OpLabel
1609 OpBranch %28
1610 %28 = OpLabel
1611 %60 = OpCopyObject %bool %64
1612 OpBranch %17
1613 %17 = OpLabel
1614 OpReturnValue %false
1615 OpFunctionEnd
1616 )";
1617
1618 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1619 SinglePassRunAndMatch<MergeReturnPass>(before, false);
1620 }
1621
1622 TEST_F(MergeReturnPassTest, SerialLoopsUpdateBlockMapping) {
1623 // #2455: This test case triggers phi insertions that use previously inserted
1624 // phis. Without the fix, it fails to validate.
1625 const std::string spirv = R"(
1626 ; CHECK: OpSelectionMerge
1627 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
1628 ; CHECK-NEXT: [[def_bb1]] = OpLabel
1629 ; CHECK: OpLoopMerge
1630 ; CHECK: OpLoopMerge
1631 ; CHECK: OpLoopMerge [[merge:%\w+]]
1632 ; CHECK: [[def:%\w+]] = OpFOrdLessThan
1633 ; CHECK: [[merge]] = OpLabel
1634 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi {{%\w+}} {{%\w+}} {{%\w+}} [[def]]
1635 ; CHECK: OpLoopMerge [[merge:%\w+]] [[cont:%\w+]]
1636 ; CHECK: [[cont]] = OpLabel
1637 ; CHECK-NEXT: OpBranchConditional [[phi]]
1638 ; CHECK-NOT: [[def]]
1639 OpCapability Shader
1640 %1 = OpExtInstImport "GLSL.std.450"
1641 OpMemoryModel Logical GLSL450
1642 OpEntryPoint Fragment %4 "main" %53
1643 OpExecutionMode %4 OriginUpperLeft
1644 OpSource ESSL 310
1645 OpDecorate %20 RelaxedPrecision
1646 OpDecorate %27 RelaxedPrecision
1647 OpDecorate %53 BuiltIn FragCoord
1648 %2 = OpTypeVoid
1649 %3 = OpTypeFunction %2
1650 %6 = OpTypeFloat 32
1651 %7 = OpTypeVector %6 3
1652 %8 = OpTypeFunction %7
1653 %11 = OpTypeBool
1654 %12 = OpConstantFalse %11
1655 %15 = OpConstant %6 1
1656 %16 = OpConstantComposite %7 %15 %15 %15
1657 %18 = OpTypeInt 32 1
1658 %19 = OpTypePointer Function %18
1659 %21 = OpConstant %18 1
1660 %28 = OpConstant %18 0
1661 %31 = OpTypePointer Function %11
1662 %33 = OpConstantTrue %11
1663 %51 = OpTypeVector %6 4
1664 %52 = OpTypePointer Input %51
1665 %53 = OpVariable %52 Input
1666 %54 = OpTypeInt 32 0
1667 %55 = OpConstant %54 0
1668 %56 = OpTypePointer Input %6
1669 %59 = OpConstant %6 0
1670 %76 = OpUndef %18
1671 %77 = OpUndef %11
1672 %78 = OpUndef %6
1673 %4 = OpFunction %2 None %3
1674 %5 = OpLabel
1675 %75 = OpFunctionCall %7 %9
1676 OpReturn
1677 OpFunctionEnd
1678 %9 = OpFunction %7 None %8
1679 %10 = OpLabel
1680 %20 = OpVariable %19 Function
1681 OpBranch %14
1682 %14 = OpLabel
1683 OpBranch %22
1684 %22 = OpLabel
1685 %27 = OpLoad %18 %20
1686 OpLoopMerge %24 %25 None
1687 OpBranch %24
1688 %25 = OpLabel
1689 OpBranch %22
1690 %24 = OpLabel
1691 OpBranch %34
1692 %34 = OpLabel
1693 OpLoopMerge %36 %40 None
1694 OpBranch %35
1695 %35 = OpLabel
1696 OpBranchConditional %77 %39 %40
1697 %39 = OpLabel
1698 OpReturnValue %16
1699 %40 = OpLabel
1700 OpBranchConditional %12 %34 %36
1701 %36 = OpLabel
1702 OpBranch %43
1703 %43 = OpLabel
1704 OpLoopMerge %45 %49 None
1705 OpBranch %44
1706 %44 = OpLabel
1707 OpBranchConditional %77 %48 %49
1708 %48 = OpLabel
1709 OpReturnValue %16
1710 %49 = OpLabel
1711 %60 = OpFOrdLessThan %11 %15 %59
1712 OpBranchConditional %12 %43 %45
1713 %45 = OpLabel
1714 OpBranch %62
1715 %62 = OpLabel
1716 OpLoopMerge %64 %68 None
1717 OpBranch %63
1718 %63 = OpLabel
1719 OpBranchConditional %77 %67 %68
1720 %67 = OpLabel
1721 OpReturnValue %16
1722 %68 = OpLabel
1723 OpBranchConditional %60 %62 %64
1724 %64 = OpLabel
1725 OpReturnValue %16
1726 OpFunctionEnd
1727 )";
1728
1729 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1730 SinglePassRunAndMatch<MergeReturnPass>(spirv, true);
1731 }
1732
1733 TEST_F(MergeReturnPassTest, InnerLoopMergeIsOuterLoopContinue) {
1734 const std::string before =
1735 R"(
1736 ; CHECK: OpSelectionMerge
1737 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
1738 ; CHECK-NEXT: [[def_bb1]] = OpLabel
1739 ; CHECK-NEXT: OpBranch [[outer_loop_header:%\w+]]
1740 ; CHECK: [[outer_loop_header]] = OpLabel
1741 ; CHECK-NEXT: OpLoopMerge [[outer_loop_merge:%\w+]] [[outer_loop_continue:%\w+]] None
1742 ; CHECK: [[outer_loop_continue]] = OpLabel
1743 ; CHECK-NEXT: OpBranch [[outer_loop_header]]
1744 OpCapability Shader
1745 %1 = OpExtInstImport "GLSL.std.450"
1746 OpMemoryModel Logical GLSL450
1747 OpEntryPoint Fragment %2 "main"
1748 OpExecutionMode %2 OriginUpperLeft
1749 OpSource ESSL 310
1750 %void = OpTypeVoid
1751 %4 = OpTypeFunction %void
1752 %bool = OpTypeBool
1753 %6 = OpTypeFunction %bool
1754 %true = OpConstantTrue %bool
1755 %2 = OpFunction %void None %4
1756 %8 = OpLabel
1757 %9 = OpFunctionCall %bool %10
1758 OpReturn
1759 OpFunctionEnd
1760 %10 = OpFunction %bool None %6
1761 %11 = OpLabel
1762 OpBranch %12
1763 %12 = OpLabel
1764 OpLoopMerge %13 %14 None
1765 OpBranchConditional %true %15 %13
1766 %15 = OpLabel
1767 OpLoopMerge %14 %16 None
1768 OpBranchConditional %true %17 %14
1769 %17 = OpLabel
1770 OpReturnValue %true
1771 %16 = OpLabel
1772 OpBranch %15
1773 %14 = OpLabel
1774 OpBranch %12
1775 %13 = OpLabel
1776 OpReturnValue %true
1777 OpFunctionEnd
1778 )";
1779
1780 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1781 SinglePassRunAndMatch<MergeReturnPass>(before, false);
1782 }
1783
1784 TEST_F(MergeReturnPassTest, BreakFromLoopUseNoLongerDominated) {
1785 const std::string spirv = R"(
1786 ; CHECK: [[undef:%\w+]] = OpUndef
1787 ; CHECK: OpSelectionMerge
1788 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
1789 ; CHECK-NEXT: [[def_bb1]] = OpLabel
1790 ; CHECK: OpLoopMerge [[merge:%\w+]] [[cont:%\w+]]
1791 ; CHECK-NEXT: OpBranch [[body:%\w+]]
1792 ; CHECK: [[body]] = OpLabel
1793 ; CHECK-NEXT: OpSelectionMerge [[non_ret:%\w+]]
1794 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[ret:%\w+]] [[non_ret]]
1795 ; CHECK: [[ret]] = OpLabel
1796 ; CHECK-NEXT: OpStore
1797 ; CHECK-NEXT: OpBranch [[merge]]
1798 ; CHECK: [[non_ret]] = OpLabel
1799 ; CHECK-NEXT: [[def:%\w+]] = OpLogicalNot
1800 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[break:%\w+]] [[cont]]
1801 ; CHECK: [[break]] = OpLabel
1802 ; CHECK-NEXT: OpBranch [[merge]]
1803 ; CHECK: [[cont]] = OpLabel
1804 ; CHECK-NEXT: OpBranchConditional {{%\w+}} {{%\w+}} [[merge]]
1805 ; CHECK: [[merge]] = OpLabel
1806 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi {{%\w+}} [[undef]] [[ret]] [[def]] [[break]] [[def]] [[cont]]
1807 ; CHECK: OpLogicalNot {{%\w+}} [[phi]]
1808 OpCapability Shader
1809 OpMemoryModel Logical GLSL450
1810 OpEntryPoint GLCompute %func "func"
1811 OpExecutionMode %func LocalSize 1 1 1
1812 %void = OpTypeVoid
1813 %void_fn = OpTypeFunction %void
1814 %bool = OpTypeBool
1815 %true = OpConstantTrue %bool
1816 %func = OpFunction %void None %void_fn
1817 %1 = OpLabel
1818 OpBranch %2
1819 %2 = OpLabel
1820 OpLoopMerge %8 %7 None
1821 OpBranch %3
1822 %3 = OpLabel
1823 OpSelectionMerge %5 None
1824 OpBranchConditional %true %4 %5
1825 %4 = OpLabel
1826 OpReturn
1827 %5 = OpLabel
1828 %def = OpLogicalNot %bool %true
1829 OpBranchConditional %true %6 %7
1830 %6 = OpLabel
1831 OpBranch %8
1832 %7 = OpLabel
1833 OpBranchConditional %true %2 %8
1834 %8 = OpLabel
1835 OpBranch %9
1836 %9 = OpLabel
1837 %use = OpLogicalNot %bool %def
1838 OpReturn
1839 OpFunctionEnd
1840 )";
1841
1842 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1843 SinglePassRunAndMatch<MergeReturnPass>(spirv, true);
1844 }
1845
1846 TEST_F(MergeReturnPassTest, TwoBreaksFromLoopUsesNoLongerDominated) {
1847 const std::string spirv = R"(
1848 ; CHECK: [[undef:%\w+]] = OpUndef
1849 ; CHECK: OpSelectionMerge
1850 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
1851 ; CHECK-NEXT: [[def_bb1]] = OpLabel
1852 ; CHECK: OpLoopMerge [[merge:%\w+]] [[cont:%\w+]]
1853 ; CHECK-NEXT: OpBranch [[body:%\w+]]
1854 ; CHECK: [[body]] = OpLabel
1855 ; CHECK-NEXT: OpSelectionMerge [[body2:%\w+]]
1856 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[ret1:%\w+]] [[body2]]
1857 ; CHECK: [[ret1]] = OpLabel
1858 ; CHECK-NEXT: OpStore
1859 ; CHECK-NEXT: OpBranch [[merge]]
1860 ; CHECK: [[body2]] = OpLabel
1861 ; CHECK-NEXT: [[def1:%\w+]] = OpLogicalNot
1862 ; CHECK-NEXT: OpSelectionMerge [[body3:%\w+]]
1863 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[ret2:%\w+]] [[body3:%\w+]]
1864 ; CHECK: [[ret2]] = OpLabel
1865 ; CHECK-NEXT: OpStore
1866 ; CHECK-NEXT: OpBranch [[merge]]
1867 ; CHECK: [[body3]] = OpLabel
1868 ; CHECK-NEXT: [[def2:%\w+]] = OpLogicalAnd
1869 ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[break:%\w+]] [[cont]]
1870 ; CHECK: [[break]] = OpLabel
1871 ; CHECK-NEXT: OpBranch [[merge]]
1872 ; CHECK: [[cont]] = OpLabel
1873 ; CHECK-NEXT: OpBranchConditional {{%\w+}} {{%\w+}} [[merge]]
1874 ; CHECK: [[merge]] = OpLabel
1875 ; CHECK-NEXT: [[phi1:%\w+]] = OpPhi {{%\w+}} [[undef]] [[ret1]] [[undef]] [[ret2]] [[def1]] [[break]] [[def1]] [[cont]]
1876 ; CHECK-NEXT: [[phi2:%\w+]] = OpPhi {{%\w+}} [[undef]] [[ret1]] [[undef]] [[ret2]] [[def2]] [[break]] [[def2]] [[cont]]
1877 ; CHECK: OpLogicalNot {{%\w+}} [[phi1]]
1878 ; CHECK: OpLogicalAnd {{%\w+}} [[phi2]]
1879 OpCapability Shader
1880 OpMemoryModel Logical GLSL450
1881 OpEntryPoint GLCompute %func "func"
1882 OpExecutionMode %func LocalSize 1 1 1
1883 %void = OpTypeVoid
1884 %void_fn = OpTypeFunction %void
1885 %bool = OpTypeBool
1886 %true = OpConstantTrue %bool
1887 %func = OpFunction %void None %void_fn
1888 %1 = OpLabel
1889 OpBranch %2
1890 %2 = OpLabel
1891 OpLoopMerge %10 %9 None
1892 OpBranch %3
1893 %3 = OpLabel
1894 OpSelectionMerge %5 None
1895 OpBranchConditional %true %4 %5
1896 %4 = OpLabel
1897 OpReturn
1898 %5 = OpLabel
1899 %def1 = OpLogicalNot %bool %true
1900 OpSelectionMerge %7 None
1901 OpBranchConditional %true %6 %7
1902 %6 = OpLabel
1903 OpReturn
1904 %7 = OpLabel
1905 %def2 = OpLogicalAnd %bool %true %true
1906 OpBranchConditional %true %8 %9
1907 %8 = OpLabel
1908 OpBranch %10
1909 %9 = OpLabel
1910 OpBranchConditional %true %2 %10
1911 %10 = OpLabel
1912 OpBranch %11
1913 %11 = OpLabel
1914 %use1 = OpLogicalNot %bool %def1
1915 %use2 = OpLogicalAnd %bool %def2 %true
1916 OpReturn
1917 OpFunctionEnd
1918 )";
1919
1920 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1921 SinglePassRunAndMatch<MergeReturnPass>(spirv, true);
1922 }
1923
1924 TEST_F(MergeReturnPassTest, PredicateBreakBlock) {
1925 const std::string spirv = R"(
1926 ; IDs are being preserved so we can rely on basic block labels.
1927 ; CHECK: [[undef:%\w+]] = OpUndef
1928 ; CHECK: [[undef:%\w+]] = OpUndef
1929 ; CHECK: %13 = OpLabel
1930 ; CHECK-NEXT: [[def:%\w+]] = OpLogicalNot
1931 ; CHECK: %8 = OpLabel
1932 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi {{%\w+}} [[undef]] {{%\w+}} [[undef]] {{%\w+}} [[def]] %13 [[undef]] {{%\w+}}
1933 ; CHECK: OpLogicalAnd {{%\w+}} [[phi]]
1934 OpCapability Shader
1935 OpMemoryModel Logical GLSL450
1936 OpEntryPoint GLCompute %1 "func"
1937 OpExecutionMode %1 LocalSize 1 1 1
1938 %void = OpTypeVoid
1939 %3 = OpTypeFunction %void
1940 %bool = OpTypeBool
1941 %true = OpUndef %bool
1942 %1 = OpFunction %void None %3
1943 %6 = OpLabel
1944 OpBranch %7
1945 %7 = OpLabel
1946 OpLoopMerge %8 %9 None
1947 OpBranch %10
1948 %10 = OpLabel
1949 OpSelectionMerge %11 None
1950 OpBranchConditional %true %12 %13
1951 %12 = OpLabel
1952 OpLoopMerge %14 %15 None
1953 OpBranch %16
1954 %16 = OpLabel
1955 OpReturn
1956 %15 = OpLabel
1957 OpBranch %12
1958 %14 = OpLabel
1959 OpUnreachable
1960 %13 = OpLabel
1961 %17 = OpLogicalNot %bool %true
1962 OpBranch %8
1963 %11 = OpLabel
1964 OpUnreachable
1965 %9 = OpLabel
1966 OpBranch %7
1967 %8 = OpLabel
1968 OpBranch %18
1969 %18 = OpLabel
1970 %19 = OpLogicalAnd %bool %17 %true
1971 OpReturn
1972 OpFunctionEnd
1973 )";
1974
1975 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1976 SinglePassRunAndMatch<MergeReturnPass>(spirv, true);
1977 }
1978
1979 TEST_F(MergeReturnPassTest, SingleReturnInLoop) {
1980 const std::string predefs =
1981 R"(OpCapability Shader
1982 %1 = OpExtInstImport "GLSL.std.450"
1983 OpMemoryModel Logical GLSL450
1984 OpEntryPoint Fragment %main "main"
1985 OpExecutionMode %main OriginUpperLeft
1986 OpSource ESSL 310
1987 %void = OpTypeVoid
1988 %7 = OpTypeFunction %void
1989 %float = OpTypeFloat 32
1990 %9 = OpTypeFunction %float
1991 %float_1 = OpConstant %float 1
1992 )";
1993
1994 const std::string caller =
1995 R"(
1996 ; CHECK: OpFunction
1997 ; CHECK: OpFunctionEnd
1998 %main = OpFunction %void None %7
1999 %22 = OpLabel
2000 %30 = OpFunctionCall %float %f_
2001 OpReturn
2002 OpFunctionEnd
2003 )";
2004
2005 const std::string callee =
2006 R"(
2007 ; CHECK: OpFunction
2008 ; CHECK: OpLoopMerge [[merge:%\w+]]
2009 ; CHECK: [[merge]] = OpLabel
2010 ; CHECK: OpReturnValue
2011 ; CHECK-NEXT: OpFunctionEnd
2012 %f_ = OpFunction %float None %9
2013 %33 = OpLabel
2014 OpBranch %34
2015 %34 = OpLabel
2016 OpLoopMerge %35 %36 None
2017 OpBranch %37
2018 %37 = OpLabel
2019 OpReturnValue %float_1
2020 %36 = OpLabel
2021 OpBranch %34
2022 %35 = OpLabel
2023 OpUnreachable
2024 OpFunctionEnd
2025 )";
2026
2027 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2028 SinglePassRunAndMatch<MergeReturnPass>(predefs + caller + callee, true);
2029 }
2030
2031 TEST_F(MergeReturnPassTest, MergeToMergeBranch) {
2032 const std::string text =
2033 R"(
2034 ; CHECK: [[new_undef:%\w+]] = OpUndef %uint
2035 ; CHECK: OpSelectionMerge
2036 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
2037 ; CHECK-NEXT: [[def_bb1]] = OpLabel
2038 ; CHECK: OpLoopMerge [[merge1:%\w+]]
2039 ; CHECK: OpLoopMerge [[merge2:%\w+]]
2040 ; CHECK: [[merge1]] = OpLabel
2041 ; CHECK-NEXT: OpPhi %uint [[new_undef]] [[merge2]]
2042 OpCapability Shader
2043 %1 = OpExtInstImport "GLSL.std.450"
2044 OpMemoryModel Logical GLSL450
2045 OpEntryPoint GLCompute %2 "main"
2046 OpExecutionMode %2 LocalSize 100 1 1
2047 OpSource ESSL 310
2048 %void = OpTypeVoid
2049 %4 = OpTypeFunction %void
2050 %uint = OpTypeInt 32 0
2051 %uint_1 = OpConstant %uint 1
2052 %bool = OpTypeBool
2053 %false = OpConstantFalse %bool
2054 %uint_0 = OpConstant %uint 0
2055 %int = OpTypeInt 32 1
2056 %int_0 = OpConstant %int 0
2057 %int_1 = OpConstant %int 1
2058 %13 = OpUndef %bool
2059 %2 = OpFunction %void None %4
2060 %14 = OpLabel
2061 OpBranch %15
2062 %15 = OpLabel
2063 OpLoopMerge %16 %17 None
2064 OpBranch %18
2065 %18 = OpLabel
2066 OpLoopMerge %19 %20 None
2067 OpBranchConditional %13 %21 %19
2068 %21 = OpLabel
2069 OpReturn
2070 %20 = OpLabel
2071 OpBranch %18
2072 %19 = OpLabel
2073 %22 = OpUndef %uint
2074 OpBranch %23
2075 %23 = OpLabel
2076 OpBranch %16
2077 %17 = OpLabel
2078 OpBranch %15
2079 %16 = OpLabel
2080 %24 = OpCopyObject %uint %22
2081 OpReturn
2082 OpFunctionEnd
2083 )";
2084
2085 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2086 SinglePassRunAndMatch<MergeReturnPass>(text, true);
2087 }
2088
2089 TEST_F(MergeReturnPassTest, PhiInSecondMerge) {
2090 // Add and use a phi in the second merge block from the return.
2091 const std::string text =
2092 R"(
2093 ; CHECK: OpSelectionMerge
2094 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
2095 ; CHECK-NEXT: [[def_bb1]] = OpLabel
2096 ; CHECK: OpLoopMerge [[merge_bb:%\w+]] [[continue_bb:%\w+]]
2097 ; CHECK: [[continue_bb]] = OpLabel
2098 ; CHECK-NEXT: [[val:%\w+]] = OpUndef %float
2099 ; CHECK: [[merge_bb]] = OpLabel
2100 ; CHECK-NEXT: [[phi:%\w+]] = OpPhi %float {{%\w+}} {{%\w+}} [[val]] [[continue_bb]]
2101 ; CHECK-NOT: OpLabel
2102 ; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} [[old_merge:%\w+]]
2103 ; CHECK: [[old_merge]] = OpLabel
2104 ; CHECK-NEXT: OpConvertFToS %int [[phi]]
2105 OpCapability Shader
2106 %1 = OpExtInstImport "GLSL.std.450"
2107 OpMemoryModel Logical GLSL450
2108 OpEntryPoint Fragment %2 "main"
2109 OpExecutionMode %2 OriginUpperLeft
2110 OpSource ESSL 310
2111 %void = OpTypeVoid
2112 %4 = OpTypeFunction %void
2113 %int = OpTypeInt 32 1
2114 %float = OpTypeFloat 32
2115 %bool = OpTypeBool
2116 %8 = OpUndef %bool
2117 %2 = OpFunction %void None %4
2118 %9 = OpLabel
2119 OpBranch %10
2120 %10 = OpLabel
2121 OpLoopMerge %11 %12 None
2122 OpBranch %13
2123 %13 = OpLabel
2124 OpLoopMerge %18 %14 None
2125 OpBranchConditional %8 %15 %18
2126 %15 = OpLabel
2127 OpReturn
2128 %14 = OpLabel
2129 OpBranch %13
2130 %18 = OpLabel
2131 OpBranch %12
2132 %12 = OpLabel
2133 %16 = OpUndef %float
2134 OpBranchConditional %8 %10 %11
2135 %11 = OpLabel
2136 %17 = OpConvertFToS %int %16
2137 OpReturn
2138 OpFunctionEnd
2139 )";
2140
2141 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2142 SinglePassRunAndMatch<MergeReturnPass>(text, true);
2143 }
2144
2145 TEST_F(MergeReturnPassTest, ReturnsInSwitch) {
2146 // Cannot branch directly to single case switch merge block from original
2147 // switch. Must branch to merge block of original switch and then do
2148 // predicated branch to merge block of single case switch.
2149 const std::string text =
2150 R"(
2151 ; CHECK: OpSelectionMerge [[single_case_switch_merge_bb:%\w+]]
2152 ; CHECK-NEXT: OpSwitch {{%\w+}} [[def_bb1:%\w+]]
2153 ; CHECK-NEXT: [[def_bb1]] = OpLabel
2154 ; CHECK: OpSelectionMerge
2155 ; CHECK-NEXT: OpSwitch {{%\w+}} [[inner_merge_bb:%\w+]] 0 {{%\w+}} 1 {{%\w+}}
2156 ; CHECK: OpBranch [[inner_merge_bb]]
2157 ; CHECK: OpBranch [[inner_merge_bb]]
2158 ; CHECK-NEXT: [[inner_merge_bb]] = OpLabel
2159 ; CHECK: OpBranchConditional {{%\w+}} [[single_case_switch_merge_bb]] {{%\w+}}
2160 OpCapability Shader
2161 %1 = OpExtInstImport "GLSL.std.450"
2162 OpMemoryModel Logical GLSL450
2163 OpEntryPoint Fragment %PSMain "PSMain" %_entryPointOutput_color
2164 OpExecutionMode %PSMain OriginUpperLeft
2165 OpSource HLSL 500
2166 OpMemberDecorate %cb 0 Offset 0
2167 OpMemberDecorate %cb 1 Offset 16
2168 OpMemberDecorate %cb 2 Offset 32
2169 OpMemberDecorate %cb 3 Offset 48
2170 OpDecorate %cb Block
2171 OpDecorate %_ DescriptorSet 0
2172 OpDecorate %_ Binding 0
2173 OpDecorate %_entryPointOutput_color Location 0
2174 %void = OpTypeVoid
2175 %3 = OpTypeFunction %void
2176 %float = OpTypeFloat 32
2177 %v4float = OpTypeVector %float 4
2178 %8 = OpTypeFunction %v4float
2179 %int = OpTypeInt 32 1
2180 %cb = OpTypeStruct %v4float %v4float %v4float %int
2181 %_ptr_Uniform_cb = OpTypePointer Uniform %cb
2182 %_ = OpVariable %_ptr_Uniform_cb Uniform
2183 %int_3 = OpConstant %int 3
2184 %_ptr_Uniform_int = OpTypePointer Uniform %int
2185 %int_0 = OpConstant %int 0
2186 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
2187 %int_1 = OpConstant %int 1
2188 %int_2 = OpConstant %int 2
2189 %float_0 = OpConstant %float 0
2190 %float_1 = OpConstant %float 1
2191 %45 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_1
2192 %_ptr_Function_v4float = OpTypePointer Function %v4float
2193 %_ptr_Output_v4float = OpTypePointer Output %v4float
2194 %_entryPointOutput_color = OpVariable %_ptr_Output_v4float Output
2195 %PSMain = OpFunction %void None %3
2196 %5 = OpLabel
2197 %50 = OpFunctionCall %v4float %BlendValue_
2198 OpStore %_entryPointOutput_color %50
2199 OpReturn
2200 OpFunctionEnd
2201 %BlendValue_ = OpFunction %v4float None %8
2202 %10 = OpLabel
2203 %21 = OpAccessChain %_ptr_Uniform_int %_ %int_3
2204 %22 = OpLoad %int %21
2205 OpSelectionMerge %25 None
2206 OpSwitch %22 %25 0 %23 1 %24
2207 %23 = OpLabel
2208 %28 = OpAccessChain %_ptr_Uniform_v4float %_ %int_0
2209 %29 = OpLoad %v4float %28
2210 OpReturnValue %29
2211 %24 = OpLabel
2212 %31 = OpAccessChain %_ptr_Uniform_v4float %_ %int_0
2213 %32 = OpLoad %v4float %31
2214 %34 = OpAccessChain %_ptr_Uniform_v4float %_ %int_1
2215 %35 = OpLoad %v4float %34
2216 %37 = OpAccessChain %_ptr_Uniform_v4float %_ %int_2
2217 %38 = OpLoad %v4float %37
2218 %39 = OpFMul %v4float %35 %38
2219 %40 = OpFAdd %v4float %32 %39
2220 OpReturnValue %40
2221 %25 = OpLabel
2222 OpReturnValue %45
2223 OpFunctionEnd
2224 )";
2225
2226 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2227 SinglePassRunAndMatch<MergeReturnPass>(text, true);
2228 }
2229
2230 TEST_F(MergeReturnPassTest, UnreachableMergeAndContinue) {
2231 // Make sure that the pass can handle a single block that is both a merge and
2232 // a continue. Note that this is invalid SPIR-V.
2233 const std::string text =
2234 R"(
2235 OpCapability Shader
2236 %1 = OpExtInstImport "GLSL.std.450"
2237 OpMemoryModel Logical GLSL450
2238 OpEntryPoint Fragment %2 "main"
2239 OpExecutionMode %2 OriginUpperLeft
2240 OpSource ESSL 310
2241 %void = OpTypeVoid
2242 %4 = OpTypeFunction %void
2243 %bool = OpTypeBool
2244 %true = OpConstantTrue %bool
2245 %2 = OpFunction %void None %4
2246 %7 = OpLabel
2247 OpBranch %8
2248 %8 = OpLabel
2249 OpLoopMerge %9 %10 None
2250 OpBranch %11
2251 %11 = OpLabel
2252 OpSelectionMerge %10 None
2253 OpBranchConditional %true %12 %13
2254 %12 = OpLabel
2255 OpReturn
2256 %13 = OpLabel
2257 OpReturn
2258 %10 = OpLabel
2259 OpBranch %8
2260 %9 = OpLabel
2261 OpUnreachable
2262 OpFunctionEnd
2263 )";
2264
2265 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2266 auto result = SinglePassRunAndDisassemble<MergeReturnPass>(text, true, false);
2267
2268 // Not looking for any particular output. Other tests do that.
2269 // Just want to make sure the check for unreachable blocks does not emit an
2270 // error.
2271 EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
2272 }
2273
2274 TEST_F(MergeReturnPassTest, SingleReturnInMiddle) {
2275 const std::string before =
2276 R"(
2277 ; CHECK: OpFunction
2278 ; CHECK: OpReturn
2279 ; CHECK-NEXT: OpFunctionEnd
2280 OpCapability Shader
2281 OpMemoryModel Logical GLSL450
2282 OpEntryPoint Vertex %main "main"
2283 OpSource GLSL 450
2284 OpName %main "main"
2285 OpName %foo_ "foo("
2286 %void = OpTypeVoid
2287 %4 = OpTypeFunction %void
2288 %bool = OpTypeBool
2289 %true = OpConstantTrue %bool
2290 %foo_ = OpFunction %void None %4
2291 %7 = OpLabel
2292 OpSelectionMerge %8 None
2293 OpBranchConditional %true %9 %8
2294 %8 = OpLabel
2295 OpReturn
2296 %9 = OpLabel
2297 OpBranch %8
2298 OpFunctionEnd
2299 %main = OpFunction %void None %4
2300 %10 = OpLabel
2301 %11 = OpFunctionCall %void %foo_
2302 OpReturn
2303 OpFunctionEnd
2304 )";
2305
2306 SinglePassRunAndMatch<MergeReturnPass>(before, false);
2307 }
2308
2309 TEST_F(MergeReturnPassTest, PhiWithTooManyEntries) {
2310 // Check that the OpPhi node has the correct number of entries. This is
2311 // checked by doing validation with the match.
2312 const std::string before =
2313 R"(
2314 ; CHECK: OpLoopMerge [[merge:%\w+]]
2315 ; CHECK: [[merge]] = OpLabel
2316 ; CHECK-NEXT: {{%\w+}} = OpPhi %int {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}} {{%\w+}}
2317 OpCapability Shader
2318 %1 = OpExtInstImport "GLSL.std.450"
2319 OpMemoryModel Logical GLSL450
2320 OpEntryPoint Fragment %2 "main"
2321 OpExecutionMode %2 OriginUpperLeft
2322 %void = OpTypeVoid
2323 %4 = OpTypeFunction %void
2324 %int = OpTypeInt 32 1
2325 %6 = OpTypeFunction %int
2326 %bool = OpTypeBool
2327 %int_1 = OpConstant %int 1
2328 %false = OpConstantFalse %bool
2329 %2 = OpFunction %void None %4
2330 %10 = OpLabel
2331 %11 = OpFunctionCall %int %12
2332 OpReturn
2333 OpFunctionEnd
2334 %12 = OpFunction %int None %6
2335 %13 = OpLabel
2336 OpBranch %14
2337 %14 = OpLabel
2338 %15 = OpPhi %int %int_1 %13 %16 %17
2339 OpLoopMerge %18 %17 None
2340 OpBranch %19
2341 %19 = OpLabel
2342 %20 = OpUndef %bool
2343 OpBranch %21
2344 %21 = OpLabel
2345 OpLoopMerge %22 %23 None
2346 OpBranch %24
2347 %24 = OpLabel
2348 OpSelectionMerge %25 None
2349 OpBranchConditional %20 %22 %25
2350 %25 = OpLabel
2351 OpReturnValue %int_1
2352 %23 = OpLabel
2353 OpBranch %21
2354 %22 = OpLabel
2355 OpSelectionMerge %26 None
2356 OpBranchConditional %20 %27 %26
2357 %27 = OpLabel
2358 OpBranch %28
2359 %28 = OpLabel
2360 OpLoopMerge %29 %30 None
2361 OpBranch %31
2362 %31 = OpLabel
2363 OpReturnValue %int_1
2364 %30 = OpLabel
2365 OpBranch %28
2366 %29 = OpLabel
2367 OpUnreachable
2368 %26 = OpLabel
2369 OpBranch %17
2370 %17 = OpLabel
2371 %16 = OpPhi %int %15 %26
2372 OpBranchConditional %false %14 %18
2373 %18 = OpLabel
2374 OpReturnValue %16
2375 OpFunctionEnd
2376 )";
2377
2378 SinglePassRunAndMatch<MergeReturnPass>(before, true);
2379 }
2380
2381 TEST_F(MergeReturnPassTest, PointerUsedAfterLoop) {
2382 // Make sure that a Phi instruction is not generated for an id whose type is a
2383 // pointer. It needs to be regenerated.
2384 const std::string before =
2385 R"(
2386 ; CHECK: OpFunction %void
2387 ; CHECK: OpFunction %void
2388 ; CHECK-NEXT: [[param:%\w+]] = OpFunctionParameter %_ptr_Function_v2uint
2389 ; CHECK: OpLoopMerge [[merge_bb:%\w+]]
2390 ; CHECK: [[merge_bb]] = OpLabel
2391 ; CHECK-NEXT: [[ac:%\w+]] = OpAccessChain %_ptr_Function_uint [[param]] %uint_1
2392 ; CHECK: OpStore [[ac]] %uint_1
2393 OpCapability Shader
2394 %1 = OpExtInstImport "GLSL.std.450"
2395 OpMemoryModel Logical GLSL450
2396 OpEntryPoint Fragment %2 "main"
2397 OpExecutionMode %2 OriginUpperLeft
2398 OpSource ESSL 310
2399 %void = OpTypeVoid
2400 %4 = OpTypeFunction %void
2401 %uint = OpTypeInt 32 0
2402 %v2uint = OpTypeVector %uint 2
2403 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
2404 %8 = OpTypeFunction %void %_ptr_Function_v2uint
2405 %uint_1 = OpConstant %uint 1
2406 %bool = OpTypeBool
2407 %_ptr_Function_uint = OpTypePointer Function %uint
2408 %false = OpConstantFalse %bool
2409 %2 = OpFunction %void None %4
2410 %13 = OpLabel
2411 %14 = OpVariable %_ptr_Function_v2uint Function
2412 %15 = OpFunctionCall %void %16 %14
2413 OpReturn
2414 OpFunctionEnd
2415 %16 = OpFunction %void None %8
2416 %17 = OpFunctionParameter %_ptr_Function_v2uint
2417 %18 = OpLabel
2418 OpBranch %19
2419 %19 = OpLabel
2420 OpLoopMerge %20 %21 None
2421 OpBranch %22
2422 %22 = OpLabel
2423 OpSelectionMerge %23 None
2424 OpBranchConditional %false %24 %23
2425 %24 = OpLabel
2426 OpReturn
2427 %23 = OpLabel
2428 OpBranch %21
2429 %21 = OpLabel
2430 %25 = OpAccessChain %_ptr_Function_uint %17 %uint_1
2431 OpBranchConditional %false %19 %20
2432 %20 = OpLabel
2433 OpStore %25 %uint_1
2434 OpReturn
2435 OpFunctionEnd
2436 )";
2437
2438 SinglePassRunAndMatch<MergeReturnPass>(before, true);
2439 }
2440
2441 TEST_F(MergeReturnPassTest, VariablePointerFunctionScope) {
2442 // Make sure that a Phi instruction is not generated for an id whose type is a
2443 // function scope pointer, even if the VariablePointers capability is
2444 // available. It needs to be regenerated.
2445 const std::string before =
2446 R"(
2447 ; CHECK: OpFunction %void
2448 ; CHECK: OpFunction %void
2449 ; CHECK-NEXT: [[param:%\w+]] = OpFunctionParameter %_ptr_Function_v2uint
2450 ; CHECK: OpLoopMerge [[merge_bb:%\w+]]
2451 ; CHECK: [[merge_bb]] = OpLabel
2452 ; CHECK-NEXT: [[ac:%\w+]] = OpAccessChain %_ptr_Function_uint [[param]] %uint_1
2453 ; CHECK: OpStore [[ac]] %uint_1
2454 OpCapability Shader
2455 OpCapability VariablePointers
2456 %1 = OpExtInstImport "GLSL.std.450"
2457 OpMemoryModel Logical GLSL450
2458 OpEntryPoint Fragment %2 "main"
2459 OpExecutionMode %2 OriginUpperLeft
2460 OpSource ESSL 310
2461 %void = OpTypeVoid
2462 %4 = OpTypeFunction %void
2463 %uint = OpTypeInt 32 0
2464 %v2uint = OpTypeVector %uint 2
2465 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
2466 %8 = OpTypeFunction %void %_ptr_Function_v2uint
2467 %uint_1 = OpConstant %uint 1
2468 %bool = OpTypeBool
2469 %_ptr_Function_uint = OpTypePointer Function %uint
2470 %false = OpConstantFalse %bool
2471 %2 = OpFunction %void None %4
2472 %13 = OpLabel
2473 %14 = OpVariable %_ptr_Function_v2uint Function
2474 %15 = OpFunctionCall %void %16 %14
2475 OpReturn
2476 OpFunctionEnd
2477 %16 = OpFunction %void None %8
2478 %17 = OpFunctionParameter %_ptr_Function_v2uint
2479 %18 = OpLabel
2480 OpBranch %19
2481 %19 = OpLabel
2482 OpLoopMerge %20 %21 None
2483 OpBranch %22
2484 %22 = OpLabel
2485 OpSelectionMerge %23 None
2486 OpBranchConditional %false %24 %23
2487 %24 = OpLabel
2488 OpReturn
2489 %23 = OpLabel
2490 OpBranch %21
2491 %21 = OpLabel
2492 %25 = OpAccessChain %_ptr_Function_uint %17 %uint_1
2493 OpBranchConditional %false %19 %20
2494 %20 = OpLabel
2495 OpStore %25 %uint_1
2496 OpReturn
2497 OpFunctionEnd
2498 )";
2499
2500 SinglePassRunAndMatch<MergeReturnPass>(before, true);
2501 }
2502
2503 TEST_F(MergeReturnPassTest, ChainedPointerUsedAfterLoop) {
2504 // Make sure that a Phi instruction is not generated for an id whose type is a
2505 // pointer. It needs to be regenerated.
2506 const std::string before =
2507 R"(
2508 ; CHECK: OpFunction %void
2509 ; CHECK: OpFunction %void
2510 ; CHECK-NEXT: [[param:%\w+]] = OpFunctionParameter %_ptr_Function_
2511 ; CHECK: OpLoopMerge [[merge_bb:%\w+]]
2512 ; CHECK: [[merge_bb]] = OpLabel
2513 ; CHECK-NEXT: [[ac1:%\w+]] = OpAccessChain %_ptr_Function_v2uint [[param]] %uint_1
2514 ; CHECK-NEXT: [[ac2:%\w+]] = OpAccessChain %_ptr_Function_uint [[ac1]] %uint_1
2515 ; CHECK: OpStore [[ac2]] %uint_1
2516 OpCapability Shader
2517 %1 = OpExtInstImport "GLSL.std.450"
2518 OpMemoryModel Logical GLSL450
2519 OpEntryPoint Fragment %2 "main"
2520 OpExecutionMode %2 OriginUpperLeft
2521 OpSource ESSL 310
2522 %void = OpTypeVoid
2523 %4 = OpTypeFunction %void
2524 %uint = OpTypeInt 32 0
2525 %uint_1 = OpConstant %uint 1
2526 %uint_2 = OpConstant %uint 2
2527 %v2uint = OpTypeVector %uint 2
2528 %_arr_v2uint_uint_2 = OpTypeArray %v2uint %uint_2
2529 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
2530 %_ptr_Function__arr_v2uint_uint_2 = OpTypePointer Function %_arr_v2uint_uint_2
2531 %_ptr_Function_uint = OpTypePointer Function %uint
2532 %13 = OpTypeFunction %void %_ptr_Function__arr_v2uint_uint_2
2533 %bool = OpTypeBool
2534 %false = OpConstantFalse %bool
2535 %2 = OpFunction %void None %4
2536 %16 = OpLabel
2537 %17 = OpVariable %_ptr_Function__arr_v2uint_uint_2 Function
2538 %18 = OpFunctionCall %void %19 %17
2539 OpReturn
2540 OpFunctionEnd
2541 %19 = OpFunction %void None %13
2542 %20 = OpFunctionParameter %_ptr_Function__arr_v2uint_uint_2
2543 %21 = OpLabel
2544 OpBranch %22
2545 %22 = OpLabel
2546 OpLoopMerge %23 %24 None
2547 OpBranch %25
2548 %25 = OpLabel
2549 OpSelectionMerge %26 None
2550 OpBranchConditional %false %27 %26
2551 %27 = OpLabel
2552 OpReturn
2553 %26 = OpLabel
2554 OpBranch %24
2555 %24 = OpLabel
2556 %28 = OpAccessChain %_ptr_Function_v2uint %20 %uint_1
2557 %29 = OpAccessChain %_ptr_Function_uint %28 %uint_1
2558 OpBranchConditional %false %22 %23
2559 %23 = OpLabel
2560 OpStore %29 %uint_1
2561 OpReturn
2562 OpFunctionEnd
2563 )";
2564
2565 SinglePassRunAndMatch<MergeReturnPass>(before, true);
2566 }
2567
2568 TEST_F(MergeReturnPassTest, OverflowTest1) {
2569 const std::string text =
2570 R"(
2571 ; CHECK: OpReturn
2572 ; CHECK-NOT: OpReturn
2573 ; CHECK: OpFunctionEnd
2574 OpCapability ClipDistance
2575 OpMemoryModel Logical GLSL450
2576 OpEntryPoint Fragment %2 "main"
2577 OpExecutionMode %2 OriginUpperLeft
2578 %void = OpTypeVoid
2579 %6 = OpTypeFunction %void
2580 %2 = OpFunction %void None %6
2581 %4194303 = OpLabel
2582 OpBranch %18
2583 %18 = OpLabel
2584 OpLoopMerge %19 %20 None
2585 OpBranch %21
2586 %21 = OpLabel
2587 OpReturn
2588 %20 = OpLabel
2589 OpBranch %18
2590 %19 = OpLabel
2591 OpUnreachable
2592 OpFunctionEnd
2593 )";
2594
2595 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2596 auto result =
2597 SinglePassRunToBinary<MergeReturnPass>(text, /* skip_nop = */ true);
2598 EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
2599 }
2600
2601 TEST_F(MergeReturnPassTest, DebugFunctionDefinitionStillInEntryBlock) {
2602 // Make sure that the DebugFunctionDefinition instruction is still in the
2603 // entry block
2604 const std::string text =
2605 R"(
2606 ; CHECK: OpFunction
2607 ; CHECK: OpLabel
2608 ; CHECK: DebugFunctionDefinition
2609 ; CHECK: OpSelectionMerge
2610 ; CHECK: OpCompositeExtract
2611 ; CHECK: OpUGreaterThan
2612 OpCapability Shader
2613 OpExtension "SPV_KHR_non_semantic_info"
2614 %2 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2615 OpMemoryModel Logical GLSL450
2616 OpEntryPoint GLCompute %main "main" %entryPointParam_main %gl_GlobalInvocationID
2617 %1 = OpString "test"
2618 OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
2619 OpDecorate %entryPointParam_main Location 0
2620 %void = OpTypeVoid
2621 %4 = OpExtInst %void %2 DebugSource %1
2622 %uint = OpTypeInt 32 0
2623 %uint_100 = OpConstant %uint 100
2624 %uint_5 = OpConstant %uint 5
2625 %uint_11 = OpConstant %uint 11
2626 %10 = OpExtInst %void %2 DebugCompilationUnit %uint_100 %uint_5 %4 %uint_11
2627 %12 = OpTypeFunction %void
2628 %uint_0 = OpConstant %uint 0
2629 %14 = OpExtInst %void %2 DebugTypeFunction %uint_0 %void
2630 %uint_2 = OpConstant %uint 2
2631 %16 = OpExtInst %void %2 DebugFunction %1 %14 %4 %uint_2 %uint_5 %10 %1 %uint_0 %uint_2
2632 %v3uint = OpTypeVector %uint 3
2633 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
2634 %bool = OpTypeBool
2635 %uint_3 = OpConstant %uint 3
2636 %int = OpTypeInt 32 1
2637 %_ptr_Output_int = OpTypePointer Output %int
2638 %int_1 = OpConstant %int 1
2639 %int_0 = OpConstant %int 0
2640 %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input ; BuiltIn GlobalInvocationId
2641 %entryPointParam_main = OpVariable %_ptr_Output_int Output ; Location 0
2642
2643 ; Function main
2644 %main = OpFunction %void None %12
2645 %13 = OpLabel
2646 %20 = OpExtInst %void %2 DebugScope %16
2647 %29 = OpLoad %v3uint %gl_GlobalInvocationID
2648 %37 = OpCompositeExtract %uint %29 0
2649 %39 = OpUGreaterThan %bool %37 %uint_3
2650 %19 = OpExtInst %void %2 DebugFunctionDefinition %16 %main
2651 OpSelectionMerge %21 None
2652 OpBranchConditional %39 %23 %21
2653 %21 = OpLabel
2654 OpStore %entryPointParam_main %int_1
2655 OpReturn
2656 %23 = OpLabel
2657 OpStore %entryPointParam_main %int_0
2658 OpReturn
2659 OpFunctionEnd
2660 )";
2661
2662 SinglePassRunAndMatch<MergeReturnPass>(text, true);
2663 }
2664
2665 } // namespace
2666 } // namespace opt
2667 } // namespace spvtools