Debian Codegraph @

src/asahi/compiler/agx_print.c

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