pan/bi: Ingest vecN directly (again)
[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 = clause->dependencies,
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 s.reg3 = regs.port[3];
366 s.reg2 = regs.port[2];
367 s.uniform_const = regs.uniform_constant;
368
369 memcpy(&packed, &s, sizeof(s));
370 return packed;
371 }
372
373 static void
374 bi_set_data_register(bi_clause *clause, unsigned idx)
375 {
376 assert(idx & BIR_INDEX_REGISTER);
377 unsigned reg = idx & ~BIR_INDEX_REGISTER;
378 assert(reg <= 63);
379 clause->data_register = reg;
380 }
381
382 static void
383 bi_read_data_register(bi_clause *clause, bi_instruction *ins)
384 {
385 bi_set_data_register(clause, ins->src[0]);
386 }
387
388 static void
389 bi_write_data_register(bi_clause *clause, bi_instruction *ins)
390 {
391 bi_set_data_register(clause, ins->dest);
392 }
393
394 static enum bifrost_packed_src
395 bi_get_src_reg_port(struct bi_registers *regs, unsigned src)
396 {
397 unsigned reg = src & ~BIR_INDEX_REGISTER;
398
399 if (regs->port[0] == reg && regs->enabled[0])
400 return BIFROST_SRC_PORT0;
401 else if (regs->port[1] == reg && regs->enabled[1])
402 return BIFROST_SRC_PORT1;
403 else if (regs->port[3] == reg && regs->read_port3)
404 return BIFROST_SRC_PORT3;
405 else
406 unreachable("Tried to access register with no port");
407 }
408
409 static enum bifrost_packed_src
410 bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_fma)
411 {
412 unsigned src = ins->src[s];
413
414 if (src & BIR_INDEX_REGISTER)
415 return bi_get_src_reg_port(regs, src);
416 else if (src & BIR_INDEX_ZERO && is_fma)
417 return BIFROST_SRC_STAGE;
418 else if (src & BIR_INDEX_PASS)
419 return src & ~BIR_INDEX_PASS;
420 else
421 unreachable("Unknown src");
422 }
423
424 static unsigned
425 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
426 {
427 /* (-a)(-b) = ab, so we only need one negate bit */
428 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
429
430 struct bifrost_fma_fma pack = {
431 .src0 = bi_get_src(ins, regs, 0, true),
432 .src1 = bi_get_src(ins, regs, 1, true),
433 .src2 = bi_get_src(ins, regs, 2, true),
434 .src0_abs = ins->src_abs[0],
435 .src1_abs = ins->src_abs[1],
436 .src2_abs = ins->src_abs[2],
437 .src0_neg = negate_mul,
438 .src2_neg = ins->src_neg[2],
439 .op = BIFROST_FMA_OP_FMA
440 };
441
442 RETURN_PACKED(pack);
443 }
444
445 static unsigned
446 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
447 {
448 /* TODO: fadd16 packing is a bit different */
449 assert(ins->dest_type == nir_type_float32);
450
451 struct bifrost_fma_add pack = {
452 .src0 = bi_get_src(ins, regs, 0, true),
453 .src1 = bi_get_src(ins, regs, 1, true),
454 .src0_abs = ins->src_abs[0],
455 .src1_abs = ins->src_abs[1],
456 .src0_neg = ins->src_neg[0],
457 .src1_neg = ins->src_neg[1],
458 .unk = 0x0,
459 .outmod = ins->outmod,
460 .roundmode = ins->roundmode,
461 .op = BIFROST_FMA_OP_FADD32
462 };
463
464 RETURN_PACKED(pack);
465 }
466
467 static unsigned
468 bi_pack_fma_1src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
469 {
470 struct bifrost_fma_inst pack = {
471 .src0 = bi_get_src(ins, regs, 0, true),
472 .op = op
473 };
474
475 RETURN_PACKED(pack);
476 }
477
478 static enum bifrost_csel_cond
479 bi_cond_to_csel(enum bi_cond cond, bool *flip, bool *invert, nir_alu_type T)
480 {
481 nir_alu_type B = nir_alu_type_get_base_type(T);
482 unsigned idx = (B == nir_type_float) ? 0 :
483 ((B == nir_type_int) ? 1 : 2);
484
485 switch (cond){
486 case BI_COND_LT:
487 *flip = true;
488 case BI_COND_GT: {
489 const enum bifrost_csel_cond ops[] = {
490 BIFROST_FGT_F,
491 BIFROST_IGT_I,
492 BIFROST_UGT_I
493 };
494
495 return ops[idx];
496 }
497 case BI_COND_LE:
498 *flip = true;
499 case BI_COND_GE: {
500 const enum bifrost_csel_cond ops[] = {
501 BIFROST_FGE_F,
502 BIFROST_IGE_I,
503 BIFROST_UGE_I
504 };
505
506 return ops[idx];
507 }
508 case BI_COND_NE:
509 *invert = true;
510 case BI_COND_EQ: {
511 const enum bifrost_csel_cond ops[] = {
512 BIFROST_FEQ_F,
513 BIFROST_IEQ_F,
514 BIFROST_IEQ_F /* sign is irrelevant */
515 };
516
517 return ops[idx];
518 }
519 default:
520 unreachable("Invalid op for csel");
521 }
522 }
523
524 static unsigned
525 bi_pack_fma_csel(bi_instruction *ins, struct bi_registers *regs)
526 {
527 /* TODO: Use csel3 as well */
528 bool flip = false, invert = false;
529
530 enum bifrost_csel_cond cond =
531 bi_cond_to_csel(ins->csel_cond, &flip, &invert, ins->src_types[0]);
532
533 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
534
535 unsigned cmp_0 = (flip ? 3 : 0);
536 unsigned cmp_1 = (flip ? 0 : 3);
537 unsigned res_0 = (invert ? 2 : 1);
538 unsigned res_1 = (invert ? 1 : 2);
539
540 struct bifrost_csel4 pack = {
541 .src0 = bi_get_src(ins, regs, cmp_0, true),
542 .src1 = bi_get_src(ins, regs, cmp_1, true),
543 .src2 = bi_get_src(ins, regs, res_0, true),
544 .src3 = bi_get_src(ins, regs, res_1, true),
545 .cond = cond,
546 .op = (size == 16) ? BIFROST_FMA_OP_CSEL4_V16 :
547 BIFROST_FMA_OP_CSEL4
548 };
549
550 RETURN_PACKED(pack);
551 }
552
553 static unsigned
554 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
555 {
556 if (!bundle.fma)
557 return BIFROST_FMA_NOP;
558
559 switch (bundle.fma->type) {
560 case BI_ADD:
561 return bi_pack_fma_add(bundle.fma, regs);
562 case BI_CMP:
563 case BI_BITWISE:
564 case BI_CONVERT:
565 return BIFROST_FMA_NOP;
566 case BI_CSEL:
567 return bi_pack_fma_csel(bundle.fma, regs);
568 case BI_FMA:
569 return bi_pack_fma_fma(bundle.fma, regs);
570 case BI_FREXP:
571 case BI_ISUB:
572 case BI_MINMAX:
573 return BIFROST_FMA_NOP;
574 case BI_MOV:
575 return bi_pack_fma_1src(bundle.fma, regs, BIFROST_FMA_OP_MOV);
576 case BI_FMOV:
577 case BI_SHIFT:
578 case BI_SWIZZLE:
579 case BI_ROUND:
580 return BIFROST_FMA_NOP;
581 default:
582 unreachable("Cannot encode class as FMA");
583 }
584 }
585
586 static unsigned
587 bi_pack_add_ld_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
588 {
589 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
590 assert(size == 32 || size == 16);
591
592 unsigned op = (size == 32) ?
593 BIFROST_ADD_OP_LD_VAR_32 :
594 BIFROST_ADD_OP_LD_VAR_16;
595
596 unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
597 unsigned channels = util_bitcount(cmask);
598 assert(cmask == ((1 << channels) - 1));
599
600 unsigned packed_addr = 0;
601
602 if (ins->src[0] & BIR_INDEX_CONSTANT) {
603 /* Direct uses address field directly */
604 packed_addr = bi_get_immediate(ins, ins->src[0]);
605 assert(packed_addr < 0b1000);
606 } else {
607 /* Indirect gets an extra source */
608 packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
609 }
610
611 /* The destination is thrown in the data register */
612 assert(ins->dest & BIR_INDEX_REGISTER);
613 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
614
615 assert(channels >= 1 && channels <= 4);
616
617 struct bifrost_ld_var pack = {
618 .src0 = bi_get_src(ins, regs, 1, false),
619 .addr = packed_addr,
620 .channels = MALI_POSITIVE(channels),
621 .interp_mode = ins->load_vary.interp_mode,
622 .reuse = ins->load_vary.reuse,
623 .flat = ins->load_vary.flat,
624 .op = op
625 };
626
627 RETURN_PACKED(pack);
628 }
629
630 static unsigned
631 bi_pack_add_2src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
632 {
633 struct bifrost_add_2src pack = {
634 .src0 = bi_get_src(ins, regs, 0, true),
635 .src1 = bi_get_src(ins, regs, 1, true),
636 .op = op
637 };
638
639 RETURN_PACKED(pack);
640 }
641
642 static unsigned
643 bi_pack_add_ld_ubo(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
644 {
645 unsigned components = bi_load32_components(ins);
646
647 const unsigned ops[4] = {
648 BIFROST_ADD_OP_LD_UBO_1,
649 BIFROST_ADD_OP_LD_UBO_2,
650 BIFROST_ADD_OP_LD_UBO_3,
651 BIFROST_ADD_OP_LD_UBO_4
652 };
653
654 bi_write_data_register(clause, ins);
655 return bi_pack_add_2src(ins, regs, ops[components - 1]);
656 }
657
658 static enum bifrost_ldst_type
659 bi_pack_ldst_type(nir_alu_type T)
660 {
661 switch (T) {
662 case nir_type_float16: return BIFROST_LDST_F16;
663 case nir_type_float32: return BIFROST_LDST_F32;
664 case nir_type_int32: return BIFROST_LDST_I32;
665 case nir_type_uint32: return BIFROST_LDST_U32;
666 default: unreachable("Invalid type loaded");
667 }
668 }
669
670 static unsigned
671 bi_pack_add_ld_var_addr(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
672 {
673 struct bifrost_ld_var_addr pack = {
674 .src0 = bi_get_src(ins, regs, 1, false),
675 .src1 = bi_get_src(ins, regs, 2, false),
676 .location = bi_get_immediate(ins, ins->src[0]),
677 .type = bi_pack_ldst_type(ins->src_types[3]),
678 .op = BIFROST_ADD_OP_LD_VAR_ADDR
679 };
680
681 bi_write_data_register(clause, ins);
682 RETURN_PACKED(pack);
683 }
684
685 static unsigned
686 bi_pack_add_ld_attr(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
687 {
688 struct bifrost_ld_attr pack = {
689 .src0 = bi_get_src(ins, regs, 1, false),
690 .src1 = bi_get_src(ins, regs, 2, false),
691 .location = bi_get_immediate(ins, ins->src[0]),
692 .channels = MALI_POSITIVE(bi_load32_components(ins)),
693 .type = bi_pack_ldst_type(ins->dest_type),
694 .op = BIFROST_ADD_OP_LD_ATTR
695 };
696
697 bi_write_data_register(clause, ins);
698 RETURN_PACKED(pack);
699 }
700
701 static unsigned
702 bi_pack_add_st_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
703 {
704 assert(ins->store_channels >= 1 && ins->store_channels <= 4);
705
706 struct bifrost_st_vary pack = {
707 .src0 = bi_get_src(ins, regs, 1, false),
708 .src1 = bi_get_src(ins, regs, 2, false),
709 .src2 = bi_get_src(ins, regs, 3, false),
710 .channels = MALI_POSITIVE(ins->store_channels),
711 .op = BIFROST_ADD_OP_ST_VAR
712 };
713
714 bi_read_data_register(clause, ins);
715 RETURN_PACKED(pack);
716 }
717
718 static unsigned
719 bi_pack_add_atest(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
720 {
721 /* TODO: fp16 */
722 assert(ins->src_types[1] == nir_type_float32);
723
724 struct bifrost_add_atest pack = {
725 .src0 = bi_get_src(ins, regs, 0, false),
726 .src1 = bi_get_src(ins, regs, 1, false),
727 .component = 1, /* Set for fp32 */
728 .op = BIFROST_ADD_OP_ATEST,
729 };
730
731 /* Despite *also* writing with the usual mechanism... quirky and
732 * perhaps unnecessary, but let's match the blob */
733 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
734
735 RETURN_PACKED(pack);
736 }
737
738 static unsigned
739 bi_pack_add_blend(bi_instruction *ins, struct bi_registers *regs)
740 {
741 struct bifrost_add_inst pack = {
742 .src0 = bi_get_src(ins, regs, 0, false),
743 .op = BIFROST_ADD_OP_BLEND
744 };
745
746 /* TODO: Pack location in uniform_const */
747 assert(ins->blend_location == 0);
748
749 RETURN_PACKED(pack);
750 }
751
752 static unsigned
753 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
754 {
755 if (!bundle.add)
756 return BIFROST_ADD_NOP;
757
758 switch (bundle.add->type) {
759 case BI_ADD:
760 return BIFROST_ADD_NOP;
761 case BI_ATEST:
762 return bi_pack_add_atest(clause, bundle.add, regs);
763 case BI_BRANCH:
764 case BI_CMP:
765 return BIFROST_ADD_NOP;
766 case BI_BLEND:
767 return bi_pack_add_blend(bundle.add, regs);
768 case BI_BITWISE:
769 case BI_CONVERT:
770 case BI_DISCARD:
771 case BI_FREXP:
772 case BI_ISUB:
773 case BI_LOAD:
774 return BIFROST_ADD_NOP;
775 case BI_LOAD_ATTR:
776 return bi_pack_add_ld_attr(clause, bundle.add, regs);
777 case BI_LOAD_UNIFORM:
778 return bi_pack_add_ld_ubo(clause, bundle.add, regs);
779 case BI_LOAD_VAR:
780 return bi_pack_add_ld_vary(clause, bundle.add, regs);
781 case BI_LOAD_VAR_ADDRESS:
782 return bi_pack_add_ld_var_addr(clause, bundle.add, regs);
783 case BI_MINMAX:
784 case BI_MOV:
785 case BI_FMOV:
786 case BI_SHIFT:
787 case BI_STORE:
788 return BIFROST_ADD_NOP;
789 case BI_STORE_VAR:
790 return bi_pack_add_st_vary(clause, bundle.add, regs);
791 case BI_SPECIAL:
792 case BI_SWIZZLE:
793 case BI_TEX:
794 case BI_ROUND:
795 return BIFROST_ADD_NOP;
796 default:
797 unreachable("Cannot encode class as ADD");
798 }
799 }
800
801 struct bi_packed_bundle {
802 uint64_t lo;
803 uint64_t hi;
804 };
805
806 static struct bi_packed_bundle
807 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
808 {
809 struct bi_registers regs = bi_assign_ports(bundle, prev);
810 bi_assign_uniform_constant(clause, &regs, bundle);
811 regs.first_instruction = first_bundle;
812
813 uint64_t reg = bi_pack_registers(regs);
814 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
815 uint64_t add = bi_pack_add(clause, bundle, &regs);
816
817 struct bi_packed_bundle packed = {
818 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
819 .hi = add >> 6
820 };
821
822 return packed;
823 }
824
825 /* Packs the next two constants as a dedicated constant quadword at the end of
826 * the clause, returning the number packed. */
827
828 static unsigned
829 bi_pack_constants(bi_context *ctx, bi_clause *clause,
830 unsigned index,
831 struct util_dynarray *emission)
832 {
833 /* After these two, are we done? Determines tag */
834 bool done = clause->constant_count <= (index + 2);
835 bool only = clause->constant_count <= (index + 1);
836
837 /* TODO: Pos */
838 assert(index == 0 && clause->bundle_count == 1);
839
840 struct bifrost_fmt_constant quad = {
841 .pos = 0, /* TODO */
842 .tag = done ? BIFROST_FMTC_FINAL : BIFROST_FMTC_CONSTANTS,
843 .imm_1 = clause->constants[index + 0] >> 4,
844 .imm_2 = only ? 0 : clause->constants[index + 1] >> 4
845 };
846
847 /* XXX: On G71, Connor observed that the difference of the top 4 bits
848 * of the second constant with the first must be less than 8, otherwise
849 * we have to swap them. I am not able to reproduce this on G52,
850 * further investigation needed. Possibly an errata. XXX */
851
852 util_dynarray_append(emission, struct bifrost_fmt_constant, quad);
853
854 return 2;
855 }
856
857 static void
858 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
859 struct util_dynarray *emission)
860 {
861 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
862 assert(clause->bundle_count == 1);
863
864 /* Used to decide if we elide writes */
865 bool is_fragment = ctx->stage == MESA_SHADER_FRAGMENT;
866
867 /* State for packing constants throughout */
868 unsigned constant_index = 0;
869
870 struct bifrost_fmt1 quad_1 = {
871 .tag = clause->constant_count ? BIFROST_FMT1_CONSTANTS : BIFROST_FMT1_FINAL,
872 .header = bi_pack_header(clause, next, is_fragment),
873 .ins_1 = ins_1.lo,
874 .ins_2 = ins_1.hi & ((1 << 11) - 1),
875 .ins_0 = (ins_1.hi >> 11) & 0b111,
876 };
877
878 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
879
880 /* Pack the remaining constants */
881
882 while (constant_index < clause->constant_count) {
883 constant_index += bi_pack_constants(ctx, clause,
884 constant_index, emission);
885 }
886 }
887
888 static bi_clause *
889 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
890 {
891 /* Try the next clause in this block */
892 if (clause->link.next != &((bi_block *) block)->clauses)
893 return list_first_entry(&(clause->link), bi_clause, link);
894
895 /* Try the next block, or the one after that if it's empty, etc .*/
896 pan_block *next_block = pan_next_block(block);
897
898 bi_foreach_block_from(ctx, next_block, block) {
899 bi_block *blk = (bi_block *) block;
900
901 if (!list_is_empty(&blk->clauses))
902 return list_first_entry(&(blk->clauses), bi_clause, link);
903 }
904
905 return NULL;
906 }
907
908 void
909 bi_pack(bi_context *ctx, struct util_dynarray *emission)
910 {
911 util_dynarray_init(emission, NULL);
912
913 bi_foreach_block(ctx, _block) {
914 bi_block *block = (bi_block *) _block;
915
916 bi_foreach_clause_in_block(block, clause) {
917 bi_clause *next = bi_next_clause(ctx, _block, clause);
918 bi_pack_clause(ctx, clause, next, emission);
919 }
920 }
921 }