d22b8696648e3db1b94b46aa54f8fc6cbf0ec006
[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 unsigned uniform_constant;
75
76 /* Whether writes are actually for the last instruction */
77 bool first_instruction;
78 };
79
80 /* Assigns a port for reading, before anything is written */
81
82 static void
83 bi_assign_port_read(struct bi_registers *regs, unsigned src)
84 {
85 /* We only assign for registers */
86 if (!(src & BIR_INDEX_REGISTER))
87 return;
88
89 unsigned reg = src & ~BIR_INDEX_REGISTER;
90
91 /* Check if we already assigned the port */
92 for (unsigned i = 0; i <= 1; ++i) {
93 if (regs->port[i] == reg && regs->enabled[i])
94 return;
95 }
96
97 if (regs->port[3] == reg && regs->read_port3)
98 return;
99
100 /* Assign it now */
101
102 for (unsigned i = 0; i <= 1; ++i) {
103 if (!regs->enabled[i]) {
104 regs->port[i] = reg;
105 regs->enabled[i] = true;
106 return;
107 }
108 }
109
110 if (!regs->read_port3) {
111 regs->port[3] = reg;
112 regs->read_port3 = true;
113 }
114 }
115
116 static struct bi_registers
117 bi_assign_ports(bi_bundle now, bi_bundle prev)
118 {
119 struct bi_registers regs = { 0 };
120
121 /* We assign ports for the main register mechanism. Special ops
122 * use the data registers, which has its own mechanism entirely
123 * and thus gets skipped over here. */
124
125 unsigned read_dreg = now.add &&
126 bi_class_props[now.add->type] & BI_DATA_REG_SRC;
127
128 unsigned write_dreg = prev.add &&
129 bi_class_props[prev.add->type] & BI_DATA_REG_DEST;
130
131 /* First, assign reads */
132
133 if (now.fma)
134 bi_foreach_src(now.fma, src)
135 bi_assign_port_read(&regs, now.fma->src[src]);
136
137 if (now.add) {
138 bi_foreach_src(now.add, src) {
139 if (!(src == 0 && read_dreg))
140 bi_assign_port_read(&regs, now.add->src[src]);
141 }
142 }
143
144 /* Next, assign writes */
145
146 if (prev.fma && prev.fma->dest & BIR_INDEX_REGISTER) {
147 regs.port[2] = prev.fma->dest & ~BIR_INDEX_REGISTER;
148 regs.write_fma = true;
149 }
150
151 if (prev.add && prev.add->dest & BIR_INDEX_REGISTER && !write_dreg) {
152 unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
153
154 if (regs.write_fma) {
155 /* Scheduler constraint: cannot read 3 and write 2 */
156 assert(!regs.read_port3);
157 regs.port[3] = r;
158 } else {
159 regs.port[2] = r;
160 }
161
162 regs.write_add = true;
163 }
164
165 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
166
167 if (regs.enabled[0] && regs.enabled[1] && regs.port[1] < regs.port[0]) {
168 unsigned temp = regs.port[0];
169 regs.port[0] = regs.port[1];
170 regs.port[1] = temp;
171 }
172
173 return regs;
174 }
175
176 /* Determines the register control field, ignoring the first? flag */
177
178 static enum bifrost_reg_control
179 bi_pack_register_ctrl_lo(struct bi_registers r)
180 {
181 if (r.write_fma) {
182 if (r.write_add) {
183 assert(!r.read_port3);
184 return BIFROST_WRITE_ADD_P2_FMA_P3;
185 } else {
186 if (r.read_port3)
187 return BIFROST_WRITE_FMA_P2_READ_P3;
188 else
189 return BIFROST_WRITE_FMA_P2;
190 }
191 } else if (r.write_add) {
192 if (r.read_port3)
193 return BIFROST_WRITE_ADD_P2_READ_P3;
194 else
195 return BIFROST_WRITE_ADD_P2;
196 } else if (r.read_port3)
197 return BIFROST_READ_P3;
198 else
199 return BIFROST_REG_NONE;
200 }
201
202 /* Ditto but account for the first? flag this time */
203
204 static enum bifrost_reg_control
205 bi_pack_register_ctrl(struct bi_registers r)
206 {
207 enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
208
209 if (r.first_instruction) {
210 if (ctrl == BIFROST_REG_NONE)
211 ctrl = BIFROST_FIRST_NONE;
212 else
213 ctrl |= BIFROST_FIRST_NONE;
214 }
215
216 return ctrl;
217 }
218
219 static uint64_t
220 bi_pack_registers(struct bi_registers regs)
221 {
222 enum bifrost_reg_control ctrl = bi_pack_register_ctrl(regs);
223 struct bifrost_regs s;
224 uint64_t packed = 0;
225
226 if (regs.enabled[1]) {
227 /* Gotta save that bit!~ Required by the 63-x trick */
228 assert(regs.port[1] > regs.port[0]);
229 assert(regs.enabled[0]);
230
231 /* Do the 63-x trick, see docs/disasm */
232 if (regs.port[0] > 31) {
233 regs.port[0] = 63 - regs.port[0];
234 regs.port[1] = 63 - regs.port[1];
235 }
236
237 assert(regs.port[0] <= 31);
238 assert(regs.port[1] <= 63);
239
240 s.ctrl = ctrl;
241 s.reg1 = regs.port[1];
242 s.reg0 = regs.port[0];
243 } else {
244 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
245 s.reg1 = ctrl << 2;
246
247 if (regs.enabled[0]) {
248 /* Bit 0 upper bit of port 0 */
249 s.reg1 |= (regs.port[0] >> 5);
250
251 /* Rest of port 0 in usual spot */
252 s.reg0 = (regs.port[0] & 0b11111);
253 } else {
254 /* Bit 1 set if port 0 also disabled */
255 s.reg1 |= (1 << 1);
256 }
257 }
258
259 s.reg3 = regs.port[3];
260 s.reg2 = regs.port[2];
261 s.uniform_const = regs.uniform_constant;
262
263 memcpy(&packed, &s, sizeof(s));
264 return packed;
265 }
266
267 static enum bifrost_packed_src
268 bi_get_src_reg_port(struct bi_registers *regs, unsigned src)
269 {
270 unsigned reg = src & ~BIR_INDEX_REGISTER;
271
272 if (regs->port[0] == reg && regs->enabled[0])
273 return BIFROST_SRC_PORT0;
274 else if (regs->port[1] == reg && regs->enabled[1])
275 return BIFROST_SRC_PORT1;
276 else if (regs->port[3] == reg && regs->read_port3)
277 return BIFROST_SRC_PORT3;
278 else
279 unreachable("Tried to access register with no port");
280 }
281
282 static enum bifrost_packed_src
283 bi_get_src_const(struct bi_registers *regs, unsigned constant)
284 {
285 if (regs->uniform_constant & (1 << 7))
286 unreachable("Tried to get constant but loading uniforms");
287
288 unsigned loc = (regs->uniform_constant >> 4) & 0x7;
289
290 if (loc != 0)
291 unreachable("TODO: constants in clauses");
292
293 unsigned lo = regs->uniform_constant & 0xF;
294
295 if (lo == 0) {
296 if (constant != 0)
297 unreachable("Tried to load !0 in 0 slot");
298
299 return BIFROST_SRC_CONST_LO;
300 } else {
301 unreachable("Special slot is not a fixed immediate");
302 }
303 }
304
305 static enum bifrost_packed_src
306 bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_fma)
307 {
308 unsigned src = ins->src[s];
309
310 if (src & BIR_INDEX_REGISTER)
311 return bi_get_src_reg_port(regs, src);
312 else if (src & BIR_INDEX_ZERO && is_fma)
313 return BIFROST_SRC_STAGE;
314 else if (src & BIR_INDEX_ZERO)
315 return bi_get_src_const(regs, 0);
316 else if (src & BIR_INDEX_PASS)
317 return src & ~BIR_INDEX_PASS;
318 else if (src & BIR_INDEX_CONSTANT)
319 return bi_get_src_const(regs, 0); /*TODO ins->constant.u64 */
320 else
321 unreachable("Unknown src");
322 }
323
324 static unsigned
325 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
326 {
327 /* (-a)(-b) = ab, so we only need one negate bit */
328 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
329
330 struct bifrost_fma_fma pack = {
331 .src0 = bi_get_src(ins, regs, 0, true),
332 .src1 = bi_get_src(ins, regs, 1, true),
333 .src2 = bi_get_src(ins, regs, 2, true),
334 .src0_abs = ins->src_abs[0],
335 .src1_abs = ins->src_abs[1],
336 .src2_abs = ins->src_abs[2],
337 .src0_neg = negate_mul,
338 .src2_neg = ins->src_neg[2],
339 .op = BIFROST_FMA_OP_FMA
340 };
341
342 RETURN_PACKED(pack);
343 }
344
345 static unsigned
346 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
347 {
348 /* TODO: fadd16 packing is a bit different */
349 assert(ins->dest_type == nir_type_float32);
350
351 struct bifrost_fma_add pack = {
352 .src0 = bi_get_src(ins, regs, 0, true),
353 .src1 = bi_get_src(ins, regs, 1, true),
354 .src0_abs = ins->src_abs[0],
355 .src1_abs = ins->src_abs[1],
356 .src0_neg = ins->src_neg[0],
357 .src1_neg = ins->src_neg[1],
358 .unk = 0x0,
359 .outmod = ins->outmod,
360 .roundmode = ins->roundmode,
361 .op = BIFROST_FMA_OP_FADD32
362 };
363
364 RETURN_PACKED(pack);
365 }
366
367 static unsigned
368 bi_pack_fma_1src(bi_instruction *ins, struct bi_registers *regs, unsigned op)
369 {
370 struct bifrost_fma_inst pack = {
371 .src0 = bi_get_src(ins, regs, 0, true),
372 .op = op
373 };
374
375 RETURN_PACKED(pack);
376 }
377
378 static unsigned
379 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
380 {
381 if (!bundle.fma)
382 return BIFROST_FMA_NOP;
383
384 switch (bundle.fma->type) {
385 case BI_ADD:
386 return bi_pack_fma_add(bundle.fma, regs);
387 case BI_CMP:
388 case BI_BITWISE:
389 case BI_CONVERT:
390 case BI_CSEL:
391 return BIFROST_FMA_NOP;
392 case BI_FMA:
393 return bi_pack_fma_fma(bundle.fma, regs);
394 case BI_FREXP:
395 case BI_ISUB:
396 case BI_MINMAX:
397 return BIFROST_FMA_NOP;
398 case BI_MOV:
399 return bi_pack_fma_1src(bundle.fma, regs, BIFROST_FMA_OP_MOV);
400 case BI_FMOV:
401 case BI_SHIFT:
402 case BI_SWIZZLE:
403 case BI_ROUND:
404 return BIFROST_FMA_NOP;
405 default:
406 unreachable("Cannot encode class as FMA");
407 }
408 }
409
410 static unsigned
411 bi_pack_add_ld_vary(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
412 {
413 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
414 assert(size == 32 || size == 16);
415
416 unsigned op = (size == 32) ?
417 BIFROST_ADD_OP_LD_VAR_32 :
418 BIFROST_ADD_OP_LD_VAR_16;
419
420 unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
421 unsigned channels = util_bitcount(cmask);
422 assert(cmask == ((1 << channels) - 1));
423
424 unsigned packed_addr = 0;
425
426 if (ins->src[0] & BIR_INDEX_CONSTANT) {
427 /* Direct uses address field directly */
428 packed_addr = ins->src[0] & ~BIR_INDEX_CONSTANT;
429 assert(packed_addr < 0b1000);
430 } else {
431 /* Indirect gets an extra source */
432 packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
433 }
434
435 /* The destination is thrown in the data register */
436 assert(ins->dest & BIR_INDEX_REGISTER);
437 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
438
439 assert(channels >= 1 && channels <= 4);
440
441 struct bifrost_ld_var pack = {
442 .src0 = bi_get_src(ins, regs, 1, false),
443 .addr = packed_addr,
444 .channels = MALI_POSITIVE(channels),
445 .interp_mode = ins->load_vary.interp_mode,
446 .reuse = ins->load_vary.reuse,
447 .flat = ins->load_vary.flat,
448 .op = op
449 };
450
451 RETURN_PACKED(pack);
452 }
453
454 static unsigned
455 bi_pack_add_atest(bi_clause *clause, bi_instruction *ins, struct bi_registers *regs)
456 {
457 /* TODO: fp16 */
458 assert(ins->src_types[1] == nir_type_float32);
459
460 struct bifrost_add_atest pack = {
461 .src0 = bi_get_src(ins, regs, 0, false),
462 .src1 = bi_get_src(ins, regs, 1, false),
463 .component = 1, /* Set for fp32 */
464 .op = BIFROST_ADD_OP_ATEST,
465 };
466
467 /* Despite *also* writing with the usual mechanism... quirky and
468 * perhaps unnecessary, but let's match the blob */
469 clause->data_register = ins->dest & ~BIR_INDEX_REGISTER;
470
471 RETURN_PACKED(pack);
472 }
473
474 static unsigned
475 bi_pack_add_blend(bi_instruction *ins, struct bi_registers *regs)
476 {
477 struct bifrost_add_inst pack = {
478 .src0 = bi_get_src(ins, regs, 0, false),
479 .op = BIFROST_ADD_OP_BLEND
480 };
481
482 /* TODO: Pack location in uniform_const */
483 assert(ins->blend_location == 0);
484
485 RETURN_PACKED(pack);
486 }
487
488 static unsigned
489 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
490 {
491 if (!bundle.add)
492 return BIFROST_ADD_NOP;
493
494 switch (bundle.add->type) {
495 case BI_ADD:
496 return BIFROST_ADD_NOP;
497 case BI_ATEST:
498 return bi_pack_add_atest(clause, bundle.add, regs);
499 case BI_BRANCH:
500 case BI_CMP:
501 return BIFROST_ADD_NOP;
502 case BI_BLEND:
503 return bi_pack_add_blend(bundle.add, regs);
504 case BI_BITWISE:
505 case BI_CONVERT:
506 case BI_DISCARD:
507 case BI_FREXP:
508 case BI_ISUB:
509 case BI_LOAD:
510 case BI_LOAD_UNIFORM:
511 case BI_LOAD_ATTR:
512 return BIFROST_ADD_NOP;
513 case BI_LOAD_VAR:
514 return bi_pack_add_ld_vary(clause, bundle.add, regs);
515 case BI_LOAD_VAR_ADDRESS:
516 case BI_MINMAX:
517 case BI_MOV:
518 case BI_FMOV:
519 case BI_SHIFT:
520 case BI_STORE:
521 case BI_STORE_VAR:
522 case BI_SPECIAL:
523 case BI_SWIZZLE:
524 case BI_TEX:
525 case BI_ROUND:
526 return BIFROST_ADD_NOP;
527 default:
528 unreachable("Cannot encode class as ADD");
529 }
530 }
531
532 struct bi_packed_bundle {
533 uint64_t lo;
534 uint64_t hi;
535 };
536
537 static struct bi_packed_bundle
538 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
539 {
540 struct bi_registers regs = bi_assign_ports(bundle, prev);
541 regs.first_instruction = first_bundle;
542
543 uint64_t reg = bi_pack_registers(regs);
544 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
545 uint64_t add = bi_pack_add(clause, bundle, &regs);
546
547 struct bi_packed_bundle packed = {
548 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
549 .hi = add >> 6
550 };
551
552 return packed;
553 }
554
555 /* Packs the next two constants as a dedicated constant quadword at the end of
556 * the clause, returning the number packed. */
557
558 static unsigned
559 bi_pack_constants(bi_context *ctx, bi_clause *clause,
560 unsigned index,
561 struct util_dynarray *emission)
562 {
563 /* After these two, are we done? Determines tag */
564 bool done = clause->constant_count <= (index + 2);
565 bool only = clause->constant_count <= (index + 1);
566
567 /* TODO: Pos */
568 assert(index == 0 && clause->bundle_count == 1);
569
570 struct bifrost_fmt_constant quad = {
571 .pos = 0, /* TODO */
572 .tag = done ? BIFROST_FMTC_FINAL : BIFROST_FMTC_CONSTANTS,
573 .imm_1 = clause->constants[index + 0] >> 4,
574 .imm_2 = only ? 0 : clause->constants[index + 1] >> 4
575 };
576
577 /* XXX: On G71, Connor observed that the difference of the top 4 bits
578 * of the second constant with the first must be less than 8, otherwise
579 * we have to swap them. I am not able to reproduce this on G52,
580 * further investigation needed. Possibly an errata. XXX */
581
582 util_dynarray_append(emission, struct bifrost_fmt_constant, quad);
583
584 return 2;
585 }
586
587 static void
588 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
589 struct util_dynarray *emission)
590 {
591 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
592 assert(clause->bundle_count == 1);
593
594 /* Used to decide if we elide writes */
595 bool is_fragment = ctx->stage == MESA_SHADER_FRAGMENT;
596
597 /* State for packing constants throughout */
598 unsigned constant_index = 0;
599
600 struct bifrost_fmt1 quad_1 = {
601 .tag = clause->constant_count ? BIFROST_FMT1_CONSTANTS : BIFROST_FMT1_FINAL,
602 .header = bi_pack_header(clause, next, is_fragment),
603 .ins_1 = ins_1.lo,
604 .ins_2 = ins_1.hi & ((1 << 11) - 1),
605 .ins_0 = (ins_1.hi >> 11) & 0b111,
606 };
607
608 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
609
610 /* Pack the remaining constants */
611
612 while (constant_index < clause->constant_count) {
613 constant_index += bi_pack_constants(ctx, clause,
614 constant_index, emission);
615 }
616 }
617
618 static bi_clause *
619 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
620 {
621 /* Try the next clause in this block */
622 if (clause->link.next != &((bi_block *) block)->clauses)
623 return list_first_entry(&(clause->link), bi_clause, link);
624
625 /* Try the next block, or the one after that if it's empty, etc .*/
626 pan_block *next_block = pan_next_block(block);
627
628 bi_foreach_block_from(ctx, next_block, block) {
629 bi_block *blk = (bi_block *) block;
630
631 if (!list_is_empty(&blk->clauses))
632 return list_first_entry(&(blk->clauses), bi_clause, link);
633 }
634
635 return NULL;
636 }
637
638 void
639 bi_pack(bi_context *ctx, struct util_dynarray *emission)
640 {
641 util_dynarray_init(emission, NULL);
642
643 bi_foreach_block(ctx, _block) {
644 bi_block *block = (bi_block *) _block;
645
646 bi_foreach_clause_in_block(block, clause) {
647 bi_clause *next = bi_next_clause(ctx, _block, clause);
648 bi_pack_clause(ctx, clause, next, emission);
649 }
650 }
651 }