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