pan/bit: Add BI_CONVERT tests
[mesa.git] / src / panfrost / bifrost / test / bi_test_pack.c
1 /*
2 * Copyright (C) 2020 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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include "bit.h"
28 #include "bi_print.h"
29 #include "util/half_float.h"
30 #include "bifrost/disassemble.h"
31
32 /* Instruction packing tests */
33
34 static bool
35 bit_test_single(struct panfrost_device *dev,
36 bi_instruction *ins,
37 uint32_t input[4],
38 bool fma, enum bit_debug debug)
39 {
40 /* First, simulate the instruction */
41 struct bit_state s = { 0 };
42 memcpy(s.r, input, 16);
43 bit_step(&s, ins, fma);
44
45 /* Next, wrap it up and pack it */
46
47 bi_instruction ldubo = {
48 .type = BI_LOAD_UNIFORM,
49 .src = {
50 BIR_INDEX_CONSTANT,
51 BIR_INDEX_ZERO
52 },
53 .src_types = {
54 nir_type_uint32,
55 nir_type_uint32,
56 },
57 .dest = BIR_INDEX_REGISTER | 0,
58 .dest_type = nir_type_uint32,
59 .writemask = 0xFFFF
60 };
61
62 bi_instruction ldva = {
63 .type = BI_LOAD_VAR_ADDRESS,
64 .writemask = (1 << 12) - 1,
65 .dest = BIR_INDEX_REGISTER | 32,
66 .dest_type = nir_type_uint32,
67 .src = {
68 BIR_INDEX_CONSTANT,
69 BIR_INDEX_REGISTER | 61,
70 BIR_INDEX_REGISTER | 62,
71 0,
72 },
73 .src_types = {
74 nir_type_uint32,
75 nir_type_uint32,
76 nir_type_uint32,
77 nir_type_uint32,
78 }
79 };
80
81 bi_instruction st = {
82 .type = BI_STORE_VAR,
83 .src = {
84 BIR_INDEX_REGISTER | 0,
85 ldva.dest, ldva.dest + 1, ldva.dest + 2,
86 },
87 .src_types = {
88 nir_type_uint32,
89 nir_type_uint32, nir_type_uint32, nir_type_uint32,
90 },
91 .store_channels = 4
92 };
93
94 bi_context *ctx = rzalloc(NULL, bi_context);
95 ctx->stage = MESA_SHADER_VERTEX;
96
97 bi_block *blk = rzalloc(ctx, bi_block);
98 blk->scheduled = true;
99
100 blk->base.predecessors = _mesa_set_create(blk,
101 _mesa_hash_pointer,
102 _mesa_key_pointer_equal);
103
104 list_inithead(&ctx->blocks);
105 list_addtail(&blk->base.link, &ctx->blocks);
106 list_inithead(&blk->clauses);
107
108 bi_clause *clauses[4] = {
109 rzalloc(ctx, bi_clause),
110 rzalloc(ctx, bi_clause),
111 rzalloc(ctx, bi_clause),
112 rzalloc(ctx, bi_clause)
113 };
114
115 for (unsigned i = 0; i < 4; ++i) {
116 clauses[i]->bundle_count = 1;
117 list_addtail(&clauses[i]->link, &blk->clauses);
118 clauses[i]->scoreboard_id = (i & 1);
119
120 if (i) {
121 clauses[i]->dependencies = 1 << (~i & 1);
122 clauses[i]->data_register_write_barrier = true;
123 }
124 }
125
126 clauses[0]->bundles[0].add = &ldubo;
127 clauses[0]->clause_type = BIFROST_CLAUSE_UBO;
128
129 if (fma)
130 clauses[1]->bundles[0].fma = ins;
131 else
132 clauses[1]->bundles[0].add = ins;
133
134 clauses[0]->constant_count = 1;
135 clauses[1]->constant_count = 1;
136 clauses[1]->constants[0] = ins->constant.u64;
137
138 clauses[2]->bundles[0].add = &ldva;
139 clauses[3]->bundles[0].add = &st;
140
141 clauses[2]->clause_type = BIFROST_CLAUSE_UBO;
142 clauses[3]->clause_type = BIFROST_CLAUSE_SSBO_STORE;
143
144 panfrost_program prog;
145 bi_pack(ctx, &prog.compiled);
146
147 bool succ = bit_vertex(dev, prog, input, 16, NULL, 0,
148 s.r, 16, debug);
149
150 if (debug >= BIT_DEBUG_ALL || (!succ && debug >= BIT_DEBUG_FAIL)) {
151 bi_print_shader(ctx, stderr);
152 disassemble_bifrost(stderr, prog.compiled.data, prog.compiled.size, true);
153 }
154
155 return succ;
156 }
157
158 /* Utilities for generating tests */
159
160 static void
161 bit_generate_float4(float *mem)
162 {
163 for (unsigned i = 0; i < 4; ++i)
164 mem[i] = (float) ((rand() & 255) - 127) / 16.0;
165 }
166
167 static void
168 bit_generate_half8(uint16_t *mem)
169 {
170 for (unsigned i = 0; i < 8; ++i)
171 mem[i] = _mesa_float_to_half(((float) (rand() & 255) - 127) / 16.0);
172 }
173
174 static bi_instruction
175 bit_ins(enum bi_class C, unsigned argc, nir_alu_type base, unsigned size)
176 {
177 nir_alu_type T = base | size;
178
179 bi_instruction ins = {
180 .type = C,
181 .dest = BIR_INDEX_REGISTER | 0,
182 .dest_type = T,
183 };
184
185 for (unsigned i = 0; i < argc; ++i) {
186 ins.src[i] = BIR_INDEX_REGISTER | i;
187 ins.src_types[i] = T;
188 }
189
190 return ins;
191 }
192
193 /* Tests all 64 combinations of floating point modifiers for a given
194 * instruction / floating-type / test type */
195
196 static void
197 bit_fmod_helper(struct panfrost_device *dev,
198 enum bi_class c, unsigned size, bool fma,
199 uint32_t *input, enum bit_debug debug, unsigned op)
200 {
201 bi_instruction ins = bit_ins(c, 2, nir_type_float, size);
202
203 for (unsigned outmod = 0; outmod < 4; ++outmod) {
204 for (unsigned inmod = 0; inmod < 16; ++inmod) {
205 ins.outmod = outmod;
206 ins.op.minmax = op;
207 ins.src_abs[0] = (inmod & 0x1);
208 ins.src_abs[1] = (inmod & 0x2);
209 ins.src_neg[0] = (inmod & 0x4);
210 ins.src_neg[1] = (inmod & 0x8);
211
212 /* Skip over tests that cannot run on FMA */
213 if (fma && (size == 16) && ins.src_abs[0] && ins.src_abs[1])
214 continue;
215
216 if (!bit_test_single(dev, &ins, input, fma, debug)) {
217 fprintf(stderr, "FAIL: fmod.%s%u.%s%s.%u\n",
218 bi_class_name(c),
219 size,
220 fma ? "fma" : "add",
221 outmod ? bi_output_mod_name(outmod) : ".none",
222 inmod);
223 }
224 }
225 }
226 }
227
228 static void
229 bit_fma_helper(struct panfrost_device *dev,
230 unsigned size, uint32_t *input, enum bit_debug debug)
231 {
232 bi_instruction ins = bit_ins(BI_FMA, 3, nir_type_float, size);
233
234 for (unsigned outmod = 0; outmod < 4; ++outmod) {
235 for (unsigned inmod = 0; inmod < 8; ++inmod) {
236 ins.outmod = outmod;
237 ins.src_neg[0] = (inmod & 0x1);
238 ins.src_neg[1] = (inmod & 0x2);
239 ins.src_neg[2] = (inmod & 0x4);
240
241 if (!bit_test_single(dev, &ins, input, true, debug)) {
242 fprintf(stderr, "FAIL: fma%u%s.%u\n",
243 size,
244 outmod ? bi_output_mod_name(outmod) : ".none",
245 inmod);
246 }
247 }
248 }
249 }
250
251 static void
252 bit_csel_helper(struct panfrost_device *dev,
253 unsigned size, uint32_t *input, enum bit_debug debug)
254 {
255 bi_instruction ins = bit_ins(BI_CSEL, 4, nir_type_uint, size);
256
257 /* SCHEDULER: We can only read 3 registers at once. */
258 ins.src[2] = ins.src[0];
259
260 for (enum bi_cond cond = BI_COND_LT; cond <= BI_COND_NE; ++cond) {
261 ins.csel_cond = cond;
262
263 if (!bit_test_single(dev, &ins, input, true, debug)) {
264 fprintf(stderr, "FAIL: csel%u.%s\n",
265 size, bi_cond_name(cond));
266 }
267 }
268 }
269
270 static void
271 bit_special_helper(struct panfrost_device *dev,
272 unsigned size, uint32_t *input, enum bit_debug debug)
273 {
274 bi_instruction ins = bit_ins(BI_SPECIAL, 1, nir_type_float, size);
275
276 for (enum bi_special_op op = BI_SPECIAL_FRCP; op <= BI_SPECIAL_FRSQ; ++op) {
277 for (unsigned c = 0; c < ((size == 16) ? 2 : 1); ++c) {
278 ins.op.special = op;
279 ins.swizzle[0][0] = c;
280
281 if (!bit_test_single(dev, &ins, input, false, debug)) {
282 fprintf(stderr, "FAIL: special%u.%s\n",
283 size, bi_special_op_name(op));
284 }
285 }
286 }
287 }
288
289 static void
290 bit_convert_helper(struct panfrost_device *dev, unsigned from_size,
291 unsigned to_size, unsigned cx, unsigned cy, bool FMA,
292 enum bifrost_roundmode roundmode,
293 uint32_t *input, enum bit_debug debug)
294 {
295 bi_instruction ins = {
296 .type = BI_CONVERT,
297 .dest = BIR_INDEX_REGISTER | 0,
298 .writemask = 0xF,
299 .src = { BIR_INDEX_REGISTER | 0 }
300 };
301
302 nir_alu_type Ts[3] = { nir_type_float, nir_type_uint, nir_type_int };
303
304 for (unsigned from_base = 0; from_base < 3; ++from_base) {
305 for (unsigned to_base = 0; to_base < 3; ++to_base) {
306 /* Discard invalid combinations.. */
307 if ((from_size == to_size) && (from_base == to_base))
308 continue;
309
310 /* Can't switch signedness */
311 if (from_base && to_base)
312 continue;
313
314 /* No F16_TO_I32, etc */
315 if (from_size != to_size && from_base == 0 && to_base)
316 continue;
317
318 if (from_size != to_size && from_base && to_base == 0)
319 continue;
320
321 /* No need, just ignore the upper half */
322 if (from_size > to_size && from_base == to_base && from_base)
323 continue;
324
325 ins.dest_type = Ts[to_base] | to_size;
326 ins.src_types[0] = Ts[from_base] | from_size;
327 ins.roundmode = roundmode;
328 ins.swizzle[0][0] = cx;
329 ins.swizzle[0][1] = cy;
330
331 if (!bit_test_single(dev, &ins, input, FMA, debug)) {
332 fprintf(stderr, "FAIL: convert.%u-%u.%u-%u.%u%u\n",
333 from_base, from_size,
334 to_base, to_size,
335 cx, cy);
336 }
337 }
338 }
339 }
340
341 void
342 bit_packing(struct panfrost_device *dev, enum bit_debug debug)
343 {
344 float input32[4];
345 uint16_t input16[8];
346
347 bit_generate_float4(input32);
348 bit_generate_half8(input16);
349
350 for (unsigned sz = 16; sz <= 32; sz *= 2) {
351 uint32_t *input =
352 (sz == 16) ? (uint32_t *) input16 :
353 (uint32_t *) input32;
354
355 bit_fmod_helper(dev, BI_ADD, sz, true, input, debug, 0);
356
357 if (sz == 32) {
358 bit_fmod_helper(dev, BI_ADD, sz, false, input, debug, 0);
359 bit_fmod_helper(dev, BI_MINMAX, sz, false, input, debug, BI_MINMAX_MIN);
360 bit_fmod_helper(dev, BI_MINMAX, sz, false, input, debug, BI_MINMAX_MAX);
361 }
362
363 bit_fma_helper(dev, sz, input, debug);
364 }
365
366 for (unsigned sz = 32; sz <= 32; sz *= 2)
367 bit_csel_helper(dev, sz, (uint32_t *) input32, debug);
368
369 float special[4] = { 0.9 };
370 uint32_t special16[4] = { _mesa_float_to_half(special[0]) | (_mesa_float_to_half(0.2) << 16) };
371
372 for (unsigned sz = 16; sz <= 32; sz *= 2) {
373 uint32_t *input =
374 (sz == 16) ? special16 :
375 (uint32_t *) special;
376
377 bit_special_helper(dev, sz, input, debug);
378 }
379
380 for (unsigned rm = 0; rm < 4; ++rm) {
381 bit_convert_helper(dev, 32, 32, 0, 0, false, rm, (uint32_t *) input32, debug);
382
383 for (unsigned c = 0; c < 2; ++c)
384 bit_convert_helper(dev, 32, 16, c, 0, false, rm, (uint32_t *) input32, debug);
385
386 bit_convert_helper(dev, 16, 32, 0, 0, false, rm, (uint32_t *) input16, debug);
387
388 for (unsigned c = 0; c < 4; ++c)
389 bit_convert_helper(dev, 16, 16, c & 1, c >> 1, false, rm, (uint32_t *) input16, debug);
390 }
391
392
393 }