Debian Codegraph @

test/opt/resolve_binding_conflicts_pass_test.cpp

raw
1 // Copyright (c) 2025 Google LLC
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 <iostream>
16 #include <ostream>
17
18 #include "spirv-tools/optimizer.hpp"
19 #include "test/opt/pass_fixture.h"
20 #include "test/opt/pass_utils.h"
21
22 namespace spvtools {
23 namespace opt {
24 namespace {
25
26 struct ResolveBindingConflictsTest : public PassTest<::testing::Test> {
27 virtual void SetUp() override {
28 SetTargetEnv(SPV_ENV_VULKAN_1_1); // allow storage buffer storage class
29 // without extension.
30 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
31 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES |
32 SPV_BINARY_TO_TEXT_OPTION_INDENT |
33 SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
34 }
35 };
36
37 using StringList = std::vector<std::string>;
38 std::string EntryPointDecls(const StringList& names) {
39 std::ostringstream os;
40 for (auto& name : names) {
41 os << " OpEntryPoint GLCompute %" + name + " \"" + name +
42 "\"\n";
43 }
44 for (auto& name : names) {
45 os << " OpExecutionMode %" + name + " LocalSize 1 1 1\n";
46 }
47 for (auto& name : names) {
48 os << " OpName %" + name + " \"" + name + "\"\n";
49 }
50 return os.str();
51 }
52
53 std::string Preamble(const StringList& names = {"main"}) {
54 return R"( OpCapability Shader
55 OpMemoryModel Logical GLSL450
56 )" + EntryPointDecls(names) +
57 R"( OpName %voidfn "voidfn"
58 OpName %s_ty "s_ty"
59 OpName %i_ty "i_ty"
60 OpName %si_ty "si_ty"
61 OpName %p_s_ty "p_s_ty"
62 OpName %p_i_ty "p_i_ty"
63 OpName %p_si_ty "p_si_ty"
64 OpName %st_ty "st_ty"
65 OpName %pu_st_ty "pu_st_ty"
66 OpName %pb_st_ty "pb_st_ty"
67 )";
68 }
69
70 std::string BasicTypes() {
71 return R"( OpDecorate %st_ty Block
72 OpMemberDecorate %st_ty 0 Offset 0
73 %float = OpTypeFloat 32
74 %uint = OpTypeInt 32 0
75 %uint_0 = OpConstant %uint 0
76 %uint_3 = OpConstant %uint 3
77 %void = OpTypeVoid
78 %voidfn = OpTypeFunction %void
79 %s_ty = OpTypeSampler
80 %i_ty = OpTypeImage %float 2D 0 0 0 1 Unknown
81 %si_ty = OpTypeSampledImage %i_ty
82 %p_i_ty = OpTypePointer UniformConstant %i_ty
83 %p_s_ty = OpTypePointer UniformConstant %s_ty
84 %p_si_ty = OpTypePointer UniformConstant %si_ty
85 %st_ty = OpTypeStruct %uint
86 %pu_st_ty = OpTypePointer Uniform %st_ty
87 %pb_st_ty = OpTypePointer StorageBuffer %st_ty
88 )";
89 }
90 std::string NoCheck() { return "; CHECK-NOT: nothing to see"; }
91
92 TEST_F(ResolveBindingConflictsTest, NoBindings_NoChange) {
93 const std::string kTest = Preamble() + BasicTypes() +
94 R"( %main = OpFunction %void None %voidfn
95 %100 = OpLabel
96 OpReturn
97 OpFunctionEnd
98 )";
99 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
100 kTest + NoCheck(), /* do_validation= */ true);
101 EXPECT_EQ(status, Pass::Status::SuccessWithoutChange);
102 EXPECT_EQ(kTest, disasm);
103 }
104
105 TEST_F(ResolveBindingConflictsTest, NoConflict_UnusedVars_NoChange) {
106 const std::string kTest = Preamble() +
107 R"(
108 OpDecorate %100 DescriptorSet 0
109 OpDecorate %100 Binding 0
110 OpDecorate %101 DescriptorSet 0
111 OpDecorate %101 Binding 0
112 OpDecorate %102 DescriptorSet 0
113 OpDecorate %102 Binding 0
114 OpDecorate %103 DescriptorSet 0
115 OpDecorate %103 Binding 0
116 OpDecorate %104 DescriptorSet 0
117 OpDecorate %104 Binding 0
118
119 ; CHECK: OpDecorate %100 DescriptorSet 0
120 ; CHECK: OpDecorate %100 Binding 0
121 ; CHECK: OpDecorate %101 DescriptorSet 0
122 ; CHECK: OpDecorate %101 Binding 0
123 ; CHECK: OpDecorate %102 DescriptorSet 0
124 ; CHECK: OpDecorate %102 Binding 0
125 ; CHECK: OpDecorate %103 DescriptorSet 0
126 ; CHECK: OpDecorate %103 Binding 0
127 ; CHECK: OpDecorate %104 DescriptorSet 0
128 ; CHECK: OpDecorate %104 Binding 0
129
130 )" + BasicTypes() + R"(
131
132 ; Unused variables
133
134 %100 = OpVariable %p_i_ty UniformConstant ; image
135 %101 = OpVariable %p_s_ty UniformConstant ; sampler
136 %102 = OpVariable %pu_st_ty Uniform ; UBO
137 %103 = OpVariable %pb_st_ty StorageBuffer ; SSBO
138 %104 = OpVariable %p_si_ty UniformConstant ; combined sampled image
139
140 %main = OpFunction %void None %voidfn
141 %10 = OpLabel
142 OpReturn
143 OpFunctionEnd
144 )";
145
146 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
147 kTest, /* do_validation= */ true);
148 EXPECT_EQ(status, Pass::Status::SuccessWithoutChange) << disasm;
149 }
150
151 TEST_F(ResolveBindingConflictsTest, NoConflict_UsedVars_NoChange) {
152 const std::string kTest = Preamble() +
153 R"(
154 OpDecorate %100 DescriptorSet 0
155 OpDecorate %100 Binding 0
156 OpDecorate %101 DescriptorSet 0
157 OpDecorate %101 Binding 1
158 OpDecorate %102 DescriptorSet 0
159 OpDecorate %102 Binding 2
160 OpDecorate %103 DescriptorSet 0
161 OpDecorate %103 Binding 3
162 OpDecorate %110 DescriptorSet 1
163 OpDecorate %110 Binding 0
164
165 ; CHECK: OpDecorate %100 DescriptorSet 0
166 ; CHECK: OpDecorate %100 Binding 0
167 ; CHECK: OpDecorate %101 DescriptorSet 0
168 ; CHECK: OpDecorate %101 Binding 1
169 ; CHECK: OpDecorate %102 DescriptorSet 0
170 ; CHECK: OpDecorate %102 Binding 2
171 ; CHECK: OpDecorate %103 DescriptorSet 0
172 ; CHECK: OpDecorate %103 Binding 3
173 ; CHECK: OpDecorate %110 DescriptorSet 1
174 ; CHECK: OpDecorate %110 Binding 0
175
176 )" + BasicTypes() + R"(
177
178 %100 = OpVariable %p_i_ty UniformConstant ; image
179 %101 = OpVariable %p_s_ty UniformConstant ; sampler
180 %102 = OpVariable %pu_st_ty Uniform ; UBO
181 %103 = OpVariable %pb_st_ty StorageBuffer ; SSBO
182 %110 = OpVariable %p_si_ty UniformConstant ; combined sampled image
183
184 %main = OpFunction %void None %voidfn
185 %10 = OpLabel
186 %11 = OpCopyObject %p_i_ty %100
187 %12 = OpCopyObject %p_s_ty %101
188 %13 = OpCopyObject %pu_st_ty %102
189 %14 = OpCopyObject %pb_st_ty %103
190 %15 = OpCopyObject %p_si_ty %110
191 OpReturn
192 OpFunctionEnd
193 )";
194
195 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
196 kTest, /* do_validation= */ true);
197 EXPECT_EQ(status, Pass::Status::SuccessWithoutChange) << disasm;
198 }
199
200 TEST_F(ResolveBindingConflictsTest,
201 OneEntryPoint_SamplerFirstConflict_Resolves) {
202 const std::string kTest = Preamble() +
203 R"(
204 OpDecorate %100 DescriptorSet 0
205 OpDecorate %100 Binding 0
206 OpDecorate %101 DescriptorSet 0
207 OpDecorate %101 Binding 0
208
209 ; The sampler's binding number is incremented, even when listed first.
210 ; CHECK: OpDecorate %100 DescriptorSet 0
211 ; CHECK: OpDecorate %100 Binding 1
212 ; CHECK: OpDecorate %101 DescriptorSet 0
213 ; CHECK: OpDecorate %101 Binding 0
214
215 )" + BasicTypes() + R"(
216
217 %100 = OpVariable %p_s_ty UniformConstant ; sampler listed first
218 %101 = OpVariable %p_i_ty UniformConstant
219
220 %main = OpFunction %void None %voidfn
221 %10 = OpLabel
222 %11 = OpCopyObject %p_s_ty %100
223 %12 = OpCopyObject %p_i_ty %101
224 OpReturn
225 OpFunctionEnd
226 )";
227
228 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
229 kTest, /* do_validation= */ true);
230 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
231 }
232
233 TEST_F(ResolveBindingConflictsTest,
234 OneEntryPoint_SamplerSecondConflict_Resolves) {
235 const std::string kTest = Preamble() +
236 R"(
237 OpDecorate %100 DescriptorSet 0
238 OpDecorate %100 Binding 0
239 OpDecorate %101 DescriptorSet 0
240 OpDecorate %101 Binding 0
241
242 ; The sampler's binding number is incremented, even when listed second.
243 ; CHECK: OpDecorate %100 DescriptorSet 0
244 ; CHECK: OpDecorate %100 Binding 0
245 ; CHECK: OpDecorate %101 DescriptorSet 0
246 ; CHECK: OpDecorate %101 Binding 1
247
248 )" + BasicTypes() + R"(
249
250 %100 = OpVariable %p_i_ty UniformConstant
251 %101 = OpVariable %p_s_ty UniformConstant ; sampler listed second
252
253 %main = OpFunction %void None %voidfn
254 %10 = OpLabel
255 %11 = OpCopyObject %p_i_ty %100
256 %12 = OpCopyObject %p_s_ty %101
257 OpReturn
258 OpFunctionEnd
259 )";
260
261 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
262 kTest, /* do_validation= */ true);
263 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
264 }
265
266 TEST_F(ResolveBindingConflictsTest, OneEntryPoint_Conflict_Ripples) {
267 const std::string kTest = Preamble() +
268 R"(
269 OpDecorate %100 DescriptorSet 0
270 OpDecorate %100 Binding 0
271 OpDecorate %101 DescriptorSet 0
272 OpDecorate %101 Binding 0
273 OpDecorate %102 DescriptorSet 0
274 OpDecorate %102 Binding 1
275 OpDecorate %103 DescriptorSet 0
276 OpDecorate %103 Binding 2
277 OpDecorate %104 DescriptorSet 0
278 OpDecorate %104 Binding 3
279
280 ; The sampler's binding number is incremented, and later
281 ; bindings move out of the way.
282 ; CHECK: OpDecorate %100 DescriptorSet 0
283 ; CHECK: OpDecorate %100 Binding 1
284 ; CHECK: OpDecorate %101 DescriptorSet 0
285 ; CHECK: OpDecorate %101 Binding 0
286 ; CHECK: OpDecorate %102 DescriptorSet 0
287 ; CHECK: OpDecorate %102 Binding 2
288 ; CHECK: OpDecorate %103 DescriptorSet 0
289 ; CHECK: OpDecorate %103 Binding 3
290 ; CHECK: OpDecorate %104 DescriptorSet 0
291 ; CHECK: OpDecorate %104 Binding 4
292
293 )" + BasicTypes() + R"(
294
295 %100 = OpVariable %p_s_ty UniformConstant ; sampler comes first
296 %101 = OpVariable %p_i_ty UniformConstant
297 %102 = OpVariable %pu_st_ty Uniform
298 %103 = OpVariable %pb_st_ty StorageBuffer
299 %104 = OpVariable %p_si_ty UniformConstant
300
301 %main = OpFunction %void None %voidfn
302 %10 = OpLabel
303 %11 = OpCopyObject %p_s_ty %100
304 %12 = OpCopyObject %p_i_ty %101
305 %13 = OpCopyObject %pu_st_ty %102
306 %14 = OpCopyObject %pb_st_ty %103
307 %15 = OpCopyObject %p_si_ty %104
308 OpReturn
309 OpFunctionEnd
310 )";
311
312 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
313 kTest, /* do_validation= */ true);
314 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
315 }
316
317 TEST_F(ResolveBindingConflictsTest,
318 OneEntryPoint_Conflict_RippleStopsAtFirstHole) {
319 const std::string kTest = Preamble() +
320 R"(
321 OpDecorate %100 DescriptorSet 0
322 OpDecorate %100 Binding 0
323 OpDecorate %101 DescriptorSet 0
324 OpDecorate %101 Binding 0
325 OpDecorate %102 DescriptorSet 0
326 OpDecorate %102 Binding 1
327 ; Leave a hole at (0, 2)
328 OpDecorate %103 DescriptorSet 0
329 OpDecorate %103 Binding 3
330 OpDecorate %104 DescriptorSet 0
331 OpDecorate %104 Binding 4
332
333 ; There was a hole at binding 2. The ripple stops there.
334 ; CHECK: OpDecorate %100 DescriptorSet 0
335 ; CHECK: OpDecorate %100 Binding 1
336 ; CHECK: OpDecorate %101 DescriptorSet 0
337 ; CHECK: OpDecorate %101 Binding 0
338 ; CHECK: OpDecorate %102 DescriptorSet 0
339 ; CHECK: OpDecorate %102 Binding 2
340 ; CHECK: OpDecorate %103 DescriptorSet 0
341 ; CHECK: OpDecorate %103 Binding 3
342 ; CHECK: OpDecorate %104 DescriptorSet 0
343 ; CHECK: OpDecorate %104 Binding 4
344
345 )" + BasicTypes() + R"(
346
347 %100 = OpVariable %p_s_ty UniformConstant ; sampler comes first
348 %101 = OpVariable %p_i_ty UniformConstant
349 %102 = OpVariable %pu_st_ty Uniform
350 %103 = OpVariable %pb_st_ty StorageBuffer
351 %104 = OpVariable %p_si_ty UniformConstant
352
353 %main = OpFunction %void None %voidfn
354 %10 = OpLabel
355 %11 = OpCopyObject %p_s_ty %100
356 %12 = OpCopyObject %p_i_ty %101
357 %13 = OpCopyObject %pu_st_ty %102
358 %14 = OpCopyObject %pb_st_ty %103
359 %15 = OpCopyObject %p_si_ty %104
360 OpReturn
361 OpFunctionEnd
362 )";
363
364 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
365 kTest, /* do_validation= */ true);
366 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
367 }
368
369 TEST_F(ResolveBindingConflictsTest, OneEntryPoint_MultiConflict_Resolves) {
370 const std::string kTest = Preamble() +
371 R"(
372 ; Two conflicts: at Bindings 0, and 1
373 OpDecorate %100 DescriptorSet 0
374 OpDecorate %100 Binding 0
375 OpDecorate %101 DescriptorSet 0
376 OpDecorate %101 Binding 0
377 OpDecorate %102 DescriptorSet 0
378 OpDecorate %102 Binding 1
379 OpDecorate %103 DescriptorSet 0
380 OpDecorate %103 Binding 1
381 OpDecorate %104 DescriptorSet 0
382 OpDecorate %104 Binding 2
383
384 ; CHECK: OpDecorate %100 DescriptorSet 0
385 ; CHECK: OpDecorate %100 Binding 1
386 ; CHECK: OpDecorate %101 DescriptorSet 0
387 ; CHECK: OpDecorate %101 Binding 0
388 ; CHECK: OpDecorate %102 DescriptorSet 0
389 ; CHECK: OpDecorate %102 Binding 2
390 ; CHECK: OpDecorate %103 DescriptorSet 0
391 ; CHECK: OpDecorate %103 Binding 3
392 ; CHECK: OpDecorate %104 DescriptorSet 0
393 ; CHECK: OpDecorate %104 Binding 4
394
395 )" + BasicTypes() + R"(
396
397 %100 = OpVariable %p_s_ty UniformConstant ; sampler first
398 %101 = OpVariable %p_i_ty UniformConstant
399 %102 = OpVariable %p_i_ty UniformConstant
400 %103 = OpVariable %p_s_ty UniformConstant ; sampler second
401 %104 = OpVariable %pu_st_ty Uniform
402
403 %main = OpFunction %void None %voidfn
404 %10 = OpLabel
405 %11 = OpCopyObject %p_s_ty %100
406 %12 = OpCopyObject %p_i_ty %101
407 %13 = OpCopyObject %p_i_ty %102
408 %14 = OpCopyObject %p_s_ty %103
409 %15 = OpCopyObject %pu_st_ty %104
410 OpReturn
411 OpFunctionEnd
412 )";
413
414 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
415 kTest, /* do_validation= */ true);
416 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
417 }
418
419 TEST_F(ResolveBindingConflictsTest,
420 OneEntryPoint_MultiConflict_ComplexCallGraph_Resolves) {
421 // Check that uses are seen even when used at various points in a complex call
422 // graph.
423 const std::string kTest = Preamble() +
424 R"(
425 OpDecorate %100 DescriptorSet 0
426 OpDecorate %100 Binding 0
427 OpDecorate %101 DescriptorSet 0
428 OpDecorate %101 Binding 0
429 OpDecorate %102 DescriptorSet 0
430 OpDecorate %102 Binding 1
431 OpDecorate %103 DescriptorSet 0
432 OpDecorate %103 Binding 1
433 OpDecorate %104 DescriptorSet 0
434 OpDecorate %104 Binding 2
435
436 ; CHECK: OpDecorate %100 DescriptorSet 0
437 ; CHECK: OpDecorate %100 Binding 1
438 ; CHECK: OpDecorate %101 DescriptorSet 0
439 ; CHECK: OpDecorate %101 Binding 0
440 ; CHECK: OpDecorate %102 DescriptorSet 0
441 ; CHECK: OpDecorate %102 Binding 2
442 ; CHECK: OpDecorate %103 DescriptorSet 0
443 ; CHECK: OpDecorate %103 Binding 3
444 ; CHECK: OpDecorate %104 DescriptorSet 0
445 ; CHECK: OpDecorate %104 Binding 4
446
447 )" + BasicTypes() + R"(
448
449 %100 = OpVariable %p_s_ty UniformConstant ; used in %200
450 %101 = OpVariable %p_i_ty UniformConstant ; used in %300, %400
451 %102 = OpVariable %p_i_ty UniformConstant ; used in %500
452 %103 = OpVariable %p_s_ty UniformConstant ; used in %400 twice
453 %104 = OpVariable %pu_st_ty Uniform ; used in %600
454
455 %200 = OpFunction %void None %voidfn
456 %201 = OpLabel
457 %202 = OpCopyObject %p_s_ty %100
458 OpReturn
459 OpFunctionEnd
460
461 %300 = OpFunction %void None %voidfn
462 %301 = OpLabel
463 %302 = OpCopyObject %p_i_ty %101
464 OpReturn
465 OpFunctionEnd
466
467 %400 = OpFunction %void None %voidfn
468 %401 = OpLabel
469 %402 = OpFunctionCall %void %200
470 %403 = OpCopyObject %p_s_ty %103
471 %404 = OpCopyObject %p_i_ty %101
472 %405 = OpCopyObject %p_s_ty %103
473 %406 = OpFunctionCall %void %300
474 OpReturn
475 OpFunctionEnd
476
477 %500 = OpFunction %void None %voidfn
478 %501 = OpLabel
479 %502 = OpFunctionCall %void %400
480 %503 = OpCopyObject %p_i_ty %102
481 %504 = OpFunctionCall %void %300
482 OpReturn
483 OpFunctionEnd
484
485 %600 = OpFunction %void None %voidfn
486 %601 = OpLabel
487 %602 = OpFunctionCall %void %300
488 %603 = OpFunctionCall %void %500
489 %604 = OpCopyObject %pu_st_ty %104
490 OpReturn
491 OpFunctionEnd
492
493 %main = OpFunction %void None %voidfn
494 %1000 = OpLabel
495 %1001 = OpFunctionCall %void %600
496 OpReturn
497 OpFunctionEnd
498 )";
499
500 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
501 kTest, /* do_validation= */ true);
502 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
503 }
504
505 TEST_F(ResolveBindingConflictsTest,
506 MultiEntryPoint_DuplicatConflicts_ResolvesOnlyOnce) {
507 // Before:
508 //
509 // Binding: 0 1
510 // Alpha: %100,%101
511 // Beta: %100,%101
512 //
513 // After:
514 //
515 // Binding: 0 1
516 // Alpha: %101 %100
517 // Beta: %101 %100
518 const std::string kTest = Preamble({"alpha", "beta"}) +
519 R"(
520 OpDecorate %100 DescriptorSet 0
521 OpDecorate %100 Binding 0
522 OpDecorate %101 DescriptorSet 0
523 OpDecorate %101 Binding 0
524
525 ; CHECK: OpDecorate %100 DescriptorSet 0
526 ; CHECK: OpDecorate %100 Binding 1
527 ; CHECK: OpDecorate %101 DescriptorSet 0
528 ; CHECK: OpDecorate %101 Binding 0
529
530 )" + BasicTypes() + R"(
531
532 %100 = OpVariable %p_s_ty UniformConstant
533 %101 = OpVariable %p_i_ty UniformConstant
534
535 %alpha = OpFunction %void None %voidfn
536 %1000 = OpLabel
537 %1001 = OpCopyObject %p_s_ty %100
538 %1002 = OpCopyObject %p_i_ty %101
539 OpReturn
540 OpFunctionEnd
541
542 %beta = OpFunction %void None %voidfn
543 %2000 = OpLabel
544 %2001 = OpCopyObject %p_s_ty %100
545 %2002 = OpCopyObject %p_i_ty %101
546 OpReturn
547 OpFunctionEnd
548 )";
549
550 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
551 kTest, /* do_validation= */ true);
552 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
553 }
554
555 TEST_F(ResolveBindingConflictsTest,
556 MultiEntryPoint_IndependentConflicts_Resolves) {
557 // Before:
558 //
559 // Binding: 0 1
560 // Alpha: %100,%101
561 // Beta: %102,%103
562 //
563 // After:
564 //
565 // Binding: 0 1
566 // Alpha: %101 %100
567 // Beta: %102 %103
568 const std::string kTest = Preamble({"alpha", "beta"}) +
569 R"(
570 OpDecorate %100 DescriptorSet 0
571 OpDecorate %100 Binding 0
572 OpDecorate %101 DescriptorSet 0
573 OpDecorate %101 Binding 0
574 OpDecorate %102 DescriptorSet 0
575 OpDecorate %102 Binding 0
576 OpDecorate %103 DescriptorSet 0
577 OpDecorate %103 Binding 0
578
579 ; CHECK: OpDecorate %100 DescriptorSet 0
580 ; CHECK: OpDecorate %100 Binding 1
581 ; CHECK: OpDecorate %101 DescriptorSet 0
582 ; CHECK: OpDecorate %101 Binding 0
583 ; CHECK: OpDecorate %102 DescriptorSet 0
584 ; CHECK: OpDecorate %102 Binding 0
585 ; CHECK: OpDecorate %103 DescriptorSet 0
586 ; CHECK: OpDecorate %103 Binding 1
587
588 )" + BasicTypes() + R"(
589
590 %100 = OpVariable %p_s_ty UniformConstant
591 %101 = OpVariable %p_i_ty UniformConstant
592 %102 = OpVariable %p_i_ty UniformConstant
593 %103 = OpVariable %p_s_ty UniformConstant
594
595 %alpha = OpFunction %void None %voidfn
596 %1000 = OpLabel
597 %1001 = OpCopyObject %p_s_ty %100
598 %1002 = OpCopyObject %p_i_ty %101
599 OpReturn
600 OpFunctionEnd
601
602 %beta = OpFunction %void None %voidfn
603 %2000 = OpLabel
604 %2001 = OpCopyObject %p_i_ty %102
605 %2002 = OpCopyObject %p_s_ty %103
606 OpReturn
607 OpFunctionEnd
608 )";
609
610 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
611 kTest, /* do_validation= */ true);
612 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
613 }
614
615 TEST_F(ResolveBindingConflictsTest,
616 MultiEntryPoint_SameVarConflictsAcrossMultiEntryPoints_Resolves) {
617 // A sampler variable is bumped, causing potential conflicts in other shaders.
618 //
619 // Before:
620 //
621 // Binding: 0 1 2
622 // Alpha: %100,%101
623 // Beta: %100 %102
624 // Gamma: %100 %103
625 //
626 // After:
627 //
628 // Binding: 0 1 2
629 // Alpha: %101 %100
630 // Beta: %100 %102
631 // Gamma: %100 %103
632 //
633 const std::string kTest = Preamble({"alpha", "beta", "gamma"}) +
634 R"(
635 OpDecorate %100 DescriptorSet 0 ; The sampler
636 OpDecorate %100 Binding 0
637 OpDecorate %101 DescriptorSet 0
638 OpDecorate %101 Binding 0
639 OpDecorate %102 DescriptorSet 0
640 OpDecorate %102 Binding 1
641 OpDecorate %103 DescriptorSet 0
642 OpDecorate %103 Binding 2
643
644 ; bumped once
645 ; CHECK: OpDecorate %100 DescriptorSet 0
646 ; CHECK: OpDecorate %100 Binding 1
647
648 ; CHECK: OpDecorate %101 DescriptorSet 0
649 ; CHECK: OpDecorate %101 Binding 0
650
651 ; pushed back from bump of %100
652 ; CHECK: OpDecorate %102 DescriptorSet 0
653 ; CHECK: OpDecorate %102 Binding 2
654
655 ; does not need to be bumped
656 ; CHECK: OpDecorate %103 DescriptorSet 0
657 ; CHECK: OpDecorate %103 Binding 2
658
659 )" + BasicTypes() + R"(
660
661 %100 = OpVariable %p_s_ty UniformConstant ; used in alpha, beta, gamma
662 %101 = OpVariable %p_i_ty UniformConstant ; used in alpha
663 %102 = OpVariable %pu_st_ty Uniform ; used in beta
664 %103 = OpVariable %pb_st_ty StorageBuffer ; used in gamma
665
666 %alpha = OpFunction %void None %voidfn
667 %1000 = OpLabel
668 %1001 = OpCopyObject %p_s_ty %100
669 %1002 = OpCopyObject %p_i_ty %101
670 OpReturn
671 OpFunctionEnd
672
673 %beta = OpFunction %void None %voidfn
674 %2000 = OpLabel
675 %2001 = OpCopyObject %p_s_ty %100
676 %2002 = OpCopyObject %pu_st_ty %102
677 OpReturn
678 OpFunctionEnd
679
680 %gamma = OpFunction %void None %voidfn
681 %3000 = OpLabel
682 %3001 = OpCopyObject %p_s_ty %100
683 %3002 = OpCopyObject %pb_st_ty %103
684 OpReturn
685 OpFunctionEnd
686 )";
687
688 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
689 kTest, /* do_validation= */ true);
690 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
691 }
692
693 TEST_F(ResolveBindingConflictsTest, MultiEntryPoint_ConflictCascade_Resolves) {
694 // Before:
695 //
696 // Binding: 0 1 2 3
697 // Alpha: %100,%101
698 // Beta: %100 %102
699 // Gamma: %102 %103
700 // Delta: %103 %104
701 //
702 // After:
703 //
704 // Binding: 0 1 2 3 4
705 // Alpha: %101 %100
706 // Beta: %100 %102
707 // Gamma: %102 %103
708 // Delta: %103 %104
709 //
710 const std::string kTest = Preamble({"alpha", "beta", "gamma", "delta"}) +
711 R"(
712 OpDecorate %100 DescriptorSet 0 ; The sampler
713 OpDecorate %100 Binding 0
714 OpDecorate %101 DescriptorSet 0
715 OpDecorate %101 Binding 0
716 OpDecorate %102 DescriptorSet 0
717 OpDecorate %102 Binding 1
718 OpDecorate %103 DescriptorSet 0
719 OpDecorate %103 Binding 2
720 OpDecorate %104 DescriptorSet 0
721 OpDecorate %104 Binding 3
722
723 ; %100 is bumped once:
724 ; CHECK: OpDecorate %100 DescriptorSet 0
725 ; CHECK: OpDecorate %100 Binding 1
726
727 ; CHECK: OpDecorate %101 DescriptorSet 0
728 ; CHECK: OpDecorate %101 Binding 0
729
730 ; pushed back from bump of %100
731 ; CHECK: OpDecorate %102 DescriptorSet 0
732 ; CHECK: OpDecorate %102 Binding 2
733
734 ; pushed back from bump of %102
735 ; CHECK: OpDecorate %103 DescriptorSet 0
736 ; CHECK: OpDecorate %103 Binding 3
737
738 ; pushed back from bump of %103
739 ; CHECK: OpDecorate %104 DescriptorSet 0
740 ; CHECK: OpDecorate %104 Binding 4
741
742 )" + BasicTypes() + R"(
743
744 %100 = OpVariable %p_s_ty UniformConstant ; used in alpha, beta
745 %101 = OpVariable %p_i_ty UniformConstant ; used in alpha
746 %102 = OpVariable %pu_st_ty Uniform ; used in beta, gamma
747 %103 = OpVariable %pb_st_ty StorageBuffer ; used in gamma, delta
748 %104 = OpVariable %p_si_ty UniformConstant ; used delta
749
750 %alpha = OpFunction %void None %voidfn
751 %1000 = OpLabel
752 %1001 = OpCopyObject %p_s_ty %100
753 %1002 = OpCopyObject %p_i_ty %101
754 OpReturn
755 OpFunctionEnd
756
757 %beta = OpFunction %void None %voidfn
758 %2000 = OpLabel
759 %2001 = OpCopyObject %p_s_ty %100
760 %2002 = OpCopyObject %pu_st_ty %102
761 OpReturn
762 OpFunctionEnd
763
764 %gamma = OpFunction %void None %voidfn
765 %3000 = OpLabel
766 %3001 = OpCopyObject %pu_st_ty %102
767 %3002 = OpCopyObject %pb_st_ty %103
768 OpReturn
769 OpFunctionEnd
770
771 %delta = OpFunction %void None %voidfn
772 %4000 = OpLabel
773 %4001 = OpCopyObject %pb_st_ty %103
774 %4002 = OpCopyObject %p_si_ty %104
775 OpReturn
776 OpFunctionEnd
777 )";
778
779 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
780 kTest, /* do_validation= */ true);
781 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
782 }
783
784 TEST_F(ResolveBindingConflictsTest,
785 MultiEntryPoint_ConflictCascade_RevisitEntryPoint) {
786 // Prove that the settling algorithm knows to revisit entry points that
787 // already had all their own conflicts resolved.
788 //
789 // Before:
790 //
791 // Binding: 0 1 2 3 4
792 // Alpha: %100,%101 %103 %104 %105
793 // Beta: %101 %102 %103 %105
794 //
795 // After:
796 //
797 // Binding: 0 1 2 3 4 5
798 // Alpha: %100 %101 %103 %104 %105
799 // Beta: %101 %102 %103 %105
800 const std::string kTest = Preamble({"alpha", "beta"}) +
801 R"(
802 OpDecorate %100 DescriptorSet 0
803 OpDecorate %100 Binding 0
804 OpDecorate %101 DescriptorSet 0 ; the sampler
805 OpDecorate %101 Binding 0
806 OpDecorate %102 DescriptorSet 0
807 OpDecorate %102 Binding 1
808 OpDecorate %103 DescriptorSet 0
809 OpDecorate %103 Binding 2
810 OpDecorate %104 DescriptorSet 0
811 OpDecorate %104 Binding 3
812 OpDecorate %105 DescriptorSet 0
813 OpDecorate %105 Binding 4
814
815 ; CHECK: OpDecorate %100 DescriptorSet 0
816 ; CHECK: OpDecorate %100 Binding 0
817 ; CHECK: OpDecorate %101 DescriptorSet 0
818 ; CHECK: OpDecorate %101 Binding 1
819 ; CHECK: OpDecorate %102 DescriptorSet 0
820 ; CHECK: OpDecorate %102 Binding 2
821 ; CHECK: OpDecorate %103 DescriptorSet 0
822 ; CHECK: OpDecorate %103 Binding 3
823 ; CHECK: OpDecorate %104 DescriptorSet 0
824 ; CHECK: OpDecorate %104 Binding 4
825 ; CHECK: OpDecorate %105 DescriptorSet 0
826 ; CHECK: OpDecorate %105 Binding 5
827
828 )" + BasicTypes() + R"(
829
830 %100 = OpVariable %p_i_ty UniformConstant
831 %101 = OpVariable %p_s_ty UniformConstant
832 %102 = OpVariable %pu_st_ty Uniform
833 %103 = OpVariable %pb_st_ty StorageBuffer
834 %104 = OpVariable %p_si_ty UniformConstant
835 %105 = OpVariable %p_s_ty UniformConstant
836
837 %alpha = OpFunction %void None %voidfn
838 %1000 = OpLabel
839 %1001 = OpCopyObject %p_i_ty %100
840 %1002 = OpCopyObject %p_s_ty %101
841 %1003 = OpCopyObject %pb_st_ty %103
842 %1004 = OpCopyObject %p_si_ty %104
843 %1005 = OpCopyObject %p_s_ty %105
844 OpReturn
845 OpFunctionEnd
846
847 %beta = OpFunction %void None %voidfn
848 %2000 = OpLabel
849 %2001 = OpCopyObject %p_s_ty %101
850 %2002 = OpCopyObject %pu_st_ty %102
851 %2003 = OpCopyObject %pb_st_ty %103
852 %2004 = OpCopyObject %p_s_ty %105
853 OpReturn
854 OpFunctionEnd
855 )";
856
857 auto [disasm, status] = SinglePassRunAndMatch<ResolveBindingConflictsPass>(
858 kTest, /* do_validation= */ true);
859 EXPECT_EQ(status, Pass::Status::SuccessWithChange) << disasm;
860 }
861
862 } // namespace
863 } // namespace opt
864 } // namespace spvtools