Debian Codegraph @

src/imagination/pco/pco_print.c

raw
1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 /**
8 * \file pco_print.c
9 *
10 * \brief PCO printing functions.
11 */
12
13 #include "pco.h"
14 #include "pco_builder.h"
15 #include "pco_common.h"
16 #include "pco_internal.h"
17 #include "util/bitscan.h"
18 #include "util/list.h"
19 #include "util/macros.h"
20 #include "util/shader_stats.h"
21 #include "util/u_hexdump.h"
22
23 #include <assert.h>
24 #include <inttypes.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29
30 typedef struct _pco_print_state {
31 FILE *fp; /** The print target file pointer. */
32 pco_shader *shader; /** The shader being printed. */
33 unsigned indent; /** The current printing indent. */
34 bool is_grouped; /** Whether the shader uses igrps. */
35 bool verbose; /** Whether to print additional info. */
36 } pco_print_state;
37
38 typedef struct _pco_igrp_offsets {
39 unsigned instrs[_PCO_OP_PHASE_COUNT];
40 unsigned lower_srcs;
41 unsigned upper_srcs;
42 unsigned iss;
43 unsigned dests;
44 } pco_igrp_offsets;
45
46 /* Forward declarations. */
47 static void _pco_print_cf_node(pco_print_state *state, pco_cf_node *cf_node);
48 static void pco_print_block_name(pco_print_state *state, pco_block *block);
49 static void
50 pco_print_func_sig(pco_print_state *state, pco_func *func, bool call);
51
52 enum color_esc {
53 ESC_RESET = 0,
54 ESC_BLACK,
55 ESC_RED,
56 ESC_GREEN,
57 ESC_YELLOW,
58 ESC_BLUE,
59 ESC_PURPLE,
60 ESC_CYAN,
61 ESC_WHITE,
62 _ESC_COUNT,
63 };
64
65 static
66 const char *color_esc[2][_ESC_COUNT] = {
67 [0] = {
68 [ESC_RESET] = "",
69 [ESC_BLACK] = "",
70 [ESC_RED] = "",
71 [ESC_GREEN] = "",
72 [ESC_YELLOW] = "",
73 [ESC_BLUE] = "",
74 [ESC_PURPLE] = "",
75 [ESC_CYAN] = "",
76 [ESC_WHITE] = "",
77 },
78 [1] = {
79 [ESC_RESET] = "\033[0m",
80 [ESC_BLACK] = "\033[0;30m",
81 [ESC_RED] = "\033[0;31m",
82 [ESC_GREEN] = "\033[0;32m",
83 [ESC_YELLOW] = "\033[0;33m",
84 [ESC_BLUE] = "\033[0;34m",
85 [ESC_PURPLE] = "\033[0;35m",
86 [ESC_CYAN] = "\033[0;36m",
87 [ESC_WHITE] = "\033[0;37m",
88 },
89 };
90
91 static inline void RESET(pco_print_state *state)
92 {
93 fputs(color_esc[pco_color][ESC_RESET], state->fp);
94 }
95
96 static inline void BLACK(pco_print_state *state)
97 {
98 fputs(color_esc[pco_color][ESC_BLACK], state->fp);
99 }
100
101 static inline void RED(pco_print_state *state)
102 {
103 fputs(color_esc[pco_color][ESC_RED], state->fp);
104 }
105
106 static inline void GREEN(pco_print_state *state)
107 {
108 fputs(color_esc[pco_color][ESC_GREEN], state->fp);
109 }
110
111 static inline void YELLOW(pco_print_state *state)
112 {
113 fputs(color_esc[pco_color][ESC_YELLOW], state->fp);
114 }
115
116 static inline void BLUE(pco_print_state *state)
117 {
118 fputs(color_esc[pco_color][ESC_BLUE], state->fp);
119 }
120
121 static inline void PURPLE(pco_print_state *state)
122 {
123 fputs(color_esc[pco_color][ESC_PURPLE], state->fp);
124 }
125
126 static inline void CYAN(pco_print_state *state)
127 {
128 fputs(color_esc[pco_color][ESC_CYAN], state->fp);
129 }
130
131 static inline void WHITE(pco_print_state *state)
132 {
133 fputs(color_esc[pco_color][ESC_WHITE], state->fp);
134 }
135
136 inline static const char *true_false_str(bool b)
137 {
138 return b ? "true" : "false";
139 }
140
141 static void
142 _pco_printf(pco_print_state *state, bool nl, const char *fmt, va_list args)
143 {
144 if (nl)
145 for (unsigned u = 0; u < state->indent; ++u)
146 fputs(" ", state->fp);
147
148 vfprintf(state->fp, fmt, args);
149 }
150
151 /**
152 * \brief Formatted print.
153 *
154 * \param[in] state Print state.
155 * \param[in] fmt Print format.
156 */
157 PRINTFLIKE(2, 3)
158 static void pco_printf(pco_print_state *state, const char *fmt, ...)
159 {
160 va_list args;
161 va_start(args, fmt);
162 _pco_printf(state, false, fmt, args);
163 va_end(args);
164 }
165
166 /**
167 * \brief Formatted print, with indentation.
168 *
169 * \param[in] state Print state.
170 * \param[in] fmt Print format.
171 */
172 PRINTFLIKE(2, 3)
173 static void pco_printfi(pco_print_state *state, const char *fmt, ...)
174 {
175 va_list args;
176 va_start(args, fmt);
177 _pco_printf(state, true, fmt, args);
178 va_end(args);
179 }
180
181 /**
182 * \brief Returns whether the string is not empty.
183 *
184 * \param[in] str String.
185 * \return True if the string is not empty, else false.
186 */
187 static inline bool if_str(const char *str)
188 {
189 return str[0] != '\0';
190 }
191
192 /**
193 * \brief Print PCO reference flags/modifiers.
194 *
195 * \param[in] state Print state.
196 * \param[in] ref PCO reference.
197 */
198 static void pco_print_ref_mods(pco_print_state *state, pco_ref ref)
199 {
200 if (ref.oneminus)
201 pco_printf(state, ".oneminus");
202 if (ref.clamp)
203 pco_printf(state, ".clamp");
204 if (ref.flr)
205 pco_printf(state, ".flr");
206 if (ref.abs)
207 pco_printf(state, ".abs");
208 if (ref.neg)
209 pco_printf(state, ".neg");
210
211 u_foreach_bit (e, ref.elem) {
212 pco_printf(state, ".e%u", e);
213 }
214 }
215
216 /**
217 * \brief Print PCO reference color.
218 *
219 * \param[in] state Print state.
220 * \param[in] ref PCO reference.
221 */
222 static void pco_print_ref_color(pco_print_state *state, pco_ref ref)
223 {
224 switch (ref.type) {
225 case PCO_REF_TYPE_NULL:
226 return;
227
228 case PCO_REF_TYPE_SSA:
229 case PCO_REF_TYPE_REG:
230 YELLOW(state);
231 return;
232
233 case PCO_REF_TYPE_IMM:
234 BLUE(state);
235 return;
236
237 case PCO_REF_TYPE_IO:
238 case PCO_REF_TYPE_PRED:
239 case PCO_REF_TYPE_DRC:
240 case PCO_REF_TYPE_IDX_REG:
241 WHITE(state);
242 return;
243
244 default:
245 break;
246 }
247
248 UNREACHABLE("");
249 }
250
251 /**
252 * \brief Print PCO reference.
253 *
254 * \param[in] state Print state.
255 * \param[in] ref PCO reference.
256 */
257 static void _pco_print_ref(pco_print_state *state, pco_ref ref)
258 {
259 pco_print_ref_color(state, ref);
260 pco_printf(state, "%s", pco_ref_type_str(ref.type));
261
262 switch (ref.type) {
263 case PCO_REF_TYPE_NULL:
264 break;
265
266 case PCO_REF_TYPE_SSA:
267 pco_printf(state, "%u", ref.val);
268 break;
269
270 case PCO_REF_TYPE_REG:
271 pco_printf(state, "%s%u", pco_reg_class_str(ref.reg_class), ref.val);
272 break;
273
274 case PCO_REF_TYPE_IDX_REG: {
275 _pco_print_ref(state, pco_ref_get_idx_pointee(ref));
276 if (pco_ref_is_self_idx_reg(ref))
277 break;
278
279 pco_print_ref_color(state, ref);
280 pco_printf(state, "[idx%u", ref.idx_reg.num);
281 break;
282 }
283
284 case PCO_REF_TYPE_IMM:
285 assert(pco_ref_is_scalar(ref));
286 switch (ref.dtype) {
287 case PCO_DTYPE_ANY:
288 pco_printf(state, "0x%" PRIx64, pco_ref_get_imm(ref));
289 break;
290
291 case PCO_DTYPE_UNSIGNED:
292 pco_printf(state, "%" PRIu64, pco_ref_get_imm(ref));
293 break;
294
295 case PCO_DTYPE_SIGNED:
296 pco_printf(state, "%" PRId64, pco_ref_get_imm(ref));
297 break;
298
299 case PCO_DTYPE_FLOAT:
300 pco_printf(state, "%f", uif(pco_ref_get_imm(ref)));
301 break;
302
303 default:
304 UNREACHABLE("");
305 }
306 pco_printf(state, "%s", pco_dtype_str(ref.dtype));
307 break;
308
309 case PCO_REF_TYPE_IO:
310 assert(pco_ref_is_scalar(ref));
311 pco_printf(state, "%s", pco_io_str(ref.val));
312 break;
313
314 case PCO_REF_TYPE_PRED:
315 assert(pco_ref_is_scalar(ref));
316 pco_printf(state, "%s", pco_pred_str(ref.val));
317 break;
318
319 case PCO_REF_TYPE_DRC:
320 assert(pco_ref_is_scalar(ref));
321 pco_printf(state, "%s", pco_drc_str(ref.val));
322 break;
323
324 default:
325 UNREACHABLE("");
326 }
327
328 unsigned chans = pco_ref_get_chans(ref);
329 if (chans > 1 && !pco_ref_is_ssa(ref))
330 pco_printf(state, "..%u", ref.val + chans - 1);
331
332 if (ref.type == PCO_REF_TYPE_IDX_REG && !pco_ref_is_self_idx_reg(ref))
333 pco_printf(state, "]");
334
335 RESET(state);
336
337 /* Modifiers. */
338 pco_print_ref_mods(state, ref);
339 }
340
341 /**
342 * \brief Print PCO reference specification.
343 *
344 * \param[in] state Print state.
345 * \param[in] ref PCO reference.
346 */
347 static void pco_print_ref_spec(pco_print_state *state, pco_ref ref)
348 {
349 pco_printf(state,
350 "(%s%ux%u)",
351 pco_dtype_str(pco_ref_get_dtype(ref)),
352 pco_ref_get_bits(ref),
353 pco_ref_get_chans(ref));
354 }
355
356 /**
357 * \brief Print PCO phi source.
358 *
359 * \param[in] state Print state.
360 * \param[in] phi_src PCO phi source.
361 */
362 static void pco_print_phi_src(pco_print_state *state, pco_phi_src *phi_src)
363 {
364 pco_print_block_name(state, phi_src->pred);
365 pco_printf(state, ": ");
366 _pco_print_ref(state, phi_src->ref);
367 }
368
369 /**
370 * \brief Print PCO instruction modifiers.
371 *
372 * \param[in] state Print state.
373 * \param[in] op_info The instruction op info.
374 * \param[in] instr PCO instruction.
375 * \param[in] print_early Whether the mods are being printed before the
376 * instruction name.
377 */
378 static void pco_print_instr_mods(pco_print_state *state,
379 const struct pco_op_info *op_info,
380 pco_instr *instr,
381 bool print_early)
382 {
383 if (print_early)
384 WHITE(state);
385
386 u_foreach_bit64 (op_mod, op_info->mods) {
387 const struct pco_op_mod_info *mod_info = &pco_op_mod_info[op_mod];
388 if (mod_info->print_early != print_early)
389 continue;
390
391 uint32_t val = pco_instr_get_mod(instr, op_mod);
392
393 switch (mod_info->type) {
394 case PCO_MOD_TYPE_BOOL:
395 if (val && strlen(mod_info->str)) {
396 if (print_early)
397 pco_printf(state, "%s ", mod_info->str);
398 else
399 pco_printf(state, ".%s", mod_info->str);
400 }
401 break;
402
403 case PCO_MOD_TYPE_UINT:
404 if ((!mod_info->nzdefault || val != mod_info->nzdefault) &&
405 strlen(mod_info->str)) {
406 if (print_early)
407 pco_printf(state, "%s%u ", mod_info->str, val);
408 else
409 pco_printf(state, "%s%u", mod_info->str, val);
410 }
411 break;
412
413 case PCO_MOD_TYPE_ENUM:
414 if (mod_info->is_bitset) {
415 u_foreach_bit (bit, val) {
416 pco_printf(state, ".%s", mod_info->strs[1U << bit]);
417 }
418 } else {
419 if (strlen(mod_info->strs[val])) {
420 if (print_early)
421 pco_printf(state, "%s ", mod_info->strs[val]);
422 else
423 pco_printf(state, ".%s", mod_info->strs[val]);
424 }
425 }
426 break;
427
428 default:
429 UNREACHABLE("");
430 }
431 }
432
433 if (print_early)
434 RESET(state);
435 }
436
437 /**
438 * \brief Print PCO instruction.
439 *
440 * \param[in] state Print state.
441 * \param[in] instr PCO instruction.
442 */
443 static void _pco_print_instr(pco_print_state *state, pco_instr *instr)
444 {
445 const struct pco_op_info *info = &pco_op_info[instr->op];
446
447 if (!state->is_grouped)
448 pco_printfi(state, "%04u: ", instr->index);
449
450 /* Early mods. */
451 pco_print_instr_mods(state, info, instr, true);
452
453 if (info->type == PCO_OP_TYPE_PSEUDO)
454 RED(state);
455 else
456 GREEN(state);
457 pco_printf(state, "%s", info->str);
458 RESET(state);
459
460 /* "Late" mods. */
461 pco_print_instr_mods(state, info, instr, false);
462
463 bool printed = false;
464
465 /* Destinations. */
466 pco_foreach_instr_dest (pdest, instr) {
467 if (printed)
468 pco_printf(state, ",");
469 pco_printf(state, " ");
470 _pco_print_ref(state, *pdest);
471 printed = true;
472 }
473
474 /* Special parameters. */
475 if (info->has_target_cf_node) {
476 if (printed)
477 pco_printf(state, ",");
478 pco_printf(state, " ");
479
480 switch (instr->target_cf_node->type) {
481 case PCO_CF_NODE_TYPE_BLOCK: {
482 pco_block *target_block = pco_cf_node_as_block(instr->target_cf_node);
483 pco_print_block_name(state, target_block);
484 break;
485 }
486
487 case PCO_CF_NODE_TYPE_FUNC: {
488 pco_func *target_func = pco_cf_node_as_func(instr->target_cf_node);
489 pco_print_func_sig(state, target_func, true);
490 break;
491 }
492
493 default:
494 UNREACHABLE("");
495 }
496 printed = true;
497 } else if (!list_is_empty(&instr->phi_srcs)) {
498 pco_foreach_phi_src_in_instr (phi_src, instr) {
499 if (printed)
500 pco_printf(state, ",");
501 pco_printf(state, " ");
502 pco_print_phi_src(state, phi_src);
503 printed = true;
504 }
505 }
506
507 /* Sources. */
508 pco_foreach_instr_src (psrc, instr) {
509 if (printed)
510 pco_printf(state, ",");
511 pco_printf(state, " ");
512 _pco_print_ref(state, *psrc);
513 printed = true;
514 }
515 pco_printf(state, ";");
516
517 /* Spec for destinations. */
518 bool no_dest_specs = true;
519 pco_foreach_instr_dest (pdest, instr) {
520 if (!pco_ref_is_null(*pdest)) {
521 no_dest_specs = false;
522 break;
523 }
524 }
525
526 if (state->verbose && !state->is_grouped && !no_dest_specs) {
527 pco_printf(state, " /*");
528
529 printed = false;
530 pco_foreach_instr_dest (pdest, instr) {
531 if (pco_ref_is_null(*pdest))
532 continue;
533
534 if (printed)
535 pco_printf(state, ",");
536 pco_printf(state, " ");
537
538 _pco_print_ref(state, *pdest);
539 pco_printf(state, ":");
540 pco_print_ref_spec(state, *pdest);
541
542 printed = true;
543 }
544
545 pco_printf(state, " */");
546 }
547
548 if (state->verbose && instr->comment)
549 pco_printf(state, " /* %s */", instr->comment);
550 }
551
552 /**
553 * \brief Print the name of a phase.
554 *
555 * \param[in] state Print state.
556 * \param[in] alutype ALU type.
557 * \param[in] phase Phase.
558 */
559 static void _pco_print_phase(pco_print_state *state,
560 enum pco_alutype alutype,
561 enum pco_op_phase phase)
562 {
563 switch (alutype) {
564 case PCO_ALUTYPE_MAIN:
565 pco_printf(state, "%s", pco_op_phase_str(phase));
566 return;
567
568 case PCO_ALUTYPE_BITWISE:
569 pco_printf(state, "p%c", '0' + phase);
570 return;
571
572 case PCO_ALUTYPE_CONTROL:
573 pco_printf(state, "ctrl");
574 return;
575
576 default:
577 break;
578 }
579 UNREACHABLE("");
580 }
581
582 /**
583 * \brief Print phases present in a PCO instruction group.
584 *
585 * \param[in] state Print state.
586 * \param[in] igrp PCO instruction group.
587 */
588 static void pco_print_igrp_phases(pco_print_state *state, pco_igrp *igrp)
589 {
590 bool printed = false;
591 pco_foreach_phase_in_igrp (igrp, phase) {
592 if (printed)
593 pco_printf(state, ",");
594
595 _pco_print_phase(state, igrp->hdr.alutype, phase);
596
597 printed = true;
598 }
599 }
600
601 /**
602 * \brief Print the sources in a PCO instruction group.
603 *
604 * \param[in] state Print state.
605 * \param[in] igrp PCO instruction group.
606 * \param[in] upper Whether to print the upper sources.
607 */
608 static void
609 pco_print_igrp_srcs(pco_print_state *state, pco_igrp *igrp, bool upper)
610 {
611 unsigned offset = upper ? ROGUE_ALU_INPUT_GROUP_SIZE : 0;
612 bool printed = false;
613 for (unsigned u = 0; u < ROGUE_ALU_INPUT_GROUP_SIZE; ++u) {
614 const pco_ref *src = &igrp->srcs.s[u + offset];
615 if (pco_ref_is_null(*src))
616 continue;
617
618 if (printed)
619 pco_printf(state, ", ");
620
621 pco_printf(state, "s%u = ", u + offset);
622 _pco_print_ref(state, *src);
623 printed = true;
624 }
625 }
626
627 /**
628 * \brief Print the internal source selector in a PCO instruction group.
629 *
630 * \param[in] state Print state.
631 * \param[in] igrp PCO instruction group.
632 */
633 static void pco_print_igrp_iss(pco_print_state *state, pco_igrp *igrp)
634 {
635 bool printed = false;
636 for (unsigned u = 0; u < ROGUE_MAX_ALU_INTERNAL_SOURCES; ++u) {
637 const pco_ref *iss = &igrp->iss.is[u];
638 if (pco_ref_is_null(*iss))
639 continue;
640
641 if (printed)
642 pco_printf(state, ", ");
643
644 pco_printf(state, "is%u = ", u);
645 _pco_print_ref(state, *iss);
646 printed = true;
647 }
648 }
649
650 /**
651 * \brief Print the dests in a PCO instruction group.
652 *
653 * \param[in] state Print state.
654 * \param[in] igrp PCO instruction group.
655 */
656 static void pco_print_igrp_dests(pco_print_state *state, pco_igrp *igrp)
657 {
658 bool printed = false;
659 for (unsigned u = 0; u < ROGUE_MAX_ALU_OUTPUTS; ++u) {
660 const pco_ref *dest = &igrp->dests.w[u];
661 if (pco_ref_is_null(*dest))
662 continue;
663
664 if (printed)
665 pco_printf(state, ", ");
666
667 pco_printf(state, "w%u = ", u);
668 _pco_print_ref(state, *dest);
669 printed = true;
670 }
671 }
672
673 static inline void pco_calc_igrp_offsets(pco_igrp *igrp,
674 pco_igrp_offsets *offsets)
675 {
676 memset(offsets, 0, sizeof(*offsets));
677 unsigned offset = igrp->enc.len.hdr;
678
679 pco_foreach_phase_in_igrp_rev (igrp, p) {
680 offsets->instrs[p] = offset;
681 offset += igrp->enc.len.instrs[p];
682 }
683
684 offsets->lower_srcs = offset;
685 offset += igrp->enc.len.lower_srcs;
686
687 offsets->upper_srcs = offset;
688 offset += igrp->enc.len.upper_srcs;
689
690 offsets->iss = offset;
691 offset += igrp->enc.len.iss;
692
693 offsets->dests = offset;
694
695 #ifndef NDEBUG
696 /* Sanity check. */
697 offset += igrp->enc.len.dests;
698 offset += igrp->enc.len.word_padding;
699 offset += igrp->enc.len.align_padding;
700 assert(offset == igrp->enc.len.total);
701 #endif /* NDEBUG */
702 }
703
704 /**
705 * \brief Print PCO instruction group.
706 *
707 * \param[in] state Print state.
708 * \param[in] igrp PCO instruction group.
709 */
710 static void _pco_print_igrp(pco_print_state *state, pco_igrp *igrp)
711 {
712 bool printed = false;
713
714 pco_igrp_offsets offsets;
715 if (state->verbose)
716 pco_calc_igrp_offsets(igrp, &offsets);
717
718 pco_printfi(state, "%04u:", igrp->index);
719
720 const char *cc_str = pco_cc_str(igrp->hdr.cc);
721 if (if_str(cc_str)) {
722 WHITE(state);
723 pco_printf(state, " %s", cc_str);
724 RESET(state);
725 }
726
727 pco_printf(state, " { ");
728
729 if (state->verbose) {
730 unsigned padding_size =
731 igrp->enc.len.word_padding + igrp->enc.len.align_padding;
732 unsigned unpadded_size = igrp->enc.len.total - padding_size;
733
734 pco_printf(state, "/* @ 0x%08x [", igrp->enc.offset);
735 pco_print_igrp_phases(state, igrp);
736 pco_printf(state,
737 "] len: %u, pad: %u, total: %u, da: %u",
738 unpadded_size,
739 padding_size,
740 igrp->enc.len.total,
741 igrp->hdr.da);
742
743 if (igrp->hdr.w0p)
744 pco_printf(state, ", w0p");
745
746 if (igrp->hdr.w1p)
747 pco_printf(state, ", w1p");
748
749 pco_printf(state, " */\n");
750 ++state->indent;
751
752 pco_printfi(state,
753 "type %s /* hdr bytes: %u @ +0 */\n",
754 pco_alutype_str(igrp->hdr.alutype),
755 igrp->enc.len.hdr);
756 }
757
758 if (igrp->hdr.alutype != PCO_ALUTYPE_CONTROL && igrp->hdr.rpt > 1) {
759 if (state->verbose)
760 pco_printfi(state, "repeat %u\n", igrp->hdr.rpt);
761 else
762 pco_printf(state, "repeat %u ", igrp->hdr.rpt);
763
764 printed = true;
765 }
766
767 if (igrp->enc.len.lower_srcs) {
768 if (state->verbose)
769 pco_printfi(state, "%s", "");
770
771 if (!pco_igrp_srcs_unset(igrp, false)) {
772 if (!state->verbose && printed)
773 pco_printf(state, ", ");
774
775 pco_print_igrp_srcs(state, igrp, false);
776
777 if (state->verbose)
778 pco_printf(state, " ");
779 }
780
781 if (state->verbose)
782 pco_printf(state,
783 "/* lo src bytes: %u @ +%u */\n",
784 igrp->enc.len.lower_srcs,
785 offsets.lower_srcs);
786
787 printed = true;
788 }
789
790 if (igrp->enc.len.upper_srcs) {
791 if (state->verbose)
792 pco_printfi(state, "%s", "");
793
794 if (!pco_igrp_srcs_unset(igrp, true)) {
795 if (!state->verbose && printed)
796 pco_printf(state, ", ");
797
798 pco_print_igrp_srcs(state, igrp, true);
799
800 if (state->verbose)
801 pco_printf(state, " ");
802 }
803
804 if (state->verbose)
805 pco_printf(state,
806 "/* up src bytes: %u @ +%u */\n",
807 igrp->enc.len.upper_srcs,
808 offsets.upper_srcs);
809
810 printed = true;
811 }
812
813 if (igrp->enc.len.iss) {
814 if (state->verbose)
815 pco_printfi(state, "%s", "");
816
817 if (!pco_igrp_iss_unset(igrp)) {
818 if (!state->verbose && printed)
819 pco_printf(state, ", ");
820
821 pco_print_igrp_iss(state, igrp);
822
823 if (state->verbose)
824 pco_printf(state, " ");
825 }
826
827 if (state->verbose)
828 pco_printf(state,
829 "/* iss bytes: %u @ +%u */\n",
830 igrp->enc.len.iss,
831 offsets.iss);
832
833 printed = true;
834 }
835
836 pco_foreach_instr_in_igrp (instr, igrp) {
837 enum pco_op_phase phase = instr->phase;
838
839 if (state->verbose)
840 pco_printfi(state, "%s", "");
841 else if (printed)
842 pco_printf(state, " ");
843
844 _pco_print_phase(state, igrp->hdr.alutype, phase);
845 pco_printf(state, ": ");
846 _pco_print_instr(state, instr);
847
848 if (state->verbose) {
849 pco_printf(state, " /* ");
850 _pco_print_phase(state, igrp->hdr.alutype, phase);
851 pco_printf(state,
852 " bytes: %u @ +%u */\n",
853 igrp->enc.len.instrs[phase],
854 offsets.instrs[phase]);
855 }
856
857 printed = true;
858 }
859
860 if (igrp->enc.len.dests) {
861 if (state->verbose)
862 pco_printfi(state, "%s", "");
863
864 if (!pco_igrp_dests_unset(igrp)) {
865 if (!state->verbose && printed)
866 pco_printf(state, " ");
867
868 pco_print_igrp_dests(state, igrp);
869
870 if (state->verbose)
871 pco_printf(state, " ");
872 }
873
874 if (state->verbose)
875 pco_printf(state,
876 "/* dest bytes: %u @ +%u */\n",
877 igrp->enc.len.dests,
878 offsets.dests);
879
880 printed = true;
881 }
882
883 if (state->verbose)
884 --state->indent;
885 else
886 pco_printf(state, " ");
887
888 if (state->verbose)
889 pco_printfi(state, "}");
890 else
891 pco_printf(state, "}");
892
893 if (igrp->hdr.olchk)
894 pco_printf(state, ".olchk");
895
896 if (igrp->hdr.alutype != PCO_ALUTYPE_CONTROL) {
897 if (igrp->hdr.atom)
898 pco_printf(state, ".atom");
899
900 if (igrp->hdr.end)
901 pco_printf(state, ".end");
902 }
903
904 if (state->verbose && igrp->comment)
905 pco_printf(state, " /* %s */", igrp->comment);
906
907 pco_printf(state, "\n");
908 }
909
910 /**
911 * \brief Print PCO block name.
912 *
913 * \param[in] state Print state.
914 * \param[in] block PCO block.
915 */
916 static void pco_print_block_name(pco_print_state *state, pco_block *block)
917 {
918 pco_printf(state, "B%u", block->index);
919 }
920
921 /**
922 * \brief Print PCO block.
923 *
924 * \param[in] state Print state.
925 * \param[in] block PCO block.
926 */
927 static void pco_print_block(pco_print_state *state, pco_block *block)
928 {
929 pco_printfi(state, "block ");
930 pco_print_block_name(state, block);
931 pco_printf(state, ":\n");
932 ++state->indent;
933
934 if (state->is_grouped) {
935 pco_foreach_igrp_in_block (igrp, block) {
936 _pco_print_igrp(state, igrp);
937 }
938 } else {
939 pco_foreach_instr_in_block (instr, block) {
940 _pco_print_instr(state, instr);
941 pco_printf(state, "\n");
942 }
943 }
944
945 --state->indent;
946 }
947
948 /**
949 * \brief Print PCO if name.
950 *
951 * \param[in] state Print state.
952 * \param[in] pif PCO if.
953 */
954 static void pco_print_if_name(pco_print_state *state, pco_if *pif)
955 {
956 pco_printf(state, "I%u", pif->index);
957 }
958
959 /**
960 * \brief Print PCO if.
961 *
962 * \param[in] state Print state.
963 * \param[in] pif PCO if.
964 */
965 static void pco_print_if(pco_print_state *state, pco_if *pif)
966 {
967 pco_printfi(state, "if ");
968 pco_print_if_name(state, pif);
969
970 if (!pco_ref_is_null(pif->cond)) {
971 pco_printf(state, " (");
972 _pco_print_ref(state, pif->cond);
973 pco_printf(state, ")");
974 }
975
976 pco_printf(state, " {\n");
977
978 if (!list_is_empty(&pif->prologue)) {
979 pco_printfi(state, "/* if ");
980 pco_print_if_name(state, pif);
981 pco_printf(state, " prologue start */\n");
982
983 ++state->indent;
984 pco_foreach_cf_node_in_if_prologue (cf_node, pif) {
985 _pco_print_cf_node(state, cf_node);
986 }
987 --state->indent;
988
989 pco_printfi(state, "/* if ");
990 pco_print_if_name(state, pif);
991 pco_printf(state, " prologue end */\n");
992 }
993
994 if (!list_is_empty(&pif->then_body)) {
995 ++state->indent;
996 pco_foreach_cf_node_in_if_then (cf_node, pif) {
997 _pco_print_cf_node(state, cf_node);
998 }
999 --state->indent;
1000 }
1001
1002 if (!list_is_empty(&pif->else_body)) {
1003 pco_printfi(state, "} else {\n");
1004
1005 if (!list_is_empty(&pif->interlogue)) {
1006 pco_printfi(state, "/* if ");
1007 pco_print_if_name(state, pif);
1008 pco_printf(state, " interlogue start */\n");
1009
1010 ++state->indent;
1011 pco_foreach_cf_node_in_if_interlogue (cf_node, pif) {
1012 _pco_print_cf_node(state, cf_node);
1013 }
1014 --state->indent;
1015
1016 pco_printfi(state, "/* if ");
1017 pco_print_if_name(state, pif);
1018 pco_printf(state, " interlogue end */\n");
1019 }
1020
1021 ++state->indent;
1022 pco_foreach_cf_node_in_if_else (cf_node, pif) {
1023 _pco_print_cf_node(state, cf_node);
1024 }
1025 --state->indent;
1026 } else {
1027 assert(list_is_empty(&pif->interlogue));
1028 }
1029
1030 if (!list_is_empty(&pif->epilogue)) {
1031 pco_printfi(state, "/* if ");
1032 pco_print_if_name(state, pif);
1033 pco_printf(state, " epilogue start */\n");
1034
1035 ++state->indent;
1036 pco_foreach_cf_node_in_if_epilogue (cf_node, pif) {
1037 _pco_print_cf_node(state, cf_node);
1038 }
1039 --state->indent;
1040
1041 pco_printfi(state, "/* if ");
1042 pco_print_if_name(state, pif);
1043 pco_printf(state, " epilogue end */\n");
1044 }
1045
1046 pco_printfi(state, "}\n");
1047 }
1048
1049 /**
1050 * \brief Print PCO loop name.
1051 *
1052 * \param[in] state Print state.
1053 * \param[in] loop PCO loop.
1054 */
1055 static void pco_print_loop_name(pco_print_state *state, pco_loop *loop)
1056 {
1057 pco_printf(state, "L%u", loop->index);
1058 }
1059
1060 /**
1061 * \brief Print PCO loop.
1062 *
1063 * \param[in] state Print state.
1064 * \param[in] loop PCO loop.
1065 */
1066 static void pco_print_loop(pco_print_state *state, pco_loop *loop)
1067 {
1068 pco_printfi(state, "loop ");
1069 pco_print_loop_name(state, loop);
1070 pco_printf(state, " {\n");
1071
1072 if (!list_is_empty(&loop->prologue)) {
1073 pco_printfi(state, "/* loop ");
1074 pco_print_loop_name(state, loop);
1075 pco_printf(state, " prologue start */\n");
1076
1077 ++state->indent;
1078 pco_foreach_cf_node_in_loop_prologue (cf_node, loop) {
1079 _pco_print_cf_node(state, cf_node);
1080 }
1081 --state->indent;
1082
1083 pco_printfi(state, "/* loop ");
1084 pco_print_loop_name(state, loop);
1085 pco_printf(state, " prologue end */\n");
1086 }
1087
1088 if (!list_is_empty(&loop->body)) {
1089 ++state->indent;
1090 pco_foreach_cf_node_in_loop (cf_node, loop) {
1091 _pco_print_cf_node(state, cf_node);
1092 }
1093 --state->indent;
1094 }
1095
1096 if (!list_is_empty(&loop->interlogue)) {
1097 pco_printfi(state, "/* loop ");
1098 pco_print_loop_name(state, loop);
1099 pco_printf(state, " interlogue start */\n");
1100
1101 ++state->indent;
1102 pco_foreach_cf_node_in_loop_interlogue (cf_node, loop) {
1103 _pco_print_cf_node(state, cf_node);
1104 }
1105 --state->indent;
1106
1107 pco_printfi(state, "/* loop ");
1108 pco_print_loop_name(state, loop);
1109 pco_printf(state, " interlogue end */\n");
1110 }
1111
1112 if (!list_is_empty(&loop->epilogue)) {
1113 pco_printfi(state, "/* loop ");
1114 pco_print_loop_name(state, loop);
1115 pco_printf(state, " epilogue start */\n");
1116
1117 ++state->indent;
1118 pco_foreach_cf_node_in_loop_epilogue (cf_node, loop) {
1119 _pco_print_cf_node(state, cf_node);
1120 }
1121 --state->indent;
1122
1123 pco_printfi(state, "/* loop ");
1124 pco_print_loop_name(state, loop);
1125 pco_printf(state, " epilogue end */\n");
1126 }
1127
1128 pco_printfi(state, "}\n");
1129 }
1130
1131 /**
1132 * \brief Print PCO function signature.
1133 *
1134 * \param[in] state Print state.
1135 * \param[in] func PCO function.
1136 * \param[in] call Whether the signature is for a function call/reference.
1137 */
1138 static void
1139 pco_print_func_sig(pco_print_state *state, pco_func *func, bool call)
1140 {
1141 if (!call) {
1142 switch (func->type) {
1143 case PCO_FUNC_TYPE_CALLABLE:
1144 break;
1145
1146 case PCO_FUNC_TYPE_PREAMBLE:
1147 pco_printf(state, " PREAMBLE");
1148 break;
1149
1150 case PCO_FUNC_TYPE_ENTRYPOINT:
1151 pco_printf(state, " ENTRY");
1152 break;
1153
1154 case PCO_FUNC_TYPE_PHASE_CHANGE:
1155 pco_printf(state, " PHASE CHANGE");
1156 break;
1157
1158 default:
1159 UNREACHABLE("");
1160 }
1161 }
1162
1163 if (func->name)
1164 pco_printf(state, " %s", func->name);
1165 else
1166 pco_printf(state, " _%u", func->index);
1167
1168 pco_printf(state, "(");
1169
1170 if (!call) {
1171 /* TODO: Function parameter support. */
1172 assert(func->num_params == 0 && func->params == NULL);
1173 if (!func->num_params)
1174 pco_printf(state, "void");
1175 }
1176
1177 pco_printf(state, ")");
1178 }
1179
1180 /**
1181 * \brief Print PCO function.
1182 *
1183 * \param[in] state Print state.
1184 * \param[in] func PCO function.
1185 */
1186 static void pco_print_func(pco_print_state *state, pco_func *func)
1187 {
1188 pco_printfi(state, "func");
1189 pco_print_func_sig(state, func, false);
1190 if (state->is_grouped) {
1191 pco_printf(state, " /* temps: %u */", func->temps);
1192 pco_printf(state, " /* vtxins: %u */", func->vtxins);
1193 }
1194 pco_printf(state, "\n");
1195
1196 pco_printfi(state, "{\n");
1197
1198 pco_foreach_cf_node_in_func (cf_node, func) {
1199 _pco_print_cf_node(state, cf_node);
1200 }
1201
1202 pco_printfi(state, "}\n");
1203 }
1204
1205 /**
1206 * \brief Print PCO control flow node.
1207 *
1208 * \param[in] state Print state.
1209 * \param[in] cf_node PCO control flow node.
1210 */
1211 static void _pco_print_cf_node(pco_print_state *state, pco_cf_node *cf_node)
1212 {
1213 switch (cf_node->type) {
1214 case PCO_CF_NODE_TYPE_BLOCK:
1215 return pco_print_block(state, pco_cf_node_as_block(cf_node));
1216
1217 case PCO_CF_NODE_TYPE_IF:
1218 return pco_print_if(state, pco_cf_node_as_if(cf_node));
1219
1220 case PCO_CF_NODE_TYPE_LOOP:
1221 return pco_print_loop(state, pco_cf_node_as_loop(cf_node));
1222
1223 case PCO_CF_NODE_TYPE_FUNC:
1224 return pco_print_func(state, pco_cf_node_as_func(cf_node));
1225
1226 default:
1227 break;
1228 }
1229
1230 UNREACHABLE("");
1231 }
1232
1233 /**
1234 * \brief Print PCO shader info.
1235 *
1236 * \param[in] state Print state.
1237 * \param[in] shader PCO shader.
1238 */
1239 static void _pco_print_shader_info(pco_print_state *state, pco_shader *shader)
1240 {
1241 if (shader->name)
1242 pco_printfi(state, "name: \"%s\"\n", shader->name);
1243 pco_printfi(state, "stage: %s\n", mesa_shader_stage_name(shader->stage));
1244 pco_printfi(state, "internal: %s\n", true_false_str(shader->is_internal));
1245 }
1246
1247 /**
1248 * \brief Print PCO shader stats.
1249 *
1250 * \param[in] shader PCO state.
1251 * \param[in] fp Print target file pointer.
1252 */
1253 void pco_print_shader_stats(pco_shader *shader, FILE *fp)
1254 {
1255 pco_print_state state = {
1256 .fp = fp,
1257 .shader = shader,
1258 .indent = 0,
1259 .is_grouped = shader->is_grouped,
1260 .verbose = PCO_DEBUG_PRINT(VERBOSE),
1261 };
1262
1263 _pco_print_shader_info(&state, shader);
1264
1265 struct pvr_stats stats = pco_get_pvr_stats(shader);
1266 pvr_stats_fprintf(fp,
1267 _mesa_shader_stage_to_abbrev(shader->nir->info.stage),
1268 &stats);
1269 }
1270
1271 /**
1272 * \brief Print PCO shader.
1273 *
1274 * \param[in] shader PCO shader.
1275 * \param[in] fp Print target file pointer.
1276 * \param[in] when When the printing is being performed.
1277 */
1278 void pco_print_shader(pco_shader *shader, FILE *fp, const char *when)
1279 {
1280 pco_print_state state = {
1281 .fp = fp,
1282 .shader = shader,
1283 .indent = 0,
1284 .is_grouped = shader->is_grouped,
1285 .verbose = PCO_DEBUG_PRINT(VERBOSE),
1286 };
1287
1288 if (when)
1289 fprintf(fp, "shader ir %s:\n", when);
1290 else
1291 fputs("shader ir:\n", fp);
1292
1293 _pco_print_shader_info(&state, shader);
1294
1295 pco_foreach_func_in_shader (func, shader) {
1296 pco_print_func(&state, func);
1297 }
1298 }
1299
1300 /**
1301 * \brief Print PCO shader binary.
1302 *
1303 * \param[in] shader PCO shader.
1304 * \param[in] fp Print target file pointer.
1305 * \param[in] when When the printing is being performed.
1306 */
1307 void pco_print_binary(pco_shader *shader, FILE *fp, const char *when)
1308 {
1309 pco_print_state state = {
1310 .fp = fp,
1311 .shader = shader,
1312 .indent = 0,
1313 .is_grouped = shader->is_grouped,
1314 .verbose = PCO_DEBUG_PRINT(VERBOSE),
1315 };
1316
1317 if (when)
1318 fprintf(fp, "shader binary %s:\n", when);
1319 else
1320 fputs("shader binary:", fp);
1321
1322 _pco_print_shader_info(&state, shader);
1323
1324 return u_hexdump(fp,
1325 pco_shader_binary_data(shader),
1326 pco_shader_binary_size(shader),
1327 false);
1328 }
1329
1330 /**
1331 * \brief Print PCO reference (wrapper).
1332 *
1333 * \param[in] shader PCO shader.
1334 * \param[in] ref PCO reference.
1335 */
1336 void pco_print_ref(pco_shader *shader, pco_ref ref)
1337 {
1338 pco_print_state state = {
1339 .fp = stdout,
1340 .shader = shader,
1341 .indent = 0,
1342 .is_grouped = shader->is_grouped,
1343 .verbose = false,
1344 };
1345 return _pco_print_ref(&state, ref);
1346 }
1347
1348 /**
1349 * \brief Print PCO instruction (wrapper).
1350 *
1351 * \param[in] shader PCO shader.
1352 * \param[in] instr PCO instruction.
1353 */
1354 void pco_print_instr(pco_shader *shader, pco_instr *instr)
1355 {
1356 pco_print_state state = {
1357 .fp = stdout,
1358 .shader = shader,
1359 .indent = 0,
1360 .is_grouped = shader->is_grouped,
1361 .verbose = false,
1362 };
1363 return _pco_print_instr(&state, instr);
1364 }
1365
1366 /**
1367 * \brief Print PCO instruction group (wrapper).
1368 *
1369 * \param[in] shader PCO shader.
1370 * \param[in] igrp PCO instruction group.
1371 */
1372 void pco_print_igrp(pco_shader *shader, pco_igrp *igrp)
1373 {
1374 pco_print_state state = {
1375 .fp = stdout,
1376 .shader = shader,
1377 .indent = 0,
1378 .is_grouped = shader->is_grouped,
1379 .verbose = false,
1380 };
1381 return _pco_print_igrp(&state, igrp);
1382 }
1383
1384 /**
1385 * \brief Print PCO control flow node name (wrapper).
1386 *
1387 * \param[in] shader PCO shader.
1388 * \param[in] cf_node PCO control flow node.
1389 */
1390 void pco_print_cf_node_name(pco_shader *shader, pco_cf_node *cf_node)
1391 {
1392 pco_print_state state = {
1393 .fp = stdout,
1394 .shader = shader,
1395 .indent = 0,
1396 .is_grouped = shader->is_grouped,
1397 .verbose = false,
1398 };
1399
1400 switch (cf_node->type) {
1401 case PCO_CF_NODE_TYPE_BLOCK:
1402 pco_printf(&state, "block ");
1403 return pco_print_block_name(&state, pco_cf_node_as_block(cf_node));
1404
1405 case PCO_CF_NODE_TYPE_IF:
1406 pco_printf(&state, "if ");
1407 return pco_print_if_name(&state, pco_cf_node_as_if(cf_node));
1408
1409 case PCO_CF_NODE_TYPE_LOOP:
1410 pco_printf(&state, "loop ");
1411 return pco_print_loop_name(&state, pco_cf_node_as_loop(cf_node));
1412
1413 case PCO_CF_NODE_TYPE_FUNC:
1414 pco_printf(&state, "func");
1415 return pco_print_func_sig(&state, pco_cf_node_as_func(cf_node), true);
1416
1417 default:
1418 break;
1419 }
1420
1421 UNREACHABLE("");
1422 }
1423
1424 /**
1425 * \brief Print PCO shader info (wrapper).
1426 *
1427 * \param[in] shader PCO shader.
1428 */
1429 void pco_print_shader_info(pco_shader *shader)
1430 {
1431 pco_print_state state = {
1432 .fp = stdout,
1433 .shader = shader,
1434 .indent = 0,
1435 .is_grouped = shader->is_grouped,
1436 .verbose = false,
1437 };
1438 return _pco_print_shader_info(&state, shader);
1439 }
1440
1441 /**
1442 * \brief Print the name of a phase(wrapper).
1443 *
1444 * \param[in] shader PCO shader.
1445 * \param[in] alutype ALU type.
1446 * \param[in] phase Phase.
1447 */
1448 void pco_print_phase(pco_shader *shader,
1449 enum pco_alutype alutype,
1450 enum pco_op_phase phase)
1451 {
1452 pco_print_state state = {
1453 .fp = stdout,
1454 .shader = shader,
1455 .indent = 0,
1456 .is_grouped = shader->is_grouped,
1457 .verbose = false,
1458 };
1459
1460 return _pco_print_phase(&state, alutype, phase);
1461 }