Debian Codegraph @

src/panfrost/compiler/bifrost/valhall/va_lower_constants.c

raw
1 /*
2 * Copyright (C) 2025 Arm Ltd.
3 * Copyright (C) 2021 Collabora Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "bi_builder.h"
8 #include "va_compiler.h"
9 #include "valhall.h"
10
11 /* Only some special immediates are available, as specified in the Table of
12 * Immediates in the specification. Other immediates must be lowered, either to
13 * uniforms or to moves.
14 */
15
16 static bi_index
17 va_mov_imm(bi_builder *b, uint32_t imm)
18 {
19 bi_index zero = bi_fau(BIR_FAU_IMMEDIATE | 0, false);
20 return bi_iadd_imm_i32(b, zero, imm);
21 }
22
23 static bi_index
24 va_lut_index_32(uint32_t imm)
25 {
26 for (unsigned i = 0; i < ARRAY_SIZE(valhall_immediates); ++i) {
27 if (valhall_immediates[i] == imm)
28 return va_lut(i);
29 }
30
31 return bi_null();
32 }
33
34 static bi_index
35 va_lut_index_16(uint16_t imm)
36 {
37 uint16_t *arr16 = (uint16_t *)valhall_immediates;
38
39 for (unsigned i = 0; i < (2 * ARRAY_SIZE(valhall_immediates)); ++i) {
40 if (arr16[i] == imm)
41 return bi_half(va_lut(i >> 1), i & 1);
42 }
43
44 return bi_null();
45 }
46
47 UNUSED static bi_index
48 va_lut_index_8(uint8_t imm)
49 {
50 uint8_t *arr8 = (uint8_t *)valhall_immediates;
51
52 for (unsigned i = 0; i < (4 * ARRAY_SIZE(valhall_immediates)); ++i) {
53 if (arr8[i] == imm)
54 return bi_byte(va_lut(i >> 2), i & 3);
55 }
56
57 return bi_null();
58 }
59
60 static bi_index
61 va_demote_constant_fp16(uint32_t value)
62 {
63 uint16_t fp16 = _mesa_float_to_half(uif(value));
64
65 /* Only convert if it is exact */
66 if (fui(_mesa_half_to_float(fp16)) == value)
67 return va_lut_index_16(fp16);
68 else
69 return bi_null();
70 }
71
72 /*
73 * Test if a 32-bit word arises as a sign or zero extension of some 8/16-bit
74 * value.
75 */
76 static bool
77 is_extension_of_8(uint32_t x, bool is_signed)
78 {
79 if (is_signed)
80 return (x <= INT8_MAX) || ((x >> 7) == BITFIELD_MASK(24 + 1));
81 else
82 return (x <= UINT8_MAX);
83 }
84
85 static bool
86 is_extension_of_16(uint32_t x, bool is_signed)
87 {
88 if (is_signed)
89 return (x <= INT16_MAX) || ((x >> 15) == BITFIELD_MASK(16 + 1));
90 else
91 return (x <= UINT16_MAX);
92 }
93
94 static bi_index
95 va_move_const_to_fau(bi_builder *b, uint32_t value)
96 {
97 const unsigned push_base = b->shader->inputs->fau_consts.offset;
98 uint32_t *values = b->shader->inputs->fau_consts.values;
99
100 assert(b->shader->inputs->fau_consts.max_amount == 0 || values != NULL);
101 assert(b->shader->fau_consts_count <=
102 b->shader->inputs->fau_consts.max_amount);
103
104 for (unsigned i = 0; i < b->shader->fau_consts_count; ++i) {
105 if (values[i] == value) {
106 const unsigned idx = push_base + i;
107 return bi_fau((enum bir_fau)(BIR_FAU_UNIFORM | (idx >> 1)), idx & 1);
108 }
109 }
110
111 if (b->shader->fau_consts_count < b->shader->inputs->fau_consts.max_amount) {
112 const unsigned int idx = push_base + b->shader->fau_consts_count;
113 values[b->shader->fau_consts_count++] = value;
114 return bi_fau((enum bir_fau)(BIR_FAU_UNIFORM | (idx >> 1)), idx & 1);
115 }
116
117 return bi_null();
118 }
119
120 static bi_index
121 va_lookup_constant(uint32_t value, struct va_src_info info, bool is_signed)
122 {
123 /* Try the constant as-is */
124 {
125 bi_index lut = va_lut_index_32(value);
126 if (!bi_is_null(lut))
127 return lut;
128
129 /* ...or negated as a FP32 constant */
130 if (info.absneg && info.size == VA_SIZE_32) {
131 lut = bi_neg(va_lut_index_32(fui(-uif(value))));
132 if (!bi_is_null(lut))
133 return lut;
134 }
135
136 /* ...or negated as a FP16 constant */
137 if (info.absneg && info.size == VA_SIZE_16) {
138 lut = bi_neg(va_lut_index_32(value ^ 0x80008000));
139 if (!bi_is_null(lut))
140 return lut;
141 }
142 }
143
144 /* Try using a single half of a FP16 constant */
145 bool replicated_halves = (value & 0xFFFF) == (value >> 16);
146 if (info.swizzle && info.size == VA_SIZE_16 && replicated_halves) {
147 bi_index lut = va_lut_index_16(value & 0xFFFF);
148 if (!bi_is_null(lut))
149 return lut;
150
151 /* ...possibly negated */
152 if (info.absneg) {
153 lut = bi_neg(va_lut_index_16((value & 0xFFFF) ^ 0x8000));
154 if (!bi_is_null(lut))
155 return lut;
156 }
157 }
158
159 /* Try extending a byte */
160 if ((info.widen || info.lanes || info.lane) &&
161 is_extension_of_8(value, is_signed)) {
162
163 bi_index lut = va_lut_index_8(value & 0xFF);
164 if (!bi_is_null(lut))
165 return lut;
166 }
167
168 /* Try extending a halfword */
169 if (info.widen && is_extension_of_16(value, is_signed)) {
170
171 bi_index lut = va_lut_index_16(value & 0xFFFF);
172 if (!bi_is_null(lut))
173 return lut;
174 }
175
176 /* Try demoting the constant to FP16 */
177 if (info.swizzle && info.size == VA_SIZE_32) {
178 bi_index lut = va_demote_constant_fp16(value);
179 if (!bi_is_null(lut))
180 return lut;
181
182 if (info.absneg) {
183 bi_index lut = bi_neg(va_demote_constant_fp16(fui(-uif(value))));
184 if (!bi_is_null(lut))
185 return lut;
186 }
187 }
188
189 return bi_null();
190 }
191
192 static bi_index
193 va_resolve_constant(bi_builder *b, uint32_t value, struct va_src_info info,
194 bool is_signed, bool staging, bool try_move_to_fau)
195 {
196 if (!staging) {
197 bi_index lut = va_lookup_constant(value, info, is_signed);
198 if (!bi_is_null(lut))
199 return lut;
200 }
201
202 if (!staging && try_move_to_fau) {
203 bi_index c = va_move_const_to_fau(b, value);
204 if (!bi_is_null(c))
205 return c;
206 }
207
208 return va_mov_imm(b, value);
209 }
210
211 static uint32_t
212 va_resolve_swizzles(bi_context *ctx, bi_instr *I, unsigned s)
213 {
214 struct va_src_info info = va_src_info(I->op, s);
215 uint32_t value = I->src[s].value;
216 enum bi_swizzle swz = I->src[s].swizzle;
217
218 /* Resolve any swizzle, keeping in mind the different interpretations
219 * swizzles in different contexts.
220 */
221 if (info.size == VA_SIZE_32) {
222 /* Extracting a half from the 32-bit value */
223 if (swz == BI_SWIZZLE_H00)
224 value = (value & 0xFFFF);
225 else if (swz == BI_SWIZZLE_H11)
226 value = (value >> 16);
227 else
228 assert(swz == BI_SWIZZLE_H01);
229
230 /* FP16 -> FP32 */
231 if (info.swizzle && swz != BI_SWIZZLE_H01)
232 value = fui(_mesa_half_to_float(value));
233 } else if (info.size == VA_SIZE_16) {
234 assert(swz >= BI_SWIZZLE_H00 && swz <= BI_SWIZZLE_H11);
235 value = bi_apply_swizzle(value, swz);
236 } else if (info.size == VA_SIZE_8 && (info.lane || info.lanes)) {
237 /* 8-bit extract */
238 unsigned chan = (swz - BI_SWIZZLE_B0000);
239 assert(chan < 4);
240
241 value = (value >> (8 * chan)) & 0xFF;
242 } else {
243 /* TODO: Any other special handling? */
244 value = bi_apply_swizzle(value, swz);
245 }
246
247 return value;
248 }
249
250 void
251 va_lower_constants(bi_context *ctx, bi_instr *I, struct hash_table_u64 *counts, uint32_t min_fau_count)
252 {
253 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
254
255 bi_foreach_src(I, s) {
256 if (I->src[s].type == BI_INDEX_CONSTANT) {
257 /* abs(#c) is pointless, but -#c occurs in transcendental sequences */
258 assert(!I->src[s].abs && "redundant .abs modifier");
259
260 bool is_signed = valhall_opcodes[I->op].is_signed;
261 bool staging = (s < valhall_opcodes[I->op].nr_staging_srcs);
262 struct va_src_info info = va_src_info(I->op, s);
263 const uint32_t value = va_resolve_swizzles(ctx, I, s);
264
265 const uint32_t count = (uintptr_t)_mesa_hash_table_u64_search(counts, value);
266 const bool move_to_fau = count >= min_fau_count;
267
268 bi_index cons =
269 va_resolve_constant(&b, value, info, is_signed, staging, move_to_fau);
270 cons.neg ^= I->src[s].neg;
271 I->src[s] = cons;
272
273 /* If we're selecting a single lane, we should return a single lane
274 * to ensure the result is encodeable. By convention, applying the
275 * lane select puts the desired constant (at least) in the bottom
276 * byte/half, so we can always select the bottom byte/half.
277 */
278 if ((info.lane || info.lanes || info.halfswizzle) &&
279 I->src[s].swizzle == BI_SWIZZLE_H01) {
280 assert(info.size == VA_SIZE_8 || info.size == VA_SIZE_16);
281 if (info.size == VA_SIZE_8)
282 I->src[s] = bi_byte(I->src[s], 0);
283 else if (info.size == VA_SIZE_16)
284 I->src[s] = bi_half(I->src[s], false);
285 }
286 }
287 }
288 }
289
290 void
291 va_count_constants(bi_context *ctx, bi_instr *I, struct hash_table_u64 *counts)
292 {
293 bi_foreach_src(I, s) {
294 if (I->src[s].type != BI_INDEX_CONSTANT)
295 continue;
296
297 const bool staging = (s < valhall_opcodes[I->op].nr_staging_srcs);
298 if (staging)
299 continue;
300
301 bool is_signed = valhall_opcodes[I->op].is_signed;
302 struct va_src_info info = va_src_info(I->op, s);
303 uint32_t value = va_resolve_swizzles(ctx, I, s);
304
305 bi_index cons = va_lookup_constant(value, info, is_signed);
306
307 const bool can_lut = !bi_is_null(cons);
308
309 /* We want to move constants that can't be created from built-in
310 * constants into the FAU if they are not staging register sources. */
311 if (!can_lut) {
312 uint32_t count = (uintptr_t)_mesa_hash_table_u64_search(counts, value);
313 _mesa_hash_table_u64_insert(counts, value, (void*)(uintptr_t)(count + 1));
314 }
315 }
316 }