• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "compiler.h"
25 #include "bi_test.h"
26 #include "bi_builder.h"
27 
28 #include <gtest/gtest.h>
29 
30 class SchedulerPredicates : public testing::Test {
31 protected:
SchedulerPredicates()32    SchedulerPredicates() {
33       mem_ctx = ralloc_context(NULL);
34       b = bit_builder(mem_ctx);
35    }
~SchedulerPredicates()36    ~SchedulerPredicates() {
37       ralloc_free(mem_ctx);
38    }
39 
TMP()40    bi_index TMP() { return bi_temp(b->shader); }
41 
42    void *mem_ctx;
43    bi_builder *b;
44 };
45 
TEST_F(SchedulerPredicates,MOV)46 TEST_F(SchedulerPredicates, MOV)
47 {
48    bi_instr *mov = bi_mov_i32_to(b, TMP(), TMP());
49    ASSERT_TRUE(bi_can_fma(mov));
50    ASSERT_TRUE(bi_can_add(mov));
51    ASSERT_FALSE(bi_must_message(mov));
52    ASSERT_TRUE(bi_reads_zero(mov));
53    ASSERT_TRUE(bi_reads_temps(mov, 0));
54    ASSERT_TRUE(bi_reads_t(mov, 0));
55 }
56 
TEST_F(SchedulerPredicates,FMA)57 TEST_F(SchedulerPredicates, FMA)
58 {
59    bi_instr *fma = bi_fma_f32_to(b, TMP(), TMP(), TMP(), bi_zero());
60    ASSERT_TRUE(bi_can_fma(fma));
61    ASSERT_FALSE(bi_can_add(fma));
62    ASSERT_FALSE(bi_must_message(fma));
63    ASSERT_TRUE(bi_reads_zero(fma));
64    for (unsigned i = 0; i < 3; ++i) {
65       ASSERT_TRUE(bi_reads_temps(fma, i));
66       ASSERT_TRUE(bi_reads_t(fma, i));
67    }
68 }
69 
TEST_F(SchedulerPredicates,LOAD)70 TEST_F(SchedulerPredicates, LOAD)
71 {
72    bi_instr *load = bi_load_i128_to(b, TMP(), TMP(), TMP(), BI_SEG_UBO, 0);
73    ASSERT_FALSE(bi_can_fma(load));
74    ASSERT_TRUE(bi_can_add(load));
75    ASSERT_TRUE(bi_must_message(load));
76    for (unsigned i = 0; i < 2; ++i) {
77       ASSERT_TRUE(bi_reads_temps(load, i));
78       ASSERT_TRUE(bi_reads_t(load, i));
79    }
80 }
81 
TEST_F(SchedulerPredicates,BLEND)82 TEST_F(SchedulerPredicates, BLEND)
83 {
84    bi_instr *blend = bi_blend_to(b, TMP(), TMP(), TMP(), TMP(), TMP(), TMP(),
85                                  BI_REGISTER_FORMAT_F32, 4, 4);
86    ASSERT_FALSE(bi_can_fma(blend));
87    ASSERT_TRUE(bi_can_add(blend));
88    ASSERT_TRUE(bi_must_message(blend));
89    for (unsigned i = 0; i < 4; ++i)
90       ASSERT_TRUE(bi_reads_temps(blend, i));
91    ASSERT_FALSE(bi_reads_t(blend, 0));
92    ASSERT_TRUE(bi_reads_t(blend, 1));
93    ASSERT_FALSE(bi_reads_t(blend, 2));
94    ASSERT_FALSE(bi_reads_t(blend, 3));
95 }
96 
TEST_F(SchedulerPredicates,RestrictionsOnModifiersOfSameCycleTemporaries)97 TEST_F(SchedulerPredicates, RestrictionsOnModifiersOfSameCycleTemporaries)
98 {
99    bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP());
100    ASSERT_TRUE(bi_reads_t(fadd, 0));
101 
102    for (unsigned i = 0; i < 2; ++i) {
103       for (unsigned j = 0; j < 2; ++j) {
104          bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP());
105          fadd->src[i] = bi_swz_16(TMP(), j, j);
106          ASSERT_TRUE(bi_reads_t(fadd, 1 - i));
107          ASSERT_FALSE(bi_reads_t(fadd, i));
108       }
109    }
110 }
111 
TEST_F(SchedulerPredicates,RestrictionsOnFAddV2F16)112 TEST_F(SchedulerPredicates, RestrictionsOnFAddV2F16)
113 {
114    bi_index x = bi_register(0);
115    bi_index y = bi_register(1);
116 
117    /* Basic */
118    bi_instr *fadd = bi_fadd_v2f16_to(b, TMP(), x, x);
119 
120    ASSERT_TRUE(bi_can_fma(fadd));
121    ASSERT_TRUE(bi_can_add(fadd));
122 
123    /* With imbalanced abs */
124    fadd->src[0].abs = true;
125 
126    ASSERT_TRUE(bi_can_fma(fadd));
127    ASSERT_TRUE(bi_can_add(fadd));
128 
129    /* With equal abs */
130    fadd->src[1].abs = true;
131 
132    ASSERT_FALSE(bi_can_fma(fadd));
133    ASSERT_TRUE(bi_can_add(fadd));
134 
135    /* With equal abs but different sources */
136    fadd->src[1] = bi_abs(y);
137 
138    ASSERT_TRUE(bi_can_fma(fadd));
139    ASSERT_TRUE(bi_can_add(fadd));
140 
141    /* With clamp */
142    fadd->clamp = BI_CLAMP_CLAMP_M1_1;
143 
144    ASSERT_TRUE(bi_can_fma(fadd));
145    ASSERT_FALSE(bi_can_add(fadd));
146 
147    /* Impossible encoding (should never be seen) */
148    fadd->src[1] = fadd->src[0];
149 
150    ASSERT_FALSE(bi_can_fma(fadd));
151    ASSERT_FALSE(bi_can_add(fadd));
152 }
153 
TEST_F(SchedulerPredicates,BranchOffset)154 TEST_F(SchedulerPredicates, BranchOffset)
155 {
156    bi_instr *jump = bi_jump(b, TMP());
157    ASSERT_FALSE(bi_reads_t(jump, 0));
158 }
159