1 /*
2 * Copyright © 2014 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include <stdarg.h>
10 #include <stdio.h>
11
12 #include "util/log.h"
13 #include "ir3.h"
14
15 #define PTRID(x) ((unsigned long)(x))
16
17 /* ansi escape sequences: */
18 #define RESET "\x1b[0m"
19 #define RED "\x1b[0;31m"
20 #define GREEN "\x1b[0;32m"
21 #define BLUE "\x1b[0;34m"
22 #define MAGENTA "\x1b[0;35m"
23
24 /* syntax coloring, mostly to make it easier to see different sorts of
25 * srcs (immediate, constant, ssa, array, ...)
26 */
27 #define SYN_REG(x) RED x RESET
28 #define SYN_IMMED(x) GREEN x RESET
29 #define SYN_CONST(x) GREEN x RESET
30 #define SYN_SSA(x) BLUE x RESET
31 #define SYN_ARRAY(x) MAGENTA x RESET
32
33 static const char *
type_name(type_t type)34 type_name(type_t type)
35 {
36 static const char *type_names[] = {
37 /* clang-format off */
38 [TYPE_F16] = "f16",
39 [TYPE_F32] = "f32",
40 [TYPE_U16] = "u16",
41 [TYPE_U32] = "u32",
42 [TYPE_S16] = "s16",
43 [TYPE_S32] = "s32",
44 [TYPE_U8] = "u8",
45 [TYPE_U8_32] = "u8_32",
46 /* clang-format on */
47 };
48 return type_names[type];
49 }
50
51 static void
print_instr_name(struct log_stream * stream,struct ir3_instruction * instr,bool flags)52 print_instr_name(struct log_stream *stream, struct ir3_instruction *instr,
53 bool flags)
54 {
55 if (!instr)
56 return;
57 #if MESA_DEBUG
58 mesa_log_stream_printf(stream, "%04u:", instr->serialno);
59 #endif
60 mesa_log_stream_printf(stream, "%04u:", instr->ip);
61 if (instr->flags & IR3_INSTR_UNUSED) {
62 mesa_log_stream_printf(stream, "XXX: ");
63 } else {
64 mesa_log_stream_printf(stream, "%03u: ", instr->use_count);
65 }
66
67 if (flags) {
68 mesa_log_stream_printf(stream, "\t");
69 if (instr->flags & IR3_INSTR_SY)
70 mesa_log_stream_printf(stream, "(sy)");
71 if (instr->flags & IR3_INSTR_SS)
72 mesa_log_stream_printf(stream, "(ss)");
73 if (instr->flags & IR3_INSTR_JP)
74 mesa_log_stream_printf(stream, "(jp)");
75 if (instr->repeat)
76 mesa_log_stream_printf(stream, "(rpt%d)", instr->repeat);
77 if (instr->nop)
78 mesa_log_stream_printf(stream, "(nop%d)", instr->nop);
79 if (instr->flags & IR3_INSTR_UL)
80 mesa_log_stream_printf(stream, "(ul)");
81 if (instr->flags & IR3_INSTR_SAT)
82 mesa_log_stream_printf(stream, "(sat)");
83 } else {
84 mesa_log_stream_printf(stream, " ");
85 }
86
87 if (is_meta(instr)) {
88 switch (instr->opc) {
89 case OPC_META_INPUT:
90 mesa_log_stream_printf(stream, "_meta:in");
91 break;
92 case OPC_META_SPLIT:
93 mesa_log_stream_printf(stream, "_meta:split");
94 break;
95 case OPC_META_COLLECT:
96 mesa_log_stream_printf(stream, "_meta:collect");
97 break;
98 case OPC_META_TEX_PREFETCH:
99 mesa_log_stream_printf(stream, "_meta:tex_prefetch");
100 break;
101 case OPC_META_PARALLEL_COPY:
102 mesa_log_stream_printf(stream, "_meta:parallel_copy");
103 break;
104 case OPC_META_PHI:
105 mesa_log_stream_printf(stream, "_meta:phi");
106 break;
107
108 /* shouldn't hit here.. just for debugging: */
109 default:
110 mesa_log_stream_printf(stream, "_meta:%d", instr->opc);
111 break;
112 }
113 } else if (opc_cat(instr->opc) == 1) {
114 if (instr->opc == OPC_MOV) {
115 if (instr->cat1.src_type == instr->cat1.dst_type)
116 mesa_log_stream_printf(stream, "mov");
117 else
118 mesa_log_stream_printf(stream, "cov");
119 } else {
120 mesa_log_stream_printf(stream, "%s",
121 disasm_a3xx_instr_name(instr->opc));
122 }
123
124 if (instr->opc == OPC_SCAN_MACRO ||
125 instr->opc == OPC_SCAN_CLUSTERS_MACRO) {
126 switch (instr->cat1.reduce_op) {
127 case REDUCE_OP_ADD_U:
128 mesa_log_stream_printf(stream, ".add.u");
129 break;
130 case REDUCE_OP_ADD_F:
131 mesa_log_stream_printf(stream, ".add.f");
132 break;
133 case REDUCE_OP_MUL_U:
134 mesa_log_stream_printf(stream, ".mul.u");
135 break;
136 case REDUCE_OP_MUL_F:
137 mesa_log_stream_printf(stream, ".mul.f");
138 break;
139 case REDUCE_OP_MIN_U:
140 mesa_log_stream_printf(stream, ".min.u");
141 break;
142 case REDUCE_OP_MIN_S:
143 mesa_log_stream_printf(stream, ".min.s");
144 break;
145 case REDUCE_OP_MIN_F:
146 mesa_log_stream_printf(stream, ".min.f");
147 break;
148 case REDUCE_OP_MAX_U:
149 mesa_log_stream_printf(stream, ".max.u");
150 break;
151 case REDUCE_OP_MAX_S:
152 mesa_log_stream_printf(stream, ".max.s");
153 break;
154 case REDUCE_OP_MAX_F:
155 mesa_log_stream_printf(stream, ".max.f");
156 break;
157 case REDUCE_OP_AND_B:
158 mesa_log_stream_printf(stream, ".and.b");
159 break;
160 case REDUCE_OP_OR_B:
161 mesa_log_stream_printf(stream, ".or.b");
162 break;
163 case REDUCE_OP_XOR_B:
164 mesa_log_stream_printf(stream, ".xor.b");
165 break;
166 }
167 }
168
169 if (instr->opc != OPC_MOVMSK && instr->opc != OPC_SCAN_MACRO &&
170 instr->opc != OPC_PUSH_CONSTS_LOAD_MACRO) {
171 mesa_log_stream_printf(stream, ".%s%s",
172 type_name(instr->cat1.src_type),
173 type_name(instr->cat1.dst_type));
174 }
175 } else {
176 mesa_log_stream_printf(stream, "%s", disasm_a3xx_instr_name(instr->opc));
177 if (instr->flags & IR3_INSTR_3D)
178 mesa_log_stream_printf(stream, ".3d");
179 if (instr->flags & IR3_INSTR_A)
180 mesa_log_stream_printf(stream, ".a");
181 if (instr->flags & IR3_INSTR_O)
182 mesa_log_stream_printf(stream, ".o");
183 if (instr->flags & IR3_INSTR_P)
184 mesa_log_stream_printf(stream, ".p");
185 if (instr->flags & IR3_INSTR_S)
186 mesa_log_stream_printf(stream, ".s");
187 if (instr->flags & IR3_INSTR_V)
188 mesa_log_stream_printf(stream, ".v");
189 if (instr->flags & IR3_INSTR_A1EN)
190 mesa_log_stream_printf(stream, ".a1en");
191 if (instr->flags & IR3_INSTR_U)
192 mesa_log_stream_printf(stream, ".u");
193 if (instr->opc == OPC_LDC)
194 mesa_log_stream_printf(stream, ".offset%d", instr->cat6.d);
195 if (instr->opc == OPC_LDC_K)
196 mesa_log_stream_printf(stream, ".%d", instr->cat6.iim_val);
197 if (instr->flags & IR3_INSTR_B) {
198 mesa_log_stream_printf(
199 stream, ".base%d",
200 is_tex(instr) ? instr->cat5.tex_base : instr->cat6.base);
201 }
202 if (instr->flags & IR3_INSTR_S2EN)
203 mesa_log_stream_printf(stream, ".s2en");
204
205 static const char *cond[0x7] = {
206 "lt", "le", "gt", "ge", "eq", "ne",
207 };
208
209 switch (instr->opc) {
210 case OPC_CMPS_F:
211 case OPC_CMPS_U:
212 case OPC_CMPS_S:
213 case OPC_CMPV_F:
214 case OPC_CMPV_U:
215 case OPC_CMPV_S:
216 mesa_log_stream_printf(stream, ".%s",
217 cond[instr->cat2.condition & 0x7]);
218 break;
219 case OPC_BRAC:
220 mesa_log_stream_printf(stream, ".%u", instr->cat0.idx);
221 break;
222 default:
223 break;
224 }
225 }
226 }
227
228 static void
print_ssa_def_name(struct log_stream * stream,struct ir3_register * reg)229 print_ssa_def_name(struct log_stream *stream, struct ir3_register *reg)
230 {
231 mesa_log_stream_printf(stream, SYN_SSA("ssa_%u"), reg->instr->serialno);
232 if (reg->name != 0)
233 mesa_log_stream_printf(stream, ":%u", reg->name);
234 }
235
236 static void
print_ssa_name(struct log_stream * stream,struct ir3_register * reg,bool dst)237 print_ssa_name(struct log_stream *stream, struct ir3_register *reg, bool dst)
238 {
239 if (!dst) {
240 if (!reg->def)
241 mesa_log_stream_printf(stream, SYN_SSA("undef"));
242 else
243 print_ssa_def_name(stream, reg->def);
244 } else {
245 print_ssa_def_name(stream, reg);
246 }
247
248 if (reg->num != INVALID_REG && !(reg->flags & IR3_REG_ARRAY)) {
249 const char *prefix = "r";
250 unsigned num = reg_num(reg);
251
252 if (reg->flags & IR3_REG_PREDICATE) {
253 prefix = "p";
254 num = 0;
255 }
256
257 mesa_log_stream_printf(stream, "(" SYN_REG("%s%u.%c") ")", prefix, num,
258 "xyzw"[reg_comp(reg)]);
259 }
260 }
261
262 static void
print_reg_name(struct log_stream * stream,struct ir3_instruction * instr,struct ir3_register * reg,bool dest)263 print_reg_name(struct log_stream *stream, struct ir3_instruction *instr,
264 struct ir3_register *reg, bool dest)
265 {
266 if ((reg->flags & (IR3_REG_FABS | IR3_REG_SABS)) &&
267 (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT)))
268 mesa_log_stream_printf(stream, "(absneg)");
269 else if (reg->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT))
270 mesa_log_stream_printf(stream, "(neg)");
271 else if (reg->flags & (IR3_REG_FABS | IR3_REG_SABS))
272 mesa_log_stream_printf(stream, "(abs)");
273
274 if (reg->flags & IR3_REG_FIRST_KILL)
275 mesa_log_stream_printf(stream, "(kill)");
276 if (reg->flags & IR3_REG_UNUSED)
277 mesa_log_stream_printf(stream, "(unused)");
278
279 if (reg->flags & IR3_REG_R)
280 mesa_log_stream_printf(stream, "(r)");
281
282 if (reg->flags & IR3_REG_EARLY_CLOBBER)
283 mesa_log_stream_printf(stream, "(early_clobber)");
284
285 /* Right now all instructions that use tied registers only have one
286 * destination register, so we can just print (tied) as if it's a flag,
287 * although it's more convenient for RA if it's a pointer.
288 */
289 if (reg->tied)
290 mesa_log_stream_printf(stream, "(tied)");
291
292 if (instr->opc == OPC_BR || instr->opc == OPC_BRAA ||
293 instr->opc == OPC_BRAO) {
294 bool inv = reg == instr->srcs[0] ? instr->cat0.inv1 : instr->cat0.inv2;
295 if (inv)
296 mesa_log_stream_printf(stream, "!");
297 }
298
299 if (reg->flags & IR3_REG_SHARED)
300 mesa_log_stream_printf(stream, "s");
301 if (reg->flags & IR3_REG_HALF)
302 mesa_log_stream_printf(stream, "h");
303 if (reg->flags & IR3_REG_PREDICATE)
304 mesa_log_stream_printf(stream, "p");
305
306 if (reg->flags & IR3_REG_IMMED) {
307 mesa_log_stream_printf(stream, SYN_IMMED("imm[%f,%d,0x%x]"), reg->fim_val,
308 reg->iim_val, reg->iim_val);
309 } else if (reg->flags & IR3_REG_ARRAY) {
310 if (reg->flags & IR3_REG_SSA) {
311 print_ssa_name(stream, reg, dest);
312 mesa_log_stream_printf(stream, ":");
313 }
314 mesa_log_stream_printf(stream,
315 SYN_ARRAY("arr[id=%u, offset=%d, size=%u]"),
316 reg->array.id, reg->array.offset, reg->size);
317 if (reg->array.base != INVALID_REG)
318 mesa_log_stream_printf(stream, "(" SYN_REG("r%u.%c") ")",
319 reg->array.base >> 2,
320 "xyzw"[reg->array.base & 0x3]);
321 } else if (reg->flags & IR3_REG_SSA) {
322 print_ssa_name(stream, reg, dest);
323 } else if (reg->flags & IR3_REG_RELATIV) {
324 if (reg->flags & IR3_REG_CONST)
325 mesa_log_stream_printf(stream, SYN_CONST("c<a0.x + %d>"),
326 reg->array.offset);
327 else
328 mesa_log_stream_printf(stream, SYN_REG("r<a0.x + %d>") " (%u)",
329 reg->array.offset, reg->size);
330 } else {
331 if (reg->flags & IR3_REG_CONST)
332 mesa_log_stream_printf(stream, SYN_CONST("c%u.%c"), reg_num(reg),
333 "xyzw"[reg_comp(reg)]);
334 else if (reg->flags & IR3_REG_PREDICATE)
335 mesa_log_stream_printf(stream, SYN_REG("p0.%c"),
336 "xyzw"[reg_comp(reg)]);
337 else
338 mesa_log_stream_printf(stream, SYN_REG("r%u.%c"), reg_num(reg),
339 "xyzw"[reg_comp(reg)]);
340 }
341
342 if (reg->wrmask > 0x1)
343 mesa_log_stream_printf(stream, " (wrmask=0x%x)", reg->wrmask);
344 }
345
346 static void
tab(struct log_stream * stream,int lvl)347 tab(struct log_stream *stream, int lvl)
348 {
349 for (int i = 0; i < lvl; i++)
350 mesa_log_stream_printf(stream, "\t");
351 }
352
353 static void
print_instr(struct log_stream * stream,struct ir3_instruction * instr,int lvl)354 print_instr(struct log_stream *stream, struct ir3_instruction *instr, int lvl)
355 {
356 tab(stream, lvl);
357
358 print_instr_name(stream, instr, true);
359
360 if (is_tex(instr)) {
361 if (instr->opc == OPC_BRCST_ACTIVE)
362 mesa_log_stream_printf(stream, ".w%d", instr->cat5.cluster_size);
363 mesa_log_stream_printf(stream, " (%s)(", type_name(instr->cat5.type));
364 for (unsigned i = 0; i < 4; i++)
365 if (instr->dsts[0]->wrmask & (1 << i))
366 mesa_log_stream_printf(stream, "%c", "xyzw"[i]);
367 mesa_log_stream_printf(stream, ")");
368 } else if ((instr->srcs_count > 0 || instr->dsts_count > 0)) {
369 /* NOTE the b(ranch) instruction has a suffix, which is
370 * handled below
371 */
372 mesa_log_stream_printf(stream, " ");
373 }
374
375 if (opc_cat(instr->opc) == 1) {
376 switch (instr->cat1.round) {
377 case ROUND_ZERO:
378 break;
379 case ROUND_EVEN:
380 mesa_log_stream_printf(stream, "(even)");
381 break;
382 case ROUND_POS_INF:
383 mesa_log_stream_printf(stream, "(pos_infinity)");
384 break;
385 case ROUND_NEG_INF:
386 mesa_log_stream_printf(stream, "(neg_infinity)");
387 break;
388 }
389 }
390
391 bool first = true;
392 foreach_dst (reg, instr) {
393 if (reg->wrmask == 0)
394 continue;
395 if (!first)
396 mesa_log_stream_printf(stream, ", ");
397 print_reg_name(stream, instr, reg, true);
398 first = false;
399 }
400 foreach_src_n (reg, n, instr) {
401 if (!first)
402 mesa_log_stream_printf(stream, ", ");
403 print_reg_name(stream, instr, reg, false);
404 if (instr->opc == OPC_END || instr->opc == OPC_CHMASK)
405 mesa_log_stream_printf(stream, " (%u)", instr->end.outidxs[n]);
406 first = false;
407 }
408
409 if (is_tex(instr) && !(instr->flags & IR3_INSTR_S2EN) &&
410 !is_tex_shuffle(instr)) {
411 if (!!(instr->flags & IR3_INSTR_B) && !!(instr->flags & IR3_INSTR_A1EN)) {
412 mesa_log_stream_printf(stream, ", s#%d", instr->cat5.samp);
413 } else {
414 mesa_log_stream_printf(stream, ", s#%d, t#%d", instr->cat5.samp,
415 instr->cat5.tex);
416 }
417 }
418
419 if (instr->opc == OPC_META_SPLIT) {
420 mesa_log_stream_printf(stream, ", off=%d", instr->split.off);
421 } else if (instr->opc == OPC_META_TEX_PREFETCH) {
422 mesa_log_stream_printf(stream, ", tex=%d, samp=%d, input_offset=%d",
423 instr->prefetch.tex, instr->prefetch.samp,
424 instr->prefetch.input_offset);
425 } else if (instr->opc == OPC_PUSH_CONSTS_LOAD_MACRO) {
426 mesa_log_stream_printf(
427 stream, " dst_offset=%d, src_offset = %d, src_size = %d",
428 instr->push_consts.dst_base, instr->push_consts.src_base,
429 instr->push_consts.src_size);
430 } else if (instr->opc == OPC_SPILL_MACRO) {
431 mesa_log_stream_printf(stream, " dst_offset=%d", instr->cat6.dst_offset);
432 }
433
434 if (is_flow(instr) && instr->cat0.target) {
435 mesa_log_stream_printf(stream, " target=block%u",
436 block_id(instr->cat0.target));
437 }
438
439 if (instr->deps_count) {
440 mesa_log_stream_printf(stream, ", false-deps:");
441 unsigned n = 0;
442 for (unsigned i = 0; i < instr->deps_count; i++) {
443 if (!instr->deps[i])
444 continue;
445 if (n++ > 0)
446 mesa_log_stream_printf(stream, ", ");
447 mesa_log_stream_printf(stream, SYN_SSA("ssa_%u"),
448 instr->deps[i]->serialno);
449 }
450 }
451
452 if (ir3_instr_is_rpt(instr)) {
453 mesa_log_stream_printf(stream, ", rpt: ");
454
455 if (ir3_instr_is_first_rpt(instr)) {
456 mesa_log_stream_printf(stream, "first");
457 } else {
458 mesa_log_stream_printf(stream, "%u",
459 ir3_instr_prev_rpt(instr)->serialno);
460 }
461 }
462
463 mesa_log_stream_printf(stream, "\n");
464 }
465
466 void
ir3_print_instr_stream(struct log_stream * stream,struct ir3_instruction * instr)467 ir3_print_instr_stream(struct log_stream *stream, struct ir3_instruction *instr)
468 {
469 print_instr(stream, instr, 0);
470 }
471
472 void
ir3_print_instr(struct ir3_instruction * instr)473 ir3_print_instr(struct ir3_instruction *instr)
474 {
475 struct log_stream *stream = mesa_log_streami();
476 print_instr(stream, instr, 0);
477 mesa_log_stream_destroy(stream);
478 }
479
480 static void
print_block(struct ir3_block * block,int lvl)481 print_block(struct ir3_block *block, int lvl)
482 {
483 struct log_stream *stream = mesa_log_streami();
484
485 tab(stream, lvl);
486 mesa_log_stream_printf(stream, "%sblock%u {\n",
487 block->reconvergence_point ? "(jp)" : "",
488 block_id(block));
489
490 if (block->predecessors_count > 0) {
491 tab(stream, lvl + 1);
492 mesa_log_stream_printf(stream, "pred: ");
493 for (unsigned i = 0; i < block->predecessors_count; i++) {
494 struct ir3_block *pred = block->predecessors[i];
495 if (i != 0)
496 mesa_log_stream_printf(stream, ", ");
497 mesa_log_stream_printf(stream, "block%u", block_id(pred));
498 }
499 mesa_log_stream_printf(stream, "\n");
500 }
501
502 if (block->physical_predecessors_count > 0) {
503 tab(stream, lvl + 1);
504 mesa_log_stream_printf(stream, "physical pred: ");
505 for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
506 struct ir3_block *pred = block->physical_predecessors[i];
507 if (i != 0)
508 mesa_log_stream_printf(stream, ", ");
509 mesa_log_stream_printf(stream, "block%u", block_id(pred));
510 }
511 mesa_log_stream_printf(stream, "\n");
512 }
513
514 foreach_instr (instr, &block->instr_list) {
515 print_instr(stream, instr, lvl + 1);
516 }
517
518 tab(stream, lvl + 1);
519 mesa_log_stream_printf(stream, "/* keeps:\n");
520 for (unsigned i = 0; i < block->keeps_count; i++) {
521 print_instr(stream, block->keeps[i], lvl + 2);
522 }
523 tab(stream, lvl + 1);
524 mesa_log_stream_printf(stream, " */\n");
525
526 if (block->successors[0]) {
527 tab(stream, lvl + 1);
528 mesa_log_stream_printf(stream, "/* succs: block%u",
529 block_id(block->successors[0]));
530 if (block->successors[1]) {
531 mesa_log_stream_printf(stream, ", block%u",
532 block_id(block->successors[1]));
533
534 mesa_log_stream_printf(stream, " (%s)",
535 block->divergent_condition ? "div" : "con");
536 }
537 mesa_log_stream_printf(stream, " */\n");
538 }
539 if (block->physical_successors_count > 0) {
540 tab(stream, lvl + 1);
541 mesa_log_stream_printf(stream, "/* physical succs: ");
542 for (unsigned i = 0; i < block->physical_successors_count; i++) {
543 mesa_log_stream_printf(stream, "block%u",
544 block_id(block->physical_successors[i]));
545 if (i < block->physical_successors_count - 1)
546 mesa_log_stream_printf(stream, ", ");
547 }
548 mesa_log_stream_printf(stream, " */\n");
549 }
550 tab(stream, lvl);
551 mesa_log_stream_printf(stream, "}\n");
552 }
553
554 void
ir3_print(struct ir3 * ir)555 ir3_print(struct ir3 *ir)
556 {
557 foreach_block (block, &ir->block_list)
558 print_block(block, 0);
559 }
560