xref: /aosp_15_r20/external/mesa3d/src/asahi/compiler/agx_print.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2021 Alyssa Rosenzweig
3  * Copyright 2019-2020 Collabora, Ltd.
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include "agx_builder.h"
8 #include "agx_compiler.h"
9 
10 static void
agx_print_sized(char prefix,unsigned value,enum agx_size size,FILE * fp)11 agx_print_sized(char prefix, unsigned value, enum agx_size size, FILE *fp)
12 {
13    switch (size) {
14    case AGX_SIZE_16:
15       fprintf(fp, "%c%u%c", prefix, value >> 1, (value & 1) ? 'h' : 'l');
16       return;
17    case AGX_SIZE_32:
18       assert((value & 1) == 0);
19       fprintf(fp, "%c%u", prefix, value >> 1);
20       return;
21    case AGX_SIZE_64:
22       assert((value & 1) == 0);
23       fprintf(fp, "%c%u:%c%u", prefix, value >> 1, prefix, (value >> 1) + 1);
24       return;
25    }
26 
27    unreachable("Invalid size");
28 }
29 
30 void
agx_print_index(agx_index index,bool is_float,FILE * fp)31 agx_print_index(agx_index index, bool is_float, FILE *fp)
32 {
33    if (index.memory)
34       fprintf(fp, "m");
35 
36    switch (index.type) {
37    case AGX_INDEX_NULL:
38       fprintf(fp, "_");
39       return;
40 
41    case AGX_INDEX_NORMAL:
42       if (index.cache)
43          fprintf(fp, "$");
44 
45       if (index.discard)
46          fprintf(fp, "`");
47 
48       if (index.kill)
49          fprintf(fp, "*");
50 
51       fprintf(fp, "%u", index.value);
52       break;
53 
54    case AGX_INDEX_IMMEDIATE:
55       if (is_float) {
56          assert(index.value < 0x100);
57          fprintf(fp, "#%f", agx_minifloat_decode(index.value));
58       } else {
59          fprintf(fp, "#%u", index.value);
60       }
61 
62       break;
63 
64    case AGX_INDEX_UNDEF:
65       fprintf(fp, "undef");
66       break;
67 
68    case AGX_INDEX_UNIFORM:
69       agx_print_sized('u', index.value, index.size, fp);
70       break;
71 
72    case AGX_INDEX_REGISTER:
73       agx_print_sized('r', index.value, index.size, fp);
74 
75       if (agx_channels(index) > 1) {
76          unsigned last = index.value + agx_size_align_16(index.size) *
77                                           (agx_channels(index) - 1);
78 
79          fprintf(fp, "...");
80 
81          if (index.memory)
82             fprintf(fp, "m");
83          agx_print_sized('r', last, index.size, fp);
84       }
85       break;
86 
87    default:
88       unreachable("Invalid index type");
89    }
90 
91    /* Print length suffixes if not implied */
92    if (index.type == AGX_INDEX_NORMAL) {
93       if (index.size == AGX_SIZE_16)
94          fprintf(fp, "h");
95       else if (index.size == AGX_SIZE_64)
96          fprintf(fp, "d");
97    }
98 
99    if (index.abs)
100       fprintf(fp, ".abs");
101 
102    if (index.neg)
103       fprintf(fp, ".neg");
104 }
105 
106 static struct agx_opcode_info
agx_get_opcode_info_for_print(const agx_instr * I)107 agx_get_opcode_info_for_print(const agx_instr *I)
108 {
109    struct agx_opcode_info info = agx_opcodes_info[I->op];
110 
111    if (I->op == AGX_OPCODE_BITOP) {
112       const char *bitops[16] = {
113          [AGX_BITOP_NOR] = "nor",     [AGX_BITOP_ANDN2] = "andn2",
114          [AGX_BITOP_ANDN1] = "andn1", [AGX_BITOP_XOR] = "xor",
115          [AGX_BITOP_NAND] = "nand",   [AGX_BITOP_AND] = "and",
116          [AGX_BITOP_XNOR] = "xnor",   [AGX_BITOP_ORN2] = "orn2",
117          [AGX_BITOP_ORN1] = "orn1",   [AGX_BITOP_OR] = "or",
118       };
119 
120       if (bitops[I->truth_table] != NULL) {
121          info.name = bitops[I->truth_table];
122          info.immediates &= ~AGX_IMMEDIATE_TRUTH_TABLE;
123       }
124    }
125 
126    return info;
127 }
128 
129 void
agx_print_instr(const agx_instr * I,FILE * fp)130 agx_print_instr(const agx_instr *I, FILE *fp)
131 {
132    assert(I->op < AGX_NUM_OPCODES);
133    struct agx_opcode_info info = agx_get_opcode_info_for_print(I);
134    bool print_comma = false;
135 
136    fprintf(fp, "   ");
137 
138    agx_foreach_dest(I, d) {
139       if (print_comma)
140          fprintf(fp, ", ");
141       else
142          print_comma = true;
143 
144       agx_print_index(I->dest[d], false, fp);
145    }
146 
147    if (I->nr_dests) {
148       fprintf(fp, " = ");
149       print_comma = false;
150    }
151 
152    fprintf(fp, "%s", info.name);
153 
154    if (I->saturate)
155       fprintf(fp, ".sat");
156 
157    if (I->last)
158       fprintf(fp, ".last");
159 
160    fprintf(fp, " ");
161 
162    agx_foreach_src(I, s) {
163       if (print_comma)
164          fprintf(fp, ", ");
165       else
166          print_comma = true;
167 
168       agx_print_index(I->src[s],
169                       agx_opcodes_info[I->op].is_float &&
170                          !(s >= 2 && I->op == AGX_OPCODE_FCMPSEL),
171                       fp);
172    }
173 
174    if (I->mask) {
175       fprintf(fp, ", ");
176 
177       for (unsigned i = 0; i < 4; ++i) {
178          if (I->mask & (1 << i))
179             fprintf(fp, "%c", "xyzw"[i]);
180       }
181    }
182 
183    /* TODO: Do better for enums, truth tables, etc */
184    if (info.immediates) {
185       if (print_comma)
186          fprintf(fp, ", ");
187       else
188          print_comma = true;
189 
190       fprintf(fp, "#%" PRIx64, I->imm);
191    }
192 
193    if (info.immediates & AGX_IMMEDIATE_DIM) {
194       if (print_comma)
195          fprintf(fp, ", ");
196       else
197          print_comma = true;
198 
199       fputs(agx_dim_as_str(I->dim), fp);
200    }
201 
202    if (info.immediates & AGX_IMMEDIATE_SCOREBOARD) {
203       if (print_comma)
204          fprintf(fp, ", ");
205       else
206          print_comma = true;
207 
208       fprintf(fp, "slot %u", I->scoreboard);
209    }
210 
211    if (info.immediates & AGX_IMMEDIATE_NEST) {
212       if (print_comma)
213          fprintf(fp, ", ");
214       else
215          print_comma = true;
216 
217       fprintf(fp, "n=%u", I->nest);
218    }
219 
220    if ((info.immediates & AGX_IMMEDIATE_INVERT_COND) && I->invert_cond) {
221       if (print_comma)
222          fprintf(fp, ", ");
223       else
224          print_comma = true;
225 
226       fprintf(fp, "inv");
227    }
228 
229    fprintf(fp, "\n");
230 }
231 
232 void
agx_print_block(const agx_block * block,FILE * fp)233 agx_print_block(const agx_block *block, FILE *fp)
234 {
235    fprintf(fp, "block%u {\n", block->index);
236 
237    agx_foreach_instr_in_block(block, ins)
238       agx_print_instr(ins, fp);
239 
240    fprintf(fp, "}");
241 
242    if (block->successors[0]) {
243       fprintf(fp, " -> ");
244 
245       agx_foreach_successor(block, succ)
246          fprintf(fp, "block%u ", succ->index);
247    }
248 
249    if (block->predecessors.size) {
250       fprintf(fp, " from");
251 
252       agx_foreach_predecessor(block, pred)
253          fprintf(fp, " block%u", (*pred)->index);
254    }
255 
256    fprintf(fp, "\n\n");
257 }
258 
259 void
agx_print_shader(const agx_context * ctx,FILE * fp)260 agx_print_shader(const agx_context *ctx, FILE *fp)
261 {
262    agx_foreach_block(ctx, block)
263       agx_print_block(block, fp);
264 }
265