pan/bi: List ADD classes in bi_pack_add
[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_fma_src(bi_instruction *ins, struct bi_registers *regs, unsigned s)
263 {
264 unsigned src = ins->src[s];
265
266 if (src & BIR_INDEX_REGISTER)
267 return bi_get_src_reg_port(regs, src);
268 else if (src & BIR_INDEX_ZERO)
269 return BIFROST_SRC_STAGE;
270 else if (src & BIR_INDEX_PASS)
271 return src & ~BIR_INDEX_PASS;
272 else
273 unreachable("Unknown src in FMA");
274 }
275
276 static unsigned
277 bi_pack_fma_fma(bi_instruction *ins, struct bi_registers *regs)
278 {
279 /* (-a)(-b) = ab, so we only need one negate bit */
280 bool negate_mul = ins->src_neg[0] ^ ins->src_neg[1];
281
282 struct bifrost_fma_fma pack = {
283 .src0 = bi_get_fma_src(ins, regs, 0),
284 .src1 = bi_get_fma_src(ins, regs, 1),
285 .src2 = bi_get_fma_src(ins, regs, 2),
286 .src0_abs = ins->src_abs[0],
287 .src1_abs = ins->src_abs[1],
288 .src2_abs = ins->src_abs[2],
289 .src0_neg = negate_mul,
290 .src2_neg = ins->src_neg[2],
291 .op = BIFROST_FMA_OP_FMA
292 };
293
294 RETURN_PACKED(pack);
295 }
296
297 static unsigned
298 bi_pack_fma_add(bi_instruction *ins, struct bi_registers *regs)
299 {
300 /* TODO: fadd16 packing is a bit different */
301 assert(ins->dest_type == nir_type_float32);
302
303 struct bifrost_fma_add pack = {
304 .src0 = bi_get_fma_src(ins, regs, 0),
305 .src1 = bi_get_fma_src(ins, regs, 1),
306 .src0_abs = ins->src_abs[0],
307 .src1_abs = ins->src_abs[1],
308 .src0_neg = ins->src_neg[0],
309 .src1_neg = ins->src_neg[1],
310 .unk = 0x0,
311 .outmod = ins->outmod,
312 .roundmode = ins->roundmode,
313 .op = BIFROST_FMA_OP_FADD32
314 };
315
316 RETURN_PACKED(pack);
317 }
318
319 static unsigned
320 bi_pack_fma(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
321 {
322 if (!bundle.fma)
323 return BIFROST_FMA_NOP;
324
325 switch (bundle.fma->type) {
326 case BI_ADD:
327 return bi_pack_fma_add(bundle.fma, regs);
328 case BI_CMP:
329 case BI_BITWISE:
330 case BI_CONVERT:
331 case BI_CSEL:
332 return BIFROST_FMA_NOP;
333 case BI_FMA:
334 return bi_pack_fma_fma(bundle.fma, regs);
335 case BI_FREXP:
336 case BI_ISUB:
337 case BI_MINMAX:
338 case BI_MOV:
339 case BI_SHIFT:
340 case BI_SWIZZLE:
341 case BI_ROUND:
342 return BIFROST_FMA_NOP;
343 default:
344 unreachable("Cannot encode class as FMA");
345 }
346 }
347
348 static unsigned
349 bi_pack_add(bi_clause *clause, bi_bundle bundle, struct bi_registers *regs)
350 {
351 if (!bundle.add)
352 return BIFROST_ADD_NOP;
353
354 switch (bundle.add->type) {
355 case BI_ADD:
356 case BI_ATEST:
357 case BI_BRANCH:
358 case BI_CMP:
359 case BI_BLEND:
360 case BI_BITWISE:
361 case BI_CONVERT:
362 case BI_DISCARD:
363 case BI_FREXP:
364 case BI_ISUB:
365 case BI_LOAD:
366 case BI_LOAD_UNIFORM:
367 case BI_LOAD_ATTR:
368 case BI_LOAD_VAR:
369 case BI_LOAD_VAR_ADDRESS:
370 case BI_MINMAX:
371 case BI_MOV:
372 case BI_SHIFT:
373 case BI_STORE:
374 case BI_STORE_VAR:
375 case BI_SPECIAL:
376 case BI_SWIZZLE:
377 case BI_TEX:
378 case BI_ROUND:
379 return BIFROST_ADD_NOP;
380 default:
381 unreachable("Cannot encode class as ADD");
382 }
383 }
384
385 struct bi_packed_bundle {
386 uint64_t lo;
387 uint64_t hi;
388 };
389
390 static struct bi_packed_bundle
391 bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
392 {
393 struct bi_registers regs = bi_assign_ports(bundle, prev);
394 regs.first_instruction = first_bundle;
395
396 uint64_t reg = bi_pack_registers(regs);
397 uint64_t fma = bi_pack_fma(clause, bundle, &regs);
398 uint64_t add = bi_pack_add(clause, bundle, &regs);
399
400 struct bi_packed_bundle packed = {
401 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
402 .hi = add >> 6
403 };
404
405 return packed;
406 }
407
408 static void
409 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
410 struct util_dynarray *emission)
411 {
412 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
413 assert(clause->bundle_count == 1);
414
415 struct bifrost_fmt1 quad_1 = {
416 .tag = BIFROST_FMT1_FINAL,
417 .header = bi_pack_header(clause, next),
418 .ins_1 = ins_1.lo,
419 .ins_2 = ins_1.hi & ((1 << 11) - 1),
420 .ins_0 = (ins_1.hi >> 11) & 0b111,
421 };
422
423 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
424 }
425
426 static bi_clause *
427 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
428 {
429 /* Try the next clause in this block */
430 if (clause->link.next != &((bi_block *) block)->clauses)
431 return list_first_entry(&(clause->link), bi_clause, link);
432
433 /* Try the next block, or the one after that if it's empty, etc .*/
434 pan_block *next_block = pan_next_block(block);
435
436 bi_foreach_block_from(ctx, next_block, block) {
437 bi_block *blk = (bi_block *) block;
438
439 if (!list_is_empty(&blk->clauses))
440 return list_first_entry(&(blk->clauses), bi_clause, link);
441 }
442
443 return NULL;
444 }
445
446 void
447 bi_pack(bi_context *ctx, struct util_dynarray *emission)
448 {
449 util_dynarray_init(emission, NULL);
450
451 bi_foreach_block(ctx, _block) {
452 bi_block *block = (bi_block *) _block;
453
454 bi_foreach_clause_in_block(block, clause) {
455 bi_clause *next = bi_next_clause(ctx, _block, clause);
456 bi_pack_clause(ctx, clause, next, emission);
457 }
458 }
459 }