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