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