Blender V5.3
flow_control_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7namespace blender::gpu::tests {
8
9using namespace shader::parser;
10using namespace shader;
11using namespace std;
12
13TEST(shader_tool, Unroll)
14{
15 {
16 string input = R"(
17for (int i = 2; i < 4; i++) [[unroll]] { content += i; })";
18 string expect = R"(
19
20{
21#line 2
22 { content += 2; }
23#line 2
24 { content += 3; }
25#line 2
26 })";
27 auto [output, _, error] = process_test_local(input);
28 EXPECT_EQ(output, expect);
29 EXPECT_EQ(error, "");
30 }
31 {
32 string input = R"(
33for (int i = 2; i < 4; i++, y++) [[unroll]] { content += i; })";
34 string expect = R"(
35 {int i = 2; { content += i; }
36#line 2
37 i++, y++; { content += i; }
38#line 2
39 i++, y++; })";
40 auto [output, _, error] = process_test_local(input);
41 EXPECT_EQ(output, expect);
42 EXPECT_EQ(error, "");
43 }
44 {
45 string input = R"(
46for (int i = 2; i < 4 && i < y; i++, y++) [[unroll]] { cont += i; })";
47 string expect = R"(
48 {int i = 2;
49#line 2
50 if(i < 4 && i < y) { cont += i; }
51#line 2
52 i++, y++;
53#line 2
54 if(i < 4 && i < y) { cont += i; }
55#line 2
56 i++, y++; })";
57 auto [output, _, error] = process_test_local(input);
58 EXPECT_EQ(output, expect);
59 EXPECT_EQ(error, "");
60 }
61 {
62 string input = R"(
63for (; i < j;) [[unroll_n(2)]] { content += i; })";
64 string expect = R"(
65
66{
67#line 2
68 if(i < j) { content += i; }
69#line 2
70 if(i < j) { content += i; }
71#line 2
72 })";
73 auto [output, _, error] = process_test_local(input);
74 EXPECT_EQ(output, expect);
75 EXPECT_EQ(error, "");
76 }
77 {
78 string input = R"(
79for (; i < j;) [[unroll_n(2)]] { for (; j < k;) [[unroll_n(2)]] {} })";
80 string expect = R"(
81
82{
83#line 2
84 if(i < j) {
85{
86#line 2
87 if(j < k) {}
88#line 2
89 if(j < k) {}
90#line 2
91 } }
92#line 2
93 if(i < j) {
94{
95#line 2
96 if(j < k) {}
97#line 2
98 if(j < k) {}
99#line 2
100 } }
101#line 2
102 })";
103 auto [output, _, error] = process_test_local(input);
104 EXPECT_EQ(output, expect);
105 EXPECT_EQ(error, "");
106 }
107 {
108 string input = R"(for (; i < j;) [[unroll_n(2)]] { break; })";
109 auto [output, _, error] = process_test_local(input);
110 EXPECT_EQ(error, "Unrolled loop cannot contain \"break\" statement.");
111 }
112 {
113 string input = R"(for (; i < j;) [[unroll_n(2)]] { continue; })";
114 auto [output, _, error] = process_test_local(input);
115 EXPECT_EQ(error, "Unrolled loop cannot contain \"continue\" statement.");
116 }
117 {
118 string input = R"(
119for (; i < j;) [[unroll_n(2)]] { for (; j < k;) {break;continue;} })";
120 string expect = R"(
121
122{
123#line 2
124 if(i < j) { for (; j < k;) {break;continue;} }
125#line 2
126 if(i < j) { for (; j < k;) {break;continue;} }
127#line 2
128 })";
129 auto [output, _, error] = process_test_local(input);
130 EXPECT_EQ(output, expect);
131 EXPECT_EQ(error, "");
132 }
133 {
134 string input = R"(for (int i = 3; i > 2; i++) [[unroll]] {})";
135 auto [output, _, error] = process_test_local(input);
136 EXPECT_EQ(error, "Unsupported condition in unrolled loop.");
137 }
138 {
139 string input = R"(
140for (int a = 0; a < 2; a += 1) [[unroll]] { a; }
141for (uint b = 1; b < 3; ++b ) [[unroll]] { b; }
142for (int c = 2; c > 0; c-- ) [[unroll]] { c; }
143for (int d = 2; d > 0; --d ) [[unroll]] { d; }
144for (int e = 0; e < 4; e += 2) [[unroll]] { e; }
145for (int f = 4; f > 0; f -= 2) [[unroll]] { f; }
146for (int g = 1; g < 4; g *= 2) [[unroll]] { g; }
147for (int h = 4; h > 1; h /= 2) [[unroll]] { h; }
148for (int i = 0; i < 2; i=i+1 ) [[unroll]] { i; }
149)";
150 string expect = R"(
151 { 0; }
152#line 2
153 { 1; }
154 { 1; }
155#line 3
156 { 2; }
157 { 2; }
158#line 4
159 { 1; }
160 { 2; }
161#line 5
162 { 1; }
163 { 0; }
164#line 6
165 { 2; }
166 { 4; }
167#line 7
168 { 2; }
169 { 1; }
170#line 8
171 { 2; }
172 { 4; }
173#line 9
174 { 2; }
175 { 0; }
176#line 10
177 { 1; }
178)";
179 auto [output, _, error] = process_test_local(input, Language::BSL);
180 EXPECT_EQ(output, expect);
181 EXPECT_EQ(error, "");
182 }
183 {
184 string input = R"(
185int i;
186for (; i < 2; i++) [[unroll]] { i; }
187 )";
188 auto [output, _, error] = process_test_local(input, Language::BSL);
189 EXPECT_EQ(error, "Init statement needs to define the loop variable for unrolled loops");
190 }
191 {
192 string input = R"(
193for (float i = 0; i < 2; i++) [[unroll]] { i; }
194 )";
195 auto [output, _, error] = process_test_local(input, Language::BSL);
196 EXPECT_EQ(error, "Loop variable needs to be an integer type for unrolled loops");
197 }
198 {
199 string input = R"(
200for (int i = 0, j = 0; i < 2; i++) [[unroll]] { i; }
201 )";
202 auto [output, _, error] = process_test_local(input, Language::BSL);
203 EXPECT_EQ(error, "Multiple variables declared in unrolled loop");
204 }
205 {
206 string input = R"(
207for (float i = 0.0; i < 2.0; i += 1.0) [[unroll]] { i; }
208 )";
209 auto [output, _, error] = process_test_local(input, Language::BSL);
210 EXPECT_EQ(error, "Loop variable needs to be an integer type for unrolled loops");
211 }
212 {
213 string input = R"(
214for (int i; i < 2; i++) [[unroll]] { i; }
215 )";
216 auto [output, _, error] = process_test_local(input, Language::BSL);
217 EXPECT_EQ(error,
218 "Loop variable needs to be assigned a value (using assignment) for "
219 "unrolled loops");
220 }
221 {
222 string input = R"(
223for (int i = atomicAdd(i, i); i < 2; i++) [[unroll]] { i; }
224 )";
225 auto [output, _, error] = process_test_local(input, Language::BSL);
226 EXPECT_EQ(error, "Non-constant expression in unrolled loop control");
227 }
228 {
229 string input = R"(
230for (int i = 0; i < 2; i++, i++) [[unroll]] { i; }
231 )";
232 auto [output, _, error] = process_test_local(input, Language::BSL);
233 EXPECT_EQ(error, "Comma operator is not allowed in unrolled loop statement");
234 }
235 {
236 string input = R"(
237for (int i = 0; i < atomicAdd(i, i); i++) [[unroll]] { i; }
238 )";
239 auto [output, _, error] = process_test_local(input, Language::BSL);
240 EXPECT_EQ(error, "Non-constant expression in unrolled loop control");
241 }
242 {
243 string input = R"(
244for (int i = 0; i < 65; i++) [[unroll]] { i; }
245 )";
246 auto [output, _, error] = process_test_local(input, Language::BSL);
247 EXPECT_EQ(error, "Loop unrolling generates too many iterations (over 64)");
248 }
249 {
250 string input = R"(
251int j = 0;
252for (int i = 0; i < 2; j++) [[unroll]] { i; }
253 )";
254 auto [output, _, error] = process_test_local(input, Language::BSL);
255 EXPECT_EQ(error, "Unrolled loop expression must assign to 'i'");
256 }
257 {
258 string input = R"(
259for (int i = 0; i < 2; i + 1) [[unroll]] { i; }
260 )";
261 auto [output, _, error] = process_test_local(input, Language::BSL);
262 EXPECT_EQ(error,
263 "Expected '++i', '--i', 'i++', 'i--' or 'i = expr', 'i += expr', 'i -= expr', 'i /= "
264 "expr', 'i *= expr' for unrolled loop expression");
265 }
266 {
267 string input = R"(
268for (int i = 1; i < 2; i <<= 1) [[unroll]] { i; }
269 )";
270 auto [output, _, error] = process_test_local(input, Language::BSL);
271 EXPECT_EQ(error,
272 "Expected '++i', '--i', 'i++', 'i--' or 'i = expr', 'i += expr', 'i -= expr', 'i /= "
273 "expr', 'i *= expr' for unrolled loop expression");
274 }
275 {
276 string input = R"(
277for (int i = 0; i < 2; i += atomicAdd(i, i)) [[unroll]] { i; }
278 )";
279 auto [output, _, error] = process_test_local(input, Language::BSL);
280 EXPECT_EQ(error, "Non-constant expression in unrolled loop control");
281 }
282}
283
284TEST(shader_tool, StaticBranch)
285{
286 {
287 string input = R"(
288struct Resources {
289 [[compilation_constant]] const int use_color_band;
290
291 void fn() {
292 if (use_color_band) [[static_branch]] {
293 test;
294 }
295 }
296};
297
298void func([[resource_table]] Resources &srt)
299{
300 if (srt.use_color_band) [[static_branch]] {
301 test;
302 }
303
304 if (srt.use_color_band == 1) [[static_branch]] {
305 test;
306 } else {
307 test;
308 }
309
310 if (srt.use_color_band) [[static_branch]] {
311 test;
312 } else if (srt.use_color_band) [[static_branch]] {
313 test;
314 }
315
316 if (srt.use_color_band) [[static_branch]] {
317 test;
318 } else if (srt.use_color_band) [[static_branch]] {
319 test;
320 } else {
321 test;
322 }
323
324 if (srt.use_color_band) [[static_branch]] {
325 if (srt.use_color_band) [[static_branch]] {
326 test;
327 }
328 }
329}
330)";
331 string expect = R"(
332#define access_Resources_use_color_band() use_color_band
333#ifdef CREATE_INFO_RES_PASS_Resources
334CREATE_INFO_RES_PASS_Resources
335#endif
336#ifdef CREATE_INFO_RES_BATCH_Resources
337CREATE_INFO_RES_BATCH_Resources
338#endif
339#ifdef CREATE_INFO_RES_GEOMETRY_Resources
340CREATE_INFO_RES_GEOMETRY_Resources
341#endif
342#ifdef CREATE_INFO_RES_SHARED_VARS_Resources
343CREATE_INFO_RES_SHARED_VARS_Resources
344#endif
345#line 2
346struct Resources {
347#line 18
348int _pad;};
349
350
351#ifndef GPU_METAL
352Resources Resources_ctor_();
353void _fn(Resources this_);
354Resources Resources_new_();
355#endif
356#line 2
357 Resources Resources_ctor_() {Resources r;r._pad=0;return r;}
358
359
360
361#if defined(CREATE_INFO_Resources)
362#line 5
363 void _fn(Resources this_) {
364
365#if SRT_CONSTANT_use_color_band
366#line 6
367 {
368 test;
369 }
370
371#endif
372#line 9
373 }
374#endif
375 Resources Resources_new_()
376{
377 Resources result;
378 result._pad = 0;
379 return result;
380#line 9
381}
382
383
384
385#if defined(CREATE_INFO_Resources)
386#line 12
387void func(Resources srt)
388{
389
390#if SRT_CONSTANT_use_color_band
391#line 14
392 {
393 test;
394 }
395#endif
396
397#if SRT_CONSTANT_use_color_band == 1
398#line 18
399 {
400 test;
401 }
402#else
403#line 20
404 {
405 test;
406 }
407#endif
408
409#if SRT_CONSTANT_use_color_band
410#line 24
411 {
412 test;
413 }
414#elif SRT_CONSTANT_use_color_band
415#line 26
416 {
417 test;
418 }
419#endif
420
421#if SRT_CONSTANT_use_color_band
422#line 30
423 {
424 test;
425 }
426#elif SRT_CONSTANT_use_color_band
427#line 32
428 {
429 test;
430 }
431#else
432#line 34
433 {
434 test;
435 }
436#endif
437
438#if SRT_CONSTANT_use_color_band
439#line 38
440 {
441
442#if SRT_CONSTANT_use_color_band
443#line 39
444 {
445 test;
446 }
447
448#endif
449#line 42
450 }
451
452#endif
453#line 43
454}
455
456#endif
457#line 44
458)";
459 auto [output, _, error] = process_test_string(input);
460 EXPECT_EQ(output, expect);
461 EXPECT_EQ(error, "");
462 }
463 {
464 string input = R"(
465void func([[resource_table]] Resources &srt)
466{
467 if (srt.use_color_band) [[static_branch]] {
468 test;
469 } else if (srt.use_color_band) {
470 test;
471 }
472}
473)";
474 auto [output, _, error] = process_test_string(input);
475 EXPECT_EQ(error, "Expecting next if statement to also be a static branch.");
476 }
477 {
478 string input = R"(
479void func([[resource_table]] Resources &srt)
480{
481 if (use_color_band) [[static_branch]] {
482 test;
483 }
484}
485)";
486 auto [output, _, error] = process_test_string(input);
487 EXPECT_EQ(error,
488 "Expecting compilation or specialization constant. Make sure SRT arguments "
489 "have the [[resource_table]] attribute.");
490 }
491 {
492 string input = R"(
493void func([[resource_table]] Resources &srt)
494{
495 if (srt.use_color_band && srt.use_color_band) [[static_branch]] {
496 test;
497 }
498}
499)";
500 auto [output, _, error] = process_test_string(input);
501 EXPECT_EQ(error, "Expecting single condition.");
502 }
503 {
504 string input = R"(
505template<int N> int implicit_test(int x)
506{
507 if (N == 0) {
508 return 0;
509 }
510 else if (x == 1) {
511 return 1;
512 }
513 else if (N == 1) {
514 return 2;
515 }
516 else {
517 return 3;
518 }
519}
520
521template int implicit_test<0>();
522template int implicit_test<1>();
523template int implicit_test<2>();
524)";
525 string expect = R"(
526 int implicit_testT0(int x)
527{
528 {
529 return 0;
530 }
531#line 16
532}
533#line 2
534 int implicit_testT1(int x)
535{
536#line 7
537 if (x == 1) {
538 return 1;
539 }
540 else {
541 return 2;
542 }
543#line 16
544}
545#line 2
546 int implicit_testT2(int x)
547{
548#line 7
549 if (x == 1) {
550 return 1;
551 }
552#line 13
553 else {
554 return 3;
555 }
556}
557#line 22
558)";
559 auto [output, _, error] = process_test_string(input, Language::BSL);
560 EXPECT_EQ(output, expect);
561 EXPECT_EQ(error, "");
562 }
563 {
564 string input = R"(
565template<int B> int chain_test(int y)
566{
567 if (y == 0) {
568 return 0;
569 }
570 else if constexpr (B == 1) {
571 return 1;
572 }
573 else if (y == 2) {
574 return 2;
575 }
576 else {
577 return 3;
578 }
579}
580
581template int chain_test<1>();
582template int chain_test<0>();
583)";
584 string expect = R"(
585 int chain_testT1(int y)
586{
587 if (y == 0) {
588 return 0;
589 }
590 else {
591 return 1;
592 }
593#line 16
594}
595#line 2
596 int chain_testT0(int y)
597{
598 if (y == 0) {
599 return 0;
600 }
601#line 10
602 else if (y == 2) {
603 return 2;
604 }
605 else {
606 return 3;
607 }
608}
609#line 21
610)";
611 auto [output, _, error] = process_test_string(input, Language::BSL);
612 EXPECT_EQ(output, expect);
613 EXPECT_EQ(error, "");
614 }
615 {
616 string input = R"(
617template<int i> int f(int a)
618{
619 if constexpr (i == 0) {
620 return 0;
621 }
622 else if (i == 1) {
623 return 1;
624 }
625 else if (i + a == 1) {
626 return 3;
627 }
628 else if constexpr (i == 2) {
629 return 2;
630 }
631 else {
632 return 4;
633 }
634}
635
636template int f<0>(int);
637template int f<1>(int);
638template int f<2>(int);
639template int f<3>(int);
640)";
641 string expect = R"(
642 int fT0(int a)
643{
644 {
645 return 0;
646 }
647#line 19
648}
649#line 2
650 int fT1(int a)
651{
652#line 7
653 {
654 return 1;
655 }
656#line 19
657}
658#line 2
659 int fT2(int a)
660{
661#line 10
662 if (2 + a == 1) {
663 return 3;
664 }
665 else {
666 return 2;
667 }
668#line 19
669}
670#line 2
671 int fT3(int a)
672{
673#line 10
674 if (3 + a == 1) {
675 return 3;
676 }
677#line 16
678 else {
679 return 4;
680 }
681}
682#line 26
683)";
684 auto [output, _, error] = process_test_string(input, Language::BSL);
685 EXPECT_EQ(output, expect);
686 EXPECT_EQ(error, "");
687 }
688 {
689 string input = R"(
690void f(int a)
691{
692 if constexpr (a == 0) {
693 return;
694 }
695}
696)";
697 auto [output, _, error] = process_test_string(input, Language::BSL);
698 EXPECT_EQ(error, "Constexpr if condition is not a constant expression");
699 }
700 {
701 string input = R"(
702enum E : int {
703 ENUM = 1,
704};
705
706struct Res {
707 [[compilation_constant]] const int i;
708};
709
710void f(Res res)
711{
712 if (res.i + 1 + E::ENUM == ENUM) [[static_branch]] {
713 0;
714 }
715 else if (res.i == 1) [[static_branch]] {
716 1;
717 }
718 else {
719 2;
720 }
721}
722)";
723 string expect = R"(
724
725#define E int
726#line 3
727static constexpr int ENUM = 1;
728#define access_Res_i() i
729
730
731#ifdef CREATE_INFO_RES_PASS_Res
732CREATE_INFO_RES_PASS_Res
733#endif
734#ifdef CREATE_INFO_RES_BATCH_Res
735CREATE_INFO_RES_BATCH_Res
736#endif
737#ifdef CREATE_INFO_RES_GEOMETRY_Res
738CREATE_INFO_RES_GEOMETRY_Res
739#endif
740#ifdef CREATE_INFO_RES_SHARED_VARS_Res
741CREATE_INFO_RES_SHARED_VARS_Res
742#endif
743#line 6
744struct Res {
745
746int _pad;
747#line 8
748};
749#line 6
750Res Res_ctor_() {Res r;r._pad=0;return r;}
751#if defined(CREATE_INFO_Res)
752
753
754void f(Res res)
755{
756 #if SRT_CONSTANT_i+ 1 + 1== 1
757#line 12
758 {
759 0;
760 }
761 #elif SRT_CONSTANT_i== 1
762#line 15
763 {
764 1;
765 }
766 #else
767#line 18
768 {
769 2;
770 }
771 #endif
772#line 21
773}
774
775#endif
776#line 23
777)";
778 auto [output, _, error] = process_test_string(input, Language::BSL);
779 EXPECT_EQ(output, expect);
780 EXPECT_EQ(error, "");
781 }
782}
783
784TEST(shader_tool, BoolCast)
785{
786 {
787 string input = R"(int i; if (i == 0) {})";
788 auto [output, _, error] = process_test_local(input, Language::BSL);
789 EXPECT_EQ(error, "");
790 }
791 {
792 string input = R"(int i; if (i == 0u) {})";
793 auto [output, _, error] = process_test_local(input, Language::BSL);
794 /* TODO(fclem): Should be turned on at some point, but current codebase uses it a lot. */
795 // EXPECT_EQ(error, "Invalid operands to binary expression ('int' == 'uint')");
796 }
797 {
798 string input = R"(if (1) {})";
799 auto [output, _, error] = process_test_local(input, Language::BSL);
800 EXPECT_EQ(error, "");
801 }
802 {
803 string input = R"(
804uint i; if (i) {})";
805 string expect = R"(
806uint i; if (i!=0u) {
807#line 2
808 })";
809 auto [output, _, error] = process_test_local(input, Language::BSL);
810 EXPECT_EQ(output, expect);
811 EXPECT_EQ(error, "");
812 }
813 {
814 string input = R"(
815int i; if (i) {})";
816 string expect = R"(
817int i; if (i!=0) {
818#line 2
819 })";
820 auto [output, _, error] = process_test_local(input, Language::BSL);
821 EXPECT_EQ(output, expect);
822 EXPECT_EQ(error, "");
823 }
824}
825
826TEST(shader_tool, ForceInline)
827{
828 /* Call inside itself (recursion). */
829 {
830 string input = R"(
831[[force_inline]] void f() { f(); }
832void foo() { f(); }
833)";
834 auto [output, _, error] = process_test_string(input, Language::BSL);
835 EXPECT_EQ(error, "Cannot inline recursive call");
836 }
837 {
838 string input = R"(
839int f3(int a);
840[[force_inline]] int f2(int a) {
841 return f3(a);
842}
843[[force_inline]] int f3(int b) {
844 return f2(b);
845}
846void foo() {
847 int result = f3(5);
848}
849)";
850 auto [output, _, error] = process_test_string(input, Language::BSL);
851 EXPECT_EQ(error, "Cannot inline recursive call");
852 }
853
854 /* Call inside compound expression. */
855 {
856 string input = R"(
857[[force_inline]] int get_val() { return 42; }
858void foo() {
859 int x = get_val() + 10;
860}
861)";
862 auto [output, _, error] = process_test_string(input, Language::BSL);
863 EXPECT_EQ(error, "Cannot inline function call inside a compound expression");
864 }
865
866 /* Call inside loops. */
867 {
868 string input = R"(
869[[force_inline]] int add_one(int val) {
870 return val + 1;
871}
872void foo() {
873 int sum = 0;
874 for (int i = 0; i < 10; ++i) {
875 sum = add_one(sum);
876 }
877}
878)";
879 auto [output, _, error] = process_test_string(input, Language::BSL);
880 /* TODO(fclem): We should support that at some point. */
881 EXPECT_EQ(error, "Cannot inline function call inside a compound expression");
882 }
883
884 /* Call inside if conditions. */
885 {
886 string input = R"(
887[[force_inline]] bool is_active() { return true; }
888void foo() {
889 if (is_active()) {
890 int x = 1;
891 }
892}
893)";
894 auto [output, _, error] = process_test_string(input, Language::BSL);
895 EXPECT_EQ(error, "Cannot inline function call inside a compound expression");
896 }
897
898 /* Call inside other inlined functions (nesting). */
899 {
900 string input = R"(
901[[force_inline]] int inner(int a) {
902 return a * 2;
903}
904[[force_inline]] int outer(int b) {
905 return inner(b);
906}
907void foo() {
908 int result = outer(5);
909}
910)";
911 auto [output, _, error] = process_test_string(input, Language::BSL);
912 EXPECT_EQ(error, "");
913 }
914}
915
916} // namespace blender::gpu::tests
#define output
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)