Debian Codegraph @

src/panfrost/compiler/midgard/midgard_print.c

raw
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include <math.h>
8
9 #include "util/bitscan.h"
10 #include "util/half_float.h"
11 #include "compiler.h"
12 #include "helpers.h"
13 #include "midgard_ops.h"
14
15 /* Pretty printer for Midgard IR, for use debugging compiler-internal
16 * passes like register allocation. The output superficially resembles
17 * Midgard assembly, with the exception that unit information and such is
18 * (normally) omitted, and generic indices are usually used instead of
19 * registers */
20
21 static void
22 mir_print_index(int source)
23 {
24 if (source == ~0) {
25 printf("_");
26 return;
27 }
28
29 if (source >= SSA_FIXED_MINIMUM) {
30 /* Specific register */
31 int reg = SSA_REG_FROM_FIXED(source);
32
33 /* TODO: Moving threshold */
34 if (reg > 16 && reg < 24)
35 printf("U%d", 23 - reg);
36 else
37 printf("R%d", reg);
38 } else if (source & PAN_IS_REG) {
39 printf("r%d", source >> 1);
40 } else {
41 printf("%d", source >> 1);
42 }
43 }
44
45 static const char components[16] = "xyzwefghijklmnop";
46
47 static void
48 mir_print_mask(unsigned mask)
49 {
50 printf(".");
51
52 for (unsigned i = 0; i < 16; ++i) {
53 if (mask & (1 << i))
54 putchar(components[i]);
55 }
56 }
57
58 /*
59 * Print a swizzle. We only print the components enabled by the corresponding
60 * writemask, as the other components will be ignored by the hardware and so
61 * don't matter.
62 */
63 static void
64 mir_print_swizzle(unsigned mask, const unsigned *swizzle)
65 {
66 printf(".");
67
68 for (unsigned i = 0; i < 16; ++i) {
69 if (mask & BITFIELD_BIT(i)) {
70 unsigned C = swizzle[i];
71 putchar(components[C]);
72 }
73 }
74 }
75
76 static const char *
77 mir_get_unit(unsigned unit)
78 {
79 switch (unit) {
80 case ALU_ENAB_VEC_MUL:
81 return "vmul";
82 case ALU_ENAB_SCAL_ADD:
83 return "sadd";
84 case ALU_ENAB_VEC_ADD:
85 return "vadd";
86 case ALU_ENAB_SCAL_MUL:
87 return "smul";
88 case ALU_ENAB_VEC_LUT:
89 return "lut";
90 case ALU_ENAB_BR_COMPACT:
91 return "br";
92 case ALU_ENAB_BRANCH:
93 return "brx";
94 default:
95 return "???";
96 }
97 }
98
99 static void
100 mir_print_embedded_constant(const midgard_instruction *ins, unsigned src_idx)
101 {
102 assert(src_idx <= 1);
103
104 unsigned base_size = max_bitsize_for_alu(ins);
105 unsigned sz = nir_alu_type_get_type_size(ins->src_types[src_idx]);
106 bool half = (sz == (base_size >> 1));
107 unsigned mod = mir_pack_mod(ins, src_idx, false);
108 const unsigned *swizzle = ins->swizzle[src_idx];
109 midgard_reg_mode reg_mode = reg_mode_for_bitsize(max_bitsize_for_alu(ins));
110 unsigned comp_mask = effective_writemask(ins->op, ins->mask);
111 unsigned num_comp = util_bitcount(comp_mask);
112 unsigned max_comp = mir_components_for_type(ins->dest_type);
113 bool first = true;
114
115 printf("#");
116
117 if (num_comp > 1)
118 printf("vec%d(", num_comp);
119
120 for (unsigned comp = 0; comp < max_comp; comp++) {
121 if (!(comp_mask & (1 << comp)))
122 continue;
123
124 if (first)
125 first = false;
126 else
127 printf(", ");
128
129 mir_print_constant_component(stdout, &ins->constants, swizzle[comp],
130 reg_mode, half, mod, ins->op);
131 }
132
133 if (num_comp > 1)
134 printf(")");
135 }
136
137 static void
138 mir_print_alu_type(nir_alu_type t, FILE *fp)
139 {
140 unsigned size = nir_alu_type_get_type_size(t);
141 nir_alu_type base = nir_alu_type_get_base_type(t);
142
143 switch (base) {
144 case nir_type_int:
145 fprintf(fp, ".i");
146 break;
147 case nir_type_uint:
148 fprintf(fp, ".u");
149 break;
150 case nir_type_bool:
151 fprintf(fp, ".b");
152 break;
153 case nir_type_float:
154 fprintf(fp, ".f");
155 break;
156 default:
157 fprintf(fp, ".unknown");
158 break;
159 }
160
161 fprintf(fp, "%u", size);
162 }
163
164 static void
165 mir_print_src(const midgard_instruction *ins, unsigned c)
166 {
167 mir_print_index(ins->src[c]);
168
169 if (ins->src[c] != ~0 && ins->src_types[c] != nir_type_invalid) {
170 mir_print_alu_type(ins->src_types[c], stdout);
171 mir_print_swizzle(ins->mask, ins->swizzle[c]);
172 }
173 }
174
175 void
176 mir_print_instruction(const midgard_instruction *ins)
177 {
178 printf("\t");
179
180 if (midgard_is_branch_unit(ins->unit)) {
181 const char *branch_target_names[] = {"goto", "break", "continue",
182 "discard"};
183
184 printf("%s.", mir_get_unit(ins->unit));
185 if (ins->branch.target_type == TARGET_DISCARD)
186 printf("discard.");
187 else if (ins->writeout)
188 printf("write.");
189 else if (ins->unit == ALU_ENAB_BR_COMPACT && !ins->branch.conditional)
190 printf("uncond.");
191 else
192 printf("cond.");
193
194 if (!ins->branch.conditional)
195 printf("always");
196 else if (ins->branch.invert_conditional)
197 printf("false");
198 else
199 printf("true");
200
201 if (ins->writeout) {
202 printf(" (c: ");
203 mir_print_src(ins, 0);
204 printf(", z: ");
205 mir_print_src(ins, 2);
206 printf(", s: ");
207 mir_print_src(ins, 3);
208 printf(")");
209 }
210
211 if (ins->branch.target_type != TARGET_DISCARD)
212 printf(" %s -> block(%d)\n",
213 ins->branch.target_type < 4
214 ? branch_target_names[ins->branch.target_type]
215 : "??",
216 ins->branch.target_block);
217
218 return;
219 }
220
221 switch (ins->type) {
222 case TAG_ALU_4: {
223 midgard_alu_op op = ins->op;
224 const char *name = alu_opcode_props[op].name;
225
226 if (ins->unit)
227 printf("%s.", mir_get_unit(ins->unit));
228
229 printf("%s", name ? name : "??");
230
231 if (!(midgard_is_integer_out_op(ins->op) &&
232 ins->outmod == midgard_outmod_keeplo)) {
233 mir_print_outmod(stdout, ins->outmod,
234 midgard_is_integer_out_op(ins->op));
235 }
236
237 break;
238 }
239
240 case TAG_LOAD_STORE_4: {
241 midgard_load_store_op op = ins->op;
242 const char *name = load_store_opcode_props[op].name;
243
244 assert(name);
245 printf("%s", name);
246 break;
247 }
248
249 case TAG_TEXTURE_4: {
250 printf("TEX");
251
252 if (ins->helper_terminate)
253 printf(".terminate");
254
255 if (ins->helper_execute)
256 printf(".execute");
257
258 break;
259 }
260
261 default:
262 assert(0);
263 }
264
265 if (ins->compact_branch && ins->branch.invert_conditional)
266 printf(".not");
267
268 printf(" ");
269 mir_print_index(ins->dest);
270
271 if (ins->dest != ~0) {
272 mir_print_alu_type(ins->dest_type, stdout);
273 mir_print_mask(ins->mask);
274 }
275
276 printf(", ");
277
278 /* Only ALU can have an embedded constant, r26 as read on load/store is
279 * something else entirely */
280 bool is_alu = ins->type == TAG_ALU_4;
281 unsigned r_constant = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
282
283 if (is_alu && alu_opcode_props[ins->op].props & QUIRK_FLIPPED_R24) {
284 /* Moves (indicated by QUIRK_FLIPPED_R24) are 1-src, with their
285 * one source in the second slot
286 */
287 assert(ins->src[0] == ~0);
288 } else {
289 if (ins->src[0] == r_constant && is_alu)
290 mir_print_embedded_constant(ins, 0);
291 else
292 mir_print_src(ins, 0);
293
294 printf(", ");
295 }
296
297 if (ins->has_inline_constant)
298 printf("#%d", ins->inline_constant);
299 else if (ins->src[1] == r_constant && is_alu)
300 mir_print_embedded_constant(ins, 1);
301 else
302 mir_print_src(ins, 1);
303
304 if (is_alu) {
305 /* ALU ops are all 2-src, though CSEL is treated like a 3-src
306 * pseudo op with the third source scheduler lowered
307 */
308 switch (ins->op) {
309 case midgard_alu_op_icsel:
310 case midgard_alu_op_fcsel:
311 case midgard_alu_op_icsel_v:
312 case midgard_alu_op_fcsel_v:
313 printf(", ");
314 mir_print_src(ins, 2);
315 break;
316 default:
317 assert(ins->src[2] == ~0);
318 break;
319 }
320
321 assert(ins->src[3] == ~0);
322 } else {
323 for (unsigned c = 2; c <= 3; ++c) {
324 printf(", ");
325 mir_print_src(ins, c);
326 }
327 }
328
329 if (ins->no_spill)
330 printf(" /* no spill */");
331
332 printf("\n");
333 }
334
335 /* Dumps MIR for a block or entire shader respective */
336
337 void
338 mir_print_block(const midgard_block *block)
339 {
340 printf("block%u: {\n", block->name);
341
342 if (block->scheduled) {
343 mir_foreach_bundle_in_block(block, bundle) {
344 for (unsigned i = 0; i < bundle->instruction_count; ++i)
345 mir_print_instruction(bundle->instructions[i]);
346
347 printf("\n");
348 }
349 } else {
350 mir_foreach_instr_in_block(block, ins) {
351 mir_print_instruction(ins);
352 }
353 }
354
355 printf("}");
356
357 if (block->successors[0]) {
358 printf(" -> ");
359 mir_foreach_successor(block, succ)
360 printf(" block%u ", succ->name);
361 }
362
363 printf(" from { ");
364 mir_foreach_predecessor(block, pred)
365 printf("block%u ", pred->name);
366 printf("}");
367
368 printf("\n\n");
369 }
370
371 void
372 mir_print_shader(const compiler_context *ctx)
373 {
374 mir_foreach_block(ctx, block) {
375 mir_print_block((const midgard_block *)block);
376 }
377 }