xref: /aosp_15_r20/external/mesa3d/src/panfrost/compiler/test/test-scheduler-predicates.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2021 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "bi_builder.h"
25 #include "bi_test.h"
26 #include "compiler.h"
27 
28 #include <gtest/gtest.h>
29 
30 class SchedulerPredicates : public testing::Test {
31  protected:
SchedulerPredicates()32    SchedulerPredicates()
33    {
34       mem_ctx = ralloc_context(NULL);
35       b = bit_builder(mem_ctx);
36    }
~SchedulerPredicates()37    ~SchedulerPredicates()
38    {
39       ralloc_free(mem_ctx);
40    }
41 
TMP()42    bi_index TMP()
43    {
44       return bi_temp(b->shader);
45    }
46 
47    void *mem_ctx;
48    bi_builder *b;
49 };
50 
TEST_F(SchedulerPredicates,MOV)51 TEST_F(SchedulerPredicates, MOV)
52 {
53    bi_instr *mov = bi_mov_i32_to(b, TMP(), TMP());
54    ASSERT_TRUE(bi_can_fma(mov));
55    ASSERT_TRUE(bi_can_add(mov));
56    ASSERT_FALSE(bi_must_message(mov));
57    ASSERT_TRUE(bi_reads_zero(mov));
58    ASSERT_TRUE(bi_reads_temps(mov, 0));
59    ASSERT_TRUE(bi_reads_t(mov, 0));
60 }
61 
TEST_F(SchedulerPredicates,FMA)62 TEST_F(SchedulerPredicates, FMA)
63 {
64    bi_instr *fma = bi_fma_f32_to(b, TMP(), TMP(), TMP(), bi_zero());
65    ASSERT_TRUE(bi_can_fma(fma));
66    ASSERT_FALSE(bi_can_add(fma));
67    ASSERT_FALSE(bi_must_message(fma));
68    ASSERT_TRUE(bi_reads_zero(fma));
69    for (unsigned i = 0; i < 3; ++i) {
70       ASSERT_TRUE(bi_reads_temps(fma, i));
71       ASSERT_TRUE(bi_reads_t(fma, i));
72    }
73 }
74 
TEST_F(SchedulerPredicates,LOAD)75 TEST_F(SchedulerPredicates, LOAD)
76 {
77    bi_instr *load = bi_load_i128_to(b, TMP(), TMP(), TMP(), BI_SEG_UBO, 0);
78    ASSERT_FALSE(bi_can_fma(load));
79    ASSERT_TRUE(bi_can_add(load));
80    ASSERT_TRUE(bi_must_message(load));
81    for (unsigned i = 0; i < 2; ++i) {
82       ASSERT_TRUE(bi_reads_temps(load, i));
83       ASSERT_TRUE(bi_reads_t(load, i));
84    }
85 }
86 
TEST_F(SchedulerPredicates,BLEND)87 TEST_F(SchedulerPredicates, BLEND)
88 {
89    bi_instr *blend = bi_blend_to(b, TMP(), TMP(), TMP(), TMP(), TMP(), TMP(),
90                                  BI_REGISTER_FORMAT_F32, 4, 4);
91    ASSERT_FALSE(bi_can_fma(blend));
92    ASSERT_TRUE(bi_can_add(blend));
93    ASSERT_TRUE(bi_must_message(blend));
94    for (unsigned i = 0; i < 4; ++i)
95       ASSERT_TRUE(bi_reads_temps(blend, i));
96    ASSERT_FALSE(bi_reads_t(blend, 0));
97    ASSERT_TRUE(bi_reads_t(blend, 1));
98    ASSERT_FALSE(bi_reads_t(blend, 2));
99    ASSERT_FALSE(bi_reads_t(blend, 3));
100 }
101 
TEST_F(SchedulerPredicates,RestrictionsOnModifiersOfSameCycleTemporaries)102 TEST_F(SchedulerPredicates, RestrictionsOnModifiersOfSameCycleTemporaries)
103 {
104    bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP());
105    ASSERT_TRUE(bi_reads_t(fadd, 0));
106 
107    for (unsigned i = 0; i < 2; ++i) {
108       for (unsigned j = 0; j < 2; ++j) {
109          bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP());
110          fadd->src[i] = bi_swz_16(TMP(), j, j);
111          ASSERT_TRUE(bi_reads_t(fadd, 1 - i));
112          ASSERT_FALSE(bi_reads_t(fadd, i));
113       }
114    }
115 }
116 
TEST_F(SchedulerPredicates,RestrictionsOnFAddV2F16)117 TEST_F(SchedulerPredicates, RestrictionsOnFAddV2F16)
118 {
119    bi_index x = bi_register(0);
120    bi_index y = bi_register(1);
121 
122    /* Basic */
123    bi_instr *fadd = bi_fadd_v2f16_to(b, TMP(), x, x);
124 
125    ASSERT_TRUE(bi_can_fma(fadd));
126    ASSERT_TRUE(bi_can_add(fadd));
127 
128    /* With imbalanced abs */
129    fadd->src[0].abs = true;
130 
131    ASSERT_TRUE(bi_can_fma(fadd));
132    ASSERT_TRUE(bi_can_add(fadd));
133 
134    /* With equal abs */
135    fadd->src[1].abs = true;
136 
137    ASSERT_FALSE(bi_can_fma(fadd));
138    ASSERT_TRUE(bi_can_add(fadd));
139 
140    /* With equal abs but different sources */
141    fadd->src[1] = bi_abs(y);
142 
143    ASSERT_TRUE(bi_can_fma(fadd));
144    ASSERT_TRUE(bi_can_add(fadd));
145 
146    /* With clamp */
147    fadd->clamp = BI_CLAMP_CLAMP_M1_1;
148 
149    ASSERT_TRUE(bi_can_fma(fadd));
150    ASSERT_FALSE(bi_can_add(fadd));
151 
152    /* Impossible encoding (should never be seen) */
153    fadd->src[1] = fadd->src[0];
154 
155    ASSERT_FALSE(bi_can_fma(fadd));
156    ASSERT_FALSE(bi_can_add(fadd));
157 }
158 
TEST_F(SchedulerPredicates,BranchOffset)159 TEST_F(SchedulerPredicates, BranchOffset)
160 {
161    bi_instr *jump = bi_jump(b, TMP());
162    ASSERT_FALSE(bi_reads_t(jump, 0));
163 }
164