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 *
bi_clause_type_name(enum bifrost_clause_type T)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_FRAGZ: return "fragz";
41         case BIFROST_CLAUSE_ATEST: return "atest";
42         case BIFROST_CLAUSE_64BIT: return "64";
43         default: return "??";
44         }
45 }
46 
47 const char *
bi_output_mod_name(enum bifrost_outmod mod)48 bi_output_mod_name(enum bifrost_outmod mod)
49 {
50         switch (mod) {
51         case BIFROST_NONE: return "";
52         case BIFROST_POS: return ".pos";
53         case BIFROST_SAT_SIGNED: return ".sat_signed";
54         case BIFROST_SAT: return ".sat";
55         default: return "invalid";
56         }
57 }
58 
59 const char *
bi_minmax_mode_name(enum bifrost_minmax_mode mod)60 bi_minmax_mode_name(enum bifrost_minmax_mode mod)
61 {
62         switch (mod) {
63         case BIFROST_MINMAX_NONE: return "";
64         case BIFROST_NAN_WINS: return ".nan_wins";
65         case BIFROST_SRC1_WINS: return ".src1_wins";
66         case BIFROST_SRC0_WINS: return ".src0_wins";
67         default: return "invalid";
68         }
69 }
70 
71 const char *
bi_round_mode_name(enum bifrost_roundmode mod)72 bi_round_mode_name(enum bifrost_roundmode mod)
73 {
74         switch (mod) {
75         case BIFROST_RTE: return "";
76         case BIFROST_RTP: return ".rtp";
77         case BIFROST_RTN: return ".rtn";
78         case BIFROST_RTZ: return ".rtz";
79         default: return "invalid";
80         }
81 }
82 
83 const char *
bi_csel_cond_name(enum bifrost_csel_cond cond)84 bi_csel_cond_name(enum bifrost_csel_cond cond)
85 {
86         switch (cond) {
87         case BIFROST_FEQ_F: return "feq.f";
88         case BIFROST_FGT_F: return "fgt.f";
89         case BIFROST_FGE_F: return "fge.f";
90         case BIFROST_IEQ_F: return "ieq.f";
91         case BIFROST_IGT_I: return "igt.i";
92         case BIFROST_IGE_I: return "uge.i";
93         case BIFROST_UGT_I: return "ugt.i";
94         case BIFROST_UGE_I: return "uge.i";
95         default: return "invalid";
96         }
97 }
98 
99 const char *
bi_interp_mode_name(enum bifrost_interp_mode mode)100 bi_interp_mode_name(enum bifrost_interp_mode mode)
101 {
102         switch (mode) {
103         case BIFROST_INTERP_PER_FRAG: return ".per_frag";
104         case BIFROST_INTERP_CENTROID: return ".centroid";
105         case BIFROST_INTERP_DEFAULT: return "";
106         case BIFROST_INTERP_EXPLICIT: return ".explicit";
107         default: return ".unknown";
108         }
109 }
110 
111 const char *
bi_ldst_type_name(enum bifrost_ldst_type type)112 bi_ldst_type_name(enum bifrost_ldst_type type)
113 {
114         switch (type) {
115         case BIFROST_LDST_F16: return "f16";
116         case BIFROST_LDST_F32: return "f32";
117         case BIFROST_LDST_I32: return "i32";
118         case BIFROST_LDST_U32: return "u32";
119         default: return "invalid";
120         }
121 }
122 
123 /* The remaining functions in this file are for IR-internal
124  * structures; the disassembler doesn't use them */
125 
126 const char *
bi_class_name(enum bi_class cl)127 bi_class_name(enum bi_class cl)
128 {
129         switch (cl) {
130         case BI_ADD: return "add";
131         case BI_ATEST: return "atest";
132         case BI_BRANCH: return "branch";
133         case BI_CMP: return "cmp";
134         case BI_BLEND: return "blend";
135         case BI_BITWISE: return "bitwise";
136         case BI_COMBINE: return "combine";
137         case BI_CONVERT: return "convert";
138         case BI_CSEL: return "csel";
139         case BI_DISCARD: return "discard";
140         case BI_FMA: return "fma";
141         case BI_FMOV: return "fmov";
142         case BI_FREXP: return "frexp";
143         case BI_IMATH: return "imath";
144         case BI_LOAD: return "load";
145         case BI_LOAD_UNIFORM: return "load_uniform";
146         case BI_LOAD_ATTR: return "load_attr";
147         case BI_LOAD_VAR: return "load_var";
148         case BI_LOAD_VAR_ADDRESS: return "load_var_address";
149         case BI_MINMAX: return "minmax";
150         case BI_MOV: return "mov";
151         case BI_SELECT: return "select";
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         case BI_IMUL: return "imul";
159         default: return "unknown_class";
160         }
161 }
162 
163 static bool
bi_print_dest_index(FILE * fp,bi_instruction * ins,unsigned index)164 bi_print_dest_index(FILE *fp, bi_instruction *ins, unsigned index)
165 {
166         if (!index)
167                 fprintf(fp, "_");
168         else if (index & BIR_INDEX_REGISTER)
169                 fprintf(fp, "br%u", index & ~BIR_INDEX_REGISTER);
170         else if (index & PAN_IS_REG)
171                 fprintf(fp, "r%u", index >> 1);
172         else if (!(index & BIR_SPECIAL))
173                 fprintf(fp, "%u", (index >> 1) - 1);
174         else
175                 return false;
176 
177         return true;
178 }
179 
180 static void
bi_print_index(FILE * fp,bi_instruction * ins,unsigned index,unsigned s)181 bi_print_index(FILE *fp, bi_instruction *ins, unsigned index, unsigned s)
182 {
183         if (bi_print_dest_index(fp, ins, index))
184                 return;
185 
186         if (index & BIR_INDEX_UNIFORM)
187                 fprintf(fp, "u%u", index & ~BIR_INDEX_UNIFORM);
188         else if (index & BIR_INDEX_CONSTANT)
189                 fprintf(fp, "#0x%" PRIx64, bi_get_immediate(ins, s));
190         else if (index & BIR_INDEX_ZERO)
191                 fprintf(fp, "#0");
192         else
193                 fprintf(fp, "#err");
194 }
195 
196 static void
bi_print_src(FILE * fp,bi_instruction * ins,unsigned s)197 bi_print_src(FILE *fp, bi_instruction *ins, unsigned s)
198 {
199         unsigned src = ins->src[s];
200         bool mods = bi_has_source_mods(ins);
201         bool abs = ins->src_abs[s] && mods;
202         bool neg = ins->src_neg[s] && mods;
203 
204         if (neg)
205                 fprintf(fp, "-");
206 
207         if (abs)
208                 fprintf(fp, "abs(");
209 
210         if (ins->type == BI_BITWISE && ins->bitwise.src_invert[s])
211                 fprintf(fp, "~");
212 
213         bi_print_index(fp, ins, src, s);
214 
215         if (abs)
216                 fprintf(fp, ")");
217 }
218 
219 static void
bi_print_swizzle(bi_instruction * ins,unsigned src,FILE * fp)220 bi_print_swizzle(bi_instruction *ins, unsigned src, FILE *fp)
221 {
222         fprintf(fp, ".");
223 
224         for (unsigned u = 0; u < bi_get_component_count(ins, src); ++u) {
225                 assert(ins->swizzle[src][u] < 4);
226                 fputc("xyzw"[ins->swizzle[src][u]], fp);
227         }
228 }
229 
230 static const char *
bi_bitwise_op_name(enum bi_bitwise_op op)231 bi_bitwise_op_name(enum bi_bitwise_op op)
232 {
233         switch (op) {
234         case BI_BITWISE_AND: return "and";
235         case BI_BITWISE_OR: return "or";
236         case BI_BITWISE_XOR: return "xor";
237         default: return "invalid";
238         }
239 }
240 
241 static const char *
bi_imath_op_name(enum bi_imath_op op)242 bi_imath_op_name(enum bi_imath_op op)
243 {
244         switch (op) {
245         case BI_IMATH_ADD: return "iadd";
246         case BI_IMATH_SUB: return "isub";
247         default: return "invalid";
248         }
249 }
250 
251 const char *
bi_table_op_name(enum bi_table_op op)252 bi_table_op_name(enum bi_table_op op)
253 {
254         switch (op) {
255         case BI_TABLE_LOG2_U_OVER_U_1_LOW: return "log2.help";
256         default: return "invalid";
257         }
258 }
259 
260 const char *
bi_special_op_name(enum bi_special_op op)261 bi_special_op_name(enum bi_special_op op)
262 {
263         switch (op) {
264         case BI_SPECIAL_FRCP: return "frcp";
265         case BI_SPECIAL_FRSQ: return "frsq";
266         case BI_SPECIAL_EXP2_LOW: return "exp2_low";
267         default: return "invalid";
268         }
269 }
270 
271 const char *
bi_reduce_op_name(enum bi_reduce_op op)272 bi_reduce_op_name(enum bi_reduce_op op)
273 {
274         switch (op) {
275         case BI_REDUCE_ADD_FREXPM: return "add_frexpm";
276         default: return "invalid";
277         }
278 }
279 
280 const char *
bi_frexp_op_name(enum bi_frexp_op op)281 bi_frexp_op_name(enum bi_frexp_op op)
282 {
283         switch (op) {
284         case BI_FREXPE_LOG: return "frexpe_log";
285         default: return "invalid";
286         }
287 }
288 
289 const char *
bi_tex_op_name(enum bi_tex_op op)290 bi_tex_op_name(enum bi_tex_op op)
291 {
292         switch (op) {
293         case BI_TEX_NORMAL: return "normal";
294         case BI_TEX_COMPACT: return "compact";
295         case BI_TEX_DUAL: return "dual";
296         default: return "invalid";
297         }
298 }
299 
300 static void
bi_print_load_vary(struct bi_load_vary * load,FILE * fp)301 bi_print_load_vary(struct bi_load_vary *load, FILE *fp)
302 {
303         fprintf(fp, "%s", bi_interp_mode_name(load->interp_mode));
304 
305         if (load->reuse)
306                 fprintf(fp, ".reuse");
307 
308         if (load->flat)
309                 fprintf(fp, ".flat");
310 }
311 
312 const char *
bi_cond_name(enum bi_cond cond)313 bi_cond_name(enum bi_cond cond)
314 {
315         switch (cond) {
316         case BI_COND_ALWAYS: return "always";
317         case BI_COND_LT: return "lt";
318         case BI_COND_LE: return "le";
319         case BI_COND_GE: return "ge";
320         case BI_COND_GT: return "gt";
321         case BI_COND_EQ: return "eq";
322         case BI_COND_NE: return "ne";
323         default: return "invalid";
324         }
325 }
326 
327 static void
bi_print_texture(struct bi_texture * tex,FILE * fp)328 bi_print_texture(struct bi_texture *tex, FILE *fp)
329 {
330         fprintf(fp, " - texture %u, sampler %u",
331                         tex->texture_index, tex->sampler_index);
332 }
333 
334 void
bi_print_instruction(bi_instruction * ins,FILE * fp)335 bi_print_instruction(bi_instruction *ins, FILE *fp)
336 {
337         if (ins->type == BI_MINMAX)
338                 fprintf(fp, "%s", ins->op.minmax == BI_MINMAX_MIN ? "min" : "max");
339         else if (ins->type == BI_BITWISE)
340                 fprintf(fp, "%s", bi_bitwise_op_name(ins->op.bitwise));
341         else if (ins->type == BI_IMATH)
342                 fprintf(fp, "%s", bi_imath_op_name(ins->op.imath));
343         else if (ins->type == BI_SPECIAL)
344                 fprintf(fp, "%s", bi_special_op_name(ins->op.special));
345         else if (ins->type == BI_TABLE)
346                 fprintf(fp, "%s", bi_table_op_name(ins->op.table));
347         else if (ins->type == BI_REDUCE_FMA)
348                 fprintf(fp, "%s", bi_reduce_op_name(ins->op.reduce));
349         else if (ins->type == BI_FREXP)
350                 fprintf(fp, "%s", bi_frexp_op_name(ins->op.frexp));
351         else
352                 fprintf(fp, "%s", bi_class_name(ins->type));
353 
354         if ((ins->type == BI_ADD || ins->type == BI_FMA) && ins->op.mscale)
355                 fprintf(fp, ".mscale");
356 
357         if (ins->type == BI_MINMAX)
358                 fprintf(fp, "%s", bi_minmax_mode_name(ins->minmax));
359         else if (ins->type == BI_LOAD_VAR)
360                 bi_print_load_vary(&ins->load_vary, fp);
361         else if (ins->type == BI_BLEND)
362                 fprintf(fp, ".loc%u", ins->blend_location);
363         else if (ins->type == BI_TEX) {
364                 fprintf(fp, ".%s", bi_tex_op_name(ins->op.texture));
365         } else if (ins->type == BI_BITWISE)
366                 fprintf(fp, ".%cshift", ins->bitwise.rshift ? 'r' : 'l');
367 
368         if (bi_class_props[ins->type] & BI_CONDITIONAL)
369                 fprintf(fp, ".%s", bi_cond_name(ins->cond));
370 
371         if (ins->vector_channels)
372                 fprintf(fp, ".v%u", ins->vector_channels);
373 
374         if (ins->dest)
375                 pan_print_alu_type(ins->dest_type, fp);
376 
377         if (bi_has_outmod(ins))
378                 fprintf(fp, "%s", bi_output_mod_name(ins->outmod));
379 
380         if (bi_class_props[ins->type] & BI_ROUNDMODE)
381                 fprintf(fp, "%s", bi_round_mode_name(ins->roundmode));
382 
383         fprintf(fp, " ");
384         bool succ = bi_print_dest_index(fp, ins, ins->dest);
385         assert(succ);
386 
387         if (ins->dest_offset)
388                 fprintf(fp, "+%u", ins->dest_offset);
389 
390         fprintf(fp, ", ");
391 
392         bi_foreach_src(ins, s) {
393                 bi_print_src(fp, ins, s);
394 
395                 if (ins->src[s] && !(ins->src[s] & (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO))) {
396                         pan_print_alu_type(ins->src_types[s], fp);
397                         bi_print_swizzle(ins, s, fp);
398                 }
399 
400                 if (s < BIR_SRC_COUNT)
401                         fprintf(fp, ", ");
402         }
403 
404         if (ins->type == BI_BRANCH) {
405                 if (ins->branch_target) {
406                         fprintf(fp, "-> block%u", ins->branch_target->base.name);
407                 } else {
408                         fprintf(fp, "-> void");
409                 }
410         } else if (ins->type == BI_TEX) {
411                 bi_print_texture(&ins->texture, fp);
412         }
413 
414         fprintf(fp, "\n");
415 }
416 
417 void
bi_print_ports(bi_registers * regs,FILE * fp)418 bi_print_ports(bi_registers *regs, FILE *fp)
419 {
420         for (unsigned i = 0; i < 2; ++i) {
421                 if (regs->enabled[i])
422                         fprintf(fp, "port %u: %u\n", i, regs->port[i]);
423         }
424 
425         if (regs->write_fma || regs->write_add) {
426                 fprintf(fp, "port 2 (%s): %u\n",
427                                 regs->write_add ? "ADD" : "FMA",
428                                 regs->port[2]);
429         }
430 
431         if ((regs->write_fma && regs->write_add) || regs->read_port3) {
432                 fprintf(fp, "port 3 (%s): %u\n",
433                                 regs->read_port3 ? "read" : "FMA",
434                                 regs->port[3]);
435         }
436 }
437 
438 void
bi_print_bundle(bi_bundle * bundle,FILE * fp)439 bi_print_bundle(bi_bundle *bundle, FILE *fp)
440 {
441         bi_instruction *ins[2] = { bundle->fma, bundle->add };
442 
443         for (unsigned i = 0; i < 2; ++i) {
444                 if (ins[i])
445                         bi_print_instruction(ins[i], fp);
446                 else
447                         fprintf(fp, "nop\n");
448         }
449 }
450 
451 void
bi_print_clause(bi_clause * clause,FILE * fp)452 bi_print_clause(bi_clause *clause, FILE *fp)
453 {
454         fprintf(fp, "\tid(%u)", clause->scoreboard_id);
455 
456         if (clause->dependencies) {
457                 fprintf(fp, ", wait(");
458 
459                 for (unsigned i = 0; i < 8; ++i) {
460                         if (clause->dependencies & (1 << i))
461                                 fprintf(fp, "%u ", i);
462                 }
463 
464                 fprintf(fp, ")");
465         }
466 
467         if (!clause->back_to_back)
468                 fprintf(fp, " nbb %s", clause->branch_conditional ? "branch-cond" : "branch-uncond");
469 
470         if (clause->data_register_write_barrier)
471                 fprintf(fp, " drwb");
472 
473         fprintf(fp, "\n");
474 
475         for (unsigned i = 0; i < clause->bundle_count; ++i)
476                 bi_print_bundle(&clause->bundles[i], fp);
477 
478         if (clause->constant_count) {
479                 for (unsigned i = 0; i < clause->constant_count; ++i)
480                         fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
481 
482                 if (clause->branch_constant)
483                         fprintf(fp, "*");
484 
485                 fprintf(fp, "\n");
486         }
487 }
488 
489 void
bi_print_block(bi_block * block,FILE * fp)490 bi_print_block(bi_block *block, FILE *fp)
491 {
492         fprintf(fp, "block%u {\n", block->base.name);
493 
494         if (block->scheduled) {
495                 bi_foreach_clause_in_block(block, clause)
496                         bi_print_clause(clause, fp);
497         } else {
498                 bi_foreach_instr_in_block(block, ins)
499                         bi_print_instruction(ins, fp);
500         }
501 
502         fprintf(fp, "}");
503 
504         if (block->base.successors[0]) {
505                 fprintf(fp, " -> ");
506 
507                 pan_foreach_successor((&block->base), succ)
508                         fprintf(fp, "block%u ", succ->name);
509         }
510 
511         if (block->base.predecessors->entries) {
512                 fprintf(fp, " from");
513 
514                 bi_foreach_predecessor(block, pred)
515                         fprintf(fp, " block%u", pred->base.name);
516         }
517 
518         fprintf(fp, "\n\n");
519 }
520 
521 void
bi_print_shader(bi_context * ctx,FILE * fp)522 bi_print_shader(bi_context *ctx, FILE *fp)
523 {
524         bi_foreach_block(ctx, block)
525                 bi_print_block((bi_block *) block, fp);
526 }
527