Debian Codegraph @

src/panfrost/compiler/bifrost/bi_lower_mkvec_swz.c

raw
1 /*
2 * Copyright (C) 2026 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "bi_swizzles.h"
8 #include "compiler.h"
9 #include "valhall.h"
10
11 static bi_index
12 bi_i8_pair_as_i16(bi_index lo, bi_index hi)
13 {
14 /* If they're both constants, we can fold them together */
15 if (lo.type == BI_INDEX_CONSTANT && hi.type == BI_INDEX_CONSTANT) {
16 uint32_t lo_val = bi_apply_swizzle(lo.value, lo.swizzle) & 0xff;
17 uint32_t hi_val = bi_apply_swizzle(hi.value, hi.swizzle) & 0xff;
18 return bi_imm_u16(lo_val | (hi_val << 8));
19 }
20
21 if (!bi_is_word_equiv(lo, hi))
22 return bi_null();
23
24 unsigned lo_byte = lo.swizzle - BI_SWIZZLE_B0;
25 unsigned hi_byte = hi.swizzle - BI_SWIZZLE_B0;
26 assert(lo_byte < 4 && hi_byte < 4);
27
28 if ((lo_byte & 1) == 0 && hi_byte == lo_byte + 1) {
29 bi_index i16 = lo;
30 i16.swizzle = lo_byte == 0 ? BI_SWIZZLE_H00 : BI_SWIZZLE_H11;
31 return i16;
32 }
33
34 return bi_null();
35 }
36
37 static void
38 compact_i8_constants(bi_index *src, unsigned nr_src)
39 {
40 uint8_t values[4] = { 0, 0, 0, 0 };
41 unsigned nr_values = 0;
42
43 for (unsigned i = 0; i < nr_src; i++) {
44 if (src[i].type != BI_INDEX_CONSTANT)
45 continue;
46
47 if (src[i].value == 0) {
48 /* Sanitize zero swizzles */
49 src[i].swizzle = BI_SWIZZLE_B0;
50 continue;
51 }
52
53 /* Fold the swizzle (if any) and mask */
54 src[i].value = bi_apply_swizzle(src[i].value, src[i].swizzle) & 0xff;
55
56 unsigned v = 0;
57 for (; v < nr_values; v++) {
58 if (values[v] == src[i].value)
59 break;
60 }
61 if (v == nr_values)
62 values[nr_values++] = src[i].value;
63 }
64
65 /* If the only constants we found were zero, we're done */
66 if (nr_values == 0)
67 return;
68
69 for (unsigned i = 0; i < nr_src; i++) {
70 if (src[i].type != BI_INDEX_CONSTANT || src[i].value == 0)
71 continue;
72
73 unsigned v = 0;
74 for (; v < nr_values; v++) {
75 if (values[v] == src[i].value)
76 break;
77 }
78 assert(v < nr_values);
79
80 /* Fold two constants into one so that bi_schedule will see half the
81 * number of unique constants. We only have .b0 and .b2 swizzles on
82 * MKVEC.i8v4 on Bifrost so we can't place them in b1 or b3.
83 */
84 uint32_t v32_lo = values[v & ~1];
85 uint32_t v32_hi = values[v | 1];
86 src[i] = bi_imm_u32(v32_lo | (v32_hi << 16));
87 src[i].swizzle = (v & 1) ? BI_SWIZZLE_B2 : BI_SWIZZLE_B0;
88 }
89 }
90
91 static bi_instr *
92 build_swz_v2i16_to(bi_builder *b, bi_index dst, bi_index src)
93 {
94 if (src.swizzle == BI_SWIZZLE_H01)
95 return bi_mov_i32_to(b, dst, src);
96
97 /* On Valhall, we don't have SWZ.v2i16 but IADD has a swizzle */
98 if (b->shader->arch >= 9)
99 return bi_iadd_v2u16_to(b, dst, src, bi_zero(), false);
100 else
101 return bi_swz_v2i16_to(b, dst, src);
102 }
103
104 static bi_instr *
105 build_mkvec_v4i8_to(bi_builder *b, bi_index dst, const bi_index src[4])
106 {
107 unsigned bytes[4];
108 bool all_constant = true;
109 for (unsigned i = 0; i < 4; i++) {
110 STATIC_ASSERT(BI_SWIZZLE_B3 - BI_SWIZZLE_B0 == 3);
111 assert(src[i].swizzle >= BI_SWIZZLE_B0);
112 assert(src[i].swizzle <= BI_SWIZZLE_B3);
113 bytes[i] = src[i].swizzle - BI_SWIZZLE_B0;
114 if (src[i].type != BI_INDEX_CONSTANT)
115 all_constant = false;
116 }
117
118 if (all_constant) {
119 uint32_t v32 = 0;
120 for (unsigned i = 0; i < 4; i++) {
121 uint32_t v8 = bi_apply_swizzle(src[i].value, src[i].swizzle) & 0xff;
122 v32 |= v8 << (i * 8);
123 }
124 return bi_mov_i32_to(b, dst, bi_imm_u32(v32));
125 }
126
127 /* Check for U8_TO_U32 */
128 if (bi_is_zero(src[1]) && bi_is_zero(src[2]) && bi_is_zero(src[3]))
129 return bi_u8_to_u32_to(b, dst, src[0]);
130
131 /* Check for V2U8_TO_V2U16 */
132 enum bi_swizzle swizzle = BI_SWIZZLE_B0123;
133 if (bi_is_word_equiv(src[0], src[2]) &&
134 bi_is_zero(src[1]) && bi_is_zero(src[3])) {
135 unsigned v2u8_bytes[4] = { bytes[0], bytes[0], bytes[2], bytes[2] };
136 bool valid_swizzle =
137 bi_swizzle_from_byte_channels(v2u8_bytes, &swizzle);
138 assert(valid_swizzle);
139
140 bi_index v2u8_src = src[0];
141 v2u8_src.swizzle = swizzle;
142
143 return bi_v2u8_to_v2u16_to(b, dst, v2u8_src);
144 }
145
146 /* Check if we can do a swizzled MOV of some form */
147 if (bi_is_word_equiv(src[0], src[1]) &&
148 bi_is_word_equiv(src[0], src[2]) &&
149 bi_is_word_equiv(src[0], src[3]) &&
150 bi_swizzle_from_byte_channels(bytes, &swizzle)) {
151 bi_index swz_src = src[0];
152 swz_src.swizzle = swizzle;
153
154 /* Check for MOV.i32 and SWZ.v2i16 */
155 if (swizzle == BI_SWIZZLE_H00 ||
156 swizzle == BI_SWIZZLE_H01 ||
157 swizzle == BI_SWIZZLE_H10 ||
158 swizzle == BI_SWIZZLE_H11)
159 return build_swz_v2i16_to(b, dst, swz_src);
160
161 if (b->shader->arch >= 9) {
162 /* On v9 and v10, LSHIFT_OR.v4i8 has a limited swizzle */
163 if (bi_op_supports_swizzle(BI_OPCODE_LSHIFT_OR_V4I8, 0, swizzle,
164 b->shader->arch)) {
165 return bi_lshift_or_v4i8_to(b, dst, swz_src,
166 bi_imm_u8(0), bi_imm_u8(0));
167 }
168 } else {
169 /* Check for SWZ.v4i8 */
170 if (bi_op_supports_swizzle(BI_OPCODE_SWZ_V4I8, 0, swizzle,
171 b->shader->arch)) {
172 return bi_swz_v4i8_to(b, dst, swz_src);
173 }
174 }
175 }
176
177 bi_index v2_lo = bi_i8_pair_as_i16(src[0], src[1]);
178 bi_index v2_hi = bi_i8_pair_as_i16(src[2], src[3]);
179 if (!bi_is_null(v2_lo) && !bi_is_null(v2_hi)) {
180 /* Check for U16_TO_U32 */
181 if (bi_is_zero(v2_hi))
182 return bi_u16_to_u32_to(b, dst, v2_lo);
183
184 /* Check for MKVEC.v2i16 */
185 return bi_mkvec_v2i16_to(b, dst, v2_lo, v2_hi);
186 }
187
188 /* On Valhal+, we can do any v4i8 in two instructions */
189 if (b->shader->arch >= 9) {
190 if (bi_is_zero(src[2]) && bi_is_zero(src[3]))
191 return bi_mkvec_v2i8_to(b, dst, src[0], src[1], bi_zero());
192
193 if (bi_is_word_equiv(src[2], src[3]) && bytes[2] == 0 && bytes[3] == 1) {
194 /* src2 is implicitly read as .b01 by MKVEC.v2i8. */
195 bi_index src2 = src[2];
196 src2.swizzle = BI_SWIZZLE_H01;
197
198 return bi_mkvec_v2i8_to(b, dst, src[0], src[1], src2);
199 }
200
201 bi_index acc = bi_mkvec_v2i8(b, src[2], src[3], bi_zero());
202 return bi_mkvec_v2i8_to(b, dst, src[0], src[1], acc);
203 } else {
204 bi_index v4_src[4];
205 for (unsigned i = 0; i < 4; i++) {
206 v4_src[i] = src[i];
207
208 /* We can only swizzle to even bytes */
209 if (src[i].type != BI_INDEX_CONSTANT && (bytes[i] & 1))
210 v4_src[i] = bi_byte(bi_swz_v4i8(b, v4_src[i]), 0);
211 }
212
213 compact_i8_constants(v4_src, 4);
214
215 return bi_mkvec_v4i8_to(b, dst, v4_src[0], v4_src[1],
216 v4_src[2], v4_src[3]);
217 }
218 }
219
220 static bi_instr *
221 lower_mkvec_v4i8(bi_builder *b, bi_instr *I)
222 {
223 return build_mkvec_v4i8_to(b, I->dest[0], I->src);
224 }
225
226 static bi_instr *
227 lower_swz_v4i8(bi_builder *b, bi_instr *I)
228 {
229 unsigned bytes[4] = {0, 0, 0, 0};
230 bi_swizzle_to_byte_channels(I->src[0].swizzle, bytes);
231
232 bi_index src[4];
233 for (unsigned i = 0; i < 4; i++) {
234 src[i] = I->src[0];
235 src[i].swizzle = BI_SWIZZLE_B0 + bytes[i];
236 }
237
238 return build_mkvec_v4i8_to(b, I->dest[0], src);
239 }
240
241 static bi_instr *
242 lower_mkvec_v2i16(bi_builder *b, bi_instr *I)
243 {
244 for (unsigned i = 0; i < 2; i++) {
245 assert(I->src[i].swizzle == BI_SWIZZLE_H0 ||
246 I->src[i].swizzle == BI_SWIZZLE_H1);
247 }
248
249 if (bi_is_word_equiv(I->src[0], I->src[1])) {
250 bi_index src = I->src[0];
251 src.swizzle = bi_swizzle_from_half(I->src[0].swizzle == BI_SWIZZLE_H1,
252 I->src[1].swizzle == BI_SWIZZLE_H1);
253 return build_swz_v2i16_to(b, I->dest[0], src);
254 }
255
256 if (bi_is_zero(I->src[1]))
257 return bi_u16_to_u32_to(b, I->dest[0], I->src[0]);
258
259 return NULL;
260 }
261
262 static bi_instr *
263 lower_swz_v2i16(bi_builder *b, bi_instr *I)
264 {
265 return build_swz_v2i16_to(b, I->dest[0], I->src[0]);
266 }
267
268 static bi_instr *
269 lower(bi_builder *b, bi_instr *I)
270 {
271 switch (I->op) {
272 case BI_OPCODE_MKVEC_V2I16:
273 return lower_mkvec_v2i16(b, I);
274
275 case BI_OPCODE_MKVEC_V4I8:
276 return lower_mkvec_v4i8(b, I);
277
278 case BI_OPCODE_SWZ_V2I16:
279 return lower_swz_v2i16(b, I);
280
281 case BI_OPCODE_SWZ_V4I8:
282 return lower_swz_v4i8(b, I);
283
284 default:
285 return NULL;
286 }
287 }
288
289 void
290 bi_lower_mkvec_swz(bi_context *ctx)
291 {
292 bi_foreach_instr_global_safe(ctx, I) {
293 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
294
295 if (lower(&b, I))
296 bi_remove_instruction(I);
297 }
298 }