1 /*
2 * Copyright (C) 2022 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 "va_compiler.h"
27 #include "valhall_enums.h"
28
29 #include <gtest/gtest.h>
30
31 #define R(x) bi_register(x)
32 #define DR(x) bi_discard(R(x))
33
34 static void
strip_discard(bi_context * ctx)35 strip_discard(bi_context *ctx)
36 {
37 bi_foreach_instr_global(ctx, I) {
38 bi_foreach_src(I, s)
39 I->src[s].discard = false;
40 }
41 }
42
43 #define CASE(test) \
44 do { \
45 void *mem_ctx = ralloc_context(NULL); \
46 bi_builder *A = bit_builder(mem_ctx); \
47 bi_builder *B = bit_builder(mem_ctx); \
48 { \
49 UNUSED bi_builder *b = A; \
50 test; \
51 } \
52 strip_discard(A->shader); \
53 va_mark_last(A->shader); \
54 { \
55 UNUSED bi_builder *b = B; \
56 test; \
57 } \
58 ASSERT_SHADER_EQUAL(A->shader, B->shader); \
59 ralloc_free(mem_ctx); \
60 } while (0)
61
TEST(MarkLast,Simple)62 TEST(MarkLast, Simple)
63 {
64 CASE(bi_fadd_f32_to(b, R(0), DR(0), DR(1)));
65
66 CASE({
67 bi_fadd_f32_to(b, R(2), R(0), DR(1));
68 bi_fadd_f32_to(b, R(0), DR(0), DR(2));
69 });
70 }
71
TEST(MarkLast,SameSourceAndDestination)72 TEST(MarkLast, SameSourceAndDestination)
73 {
74 CASE({
75 bi_fadd_f32_to(b, R(0), DR(0), DR(0));
76 bi_fadd_f32_to(b, R(0), DR(0), DR(0));
77 bi_fadd_f32_to(b, R(0), DR(0), DR(0));
78 });
79 }
80
TEST(MarkLast,StagingReadBefore)81 TEST(MarkLast, StagingReadBefore)
82 {
83 CASE({
84 bi_fadd_f32_to(b, R(9), R(2), DR(7));
85 bi_st_tile(b, R(0), DR(4), DR(5), DR(6), BI_REGISTER_FORMAT_F32,
86 BI_VECSIZE_V4);
87 });
88 }
89
TEST(MarkLast,StagingReadAfter)90 TEST(MarkLast, StagingReadAfter)
91 {
92 CASE({
93 bi_st_tile(b, R(0), DR(4), DR(5), DR(6), BI_REGISTER_FORMAT_F32,
94 BI_VECSIZE_V4);
95 bi_fadd_f32_to(b, R(9), R(2), DR(7));
96 });
97 }
98
TEST(MarkLast,NonstagingSourceToAsync)99 TEST(MarkLast, NonstagingSourceToAsync)
100 {
101 CASE({
102 bi_st_tile(b, R(0), R(4), R(5), DR(6), BI_REGISTER_FORMAT_F32,
103 BI_VECSIZE_V4);
104 bi_fadd_f32_to(b, R(9), DR(4), DR(5));
105 });
106 }
107
TEST(MarkLast,Both64)108 TEST(MarkLast, Both64)
109 {
110 CASE(bi_load_i32_to(b, R(0), DR(8), DR(9), BI_SEG_NONE, 0));
111 }
112
TEST(MarkLast,Neither64ThenBoth)113 TEST(MarkLast, Neither64ThenBoth)
114 {
115 CASE({
116 bi_load_i32_to(b, R(0), R(8), R(9), BI_SEG_NONE, 0);
117 bi_load_i32_to(b, R(1), DR(8), DR(9), BI_SEG_NONE, 8);
118 });
119 }
120
TEST(MarkLast,Half64)121 TEST(MarkLast, Half64)
122 {
123 CASE({
124 bi_load_i32_to(b, R(0), R(8), R(9), BI_SEG_NONE, 0);
125 bi_fadd_f32_to(b, R(8), DR(8), DR(8));
126 });
127
128 CASE({
129 bi_load_i32_to(b, R(0), R(8), R(9), BI_SEG_NONE, 0);
130 bi_fadd_f32_to(b, R(9), DR(9), DR(9));
131 });
132 }
133
TEST(MarkLast,RegisterBlendDescriptor)134 TEST(MarkLast, RegisterBlendDescriptor)
135 {
136 CASE({
137 bi_blend_to(b, R(48), R(0), DR(60), DR(4), DR(5), bi_null(),
138 BI_REGISTER_FORMAT_F32, 4, 0);
139 });
140
141 CASE({
142 bi_blend_to(b, R(48), R(0), DR(60), R(4), R(5), bi_null(),
143 BI_REGISTER_FORMAT_F32, 4, 0);
144 bi_fadd_f32_to(b, R(4), DR(4), DR(7));
145 });
146
147 CASE({
148 bi_blend_to(b, R(48), R(0), DR(60), R(4), R(5), bi_null(),
149 BI_REGISTER_FORMAT_F32, 4, 0);
150 bi_fadd_f32_to(b, R(4), DR(5), DR(7));
151 });
152 }
153
TEST(MarkLast,ControlFlowAllFeatures)154 TEST(MarkLast, ControlFlowAllFeatures)
155 {
156 /* A
157 * / \
158 * B C
159 */
160 CASE({
161 bi_block *A = bi_start_block(&b->shader->blocks);
162 bi_block *B = bit_block(b->shader);
163 bi_block *C = bit_block(b->shader);
164
165 bi_block_add_successor(A, B);
166 bi_block_add_successor(A, C);
167
168 b->cursor = bi_after_block(A);
169 {
170 bi_instr *I = bi_st_tile(b, R(10), DR(14), DR(15), DR(16),
171 BI_REGISTER_FORMAT_F32, BI_VECSIZE_V4);
172 I->slot = 2;
173
174 bi_load_i32_to(b, R(20), R(28), R(29), BI_SEG_NONE, 0);
175
176 bi_fadd_f32_to(b, R(0), DR(0), R(3));
177 bi_fadd_f32_to(b, R(0), DR(0), R(7));
178 bi_fma_f32_to(b, R(2), R(0), R(1), DR(4));
179 }
180
181 b->cursor = bi_after_block(B);
182 {
183 bi_fadd_f32_to(b, R(2), DR(0), DR(2));
184 bi_fadd_f32_to(b, R(2), DR(2), DR(3));
185 bi_fadd_f32_to(b, R(2), DR(2), DR(7));
186 bi_fadd_f32_to(b, R(2), DR(2), DR(29));
187 }
188
189 b->cursor = bi_after_block(C);
190 {
191 bi_fadd_f32_to(b, R(2), DR(1), DR(2));
192 bi_fadd_f32_to(b, R(2), DR(2), DR(7));
193
194 bi_instr *I = bi_fadd_f32_to(b, R(2), DR(2), R(13));
195 I->flow = VA_FLOW_WAIT2;
196
197 bi_fadd_f32_to(b, R(2), DR(2), DR(11));
198 }
199 });
200 }
201