pan/bi: Pack BI_BITWISE
[mesa.git] / src / panfrost / bifrost / bi_print.c
1 /*
2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "bi_print.h"
28
29 const char *
30 bi_clause_type_name(enum bifrost_clause_type T)
31 {
32 switch (T) {
33 case BIFROST_CLAUSE_NONE: return "";
34 case BIFROST_CLAUSE_LOAD_VARY: return "load_vary";
35 case BIFROST_CLAUSE_UBO: return "ubo";
36 case BIFROST_CLAUSE_TEX: return "tex";
37 case BIFROST_CLAUSE_SSBO_LOAD: return "load";
38 case BIFROST_CLAUSE_SSBO_STORE: return "store";
39 case BIFROST_CLAUSE_BLEND: return "blend";
40 case BIFROST_CLAUSE_ATEST: return "atest";
41 case BIFROST_CLAUSE_64BIT: return "64";
42 default: return "??";
43 }
44 }
45
46 const char *
47 bi_output_mod_name(enum bifrost_outmod mod)
48 {
49 switch (mod) {
50 case BIFROST_NONE: return "";
51 case BIFROST_POS: return ".pos";
52 case BIFROST_SAT_SIGNED: return ".sat_signed";
53 case BIFROST_SAT: return ".sat";
54 default: return "invalid";
55 }
56 }
57
58 const char *
59 bi_minmax_mode_name(enum bifrost_minmax_mode mod)
60 {
61 switch (mod) {
62 case BIFROST_MINMAX_NONE: return "";
63 case BIFROST_NAN_WINS: return ".nan_wins";
64 case BIFROST_SRC1_WINS: return ".src1_wins";
65 case BIFROST_SRC0_WINS: return ".src0_wins";
66 default: return "invalid";
67 }
68 }
69
70 const char *
71 bi_round_mode_name(enum bifrost_roundmode mod)
72 {
73 switch (mod) {
74 case BIFROST_RTE: return "";
75 case BIFROST_RTP: return ".rtp";
76 case BIFROST_RTN: return ".rtn";
77 case BIFROST_RTZ: return ".rtz";
78 default: return "invalid";
79 }
80 }
81
82 const char *
83 bi_csel_cond_name(enum bifrost_csel_cond cond)
84 {
85 switch (cond) {
86 case BIFROST_FEQ_F: return "feq.f";
87 case BIFROST_FGT_F: return "fgt.f";
88 case BIFROST_FGE_F: return "fge.f";
89 case BIFROST_IEQ_F: return "ieq.f";
90 case BIFROST_IGT_I: return "igt.i";
91 case BIFROST_IGE_I: return "uge.i";
92 case BIFROST_UGT_I: return "ugt.i";
93 case BIFROST_UGE_I: return "uge.i";
94 default: return "invalid";
95 }
96 }
97
98 const char *
99 bi_interp_mode_name(enum bifrost_interp_mode mode)
100 {
101 switch (mode) {
102 case BIFROST_INTERP_PER_FRAG: return ".per_frag";
103 case BIFROST_INTERP_CENTROID: return ".centroid";
104 case BIFROST_INTERP_DEFAULT: return "";
105 case BIFROST_INTERP_EXPLICIT: return ".explicit";
106 default: return ".unknown";
107 }
108 }
109
110 const char *
111 bi_ldst_type_name(enum bifrost_ldst_type type)
112 {
113 switch (type) {
114 case BIFROST_LDST_F16: return "f16";
115 case BIFROST_LDST_F32: return "f32";
116 case BIFROST_LDST_I32: return "i32";
117 case BIFROST_LDST_U32: return "u32";
118 default: return "invalid";
119 }
120 }
121
122 /* The remaining functions in this file are for IR-internal
123 * structures; the disassembler doesn't use them */
124
125 const char *
126 bi_class_name(enum bi_class cl)
127 {
128 switch (cl) {
129 case BI_ADD: return "add";
130 case BI_ATEST: return "atest";
131 case BI_BRANCH: return "branch";
132 case BI_CMP: return "cmp";
133 case BI_BLEND: return "blend";
134 case BI_BITWISE: return "bitwise";
135 case BI_COMBINE: return "combine";
136 case BI_CONVERT: return "convert";
137 case BI_CSEL: return "csel";
138 case BI_DISCARD: return "discard";
139 case BI_FMA: return "fma";
140 case BI_FMOV: return "fmov";
141 case BI_FREXP: return "frexp";
142 case BI_ISUB: return "isub";
143 case BI_LOAD: return "load";
144 case BI_LOAD_UNIFORM: return "load_uniform";
145 case BI_LOAD_ATTR: return "load_attr";
146 case BI_LOAD_VAR: return "load_var";
147 case BI_LOAD_VAR_ADDRESS: return "load_var_address";
148 case BI_MINMAX: return "minmax";
149 case BI_MOV: return "mov";
150 case BI_SELECT: return "select";
151 case BI_SHIFT: return "shift";
152 case BI_STORE: return "store";
153 case BI_STORE_VAR: return "store_var";
154 case BI_SPECIAL: return "special";
155 case BI_TABLE: return "table";
156 case BI_TEX: return "tex";
157 case BI_ROUND: return "round";
158 default: return "unknown_class";
159 }
160 }
161
162 static bool
163 bi_print_dest_index(FILE *fp, bi_instruction *ins, unsigned index)
164 {
165 if (!index)
166 fprintf(fp, "_");
167 else if (index & BIR_INDEX_REGISTER)
168 fprintf(fp, "br%u", index & ~BIR_INDEX_REGISTER);
169 else if (index & BIR_IS_REG)
170 fprintf(fp, "r%u", index >> 1);
171 else if (!(index & BIR_SPECIAL))
172 fprintf(fp, "%u", (index >> 1) - 1);
173 else
174 return false;
175
176 return true;
177 }
178
179 static void
180 bi_print_index(FILE *fp, bi_instruction *ins, unsigned index, unsigned s)
181 {
182 if (bi_print_dest_index(fp, ins, index))
183 return;
184
185 if (index & BIR_INDEX_UNIFORM)
186 fprintf(fp, "u%u", index & ~BIR_INDEX_UNIFORM);
187 else if (index & BIR_INDEX_CONSTANT)
188 fprintf(fp, "#0x%" PRIx64, bi_get_immediate(ins, s));
189 else if (index & BIR_INDEX_ZERO)
190 fprintf(fp, "#0");
191 else
192 fprintf(fp, "#err");
193 }
194
195 static void
196 bi_print_src(FILE *fp, bi_instruction *ins, unsigned s)
197 {
198 unsigned src = ins->src[s];
199 bool mods = bi_has_source_mods(ins);
200 bool abs = ins->src_abs[s] && mods;
201 bool neg = ins->src_neg[s] && mods;
202
203 if (neg)
204 fprintf(fp, "-");
205
206 if (abs)
207 fprintf(fp, "abs(");
208
209 if (ins->type == BI_BITWISE && ins->bitwise.src_invert[s])
210 fprintf(fp, "~");
211
212 bi_print_index(fp, ins, src, s);
213
214 if (abs)
215 fprintf(fp, ")");
216 }
217
218 /* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
219
220 static void
221 bi_print_alu_type(nir_alu_type t, FILE *fp)
222 {
223 unsigned size = nir_alu_type_get_type_size(t);
224 nir_alu_type base = nir_alu_type_get_base_type(t);
225
226 switch (base) {
227 case nir_type_int:
228 fprintf(fp, ".i");
229 break;
230 case nir_type_uint:
231 fprintf(fp, ".u");
232 break;
233 case nir_type_bool:
234 fprintf(fp, ".b");
235 break;
236 case nir_type_float:
237 fprintf(fp, ".f");
238 break;
239 default:
240 fprintf(fp, ".unknown");
241 break;
242 }
243
244 fprintf(fp, "%u", size);
245 }
246
247 static void
248 bi_print_swizzle(bi_instruction *ins, unsigned src, FILE *fp)
249 {
250 fprintf(fp, ".");
251
252 for (unsigned u = 0; u < bi_get_component_count(ins, src); ++u) {
253 assert(ins->swizzle[src][u] < 4);
254 fputc("xyzw"[ins->swizzle[src][u]], fp);
255 }
256 }
257
258 static const char *
259 bi_bitwise_op_name(enum bi_bitwise_op op)
260 {
261 switch (op) {
262 case BI_BITWISE_AND: return "and";
263 case BI_BITWISE_OR: return "or";
264 case BI_BITWISE_XOR: return "xor";
265 default: return "invalid";
266 }
267 }
268
269 const char *
270 bi_table_op_name(enum bi_table_op op)
271 {
272 switch (op) {
273 case BI_TABLE_LOG2_U_OVER_U_1_LOW: return "log2.help";
274 default: return "invalid";
275 }
276 }
277
278 const char *
279 bi_special_op_name(enum bi_special_op op)
280 {
281 switch (op) {
282 case BI_SPECIAL_FRCP: return "frcp";
283 case BI_SPECIAL_FRSQ: return "frsq";
284 case BI_SPECIAL_EXP2_LOW: return "exp2_low";
285 default: return "invalid";
286 }
287 }
288
289 const char *
290 bi_reduce_op_name(enum bi_reduce_op op)
291 {
292 switch (op) {
293 case BI_REDUCE_ADD_FREXPM: return "add_frexpm";
294 default: return "invalid";
295 }
296 }
297
298 const char *
299 bi_frexp_op_name(enum bi_frexp_op op)
300 {
301 switch (op) {
302 case BI_FREXPE_LOG: return "frexpe_log";
303 default: return "invalid";
304 }
305 }
306
307 const char *
308 bi_tex_op_name(enum bi_tex_op op)
309 {
310 switch (op) {
311 case BI_TEX_NORMAL: return "normal";
312 case BI_TEX_COMPACT: return "compact";
313 case BI_TEX_DUAL: return "dual";
314 default: return "invalid";
315 }
316 }
317
318 static void
319 bi_print_load_vary(struct bi_load_vary *load, FILE *fp)
320 {
321 fprintf(fp, "%s", bi_interp_mode_name(load->interp_mode));
322
323 if (load->reuse)
324 fprintf(fp, ".reuse");
325
326 if (load->flat)
327 fprintf(fp, ".flat");
328 }
329
330 const char *
331 bi_cond_name(enum bi_cond cond)
332 {
333 switch (cond) {
334 case BI_COND_ALWAYS: return "always";
335 case BI_COND_LT: return "lt";
336 case BI_COND_LE: return "le";
337 case BI_COND_GE: return "ge";
338 case BI_COND_GT: return "gt";
339 case BI_COND_EQ: return "eq";
340 case BI_COND_NE: return "ne";
341 default: return "invalid";
342 }
343 }
344
345 static void
346 bi_print_branch(struct bi_branch *branch, FILE *fp)
347 {
348 fprintf(fp, ".%s", bi_cond_name(branch->cond));
349 }
350
351 void
352 bi_print_instruction(bi_instruction *ins, FILE *fp)
353 {
354 if (ins->type == BI_MINMAX)
355 fprintf(fp, "%s", ins->op.minmax == BI_MINMAX_MIN ? "min" : "max");
356 else if (ins->type == BI_BITWISE)
357 fprintf(fp, "%s", bi_bitwise_op_name(ins->op.bitwise));
358 else if (ins->type == BI_SPECIAL)
359 fprintf(fp, "%s", bi_special_op_name(ins->op.special));
360 else if (ins->type == BI_TABLE)
361 fprintf(fp, "%s", bi_table_op_name(ins->op.table));
362 else if (ins->type == BI_REDUCE_FMA)
363 fprintf(fp, "%s", bi_reduce_op_name(ins->op.reduce));
364 else if (ins->type == BI_FREXP)
365 fprintf(fp, "%s", bi_frexp_op_name(ins->op.frexp));
366 else
367 fprintf(fp, "%s", bi_class_name(ins->type));
368
369 if ((ins->type == BI_ADD || ins->type == BI_FMA) && ins->op.mscale)
370 fprintf(fp, ".mscale");
371
372 if (ins->type == BI_MINMAX)
373 fprintf(fp, "%s", bi_minmax_mode_name(ins->minmax));
374 else if (ins->type == BI_LOAD_VAR)
375 bi_print_load_vary(&ins->load_vary, fp);
376 else if (ins->type == BI_BRANCH)
377 bi_print_branch(&ins->branch, fp);
378 else if (ins->type == BI_CSEL || ins->type == BI_CMP)
379 fprintf(fp, ".%s", bi_cond_name(ins->cond));
380 else if (ins->type == BI_BLEND)
381 fprintf(fp, ".loc%u", ins->blend_location);
382 else if (ins->type == BI_TEX)
383 fprintf(fp, ".%s", bi_tex_op_name(ins->op.texture));
384 else if (ins->type == BI_BITWISE)
385 fprintf(fp, ".%cshift", ins->bitwise.rshift ? 'r' : 'l');
386
387 if (ins->vector_channels)
388 fprintf(fp, ".v%u", ins->vector_channels);
389
390 if (ins->dest)
391 bi_print_alu_type(ins->dest_type, fp);
392
393 if (bi_has_outmod(ins))
394 fprintf(fp, "%s", bi_output_mod_name(ins->outmod));
395
396 if (bi_class_props[ins->type] & BI_ROUNDMODE)
397 fprintf(fp, "%s", bi_round_mode_name(ins->roundmode));
398
399 fprintf(fp, " ");
400 bool succ = bi_print_dest_index(fp, ins, ins->dest);
401 assert(succ);
402
403 if (ins->dest_offset)
404 fprintf(fp, "+%u", ins->dest_offset);
405
406 fprintf(fp, ", ");
407
408 bi_foreach_src(ins, s) {
409 bi_print_src(fp, ins, s);
410
411 if (ins->src[s] && !(ins->src[s] & (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO))) {
412 bi_print_alu_type(ins->src_types[s], fp);
413 bi_print_swizzle(ins, s, fp);
414 }
415
416 if (s < BIR_SRC_COUNT)
417 fprintf(fp, ", ");
418 }
419
420 if (ins->type == BI_BRANCH) {
421 if (ins->branch.target)
422 fprintf(fp, "-> block%u", ins->branch.target->base.name);
423 else
424 fprintf(fp, "-> blockhole");
425 }
426
427 fprintf(fp, "\n");
428 }
429
430 void
431 bi_print_bundle(bi_bundle *bundle, FILE *fp)
432 {
433 bi_instruction *ins[2] = { bundle->fma, bundle->add };
434
435 for (unsigned i = 0; i < 2; ++i) {
436 if (ins[i])
437 bi_print_instruction(ins[i], fp);
438 else
439 fprintf(fp, "nop\n");
440 }
441 }
442
443 void
444 bi_print_clause(bi_clause *clause, FILE *fp)
445 {
446 fprintf(fp, "\tid(%u)", clause->scoreboard_id);
447
448 if (clause->dependencies) {
449 fprintf(fp, ", wait(");
450
451 for (unsigned i = 0; i < 8; ++i) {
452 if (clause->dependencies & (1 << i))
453 fprintf(fp, "%u ", i);
454 }
455
456 fprintf(fp, ")");
457 }
458
459 if (!clause->back_to_back)
460 fprintf(fp, " nbb %s", clause->branch_conditional ? "branch-cond" : "branch-uncond");
461
462 if (clause->data_register_write_barrier)
463 fprintf(fp, " drwb");
464
465 fprintf(fp, "\n");
466
467 if (clause->instruction_count) {
468 assert(!clause->bundle_count);
469
470 for (unsigned i = 0; i < clause->instruction_count; ++i)
471 bi_print_instruction(clause->instructions[i], fp);
472 } else {
473 assert(clause->bundle_count);
474
475 for (unsigned i = 0; i < clause->bundle_count; ++i)
476 bi_print_bundle(&clause->bundles[i], fp);
477 }
478
479 if (clause->constant_count) {
480 for (unsigned i = 0; i < clause->constant_count; ++i)
481 fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
482
483 fprintf(fp, "\n");
484 }
485 }
486
487 void
488 bi_print_block(bi_block *block, FILE *fp)
489 {
490 fprintf(fp, "block%u {\n", block->base.name);
491
492 if (block->scheduled) {
493 bi_foreach_clause_in_block(block, clause)
494 bi_print_clause(clause, fp);
495 } else {
496 bi_foreach_instr_in_block(block, ins)
497 bi_print_instruction(ins, fp);
498 }
499
500 fprintf(fp, "}");
501
502 if (block->base.successors[0]) {
503 fprintf(fp, " -> ");
504
505 pan_foreach_successor((&block->base), succ)
506 fprintf(fp, "block%u ", succ->name);
507 }
508
509 if (block->base.predecessors->entries) {
510 fprintf(fp, " from");
511
512 bi_foreach_predecessor(block, pred)
513 fprintf(fp, " block%u", pred->base.name);
514 }
515
516 fprintf(fp, "\n\n");
517 }
518
519 void
520 bi_print_shader(bi_context *ctx, FILE *fp)
521 {
522 bi_foreach_block(ctx, block)
523 bi_print_block((bi_block *) block, fp);
524 }