pan/bi: Skip over data registers in port assignment
[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)
38 {
39 struct bifrost_header header = {
40 /* stub */
41 .no_end_of_shader = (next != NULL),
42 };
43
44 uint64_t u = 0;
45 memcpy(&u, &header, sizeof(header));
46 return u;
47 }
48
49 /* Represents the assignment of ports for a given bundle */
50
51 struct bi_registers {
52 /* Register to assign to each port */
53 unsigned port[4];
54
55 /* Read ports can be disabled */
56 bool enabled[2];
57
58 /* Should we write FMA? what about ADD? If only a single port is
59 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
60 bool write_fma, write_add;
61
62 /* Should we read with port 3? */
63 bool read_port3;
64
65 /* Packed uniform/constant */
66 unsigned uniform_constant;
67
68 /* Whether writes are actually for the last instruction */
69 bool first_instruction;
70 };
71
72 /* Assigns a port for reading, before anything is written */
73
74 static void
75 bi_assign_port_read(struct bi_registers *regs, unsigned src)
76 {
77 /* We only assign for registers */
78 if (!(src & BIR_INDEX_REGISTER))
79 return;
80
81 unsigned reg = src & ~BIR_INDEX_REGISTER;
82
83 /* Check if we already assigned the port */
84 for (unsigned i = 0; i <= 1; ++i) {
85 if (regs->port[i] == reg && regs->enabled[i])
86 return;
87 }
88
89 if (regs->port[3] == reg && regs->read_port3)
90 return;
91
92 /* Assign it now */
93
94 for (unsigned i = 0; i <= 1; ++i) {
95 if (!regs->enabled[i]) {
96 regs->port[i] = reg;
97 regs->enabled[i] = true;
98 return;
99 }
100 }
101
102 if (!regs->read_port3) {
103 regs->port[3] = reg;
104 regs->read_port3 = true;
105 }
106 }
107
108 static struct bi_registers
109 bi_assign_ports(bi_bundle now, bi_bundle prev)
110 {
111 struct bi_registers regs = { 0 };
112
113 /* We assign ports for the main register mechanism. Special ops
114 * use the data registers, which has its own mechanism entirely
115 * and thus gets skipped over here. */
116
117 unsigned read_dreg = now.add &&
118 bi_class_props[now.add->type] & BI_DATA_REG_SRC;
119
120 unsigned write_dreg = prev.add &&
121 bi_class_props[prev.add->type] & BI_DATA_REG_DEST;
122
123 /* First, assign reads */
124
125 if (now.fma)
126 bi_foreach_src(now.fma, src)
127 bi_assign_port_read(&regs, now.fma->src[src]);
128
129 if (now.add) {
130 bi_foreach_src(now.add, src) {
131 if (!(src == 0 && read_dreg))
132 bi_assign_port_read(&regs, now.add->src[src]);
133 }
134 }
135
136 /* Next, assign writes */
137
138 if (prev.fma && prev.fma->dest & BIR_INDEX_REGISTER) {
139 regs.port[2] = prev.fma->dest & ~BIR_INDEX_REGISTER;
140 regs.write_fma = true;
141 }
142
143 if (prev.add && prev.add->dest & BIR_INDEX_REGISTER && !write_dreg) {
144 unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
145
146 if (regs.write_fma) {
147 /* Scheduler constraint: cannot read 3 and write 2 */
148 assert(!regs.read_port3);
149 regs.port[3] = r;
150 } else {
151 regs.port[2] = r;
152 }
153
154 regs.write_add = true;
155 }
156
157 /* Finally, ensure port 1 > port 0 for the 63-x trick to function */
158
159 if (regs.enabled[0] && regs.enabled[1] && regs.port[1] < regs.port[0]) {
160 unsigned temp = regs.port[0];
161 regs.port[0] = regs.port[1];
162 regs.port[1] = temp;
163 }
164
165 return regs;
166 }
167
168 /* Determines the register control field, ignoring the first? flag */
169
170 static enum bifrost_reg_control
171 bi_pack_register_ctrl_lo(struct bi_registers r)
172 {
173 if (r.write_fma) {
174 if (r.write_add) {
175 assert(!r.read_port3);
176 return BIFROST_WRITE_ADD_P2_FMA_P3;
177 } else {
178 if (r.read_port3)
179 return BIFROST_WRITE_FMA_P2_READ_P3;
180 else
181 return BIFROST_WRITE_FMA_P2;
182 }
183 } else if (r.write_add) {
184 if (r.read_port3)
185 return BIFROST_WRITE_ADD_P2_READ_P3;
186 else
187 return BIFROST_WRITE_ADD_P2;
188 } else if (r.read_port3)
189 return BIFROST_READ_P3;
190 else
191 return BIFROST_REG_NONE;
192 }
193
194 /* Ditto but account for the first? flag this time */
195
196 static enum bifrost_reg_control
197 bi_pack_register_ctrl(struct bi_registers r)
198 {
199 enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
200
201 if (r.first_instruction) {
202 if (ctrl == BIFROST_REG_NONE)
203 ctrl = BIFROST_FIRST_NONE;
204 else
205 ctrl |= BIFROST_FIRST_NONE;
206 }
207
208 return ctrl;
209 }
210
211 static uint64_t
212 bi_pack_registers(struct bi_registers regs)
213 {
214 enum bifrost_reg_control ctrl = bi_pack_register_ctrl(regs);
215 struct bifrost_regs s;
216 uint64_t packed = 0;
217
218 if (regs.enabled[1]) {
219 /* Gotta save that bit!~ Required by the 63-x trick */
220 assert(regs.port[1] > regs.port[0]);
221 assert(regs.enabled[0]);
222
223 /* Do the 63-x trick, see docs/disasm */
224 if (regs.port[0] > 31) {
225 regs.port[0] = 63 - regs.port[0];
226 regs.port[1] = 63 - regs.port[1];
227 }
228
229 assert(regs.port[0] <= 31);
230 assert(regs.port[1] <= 63);
231
232 s.ctrl = ctrl;
233 s.reg1 = regs.port[1];
234 s.reg0 = regs.port[0];
235 } else {
236 /* Port 1 disabled, so set to zero and use port 1 for ctrl */
237 s.reg1 = ctrl << 2;
238
239 if (regs.enabled[0]) {
240 /* Bit 0 upper bit of port 0 */
241 s.reg1 |= (regs.port[0] >> 5);
242
243 /* Rest of port 0 in usual spot */
244 s.reg0 = (regs.port[0] & 0b11111);
245 } else {
246 /* Bit 1 set if port 0 also disabled */
247 s.reg1 |= (1 << 1);
248 }
249 }
250
251 s.reg3 = regs.port[3];
252 s.reg2 = regs.port[2];
253 s.uniform_const = regs.uniform_constant;
254
255 memcpy(&packed, &s, sizeof(s));
256 return packed;
257 }
258
259 static enum bifrost_packed_src
260 bi_get_src_reg_port(struct bi_registers *regs, unsigned src)
261 {
262 unsigned reg = src & ~BIR_INDEX_REGISTER;
263
264 if (regs->port[0] == reg && regs->enabled[0])
265 return BIFROST_SRC_PORT0;
266 else if (regs->port[1] == reg && regs->enabled[1])
267 return BIFROST_SRC_PORT1;
268 else if (regs->port[3] == reg && regs->read_port3)
269 return BIFROST_SRC_PORT3;
270 else
271 unreachable("Tried to access register with no port");
272 }
273
274 static enum bifrost_packed_src
275 bi_get_src_const(struct bi_registers *regs, unsigned constant)
276 {
277 if (regs->uniform_constant & (1 << 7))
278 unreachable("Tried to get constant but loading uniforms");
279
280 unsigned loc = (regs->uniform_constant >> 4) & 0x7;
281
282 if (loc != 0)
283 unreachable("TODO: constants in clauses");
284
285 unsigned lo = regs->uniform_constant & 0xF;
286
287 if (lo == 0) {
288 if (constant != 0)
289 unreachable("Tried to load !0 in 0 slot");
290
291 return BIFROST_SRC_CONST_LO;
292 } else {
293 unreachable("Special slot is not a fixed immediate");
294 }
295 }
296
297 static enum bifrost_packed_src
298 bi_get_src(bi_instruction *ins, struct bi_registers *regs, unsigned s, bool is_fma)
299 {
300 unsigned src = ins->src[s];
301
302 if (src & BIR_INDEX_REGISTER)
303 return bi_get_src_reg_port(regs, src);
304 else if (src & BIR_INDEX_ZERO && is_fma)
305 return BIFROST_SRC_STAGE;
306 else if (src & BIR_INDEX_ZERO)
307 return bi_get_src_const(regs, 0);
308 else if (src & BIR_INDEX_PASS)
309 return src & ~BIR_INDEX_PASS;
310 else
311 unreachable("Unknown src");
312 }
313
314 static unsigned
315 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
316 {
317 /* (-a)(-b) = ab, so we only need one negate bit */
318 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
319
320 struct bifrost_fma_fma pack = {
321 .src0 = bi_get_src(ins, regs, 0, true),
322 .src1 = bi_get_src(ins, regs, 1, true),
323 .src2 = bi_get_src(ins, regs, 2, true),
324 .src0_abs = ins->src_abs[0],
325 .src1_abs = ins->src_abs[1],
326 .src2_abs = ins->src_abs[2],
327 .src0_neg = negate_mul,
328 .src2_neg = ins->src_neg[2],
329 .op = BIFROST_FMA_OP_FMA
330 };
331
332 RETURN_PACKED(pack);
333 }
334
335 static unsigned
336 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
337 {
338 /* TODO: fadd16 packing is a bit different */
339 assert(ins->dest_type == nir_type_float32);
340
341 struct bifrost_fma_add pack = {
342 .src0 = bi_get_src(ins, regs, 0, true),
343 .src1 = bi_get_src(ins, regs, 1, true),
344 .src0_abs = ins->src_abs[0],
345 .src1_abs = ins->src_abs[1],
346 .src0_neg = ins->src_neg[0],
347 .src1_neg = ins->src_neg[1],
348 .unk = 0x0,
349 .outmod = ins->outmod,
350 .roundmode = ins->roundmode,
351 .op = BIFROST_FMA_OP_FADD32
352 };
353
354 RETURN_PACKED(pack);
355 }
356
357 static unsigned
358 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
359 {
360 if (!bundle.fma)
361 return BIFROST_FMA_NOP;
362
363 switch (bundle.fma->type) {
364 case BI_ADD:
365 return bi_pack_fma_add(bundle.fma, regs);
366 case BI_CMP:
367 case BI_BITWISE:
368 case BI_CONVERT:
369 case BI_CSEL:
370 return BIFROST_FMA_NOP;
371 case BI_FMA:
372 return bi_pack_fma_fma(bundle.fma, regs);
373 case BI_FREXP:
374 case BI_ISUB:
375 case BI_MINMAX:
376 case BI_MOV:
377 case BI_SHIFT:
378 case BI_SWIZZLE:
379 case BI_ROUND:
380 return BIFROST_FMA_NOP;
381 default:
382 unreachable("Cannot encode class as FMA");
383 }
384 }
385
386 static unsigned
387 bi_pack_add_ld_vary(bi_instruction *ins, struct bi_registers *regs)
388 {
389 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
390 assert(size == 32 || size == 16);
391
392 unsigned op = (size == 32) ?
393 BIFROST_ADD_OP_LD_VAR_32 :
394 BIFROST_ADD_OP_LD_VAR_16;
395
396 unsigned cmask = bi_from_bytemask(ins->writemask, size / 8);
397 unsigned channels = util_bitcount(cmask);
398 assert(cmask == ((1 << channels) - 1));
399
400 unsigned packed_addr = 0;
401
402 if (ins->src[0] & BIR_INDEX_CONSTANT) {
403 /* Direct uses address field directly */
404 packed_addr = ins->src[0] & ~BIR_INDEX_CONSTANT;
405 assert(packed_addr < 0b1000);
406 } else {
407 /* Indirect gets an extra source */
408 packed_addr = bi_get_src(ins, regs, 0, false) | 0b11000;
409 }
410
411 assert(channels >= 1 && channels <= 4);
412
413 struct bifrost_ld_var pack = {
414 .src0 = bi_get_src(ins, regs, 1, false),
415 .addr = packed_addr,
416 .channels = MALI_POSITIVE(channels),
417 .interp_mode = ins->load_vary.interp_mode,
418 .reuse = ins->load_vary.reuse,
419 .flat = ins->load_vary.flat,
420 .op = op
421 };
422
423 RETURN_PACKED(pack);
424 }
425
426 static unsigned
427 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
428 {
429 if (!bundle.add)
430 return BIFROST_ADD_NOP;
431
432 switch (bundle.add->type) {
433 case BI_ADD:
434 case BI_ATEST:
435 case BI_BRANCH:
436 case BI_CMP:
437 case BI_BLEND:
438 case BI_BITWISE:
439 case BI_CONVERT:
440 case BI_DISCARD:
441 case BI_FREXP:
442 case BI_ISUB:
443 case BI_LOAD:
444 case BI_LOAD_UNIFORM:
445 case BI_LOAD_ATTR:
446 return BIFROST_ADD_NOP;
447 case BI_LOAD_VAR:
448 return bi_pack_add_ld_vary(bundle.add, regs);
449 case BI_LOAD_VAR_ADDRESS:
450 case BI_MINMAX:
451 case BI_MOV:
452 case BI_SHIFT:
453 case BI_STORE:
454 case BI_STORE_VAR:
455 case BI_SPECIAL:
456 case BI_SWIZZLE:
457 case BI_TEX:
458 case BI_ROUND:
459 return BIFROST_ADD_NOP;
460 default:
461 unreachable("Cannot encode class as ADD");
462 }
463 }
464
465 struct bi_packed_bundle {
466 uint64_t lo;
467 uint64_t hi;
468 };
469
470 static struct bi_packed_bundle
471 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
472 {
473 struct bi_registers regs = bi_assign_ports(bundle, prev);
474 regs.first_instruction = first_bundle;
475
476 uint64_t reg = bi_pack_registers(regs);
477 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
478 uint64_t add = bi_pack_add(clause, bundle, &regs);
479
480 struct bi_packed_bundle packed = {
481 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
482 .hi = add >> 6
483 };
484
485 return packed;
486 }
487
488 static void
489 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
490 struct util_dynarray *emission)
491 {
492 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
493 assert(clause->bundle_count == 1);
494
495 struct bifrost_fmt1 quad_1 = {
496 .tag = BIFROST_FMT1_FINAL,
497 .header = bi_pack_header(clause, next),
498 .ins_1 = ins_1.lo,
499 .ins_2 = ins_1.hi & ((1 << 11) - 1),
500 .ins_0 = (ins_1.hi >> 11) & 0b111,
501 };
502
503 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
504 }
505
506 static bi_clause *
507 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
508 {
509 /* Try the next clause in this block */
510 if (clause->link.next != &((bi_block *) block)->clauses)
511 return list_first_entry(&(clause->link), bi_clause, link);
512
513 /* Try the next block, or the one after that if it's empty, etc .*/
514 pan_block *next_block = pan_next_block(block);
515
516 bi_foreach_block_from(ctx, next_block, block) {
517 bi_block *blk = (bi_block *) block;
518
519 if (!list_is_empty(&blk->clauses))
520 return list_first_entry(&(blk->clauses), bi_clause, link);
521 }
522
523 return NULL;
524 }
525
526 void
527 bi_pack(bi_context *ctx, struct util_dynarray *emission)
528 {
529 util_dynarray_init(emission, NULL);
530
531 bi_foreach_block(ctx, _block) {
532 bi_block *block = (bi_block *) _block;
533
534 bi_foreach_clause_in_block(block, clause) {
535 bi_clause *next = bi_next_clause(ctx, _block, clause);
536 bi_pack_clause(ctx, clause, next, emission);
537 }
538 }
539 }