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 #include "bi_print_common.h"
29 
30 static const char *
bi_segment_name(enum bi_segment seg)31 bi_segment_name(enum bi_segment seg)
32 {
33         switch (seg) {
34         case BI_SEGMENT_NONE: return "global";
35         case BI_SEGMENT_WLS:  return "wls";
36         case BI_SEGMENT_UBO:  return "ubo";
37         case BI_SEGMENT_TLS:  return "tls";
38         default: return "invalid";
39         }
40 }
41 
42 const char *
bi_class_name(enum bi_class cl)43 bi_class_name(enum bi_class cl)
44 {
45         switch (cl) {
46         case BI_ADD: return "add";
47         case BI_ATEST: return "atest";
48         case BI_BRANCH: return "branch";
49         case BI_CMP: return "cmp";
50         case BI_BLEND: return "blend";
51         case BI_BITWISE: return "bitwise";
52         case BI_COMBINE: return "combine";
53         case BI_CONVERT: return "convert";
54         case BI_CSEL: return "csel";
55         case BI_DISCARD: return "discard";
56         case BI_FMA: return "fma";
57         case BI_FMOV: return "fmov";
58         case BI_FREXP: return "frexp";
59         case BI_IMATH: return "imath";
60         case BI_LOAD: return "load";
61         case BI_LOAD_UNIFORM: return "load_uniform";
62         case BI_LOAD_ATTR: return "load_attr";
63         case BI_LOAD_VAR: return "load_var";
64         case BI_LOAD_VAR_ADDRESS: return "load_var_address";
65         case BI_LOAD_TILE: return "load_tile";
66         case BI_MINMAX: return "minmax";
67         case BI_MOV: return "mov";
68         case BI_SELECT: return "select";
69         case BI_STORE: return "store";
70         case BI_STORE_VAR: return "store_var";
71         case BI_SPECIAL_ADD: return "special";
72         case BI_SPECIAL_FMA: return "special";
73         case BI_TABLE: return "table";
74         case BI_TEXS: return "texs";
75         case BI_TEXC: return "texc";
76         case BI_TEXC_DUAL: return "texc_dual";
77         case BI_ROUND: return "round";
78         case BI_IMUL: return "imul";
79         case BI_ZS_EMIT: return "zs_emit";
80         default: return "unknown_class";
81         }
82 }
83 
84 static bool
bi_print_dest_index(FILE * fp,bi_instruction * ins,unsigned index)85 bi_print_dest_index(FILE *fp, bi_instruction *ins, unsigned index)
86 {
87         if ((index & BIR_SPECIAL) && (index & BIR_SPECIAL) != BIR_INDEX_REGISTER)
88                 return false;
89 
90         if (!index)
91                 fprintf(fp, "_");
92         else if (index & BIR_INDEX_REGISTER)
93                 fprintf(fp, "br%u", index & ~BIR_INDEX_REGISTER);
94         else if (index & PAN_IS_REG)
95                 fprintf(fp, "r%u", index >> 1);
96         else
97                 fprintf(fp, "%u", (index >> 1) - 1);
98 
99         return true;
100 }
101 
102 static void
bi_print_index(FILE * fp,bi_instruction * ins,unsigned index,unsigned s)103 bi_print_index(FILE *fp, bi_instruction *ins, unsigned index, unsigned s)
104 {
105         if (bi_print_dest_index(fp, ins, index))
106                 return;
107 
108         if (index & BIR_INDEX_UNIFORM)
109                 fprintf(fp, "u%u", index & ~BIR_INDEX_UNIFORM);
110         else if (index & BIR_INDEX_CONSTANT)
111                 fprintf(fp, "#0x%" PRIx64, bi_get_immediate(ins, s));
112         else if (index & BIR_INDEX_ZERO)
113                 fprintf(fp, "#0");
114         else if (index & BIR_INDEX_BLEND)
115                 fprintf(fp, "blend_descriptor_%u.%c", ins->blend_location,
116                         (index & ~BIR_INDEX_BLEND) == BIFROST_SRC_FAU_HI ? 'y' : 'x');
117         else
118                 fprintf(fp, "#err");
119 }
120 
121 static void
bi_print_src(FILE * fp,bi_instruction * ins,unsigned s)122 bi_print_src(FILE *fp, bi_instruction *ins, unsigned s)
123 {
124         unsigned src = ins->src[s];
125         bool mods = bi_has_source_mods(ins);
126         bool abs = ins->src_abs[s] && mods;
127         bool neg = ins->src_neg[s] && mods;
128 
129         if (neg)
130                 fprintf(fp, "-");
131 
132         if (abs)
133                 fprintf(fp, "abs(");
134 
135         bi_print_index(fp, ins, src, s);
136 
137         if (ins->type == BI_BITWISE && s == 1 && ins->bitwise.src1_invert) {
138                 /* For XOR, just use the destination invert */
139                 assert(ins->op.bitwise != BI_BITWISE_XOR);
140                 fprintf(fp, ".not");
141         }
142 
143         if (abs)
144                 fprintf(fp, ")");
145 }
146 
147 static void
bi_print_swizzle(bi_instruction * ins,unsigned src,FILE * fp)148 bi_print_swizzle(bi_instruction *ins, unsigned src, FILE *fp)
149 {
150         fprintf(fp, ".");
151 
152         for (unsigned u = 0; u < bi_get_component_count(ins, src); ++u) {
153                 assert(ins->swizzle[src][u] < 4);
154                 fputc("xyzw"[ins->swizzle[src][u]], fp);
155         }
156 }
157 
158 static const char *
bi_bitwise_op_name(enum bi_bitwise_op op)159 bi_bitwise_op_name(enum bi_bitwise_op op)
160 {
161         switch (op) {
162         case BI_BITWISE_AND: return "and";
163         case BI_BITWISE_OR: return "or";
164         case BI_BITWISE_XOR: return "xor";
165         default: return "invalid";
166         }
167 }
168 
169 static const char *
bi_imath_op_name(enum bi_imath_op op)170 bi_imath_op_name(enum bi_imath_op op)
171 {
172         switch (op) {
173         case BI_IMATH_ADD: return "iadd";
174         case BI_IMATH_SUB: return "isub";
175         default: return "invalid";
176         }
177 }
178 
179 const char *
bi_table_op_name(enum bi_table_op op)180 bi_table_op_name(enum bi_table_op op)
181 {
182         switch (op) {
183         case BI_TABLE_LOG2_U_OVER_U_1_LOW: return "log2.help";
184         default: return "invalid";
185         }
186 }
187 
188 const char *
bi_special_op_name(enum bi_special_op op)189 bi_special_op_name(enum bi_special_op op)
190 {
191         switch (op) {
192         case BI_SPECIAL_FRCP: return "frcp";
193         case BI_SPECIAL_FRSQ: return "frsq";
194         case BI_SPECIAL_EXP2_LOW: return "exp2_low";
195         case BI_SPECIAL_CUBEFACE1: return "cubeface1";
196         case BI_SPECIAL_CUBEFACE2: return "cubeface2";
197         case BI_SPECIAL_CUBE_SSEL: return "cube_ssel";
198         case BI_SPECIAL_CUBE_TSEL: return "cube_tsel";
199         default: return "invalid";
200         }
201 }
202 
203 const char *
bi_reduce_op_name(enum bi_reduce_op op)204 bi_reduce_op_name(enum bi_reduce_op op)
205 {
206         switch (op) {
207         case BI_REDUCE_ADD_FREXPM: return "add_frexpm";
208         default: return "invalid";
209         }
210 }
211 
212 const char *
bi_frexp_op_name(enum bi_frexp_op op)213 bi_frexp_op_name(enum bi_frexp_op op)
214 {
215         switch (op) {
216         case BI_FREXPE_LOG: return "frexpe_log";
217         default: return "invalid";
218         }
219 }
220 
221 static void
bi_print_load_vary(struct bi_load_vary * load,FILE * fp)222 bi_print_load_vary(struct bi_load_vary *load, FILE *fp)
223 {
224         fprintf(fp, "%s", bi_interp_mode_name(load->interp_mode));
225 
226         if (load->reuse)
227                 fprintf(fp, ".reuse");
228 
229         if (load->flat)
230                 fprintf(fp, ".flat");
231 }
232 
233 const char *
bi_cond_name(enum bi_cond cond)234 bi_cond_name(enum bi_cond cond)
235 {
236         switch (cond) {
237         case BI_COND_ALWAYS: return "always";
238         case BI_COND_LT: return "lt";
239         case BI_COND_LE: return "le";
240         case BI_COND_GE: return "ge";
241         case BI_COND_GT: return "gt";
242         case BI_COND_EQ: return "eq";
243         case BI_COND_NE: return "ne";
244         default: return "invalid";
245         }
246 }
247 
248 static void
bi_print_texture(struct bi_texture * tex,FILE * fp)249 bi_print_texture(struct bi_texture *tex, FILE *fp)
250 {
251         fprintf(fp, " - texture %u, sampler %u%s",
252                         tex->texture_index, tex->sampler_index,
253                         tex->compute_lod ? ", compute lod" : "");
254 }
255 
256 void
bi_print_instruction(bi_instruction * ins,FILE * fp)257 bi_print_instruction(bi_instruction *ins, FILE *fp)
258 {
259         if (ins->type == BI_MINMAX)
260                 fprintf(fp, "%s", ins->op.minmax == BI_MINMAX_MIN ? "min" : "max");
261         else if (ins->type == BI_BITWISE)
262                 fprintf(fp, "%s", bi_bitwise_op_name(ins->op.bitwise));
263         else if (ins->type == BI_IMATH)
264                 fprintf(fp, "%s", bi_imath_op_name(ins->op.imath));
265         else if (ins->type == BI_SPECIAL_ADD || ins->type == BI_SPECIAL_FMA)
266                 fprintf(fp, "%s", bi_special_op_name(ins->op.special));
267         else if (ins->type == BI_TABLE)
268                 fprintf(fp, "%s", bi_table_op_name(ins->op.table));
269         else if (ins->type == BI_REDUCE_FMA)
270                 fprintf(fp, "%s", bi_reduce_op_name(ins->op.reduce));
271         else if (ins->type == BI_FREXP)
272                 fprintf(fp, "%s", bi_frexp_op_name(ins->op.frexp));
273         else
274                 fprintf(fp, "%s", bi_class_name(ins->type));
275 
276         if ((ins->type == BI_ADD || ins->type == BI_FMA) && ins->op.mscale)
277                 fprintf(fp, ".mscale");
278 
279         if (ins->type == BI_MINMAX)
280                 fprintf(fp, "%s", bi_minmax_mode_name(ins->minmax));
281         else if (ins->type == BI_LOAD_VAR)
282                 bi_print_load_vary(&ins->load_vary, fp);
283         else if (ins->type == BI_BLEND)
284                 fprintf(fp, ".loc%u", ins->blend_location);
285         else if (ins->type == BI_BITWISE)
286                 fprintf(fp, ".%cshift", ins->bitwise.rshift ? 'r' : 'l');
287 
288         if (bi_class_props[ins->type] & BI_CONDITIONAL)
289                 fprintf(fp, ".%s", bi_cond_name(ins->cond));
290 
291         if (ins->skip)
292                 fprintf(fp, ".skip");
293 
294         if (ins->no_spill)
295                 fprintf(fp, ".no_spill");
296 
297         if (ins->vector_channels)
298                 fprintf(fp, ".v%u", ins->vector_channels);
299 
300         if (ins->segment)
301                 fprintf(fp, ".%s", bi_segment_name(ins->segment));
302 
303         if (ins->dest)
304                 pan_print_alu_type(ins->dest_type, fp);
305 
306         if (ins->format && ins->dest != ins->format)
307                 pan_print_alu_type(ins->format, fp);
308 
309         if (bi_has_outmod(ins))
310                 fprintf(fp, "%s", bi_output_mod_name(ins->outmod));
311 
312         if (bi_class_props[ins->type] & BI_ROUNDMODE)
313                 fprintf(fp, "%s", bi_round_mode_name(ins->roundmode));
314 
315         if (ins->type == BI_BITWISE && ins->bitwise.dest_invert)
316                 fprintf(fp, ".not");
317 
318         fprintf(fp, " ");
319         ASSERTED bool succ = bi_print_dest_index(fp, ins, ins->dest);
320         assert(succ);
321 
322         if (ins->dest_offset)
323                 fprintf(fp, "+%u", ins->dest_offset);
324 
325         fprintf(fp, ", ");
326 
327         bi_foreach_src(ins, s) {
328                 bi_print_src(fp, ins, s);
329 
330                 if (ins->src[s] && !(ins->src[s] & (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO))) {
331                         pan_print_alu_type(ins->src_types[s], fp);
332                         bi_print_swizzle(ins, s, fp);
333                 }
334 
335                 if (s < BIR_SRC_COUNT)
336                         fprintf(fp, ", ");
337         }
338 
339         if (ins->type == BI_BRANCH) {
340                 if (ins->branch_target) {
341                         fprintf(fp, "-> block%u", ins->branch_target->base.name);
342                 } else {
343                         fprintf(fp, "-> void");
344                 }
345         } else if (ins->type == BI_TEXS) {
346                 bi_print_texture(&ins->texture, fp);
347         }
348 
349         fprintf(fp, "\n");
350 }
351 
352 static const char *
bi_reg_op_name(enum bifrost_reg_op op)353 bi_reg_op_name(enum bifrost_reg_op op)
354 {
355         switch (op) {
356         case BIFROST_OP_IDLE: return "idle";
357         case BIFROST_OP_READ: return "read";
358         case BIFROST_OP_WRITE: return "write";
359         case BIFROST_OP_WRITE_LO: return "write lo";
360         case BIFROST_OP_WRITE_HI: return "write hi";
361         default: return "invalid";
362         }
363 }
364 
365 void
bi_print_slots(bi_registers * regs,FILE * fp)366 bi_print_slots(bi_registers *regs, FILE *fp)
367 {
368         for (unsigned i = 0; i < 2; ++i) {
369                 if (regs->enabled[i])
370                         fprintf(fp, "slot %u: %u\n", i, regs->slot[i]);
371         }
372 
373         if (regs->slot23.slot2) {
374                 fprintf(fp, "slot 2 (%s%s): %u\n",
375                                 bi_reg_op_name(regs->slot23.slot2),
376                                 regs->slot23.slot2 >= BIFROST_OP_WRITE ?
377                                         " FMA": "",
378                                 regs->slot[2]);
379         }
380 
381         if (regs->slot23.slot3) {
382                 fprintf(fp, "slot 3 (%s %s): %u\n",
383                                 bi_reg_op_name(regs->slot23.slot3),
384                                 regs->slot23.slot3_fma ? "FMA" : "ADD",
385                                 regs->slot[3]);
386         }
387 }
388 
389 void
bi_print_bundle(bi_bundle * bundle,FILE * fp)390 bi_print_bundle(bi_bundle *bundle, FILE *fp)
391 {
392         bi_instruction *ins[2] = { bundle->fma, bundle->add };
393 
394         for (unsigned i = 0; i < 2; ++i) {
395                 if (ins[i])
396                         bi_print_instruction(ins[i], fp);
397                 else
398                         fprintf(fp, "nop\n");
399         }
400 }
401 
402 void
bi_print_clause(bi_clause * clause,FILE * fp)403 bi_print_clause(bi_clause *clause, FILE *fp)
404 {
405         fprintf(fp, "\tid(%u)", clause->scoreboard_id);
406 
407         if (clause->dependencies) {
408                 fprintf(fp, ", wait(");
409 
410                 for (unsigned i = 0; i < 8; ++i) {
411                         if (clause->dependencies & (1 << i))
412                                 fprintf(fp, "%u ", i);
413                 }
414 
415                 fprintf(fp, ")");
416         }
417 
418         fprintf(fp, " %s", bi_flow_control_name(clause->flow_control));
419 
420         if (!clause->next_clause_prefetch)
421                fprintf(fp, " no_prefetch");
422 
423         if (clause->staging_barrier)
424                 fprintf(fp, " osrb");
425 
426         fprintf(fp, "\n");
427 
428         for (unsigned i = 0; i < clause->bundle_count; ++i)
429                 bi_print_bundle(&clause->bundles[i], fp);
430 
431         if (clause->constant_count) {
432                 for (unsigned i = 0; i < clause->constant_count; ++i)
433                         fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
434 
435                 if (clause->branch_constant)
436                         fprintf(fp, "*");
437 
438                 fprintf(fp, "\n");
439         }
440 }
441 
442 void
bi_print_block(bi_block * block,FILE * fp)443 bi_print_block(bi_block *block, FILE *fp)
444 {
445         fprintf(fp, "block%u {\n", block->base.name);
446 
447         if (block->scheduled) {
448                 bi_foreach_clause_in_block(block, clause)
449                         bi_print_clause(clause, fp);
450         } else {
451                 bi_foreach_instr_in_block(block, ins)
452                         bi_print_instruction(ins, fp);
453         }
454 
455         fprintf(fp, "}");
456 
457         if (block->base.successors[0]) {
458                 fprintf(fp, " -> ");
459 
460                 pan_foreach_successor((&block->base), succ)
461                         fprintf(fp, "block%u ", succ->name);
462         }
463 
464         if (block->base.predecessors->entries) {
465                 fprintf(fp, " from");
466 
467                 bi_foreach_predecessor(block, pred)
468                         fprintf(fp, " block%u", pred->base.name);
469         }
470 
471         fprintf(fp, "\n\n");
472 }
473 
474 void
bi_print_shader(bi_context * ctx,FILE * fp)475 bi_print_shader(bi_context *ctx, FILE *fp)
476 {
477         bi_foreach_block(ctx, block)
478                 bi_print_block((bi_block *) block, fp);
479 }
480