pan/bi: Add isub op
[mesa.git] / src / panfrost / bifrost / bi_print.c
1 /*
2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "bi_print.h"
28
29 const char *
30 bi_output_mod_name(enum bifrost_outmod mod)
31 {
32 switch (mod) {
33 case BIFROST_NONE: return "";
34 case BIFROST_POS: return ".pos";
35 case BIFROST_SAT_SIGNED: return ".sat_signed";
36 case BIFROST_SAT: return ".sat";
37 default: return "invalid";
38 }
39 }
40
41 const char *
42 bi_minmax_mode_name(enum bifrost_minmax_mode mod)
43 {
44 switch (mod) {
45 case BIFROST_MINMAX_NONE: return "";
46 case BIFROST_NAN_WINS: return ".nan_wins";
47 case BIFROST_SRC1_WINS: return ".src1_wins";
48 case BIFROST_SRC0_WINS: return ".src0_wins";
49 default: return "invalid";
50 }
51 }
52
53 const char *
54 bi_round_mode_name(enum bifrost_roundmode mod)
55 {
56 switch (mod) {
57 case BIFROST_RTE: return "";
58 case BIFROST_RTP: return ".rtp";
59 case BIFROST_RTN: return ".rtn";
60 case BIFROST_RTZ: return ".rtz";
61 default: return "invalid";
62 }
63 }
64
65 const char *
66 bi_csel_cond_name(enum bifrost_csel_cond cond)
67 {
68 switch (cond) {
69 case BIFROST_FEQ_F: return "feq.f";
70 case BIFROST_FGT_F: return "fgt.f";
71 case BIFROST_FGE_F: return "fge.f";
72 case BIFROST_IEQ_F: return "ieq.f";
73 case BIFROST_IGT_I: return "igt.i";
74 case BIFROST_IGE_I: return "uge.i";
75 case BIFROST_UGT_I: return "ugt.i";
76 case BIFROST_UGE_I: return "uge.i";
77 default: return "invalid";
78 }
79 }
80
81 const char *
82 bi_interp_mode_name(enum bifrost_interp_mode mode)
83 {
84 switch (mode) {
85 case BIFROST_INTERP_PER_FRAG: return ".per_frag";
86 case BIFROST_INTERP_CENTROID: return ".centroid";
87 case BIFROST_INTERP_DEFAULT: return "";
88 case BIFROST_INTERP_EXPLICIT: return ".explicit";
89 default: return ".unknown";
90 }
91 }
92
93 const char *
94 bi_ldst_type_name(enum bifrost_ldst_type type)
95 {
96 switch (type) {
97 case BIFROST_LDST_F16: return "f16";
98 case BIFROST_LDST_F32: return "f32";
99 case BIFROST_LDST_I32: return "i32";
100 case BIFROST_LDST_U32: return "u32";
101 default: return "invalid";
102 }
103 }
104
105 /* The remaining functions in this file are for IR-internal
106 * structures; the disassembler doesn't use them */
107
108 static const char *
109 bi_class_name(enum bi_class cl)
110 {
111 switch (cl) {
112 case BI_ADD: return "add";
113 case BI_ATEST: return "atest";
114 case BI_BRANCH: return "branch";
115 case BI_CMP: return "cmp";
116 case BI_BLEND: return "blend";
117 case BI_BITWISE: return "bitwise";
118 case BI_CONVERT: return "convert";
119 case BI_CSEL: return "csel";
120 case BI_DISCARD: return "discard";
121 case BI_FMA: return "fma";
122 case BI_FREXP: return "frexp";
123 case BI_ISUB: return "isub";
124 case BI_LOAD: return "load";
125 case BI_LOAD_UNIFORM: return "load_uniform";
126 case BI_LOAD_ATTR: return "load_attr";
127 case BI_LOAD_VAR: return "load_var";
128 case BI_LOAD_VAR_ADDRESS: return "load_var_address";
129 case BI_MINMAX: return "minmax";
130 case BI_MOV: return "mov";
131 case BI_SHIFT: return "shift";
132 case BI_STORE: return "store";
133 case BI_STORE_VAR: return "store_var";
134 case BI_SPECIAL: return "special";
135 case BI_SWIZZLE: return "swizzle";
136 case BI_TEX: return "tex";
137 case BI_ROUND: return "round";
138 default: return "unknown_class";
139 }
140 }
141
142 static void
143 bi_print_index(FILE *fp, bi_instruction *ins, unsigned index)
144 {
145 if (!index)
146 fprintf(fp, "_");
147 else if (index & BIR_INDEX_REGISTER)
148 fprintf(fp, "br%u", index & ~BIR_INDEX_REGISTER);
149 else if (index & BIR_INDEX_UNIFORM)
150 fprintf(fp, "u%u", index & ~BIR_INDEX_UNIFORM);
151 else if (index & BIR_INDEX_CONSTANT)
152 fprintf(fp, "#0x%" PRIx64, ins->constant.u64);
153 else if (index & BIR_INDEX_ZERO)
154 fprintf(fp, "#0");
155 else if (index & BIR_IS_REG)
156 fprintf(fp, "r%u", index >> 1);
157 else
158 fprintf(fp, "%u", (index >> 1) - 1);
159 }
160
161 static void
162 bi_print_src(FILE *fp, bi_instruction *ins, unsigned s)
163 {
164 unsigned src = ins->src[s];
165 bool mods = bi_has_source_mods(ins);
166 bool abs = ins->src_abs[s] && mods;
167 bool neg = ins->src_neg[s] && mods;
168
169 if (neg)
170 fprintf(fp, "-");
171
172 if (abs)
173 fprintf(fp, "abs(");
174
175 bi_print_index(fp, ins, src);
176
177 if (abs)
178 fprintf(fp, ")");
179 }
180
181 /* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
182
183 static void
184 bi_print_alu_type(nir_alu_type t, FILE *fp)
185 {
186 unsigned size = nir_alu_type_get_type_size(t);
187 nir_alu_type base = nir_alu_type_get_base_type(t);
188
189 switch (base) {
190 case nir_type_int:
191 fprintf(fp, ".i");
192 break;
193 case nir_type_uint:
194 fprintf(fp, ".u");
195 break;
196 case nir_type_bool:
197 fprintf(fp, ".b");
198 break;
199 case nir_type_float:
200 fprintf(fp, ".f");
201 break;
202 default:
203 fprintf(fp, ".unknown");
204 break;
205 }
206
207 fprintf(fp, "%u", size);
208 }
209
210 static void
211 bi_print_swizzle(bi_instruction *ins, unsigned src, FILE *fp)
212 {
213 unsigned size = MAX2(nir_alu_type_get_type_size(ins->dest_type), 8);
214 unsigned count = (size == 64) ? 1 : (32 / size);
215
216 fprintf(fp, ".");
217
218 for (unsigned u = 0; u < count; ++u) {
219 assert(ins->swizzle[src][u] < 4);
220 fputc("xyzw"[ins->swizzle[src][u]], fp);
221 }
222 }
223
224 static const char *
225 bi_bitwise_op_name(enum bi_bitwise_op op)
226 {
227 switch (op) {
228 case BI_BITWISE_AND: return "and";
229 case BI_BITWISE_OR: return "or";
230 case BI_BITWISE_XOR: return "xor";
231 default: return "invalid";
232 }
233 }
234
235 static const char *
236 bi_special_op_name(enum bi_special_op op)
237 {
238 switch (op) {
239 case BI_SPECIAL_FRCP: return "frcp";
240 case BI_SPECIAL_FRSQ: return "frsq";
241 case BI_SPECIAL_FATAN: return "fatan";
242 case BI_SPECIAL_FSIN: return "fsin";
243 case BI_SPECIAL_FCOS: return "fcos";
244 case BI_SPECIAL_FEXP: return "fexp";
245 case BI_SPECIAL_FLOG2: return "flog2";
246 case BI_SPECIAL_FLOGE: return "flog";
247 default: return "invalid";
248 }
249 }
250
251 static void
252 bi_print_load_vary(struct bi_load_vary *load, FILE *fp)
253 {
254 fprintf(fp, "%s", bi_interp_mode_name(load->interp_mode));
255
256 if (load->reuse)
257 fprintf(fp, ".reuse");
258
259 if (load->flat)
260 fprintf(fp, ".flat");
261 }
262
263 static const char *
264 bi_cond_name(enum bi_cond cond)
265 {
266 switch (cond) {
267 case BI_COND_ALWAYS: return ".always";
268 case BI_COND_LT: return ".lt";
269 case BI_COND_LE: return ".le";
270 case BI_COND_GE: return ".ge";
271 case BI_COND_GT: return ".gt";
272 case BI_COND_EQ: return ".eq";
273 case BI_COND_NE: return ".ne";
274 default: return "invalid";
275 }
276 }
277
278 static void
279 bi_print_branch(struct bi_branch *branch, FILE *fp)
280 {
281 fprintf(fp, "%s", bi_cond_name(branch->cond));
282 }
283
284 static void
285 bi_print_writemask(bi_instruction *ins, FILE *fp)
286 {
287 unsigned bits_per_comp = nir_alu_type_get_type_size(ins->dest_type);
288 assert(bits_per_comp);
289 unsigned bytes_per_comp = MAX2(bits_per_comp / 8, 1);
290 unsigned comps = 16 / bytes_per_comp;
291 unsigned smask = (1 << bytes_per_comp) - 1;
292 fprintf(fp, ".");
293
294 for (unsigned i = 0; i < comps; ++i) {
295 unsigned masked = (ins->writemask >> (i * bytes_per_comp)) & smask;
296 if (!masked)
297 continue;
298
299 assert(masked == smask);
300 assert(i < 4);
301 fputc("xyzw"[i], fp);
302 }
303 }
304
305 void
306 bi_print_instruction(bi_instruction *ins, FILE *fp)
307 {
308 if (ins->type == BI_MINMAX)
309 fprintf(fp, "%s", ins->op.minmax == BI_MINMAX_MIN ? "min" : "max");
310 else if (ins->type == BI_BITWISE)
311 fprintf(fp, "%s", bi_bitwise_op_name(ins->op.bitwise));
312 else if (ins->type == BI_ROUND)
313 fprintf(fp, ins->op.round == BI_ROUND_MODE ? "roundMode": "round");
314 else if (ins->type == BI_SPECIAL)
315 fprintf(fp, "%s", bi_special_op_name(ins->op.special));
316 else
317 fprintf(fp, "%s", bi_class_name(ins->type));
318
319 if (ins->type == BI_MINMAX)
320 fprintf(fp, "%s", bi_minmax_mode_name(ins->minmax));
321 else if (ins->type == BI_LOAD_VAR)
322 bi_print_load_vary(&ins->load_vary, fp);
323 else if (ins->type == BI_BRANCH)
324 bi_print_branch(&ins->branch, fp);
325 else if (ins->type == BI_CSEL)
326 fprintf(fp, "%s", bi_cond_name(ins->csel_cond));
327 else if (ins->type == BI_BLEND)
328 fprintf(fp, ".loc%u", ins->blend_location);
329
330 if (ins->dest)
331 bi_print_alu_type(ins->dest_type, fp);
332
333 if (bi_has_outmod(ins))
334 fprintf(fp, "%s", bi_output_mod_name(ins->outmod));
335
336 if (bi_class_props[ins->type] & BI_ROUNDMODE)
337 fprintf(fp, "%s", bi_round_mode_name(ins->roundmode));
338
339 fprintf(fp, " ");
340 bi_print_index(fp, ins, ins->dest);
341
342 if (ins->dest)
343 bi_print_writemask(ins, fp);
344
345 fprintf(fp, ", ");
346
347 bi_foreach_src(ins, s) {
348 bi_print_src(fp, ins, s);
349
350 if (ins->src[s] && !(ins->src[s] & (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO)))
351 bi_print_swizzle(ins, s, fp);
352
353 bool is_convert = ins->type == BI_CONVERT && s == 0;
354 bool is_branch = ins->type == BI_BRANCH && s < 2 && ins->branch.cond != BI_COND_ALWAYS;
355 bool is_csel = ins->type == BI_CSEL && s < 2;
356
357 if (is_convert || is_branch || is_csel)
358 bi_print_alu_type(ins->src_types[s], fp);
359
360 if (s < BIR_SRC_COUNT)
361 fprintf(fp, ", ");
362 }
363
364 if (ins->type == BI_BRANCH) {
365 if (ins->branch.target)
366 fprintf(fp, "-> block%u", ins->branch.target->name);
367 else
368 fprintf(fp, "-> blockhole");
369 }
370
371 fprintf(fp, "\n");
372 }
373
374 void
375 bi_print_bundle(bi_bundle *bundle, FILE *fp)
376 {
377 bi_instruction *ins[2] = { bundle->fma, bundle->add };
378
379 for (unsigned i = 0; i < 2; ++i) {
380 if (ins[i])
381 bi_print_instruction(ins[i], fp);
382 else
383 fprintf(fp, "nop\n");
384 }
385 }
386
387 void
388 bi_print_clause(bi_clause *clause, FILE *fp)
389 {
390 fprintf(fp, "\tid(%u)", clause->scoreboard_id);
391
392 if (clause->dependencies) {
393 fprintf(fp, ", wait(");
394
395 for (unsigned i = 0; i < 8; ++i) {
396 if (clause->dependencies & (1 << i))
397 fprintf(fp, "%u ", i);
398 }
399
400 fprintf(fp, ")");
401 }
402
403 if (!clause->back_to_back)
404 fprintf(fp, " nbb %s", clause->branch_conditional ? "branch-cond" : "branch-uncond");
405
406 if (clause->data_register_write_barrier)
407 fprintf(fp, " drwb");
408
409 fprintf(fp, "\n");
410
411 if (clause->instruction_count) {
412 assert(!clause->bundle_count);
413
414 for (unsigned i = 0; i < clause->instruction_count; ++i)
415 bi_print_instruction(clause->instructions[i], fp);
416 } else {
417 assert(clause->bundle_count);
418
419 for (unsigned i = 0; i < clause->bundle_count; ++i)
420 bi_print_bundle(&clause->bundles[i], fp);
421 }
422
423 if (clause->constant_count) {
424 for (unsigned i = 0; i < clause->constant_count; ++i)
425 fprintf(fp, "%" PRIx64 " ", clause->constants[i]);
426
427 fprintf(fp, "\n");
428 }
429 }
430
431 void
432 bi_print_block(bi_block *block, FILE *fp)
433 {
434 fprintf(fp, "block%u {\n", block->name);
435
436 if (block->scheduled) {
437 bi_foreach_clause_in_block(block, clause)
438 bi_print_clause(clause, fp);
439 } else {
440 bi_foreach_instr_in_block(block, ins)
441 bi_print_instruction(ins, fp);
442 }
443
444 fprintf(fp, "}");
445
446 if (block->successors[0]) {
447 fprintf(fp, " -> ");
448
449 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
450 if (block->successors[i])
451 fprintf(fp, "block%u ", block->successors[i]->name);
452 }
453 }
454
455 if (block->predecessors->entries) {
456 fprintf(fp, " from");
457
458 bi_foreach_predecessor(block, pred)
459 fprintf(fp, " block%u", pred->name);
460 }
461
462 fprintf(fp, "\n\n");
463 }
464
465 void
466 bi_print_shader(bi_context *ctx, FILE *fp)
467 {
468 bi_foreach_block(ctx, block)
469 bi_print_block(block, fp);
470 }