pan/bi: Fix duplicated source in ADD.v2f16
[mesa.git] / src / panfrost / bifrost / bi_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
24 #include "compiler.h"
25
26 #define RETURN_PACKED(str) { \
27 uint64_t temp = 0; \
28 memcpy(&temp, &str, sizeof(str)); \
29 return temp; \
30 }
31
32 /* This file contains the final passes of the compiler. Running after
33 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
34 * bits on the wire (as well as fixup branches) */
35
36 static uint64_t
37 bi_pack_header(bi_clause *clause, bi_clause *next, bool is_fragment)
38 {
39 struct bifrost_header header = {
40 .back_to_back = clause->back_to_back,
41 .no_end_of_shader = (next != NULL),
42 .elide_writes = is_fragment,
43 .branch_cond = clause->branch_conditional,
44 .datareg_writebarrier = clause->data_register_write_barrier,
45 .datareg = clause->data_register,
46 .scoreboard_deps = next ? next->dependencies : 0,
47 .scoreboard_index = clause->scoreboard_id,
48 .clause_type = clause->clause_type,
49 .next_clause_type = next ? next->clause_type : 0,
50 };
51
52 uint64_t u = 0;
53 memcpy(&u, &header, sizeof(header));
54 return u;
55 }
56
57 /* Represents the assignment of ports for a given bundle */
58
59 struct bi_registers {
60 /* Register to assign to each port */
61 unsigned port[4];
62
63 /* Read ports can be disabled */
64 bool enabled[2];
65
66 /* Should we write FMA? what about ADD? If only a single port is
67 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
68 bool write_fma, write_add;
69
70 /* Should we read with port 3? */
71 bool read_port3;
72
73 /* Packed uniform/constant */
74 uint8_t uniform_constant;
75
76 /* Whether writes are actually for the last instruction */
77 bool first_instruction;
78 };
79
80 /* The uniform/constant slot allows loading a contiguous 64-bit immediate or
81 * pushed uniform per bundle. Figure out which one we need in the bundle (the
82 * scheduler needs to ensure we only have one type per bundle), validate
83 * everything, and rewrite away the register/uniform indices to use 3-bit
84 * sources directly. */
85
86 static unsigned
87 bi_lookup_constant(bi_clause *clause, uint64_t cons, bool *hi, bool b64)
88 {
89 uint64_t want = (cons >> 4);
90
91 for (unsigned i = 0; i < clause->constant_count; ++i) {
92 /* Only check top 60-bits since that's what's actually embedded
93 * in the clause, the bottom 4-bits are bundle-inline */
94
95 unsigned candidates[2] = {
96 clause->constants[i] >> 4,
97 clause->constants[i] >> 36
98 };
99
100 if (!b64)
101 candidates[0] &= 0xFFFFFFFF;
102
103 if (candidates[0] == want)
104 return i;
105
106 if (candidates[1] == want && !b64) {
107 *hi = true;
108 return i;
109 }
110 }
111
112 unreachable("Invalid constant accessed");
113 }
114
115 static unsigned
116 bi_constant_field(unsigned idx)
117 {
118 assert(idx <= 5);
119
120 const unsigned values[] = {
121 4, 5, 6, 7, 2, 3
122 };
123
124 return values[idx] << 4;
125 }
126
127 static bool
128 bi_assign_uniform_constant_single(
129 struct bi_registers *regs,
130 bi_clause *clause,
131 bi_instruction *ins, bool assigned, bool fast_zero)
132 {
133 if (!ins)
134 return assigned;
135
136 bi_foreach_src(ins, s) {
137 if (s == 0 && (ins->type == BI_LOAD_VAR_ADDRESS || ins->type == BI_LOAD_ATTR)) continue;
138
139 if (ins->src[s] & BIR_INDEX_CONSTANT) {
140 bool hi = false;
141 bool b64 = nir_alu_type_get_type_size(ins->src_types[s]) > 32;
142 uint64_t cons = bi_get_immediate(ins, ins->src[s]);
143 unsigned idx = bi_lookup_constant(clause, cons, &hi, b64);
144 unsigned f = bi_constant_field(idx) | (cons & 0xF);
145
146 if (assigned && regs->uniform_constant != f)
147 unreachable("Mismatched uniform/const field: imm");
148
149 regs->uniform_constant = f;
150 ins->src[s] = BIR_INDEX_PASS | (hi ? BIFROST_SRC_CONST_HI : BIFROST_SRC_CONST_LO);
151 assigned = true;
152 } else if (ins->src[s] & BIR_INDEX_ZERO && (ins->type == BI_LOAD_UNIFORM || ins->type == BI_LOAD_VAR)) {
153 /* XXX: HACK UNTIL WE HAVE HI MATCHING DUE TO OVERFLOW XXX */
154 ins->src[s] = BIR_INDEX_PASS | BIFROST_SRC_CONST_HI;
155 } else if (ins->src[s] & BIR_INDEX_ZERO && !fast_zero) {
156 /* FMAs have a fast zero port, ADD needs to use the
157 * uniform/const port's special 0 mode handled here */
158 unsigned f = 0;
159
160 if (assigned && regs->uniform_constant != f)
161 unreachable("Mismatched uniform/const field: 0");
162
163 regs->uniform_constant = f;
164 ins->src[s] = BIR_INDEX_PASS | BIFROST_SRC_CONST_LO;
165 assigned = true;
166 } else if (s & BIR_INDEX_UNIFORM) {
167 unreachable("Push uniforms not implemented yet");
168 }
169 }
170
171 return assigned;
172 }
173
174 static void
175 bi_assign_uniform_constant(
176 bi_clause *clause,
177 struct bi_registers *regs,
178 bi_bundle bundle)
179 {
180 bool assigned =
181 bi_assign_uniform_constant_single(regs, clause, bundle.fma, false, true);
182
183 bi_assign_uniform_constant_single(regs, clause, bundle.add, assigned, false);
184 }
185
186 /* Assigns a port for reading, before anything is written */
187
188 static void
189 bi_assign_port_read(struct bi_registers *regs, unsigned src)
190 {
191 /* We only assign for registers */
192 if (!(src & BIR_INDEX_REGISTER))
193 return;
194
195 unsigned reg = src & ~BIR_INDEX_REGISTER;
196
197 /* Check if we already assigned the port */
198 for (unsigned i = 0; i <= 1; ++i) {
199 if (regs->port[i] == reg && regs->enabled[i])
200 return;
201 }
202
203 if (regs->port[3] == reg && regs->read_port3)
204 return;
205
206 /* Assign it now */
207
208 for (unsigned i = 0; i <= 1; ++i) {
209 if (!regs->enabled[i]) {
210 regs->port[i] = reg;
211 regs->enabled[i] = true;
212 return;
213 }
214 }
215
216 if (!regs->read_port3) {
217 regs->port[3] = reg;
218 regs->read_port3 = true;
219 }
220 }
221
222 static struct bi_registers
223 bi_assign_ports(bi_bundle now, bi_bundle prev)
224 {
225 struct bi_registers regs = { 0 };
226
227 /* We assign ports for the main register mechanism. Special ops
228 * use the data registers, which has its own mechanism entirely
229 * and thus gets skipped over here. */
230
231 unsigned read_dreg = now.add &&
232 bi_class_props[now.add->type] & BI_DATA_REG_SRC;
233
234 unsigned write_dreg = prev.add &&
235 bi_class_props[prev.add->type] & BI_DATA_REG_DEST;
236
237 /* First, assign reads */
238
239 if (now.fma)
240 bi_foreach_src(now.fma, src)
241 bi_assign_port_read(&regs, now.fma->src[src]);
242
243 if (now.add) {
244 bi_foreach_src(now.add, src) {
245 if (!(src == 0 && read_dreg))
246 bi_assign_port_read(&regs, now.add->src[src]);
247 }
248 }
249
250 /* Next, assign writes */
251
252 if (prev.fma && prev.fma->dest & BIR_INDEX_REGISTER) {
253 regs.port[2] = prev.fma->dest & ~BIR_INDEX_REGISTER;
254 regs.write_fma = true;
255 }
256
257 if (prev.add && prev.add->dest & BIR_INDEX_REGISTER && !write_dreg) {
258 unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
259
260 if (regs.write_fma) {
261 /* Scheduler constraint: cannot read 3 and write 2 */
262 assert(!regs.read_port3);
263 regs.port[3] = r;
264 } else {
265 regs.port[2] = r;
266 }
267
268 regs.write_add = true;
269 }
270
271 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
272
273 if (regs.enabled[0] && regs.enabled[1] && regs.port[1] < regs.port[0]) {
274 unsigned temp = regs.port[0];
275 regs.port[0] = regs.port[1];
276 regs.port[1] = temp;
277 }
278
279 return regs;
280 }
281
282 /* Determines the register control field, ignoring the first? flag */
283
284 static enum bifrost_reg_control
285 bi_pack_register_ctrl_lo(struct bi_registers r)
286 {
287 if (r.write_fma) {
288 if (r.write_add) {
289 assert(!r.read_port3);
290 return BIFROST_WRITE_ADD_P2_FMA_P3;
291 } else {
292 if (r.read_port3)
293 return BIFROST_WRITE_FMA_P2_READ_P3;
294 else
295 return BIFROST_WRITE_FMA_P2;
296 }
297 } else if (r.write_add) {
298 if (r.read_port3)
299 return BIFROST_WRITE_ADD_P2_READ_P3;
300 else
301 return BIFROST_WRITE_ADD_P2;
302 } else if (r.read_port3)
303 return BIFROST_READ_P3;
304 else
305 return BIFROST_REG_NONE;
306 }
307
308 /* Ditto but account for the first? flag this time */
309
310 static enum bifrost_reg_control
311 bi_pack_register_ctrl(struct bi_registers r)
312 {
313 enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
314
315 if (r.first_instruction) {
316 if (ctrl == BIFROST_REG_NONE)
317 ctrl = BIFROST_FIRST_NONE;
318 else
319 ctrl |= BIFROST_FIRST_NONE;
320 }
321
322 return ctrl;
323 }
324
325 static uint64_t
326 bi_pack_registers(struct bi_registers regs)
327 {
328 enum bifrost_reg_control ctrl = bi_pack_register_ctrl(regs);
329 struct bifrost_regs s;
330 uint64_t packed = 0;
331
332 if (regs.enabled[1]) {
333 /* Gotta save that bit!~ Required by the 63-x trick */
334 assert(regs.port[1] > regs.port[0]);
335 assert(regs.enabled[0]);
336
337 /* Do the 63-x trick, see docs/disasm */
338 if (regs.port[0] > 31) {
339 regs.port[0] = 63 - regs.port[0];
340 regs.port[1] = 63 - regs.port[1];
341 }
342
343 assert(regs.port[0] <= 31);
344 assert(regs.port[1] <= 63);
345
346 s.ctrl = ctrl;
347 s.reg1 = regs.port[1];
348 s.reg0 = regs.port[0];
349 } else {
350 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
351 s.reg1 = ctrl << 2;
352
353 if (regs.enabled[0]) {
354 /* Bit 0 upper bit of port 0 */
355 s.reg1 |= (regs.port[0] >> 5);
356
357 /* Rest of port 0 in usual spot */
358 s.reg0 = (regs.port[0] & 0b11111);
359 } else {
360 /* Bit 1 set if port 0 also disabled */
361 s.reg1 |= (1 << 1);
362 }
363 }
364
365 /* When port 3 isn't used, we have to set it to port 2, and vice versa,
366 * or we an INSTR_INVALID_ENC is raised. The reason is unknown. */
367
368 if (!regs.port[3])
369 regs.port[3] = regs.port[2];
370
371 if (!regs.port[2])
372 regs.port[2] = regs.port[3];
373
374 s.reg3 = regs.port[3];
375 s.reg2 = regs.port[2];
376 s.uniform_const = regs.uniform_constant;
377
378 memcpy(&packed, &s, sizeof(s));
379 return packed;
380 }
381
382 static void
383 bi_set_data_register(bi_clause *clause, unsigned idx)
384 {
385 assert(idx & BIR_INDEX_REGISTER);
386 unsigned reg = idx & ~BIR_INDEX_REGISTER;
387 assert(reg <= 63);
388 clause->data_register = reg;
389 }
390
391 static void
392 bi_read_data_register(bi_clause *clause, bi_instruction *ins)
393 {
394 bi_set_data_register(clause, ins->src[0]);
395 }
396
397 static void
398 bi_write_data_register(bi_clause *clause, bi_instruction *ins)
399 {
400 bi_set_data_register(clause, ins->dest);
401 }
402
403 static enum bifrost_packed_src
404 bi_get_src_reg_port(struct bi_registers *regs, unsigned src)
405 {
406 unsigned reg = src & ~BIR_INDEX_REGISTER;
407
408 if (regs->port[0] == reg && regs->enabled[0])
409 return BIFROST_SRC_PORT0;
410 else if (regs->port[1] == reg && regs->enabled[1])
411 return BIFROST_SRC_PORT1;
412 else if (regs->port[3] == reg && regs->read_port3)
413 return BIFROST_SRC_PORT3;
414 else
415 unreachable("Tried to access register with no port");
416 }
417
418 static enum bifrost_packed_src
419 bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_fma)
420 {
421 unsigned src = ins->src[s];
422
423 if (src & BIR_INDEX_REGISTER)
424 return bi_get_src_reg_port(regs, src);
425 else if (src & BIR_INDEX_ZERO && is_fma)
426 return BIFROST_SRC_STAGE;
427 else if (src & BIR_INDEX_PASS)
428 return src & ~BIR_INDEX_PASS;
429 else
430 unreachable("Unknown src");
431 }
432
433 /* Constructs a packed 2-bit swizzle for a 16-bit vec2 source. Source must be
434 * 16-bit and written components must correspond to valid swizzles (component x
435 * or y). */
436
437 static unsigned
438 bi_swiz16(bi_instruction *ins, unsigned src)
439 {
440 assert(nir_alu_type_get_type_size(ins->src_types[src]) == 16);
441 unsigned swizzle = 0;
442
443 for (unsigned c = 0; c < 2; ++c) {
444 if (!bi_writes_component(ins, src)) continue;
445
446 unsigned k = ins->swizzle[src][c];
447 assert(k < 1);
448 swizzle |= (k << c);
449 }
450
451 return swizzle;
452 }
453
454 static unsigned
455 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
456 {
457 /* (-a)(-b) = ab, so we only need one negate bit */
458 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
459
460 if (ins->dest_type == nir_type_float32) {
461 struct bifrost_fma_fma pack = {
462 .src0 = bi_get_src(ins, regs, 0, true),
463 .src1 = bi_get_src(ins, regs, 1, true),
464 .src2 = bi_get_src(ins, regs, 2, true),
465 .src0_abs = ins->src_abs[0],
466 .src1_abs = ins->src_abs[1],
467 .src2_abs = ins->src_abs[2],
468 .src0_neg = negate_mul,
469 .src2_neg = ins->src_neg[2],
470 .outmod = ins->outmod,
471 .roundmode = ins->roundmode,
472 .op = BIFROST_FMA_OP_FMA
473 };
474
475 RETURN_PACKED(pack);
476 } else if (ins->dest_type == nir_type_float16) {
477 struct bifrost_fma_fma16 pack = {
478 .src0 = bi_get_src(ins, regs, 0, true),
479 .src1 = bi_get_src(ins, regs, 1, true),
480 .src2 = bi_get_src(ins, regs, 2, true),
481 .swizzle_0 = bi_swiz16(ins, 0),
482 .swizzle_1 = bi_swiz16(ins, 1),
483 .swizzle_2 = bi_swiz16(ins, 2),
484 .src0_neg = negate_mul,
485 .src2_neg = ins->src_neg[2],
486 .outmod = ins->outmod,
487 .roundmode = ins->roundmode,
488 .op = BIFROST_FMA_OP_FMA16
489 };
490
491 RETURN_PACKED(pack);
492 } else {
493 unreachable("Invalid fma dest type");
494 }
495 }
496
497 static unsigned
498 bi_pack_fma_add_f32(bi_instruction *ins, struct bi_registers *regs)
499 {
500 struct bifrost_fma_add pack = {
501 .src0 = bi_get_src(ins, regs, 0, true),
502 .src1 = bi_get_src(ins, regs, 1, true),
503 .src0_abs = ins->src_abs[0],
504 .src1_abs = ins->src_abs[1],
505 .src0_neg = ins->src_neg[0],
506 .src1_neg = ins->src_neg[1],
507 .unk = 0x0,
508 .outmod = ins->outmod,
509 .roundmode = ins->roundmode,
510 .op = BIFROST_FMA_OP_FADD32
511 };
512
513 RETURN_PACKED(pack);
514 }
515
516 static unsigned
517 bi_pack_fma_addmin_f16(bi_instruction *ins, struct bi_registers *regs)
518 {
519 unsigned op =
520 (ins->type == BI_ADD) ? BIFROST_FMA_OP_FADD16 :
521 (ins->op.minmax == BI_MINMAX_MIN) ? BIFROST_FMA_OP_FMIN16 :
522 BIFROST_FMA_OP_FMAX16;
523
524 /* Absolute values are packed in a quirky way. Let k = src1 < src0. Let
525 * l be an auxiliary bit we encode. Then the hardware determines:
526 *
527 * abs0 = l || k
528 * abs1 = l && k
529 *
530 * Since add/min/max are commutative, this saves a bit by using the
531 * order of the operands as a bit (k). To pack this, first note:
532 *
533 * (l && k) implies (l || k).
534 *
535 * That is, if the second argument is abs'd, then the first argument
536 * also has abs. So there are three cases:
537 *
538 * Case 0: Neither src has absolute value. Then we have l = k = 0.
539 *
540 * Case 1: Exactly one src has absolute value. Assign that source to
541 * src0 and the other source to src1. Compute k = src1 < src0 based on
542 * that assignment. Then l = ~k.
543 *
544 * Case 2: Both sources have absolute value. Then we have l = k = 1.
545 * Note to force k = 1 requires that (src1 < src0) OR (src0 < src1).
546 * That is, this encoding is only valid if src1 and src0 are distinct.
547 * This is a scheduling restriction (XXX); if an op of this type
548 * requires both identical sources to have abs value, then we must
549 * schedule to ADD (which does not use this ordering trick).
550 */
551
552 unsigned abs_0 = ins->src_abs[0], abs_1 = ins->src_abs[1];
553 unsigned src_0 = bi_get_src(ins, regs, 0, true);
554 unsigned src_1 = bi_get_src(ins, regs, 1, true);
555 bool l = false;
556
557 if (!abs_0 && !abs_1) {
558 /* Force k = 0 <===> NOT(src1 < src0) <==> src1 >= src0 */
559 if (src_0 < src_1) {
560 unsigned tmp = src_0;
561 src_0 = src_1;
562 src_1 = tmp;
563 }
564 } else if (abs_0 && !abs_1) {
565 l = src_1 >= src_0;
566 } else if (abs_1 && !abs_0) {
567 unsigned tmp = src_0;
568 src_0 = src_1;
569 src_0 = tmp;
570
571 l = src_1 >= src_0;
572 } else {
573 if (src_0 >= src_1) {
574 unsigned tmp = src_0;
575 src_0 = src_1;
576 src_1 = tmp;
577 }
578
579 l = true;
580 }
581
582 struct bifrost_fma_add_minmax16 pack = {
583 .src0 = src_0,
584 .src1 = src_1,
585 .src0_neg = ins->src_neg[0],
586 .src1_neg = ins->src_neg[1],
587 .abs1 = l,
588 .outmod = ins->outmod,
589 .mode = (ins->type == BI_ADD) ? ins->roundmode : ins->minmax,
590 .op = op
591 };
592
593 RETURN_PACKED(pack);
594 }
595
596 static unsigned
597 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
598 {
599 if (ins->dest_type == nir_type_float32)
600 return bi_pack_fma_add_f32(ins, regs);
601 else if(ins->dest_type == nir_type_float16)
602 return bi_pack_fma_addmin_f16(ins, regs);
603 else
604 unreachable("Unknown FMA/ADD type");
605 }
606
607 static unsigned
608 bi_pack_fma_1src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
609 {
610 struct bifrost_fma_inst pack = {
611 .src0 = bi_get_src(ins, regs, 0, true),
612 .op = op
613 };
614
615 RETURN_PACKED(pack);
616 }
617
618 static enum bifrost_csel_cond
619 bi_cond_to_csel(enum bi_cond cond, bool *flip, bool *invert, nir_alu_type T)
620 {
621 nir_alu_type B = nir_alu_type_get_base_type(T);
622 unsigned idx = (B == nir_type_float) ? 0 :
623 ((B == nir_type_int) ? 1 : 2);
624
625 switch (cond){
626 case BI_COND_LT:
627 *flip = true;
628 case BI_COND_GT: {
629 const enum bifrost_csel_cond ops[] = {
630 BIFROST_FGT_F,
631 BIFROST_IGT_I,
632 BIFROST_UGT_I
633 };
634
635 return ops[idx];
636 }
637 case BI_COND_LE:
638 *flip = true;
639 case BI_COND_GE: {
640 const enum bifrost_csel_cond ops[] = {
641 BIFROST_FGE_F,
642 BIFROST_IGE_I,
643 BIFROST_UGE_I
644 };
645
646 return ops[idx];
647 }
648 case BI_COND_NE:
649 *invert = true;
650 case BI_COND_EQ: {
651 const enum bifrost_csel_cond ops[] = {
652 BIFROST_FEQ_F,
653 BIFROST_IEQ_F,
654 BIFROST_IEQ_F /* sign is irrelevant */
655 };
656
657 return ops[idx];
658 }
659 default:
660 unreachable("Invalid op for csel");
661 }
662 }
663
664 static unsigned
665 bi_pack_fma_csel(bi_instruction *ins, struct bi_registers *regs)
666 {
667 /* TODO: Use csel3 as well */
668 bool flip = false, invert = false;
669
670 enum bifrost_csel_cond cond =
671 bi_cond_to_csel(ins->csel_cond, &flip, &invert, ins->src_types[0]);
672
673 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
674
675 unsigned cmp_0 = (flip ? 3 : 0);
676 unsigned cmp_1 = (flip ? 0 : 3);
677 unsigned res_0 = (invert ? 2 : 1);
678 unsigned res_1 = (invert ? 1 : 2);
679
680 struct bifrost_csel4 pack = {
681 .src0 = bi_get_src(ins, regs, cmp_0, true),
682 .src1 = bi_get_src(ins, regs, cmp_1, true),
683 .src2 = bi_get_src(ins, regs, res_0, true),
684 .src3 = bi_get_src(ins, regs, res_1, true),
685 .cond = cond,
686 .op = (size == 16) ? BIFROST_FMA_OP_CSEL4_V16 :
687 BIFROST_FMA_OP_CSEL4
688 };
689
690 RETURN_PACKED(pack);
691 }
692
693 /* We have a single convert opcode in the IR but a number of opcodes that could
694 * come out. In particular we have native opcodes for:
695 *
696 * [ui]16 --> [fui]32 -- int16_to_32
697 * f16 --> f32 -- float16_to_32
698 * f32 --> f16 -- float32_to_16
699 * f32 --> [ui]32 -- float32_to_int
700 * [ui]32 --> f32 -- int_to_float32
701 * [fui]16 --> [fui]16 -- f2i_i2f16
702 */
703
704 static unsigned
705 bi_pack_fma_convert(bi_instruction *ins, struct bi_registers *regs)
706 {
707 nir_alu_type from_base = nir_alu_type_get_base_type(ins->src_types[0]);
708 unsigned from_size = nir_alu_type_get_type_size(ins->src_types[0]);
709 bool from_unsigned = from_base == nir_type_uint;
710
711 nir_alu_type to_base = nir_alu_type_get_base_type(ins->dest_type);
712 unsigned to_size = nir_alu_type_get_type_size(ins->dest_type);
713 bool to_unsigned = to_base == nir_type_uint;
714
715 /* Sanity check */
716 assert((from_base != to_base) || (from_size != to_size));
717 assert((MAX2(from_size, to_size) / MIN2(from_size, to_size)) <= 2);
718
719 if (from_size == 16 && to_size == 16) {
720 /* f2i_i2f16 */
721 unreachable("i16 not yet implemented");
722 } else if (from_size == 32 && to_size == 32) {
723 unsigned op = 0;
724
725 if (from_base == nir_type_float) {
726 op = BIFROST_FMA_FLOAT32_TO_INT(to_unsigned);
727 } else {
728 op = BIFROST_FMA_INT_TO_FLOAT32(from_unsigned);
729 }
730
731 return bi_pack_fma_1src(ins, regs, op);
732 } else if (from_size == 16 && to_size == 32) {
733 bool from_y = ins->swizzle[0][0];
734
735 if (from_base == nir_type_float) {
736 return bi_pack_fma_1src(ins, regs,
737 BIFROST_FMA_FLOAT16_TO_32(from_y));
738 } else {
739 unreachable("i16 not yet implemented");
740 }
741 } else if (from_size == 32 && to_size == 16) {
742 if (from_base == nir_type_float) {
743 /* TODO: second vectorized source? */
744 struct bifrost_fma_2src pack = {
745 .src0 = bi_get_src(ins, regs, 0, true),
746 .src1 = BIFROST_SRC_STAGE, /* 0 */
747 .op = BIFROST_FMA_FLOAT32_TO_16
748 };
749
750 RETURN_PACKED(pack);
751 } else {
752 unreachable("i16 not yet implemented");
753 }
754 }
755
756 unreachable("Unknown convert");
757 }
758
759 static unsigned
760 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
761 {
762 if (!bundle.fma)
763 return BIFROST_FMA_NOP;
764
765 switch (bundle.fma->type) {
766 case BI_ADD:
767 return bi_pack_fma_add(bundle.fma, regs);
768 case BI_CMP:
769 case BI_BITWISE:
770 return BIFROST_FMA_NOP;
771 case BI_CONVERT:
772 return bi_pack_fma_convert(bundle.fma, regs);
773 case BI_CSEL:
774 return bi_pack_fma_csel(bundle.fma, regs);
775 case BI_FMA:
776 return bi_pack_fma_fma(bundle.fma, regs);
777 case BI_FREXP:
778 case BI_ISUB:
779 case BI_MINMAX:
780 return BIFROST_FMA_NOP;
781 case BI_MOV:
782 return bi_pack_fma_1src(bundle.fma, regs, BIFROST_FMA_OP_MOV);
783 case BI_SHIFT:
784 case BI_SWIZZLE:
785 case BI_ROUND:
786 return BIFROST_FMA_NOP;
787 default:
788 unreachable("Cannot encode class as FMA");
789 }
790 }
791
792 static unsigned
793 bi_pack_add_ld_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
794 {
795 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
796 assert(size == 32 || size == 16);
797
798 unsigned op = (size == 32) ?
799 BIFROST_ADD_OP_LD_VAR_32 :
800 BIFROST_ADD_OP_LD_VAR_16;
801
802 unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
803 unsigned channels = util_bitcount(cmask);
804 assert(cmask == ((1 << channels) - 1));
805
806 unsigned packed_addr = 0;
807
808 if (ins->src[0] & BIR_INDEX_CONSTANT) {
809 /* Direct uses address field directly */
810 packed_addr = bi_get_immediate(ins, ins->src[0]);
811 assert(packed_addr < 0b1000);
812 } else {
813 /* Indirect gets an extra source */
814 packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
815 }
816
817 /* The destination is thrown in the data register */
818 assert(ins->dest & BIR_INDEX_REGISTER);
819 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
820
821 assert(channels >= 1 && channels <= 4);
822
823 struct bifrost_ld_var pack = {
824 .src0 = bi_get_src(ins, regs, 1, false),
825 .addr = packed_addr,
826 .channels = MALI_POSITIVE(channels),
827 .interp_mode = ins->load_vary.interp_mode,
828 .reuse = ins->load_vary.reuse,
829 .flat = ins->load_vary.flat,
830 .op = op
831 };
832
833 RETURN_PACKED(pack);
834 }
835
836 static unsigned
837 bi_pack_add_2src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
838 {
839 struct bifrost_add_2src pack = {
840 .src0 = bi_get_src(ins, regs, 0, true),
841 .src1 = bi_get_src(ins, regs, 1, true),
842 .op = op
843 };
844
845 RETURN_PACKED(pack);
846 }
847
848 static unsigned
849 bi_pack_add_ld_ubo(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
850 {
851 unsigned components = bi_load32_components(ins);
852
853 const unsigned ops[4] = {
854 BIFROST_ADD_OP_LD_UBO_1,
855 BIFROST_ADD_OP_LD_UBO_2,
856 BIFROST_ADD_OP_LD_UBO_3,
857 BIFROST_ADD_OP_LD_UBO_4
858 };
859
860 bi_write_data_register(clause, ins);
861 return bi_pack_add_2src(ins, regs, ops[components - 1]);
862 }
863
864 static enum bifrost_ldst_type
865 bi_pack_ldst_type(nir_alu_type T)
866 {
867 switch (T) {
868 case nir_type_float16: return BIFROST_LDST_F16;
869 case nir_type_float32: return BIFROST_LDST_F32;
870 case nir_type_int32: return BIFROST_LDST_I32;
871 case nir_type_uint32: return BIFROST_LDST_U32;
872 default: unreachable("Invalid type loaded");
873 }
874 }
875
876 static unsigned
877 bi_pack_add_ld_var_addr(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
878 {
879 struct bifrost_ld_var_addr pack = {
880 .src0 = bi_get_src(ins, regs, 1, false),
881 .src1 = bi_get_src(ins, regs, 2, false),
882 .location = bi_get_immediate(ins, ins->src[0]),
883 .type = bi_pack_ldst_type(ins->src_types[3]),
884 .op = BIFROST_ADD_OP_LD_VAR_ADDR
885 };
886
887 bi_write_data_register(clause, ins);
888 RETURN_PACKED(pack);
889 }
890
891 static unsigned
892 bi_pack_add_ld_attr(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
893 {
894 struct bifrost_ld_attr pack = {
895 .src0 = bi_get_src(ins, regs, 1, false),
896 .src1 = bi_get_src(ins, regs, 2, false),
897 .location = bi_get_immediate(ins, ins->src[0]),
898 .channels = MALI_POSITIVE(bi_load32_components(ins)),
899 .type = bi_pack_ldst_type(ins->dest_type),
900 .op = BIFROST_ADD_OP_LD_ATTR
901 };
902
903 bi_write_data_register(clause, ins);
904 RETURN_PACKED(pack);
905 }
906
907 static unsigned
908 bi_pack_add_st_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
909 {
910 assert(ins->store_channels >= 1 && ins->store_channels <= 4);
911
912 struct bifrost_st_vary pack = {
913 .src0 = bi_get_src(ins, regs, 1, false),
914 .src1 = bi_get_src(ins, regs, 2, false),
915 .src2 = bi_get_src(ins, regs, 3, false),
916 .channels = MALI_POSITIVE(ins->store_channels),
917 .op = BIFROST_ADD_OP_ST_VAR
918 };
919
920 bi_read_data_register(clause, ins);
921 RETURN_PACKED(pack);
922 }
923
924 static unsigned
925 bi_pack_add_atest(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
926 {
927 /* TODO: fp16 */
928 assert(ins->src_types[1] == nir_type_float32);
929
930 struct bifrost_add_atest pack = {
931 .src0 = bi_get_src(ins, regs, 0, false),
932 .src1 = bi_get_src(ins, regs, 1, false),
933 .component = 1, /* Set for fp32 */
934 .op = BIFROST_ADD_OP_ATEST,
935 };
936
937 /* Despite *also* writing with the usual mechanism... quirky and
938 * perhaps unnecessary, but let's match the blob */
939 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
940
941 RETURN_PACKED(pack);
942 }
943
944 static unsigned
945 bi_pack_add_blend(bi_instruction *ins, struct bi_registers *regs)
946 {
947 struct bifrost_add_inst pack = {
948 .src0 = bi_get_src(ins, regs, 0, false),
949 .op = BIFROST_ADD_OP_BLEND
950 };
951
952 /* TODO: Pack location in uniform_const */
953 assert(ins->blend_location == 0);
954
955 RETURN_PACKED(pack);
956 }
957
958 static unsigned
959 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
960 {
961 if (!bundle.add)
962 return BIFROST_ADD_NOP;
963
964 switch (bundle.add->type) {
965 case BI_ADD:
966 return BIFROST_ADD_NOP;
967 case BI_ATEST:
968 return bi_pack_add_atest(clause, bundle.add, regs);
969 case BI_BRANCH:
970 case BI_CMP:
971 return BIFROST_ADD_NOP;
972 case BI_BLEND:
973 return bi_pack_add_blend(bundle.add, regs);
974 case BI_BITWISE:
975 case BI_CONVERT:
976 case BI_DISCARD:
977 case BI_FREXP:
978 case BI_ISUB:
979 case BI_LOAD:
980 return BIFROST_ADD_NOP;
981 case BI_LOAD_ATTR:
982 return bi_pack_add_ld_attr(clause, bundle.add, regs);
983 case BI_LOAD_UNIFORM:
984 return bi_pack_add_ld_ubo(clause, bundle.add, regs);
985 case BI_LOAD_VAR:
986 return bi_pack_add_ld_vary(clause, bundle.add, regs);
987 case BI_LOAD_VAR_ADDRESS:
988 return bi_pack_add_ld_var_addr(clause, bundle.add, regs);
989 case BI_MINMAX:
990 case BI_MOV:
991 case BI_SHIFT:
992 case BI_STORE:
993 return BIFROST_ADD_NOP;
994 case BI_STORE_VAR:
995 return bi_pack_add_st_vary(clause, bundle.add, regs);
996 case BI_SPECIAL:
997 case BI_SWIZZLE:
998 case BI_TEX:
999 case BI_ROUND:
1000 return BIFROST_ADD_NOP;
1001 default:
1002 unreachable("Cannot encode class as ADD");
1003 }
1004 }
1005
1006 struct bi_packed_bundle {
1007 uint64_t lo;
1008 uint64_t hi;
1009 };
1010
1011 static struct bi_packed_bundle
1012 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
1013 {
1014 struct bi_registers regs = bi_assign_ports(bundle, prev);
1015 bi_assign_uniform_constant(clause, &regs, bundle);
1016 regs.first_instruction = first_bundle;
1017
1018 uint64_t reg = bi_pack_registers(regs);
1019 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
1020 uint64_t add = bi_pack_add(clause, bundle, &regs);
1021
1022 struct bi_packed_bundle packed = {
1023 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
1024 .hi = add >> 6
1025 };
1026
1027 return packed;
1028 }
1029
1030 /* Packs the next two constants as a dedicated constant quadword at the end of
1031 * the clause, returning the number packed. */
1032
1033 static unsigned
1034 bi_pack_constants(bi_context *ctx, bi_clause *clause,
1035 unsigned index,
1036 struct util_dynarray *emission)
1037 {
1038 /* After these two, are we done? Determines tag */
1039 bool done = clause->constant_count <= (index + 2);
1040 bool only = clause->constant_count <= (index + 1);
1041
1042 /* TODO: Pos */
1043 assert(index == 0 && clause->bundle_count == 1);
1044
1045 struct bifrost_fmt_constant quad = {
1046 .pos = 0, /* TODO */
1047 .tag = done ? BIFROST_FMTC_FINAL : BIFROST_FMTC_CONSTANTS,
1048 .imm_1 = clause->constants[index + 0] >> 4,
1049 .imm_2 = only ? 0 : clause->constants[index + 1] >> 4
1050 };
1051
1052 /* XXX: On G71, Connor observed that the difference of the top 4 bits
1053 * of the second constant with the first must be less than 8, otherwise
1054 * we have to swap them. I am not able to reproduce this on G52,
1055 * further investigation needed. Possibly an errata. XXX */
1056
1057 util_dynarray_append(emission, struct bifrost_fmt_constant, quad);
1058
1059 return 2;
1060 }
1061
1062 static void
1063 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
1064 struct util_dynarray *emission)
1065 {
1066 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
1067 assert(clause->bundle_count == 1);
1068
1069 /* Used to decide if we elide writes */
1070 bool is_fragment = ctx->stage == MESA_SHADER_FRAGMENT;
1071
1072 /* State for packing constants throughout */
1073 unsigned constant_index = 0;
1074
1075 struct bifrost_fmt1 quad_1 = {
1076 .tag = clause->constant_count ? BIFROST_FMT1_CONSTANTS : BIFROST_FMT1_FINAL,
1077 .header = bi_pack_header(clause, next, is_fragment),
1078 .ins_1 = ins_1.lo,
1079 .ins_2 = ins_1.hi & ((1 << 11) - 1),
1080 .ins_0 = (ins_1.hi >> 11) & 0b111,
1081 };
1082
1083 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
1084
1085 /* Pack the remaining constants */
1086
1087 while (constant_index < clause->constant_count) {
1088 constant_index += bi_pack_constants(ctx, clause,
1089 constant_index, emission);
1090 }
1091 }
1092
1093 static bi_clause *
1094 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
1095 {
1096 /* Try the next clause in this block */
1097 if (clause->link.next != &((bi_block *) block)->clauses)
1098 return list_first_entry(&(clause->link), bi_clause, link);
1099
1100 /* Try the next block, or the one after that if it's empty, etc .*/
1101 pan_block *next_block = pan_next_block(block);
1102
1103 bi_foreach_block_from(ctx, next_block, block) {
1104 bi_block *blk = (bi_block *) block;
1105
1106 if (!list_is_empty(&blk->clauses))
1107 return list_first_entry(&(blk->clauses), bi_clause, link);
1108 }
1109
1110 return NULL;
1111 }
1112
1113 void
1114 bi_pack(bi_context *ctx, struct util_dynarray *emission)
1115 {
1116 util_dynarray_init(emission, NULL);
1117
1118 bi_foreach_block(ctx, _block) {
1119 bi_block *block = (bi_block *) _block;
1120
1121 bi_foreach_clause_in_block(block, clause) {
1122 bi_clause *next = bi_next_clause(ctx, _block, clause);
1123 bi_pack_clause(ctx, clause, next, emission);
1124 }
1125 }
1126 }